Add AnomalyDetectionPolicy

AnomalyDetectionPolicy contains all the knobs for anomaly detection.

Since Robolectric doesn't support KeyValueListParser, this cl creates
KeyValueListParserWrapper so we could mock it and test
AnomalyDetectionPolicy

Bug: 36924669
Test: RunSettingsRoboTests
Change-Id: I71138f85d37a21b5c4c9c806a41e00a4aa924f5f
This commit is contained in:
jackqdyulei
2017-05-11 15:12:13 -07:00
parent 1949776c59
commit 9db95a35ba
4 changed files with 289 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}