Merge "Add AnomalyDetectionPolicy"
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.KeyValueListParser;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Class to store the policy for anomaly detection, which comes from
|
||||
* {@link android.provider.Settings.Global}
|
||||
*/
|
||||
public class AnomalyDetectionPolicy {
|
||||
public static final String TAG = "AnomalyDetectionPolicy";
|
||||
|
||||
@VisibleForTesting
|
||||
static final String KEY_ANOMALY_DETECTION_ENABLED = "anomaly_detection_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKELOCK_DETECTION_ENABLED = "wakelock_enabled";
|
||||
@VisibleForTesting
|
||||
static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold";
|
||||
|
||||
/**
|
||||
* {@code true} if general anomaly detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_ANOMALY_DETECTION_ENABLED
|
||||
*/
|
||||
public final boolean anomalyDetectionEnabled;
|
||||
|
||||
/**
|
||||
* {@code true} if wakelock anomaly detection is enabled
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKELOCK_DETECTION_ENABLED
|
||||
*/
|
||||
public final boolean wakeLockDetectionEnabled;
|
||||
|
||||
/**
|
||||
* Threshold for wakelock time in milli seconds
|
||||
*
|
||||
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
|
||||
* @see #KEY_WAKELOCK_THRESHOLD
|
||||
*/
|
||||
public final long wakeLockThreshold;
|
||||
|
||||
private final KeyValueListParserWrapper mParserWrapper;
|
||||
|
||||
public AnomalyDetectionPolicy(Context context) {
|
||||
this(context, new KeyValueListParserWrapperImpl(new KeyValueListParser(',')));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
AnomalyDetectionPolicy(Context context, KeyValueListParserWrapper parserWrapper) {
|
||||
mParserWrapper = parserWrapper;
|
||||
final String value = Settings.Global.getString(context.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS);
|
||||
|
||||
try {
|
||||
mParserWrapper.setString(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(TAG, "Bad anomaly detection constants");
|
||||
}
|
||||
|
||||
anomalyDetectionEnabled = mParserWrapper.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, true);
|
||||
wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true);
|
||||
wakeLockThreshold = mParserWrapper.getLong(KEY_WAKELOCK_THRESHOLD,
|
||||
DateUtils.HOUR_IN_MILLIS);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.util.KeyValueListParser;
|
||||
|
||||
/**
|
||||
* This interface replicates a subset of the {@link KeyValueListParser}. The interface
|
||||
* exists so that we can use a thin wrapper around the PM in production code and a mock in tests.
|
||||
* We cannot directly mock or shadow the {@link KeyValueListParser}, because some of the methods
|
||||
* we rely on are newer than the API version supported by Robolectric.
|
||||
*/
|
||||
public interface KeyValueListParserWrapper {
|
||||
|
||||
/**
|
||||
* Get real {@link KeyValueListParser}
|
||||
*/
|
||||
KeyValueListParser getKeyValueListParser();
|
||||
|
||||
/**
|
||||
* Resets the parser with a new string to parse. The string is expected to be in the following
|
||||
* format:
|
||||
* <pre>key1=value,key2=value,key3=value</pre>
|
||||
*
|
||||
* where the delimiter is a comma.
|
||||
*
|
||||
* @param str the string to parse.
|
||||
* @throws IllegalArgumentException if the string is malformed.
|
||||
*/
|
||||
void setString(String str) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get the value for key as a boolean.
|
||||
* @param key The key to lookup.
|
||||
* @param defaultValue The value to return if the key was not found.
|
||||
* @return the string value associated with the key.
|
||||
*/
|
||||
boolean getBoolean(String key, boolean defaultValue);
|
||||
|
||||
/**
|
||||
* Get the value for key as a long.
|
||||
* @param key The key to lookup.
|
||||
* @param defaultValue The value to return if the key was not found, or the value was not a
|
||||
* long.
|
||||
* @return the long value associated with the key.
|
||||
*/
|
||||
long getLong(String key, long defaultValue);
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import android.util.KeyValueListParser;
|
||||
|
||||
/**
|
||||
* Impl of {@link KeyValueListParser}
|
||||
*/
|
||||
public class KeyValueListParserWrapperImpl implements KeyValueListParserWrapper {
|
||||
private KeyValueListParser mParser;
|
||||
|
||||
public KeyValueListParserWrapperImpl(KeyValueListParser parser) {
|
||||
mParser = parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyValueListParser getKeyValueListParser() {
|
||||
return mParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setString(String str) throws IllegalArgumentException {
|
||||
mParser.setString(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defaultValue) {
|
||||
return mParser.getBoolean(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key, long defaultValue) {
|
||||
return mParser.getLong(key, defaultValue);
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.KeyValueListParser;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyDetectionPolicyTest {
|
||||
private static final String ANOMALY_DETECTION_CONSTANTS_VALUE = "anomaly_detection_enabled=true"
|
||||
+ ",wakelock_enabled=false"
|
||||
+ ",wakelock_threshold=3000";
|
||||
private Context mContext;
|
||||
private KeyValueListParserWrapper mKeyValueListParserWrapper;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mKeyValueListParserWrapper = spy(
|
||||
new KeyValueListParserWrapperImpl(new KeyValueListParser(',')));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDataFromSettings() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ANOMALY_DETECTION_CONSTANTS_VALUE);
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, true);
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(
|
||||
AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true);
|
||||
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit_containsDefaultData() {
|
||||
Settings.Global.putString(mContext.getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
|
||||
// Mock it to avoid noSuchMethodError
|
||||
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(true));
|
||||
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(false));
|
||||
|
||||
AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext,
|
||||
mKeyValueListParserWrapper);
|
||||
|
||||
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isTrue();
|
||||
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user