Clean up code.
1. Create a method for duplicate code. 2. Address style/lint issues. 3. Properly parse settings constant string. The original iteration wouldn't reset values to their default if the constant string didn't have a value set. Bug: 158300259 Test: manual Change-Id: I994b872ba16f12c8e06ce85aedc526b84e5fa31b
This commit is contained in:
@@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
package com.android.settings.development.tare;
|
package com.android.settings.development.tare;
|
||||||
|
|
||||||
|
import static android.provider.Settings.Global.TARE_ALARM_MANAGER_CONSTANTS;
|
||||||
|
import static android.provider.Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.app.tare.EconomyManager;
|
import android.app.tare.EconomyManager;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -49,11 +53,10 @@ public class TareFactorController {
|
|||||||
mContentResolver = context.getContentResolver();
|
mContentResolver = context.getContentResolver();
|
||||||
mResources = context.getResources();
|
mResources = context.getResources();
|
||||||
|
|
||||||
mAlarmManagerConstants = Settings.Global
|
mAlarmManagerConstants =
|
||||||
.getString(mContentResolver, Settings.Global.TARE_ALARM_MANAGER_CONSTANTS);
|
Settings.Global.getString(mContentResolver, TARE_ALARM_MANAGER_CONSTANTS);
|
||||||
|
mJobSchedulerConstants =
|
||||||
mJobSchedulerConstants = Settings.Global
|
Settings.Global.getString(mContentResolver, TARE_JOB_SCHEDULER_CONSTANTS);
|
||||||
.getString(mContentResolver, Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS);
|
|
||||||
|
|
||||||
initAlarmManagerMap();
|
initAlarmManagerMap();
|
||||||
parseAlarmManagerGlobalSettings();
|
parseAlarmManagerGlobalSettings();
|
||||||
@@ -408,8 +411,7 @@ public class TareFactorController {
|
|||||||
mJobSchedulerMap.put(
|
mJobSchedulerMap.put(
|
||||||
EconomyManager.KEY_JS_ACTION_JOB_LOW_RUNNING_BASE_PRICE,
|
EconomyManager.KEY_JS_ACTION_JOB_LOW_RUNNING_BASE_PRICE,
|
||||||
new TareFactorData(mResources.getString(R.string.tare_job_low_running),
|
new TareFactorData(mResources.getString(R.string.tare_job_low_running),
|
||||||
EconomyManager
|
EconomyManager.DEFAULT_JS_ACTION_JOB_LOW_RUNNING_BASE_PRICE,
|
||||||
.DEFAULT_JS_ACTION_JOB_LOW_RUNNING_BASE_PRICE,
|
|
||||||
POLICY_JOB_SCHEDULER));
|
POLICY_JOB_SCHEDULER));
|
||||||
mJobSchedulerMap.put(EconomyManager.KEY_JS_ACTION_JOB_MIN_START_BASE_PRICE,
|
mJobSchedulerMap.put(EconomyManager.KEY_JS_ACTION_JOB_MIN_START_BASE_PRICE,
|
||||||
new TareFactorData(mResources.getString(R.string.tare_job_min_start),
|
new TareFactorData(mResources.getString(R.string.tare_job_min_start),
|
||||||
@@ -425,68 +427,50 @@ public class TareFactorController {
|
|||||||
POLICY_JOB_SCHEDULER));
|
POLICY_JOB_SCHEDULER));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes a key and factor policy as input and grabs the default value linked to it.
|
|
||||||
*
|
|
||||||
* @param key the key of the factor you want to get the default value of
|
|
||||||
* @param factorPolicy the policy you want the default value of
|
|
||||||
*/
|
|
||||||
private int getDefaultValue(String key, int factorPolicy) {
|
|
||||||
ArrayMap<String, TareFactorData> currentMap;
|
|
||||||
switch (factorPolicy) {
|
|
||||||
case POLICY_ALARM_MANAGER:
|
|
||||||
currentMap = mAlarmManagerMap;
|
|
||||||
break;
|
|
||||||
case POLICY_JOB_SCHEDULER:
|
|
||||||
currentMap = mJobSchedulerMap;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Invalid factor policy given");
|
|
||||||
}
|
|
||||||
return currentMap.get(key).defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the AM constant from Settings.Global to get to the current value.
|
* Parses the AM constant from Settings.Global to get to the current value.
|
||||||
*/
|
*/
|
||||||
private void parseAlarmManagerGlobalSettings() {
|
private void parseAlarmManagerGlobalSettings() {
|
||||||
try {
|
parseSettingsIntoMap(mAlarmManagerConstants, mAlarmManagerMap);
|
||||||
mParser.setString(mAlarmManagerConstants);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Slog.e(TAG, "Bad value string constants", e);
|
|
||||||
}
|
|
||||||
int size = mParser.size();
|
|
||||||
|
|
||||||
for (int i = 0; i < size - 1; i++) {
|
|
||||||
String key = mParser.keyAt(i);
|
|
||||||
TareFactorData data = mAlarmManagerMap.get(key);
|
|
||||||
data.currentValue = mParser.getInt(key, getDefaultValue(key, getFactorType(key)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the JS constant from Settings.Global to get to the current value.
|
* Parses the JS constant from Settings.Global to get to the current value.
|
||||||
*/
|
*/
|
||||||
private void parseJobSchedulerGlobalSettings() {
|
private void parseJobSchedulerGlobalSettings() {
|
||||||
try {
|
parseSettingsIntoMap(mJobSchedulerConstants, mJobSchedulerMap);
|
||||||
mParser.setString(mJobSchedulerConstants);
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
Slog.e(TAG, "Bad value string constants", e);
|
|
||||||
}
|
|
||||||
int size = mParser.size();
|
|
||||||
|
|
||||||
for (int i = 0; i < size - 1; i++) {
|
private void parseSettingsIntoMap(String constants, ArrayMap<String, TareFactorData> map) {
|
||||||
String key = mParser.keyAt(i);
|
try {
|
||||||
TareFactorData data = mJobSchedulerMap.get(key);
|
mParser.setString(constants);
|
||||||
data.currentValue = mParser.getInt(key, getDefaultValue(key, getFactorType(key)));
|
} catch (Exception e) {
|
||||||
|
Slog.e(TAG, "Bad string constants value", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = map.size() - 1; i >= 0; --i) {
|
||||||
|
final String key = map.keyAt(i);
|
||||||
|
final TareFactorData data = map.valueAt(i);
|
||||||
|
data.currentValue = mParser.getInt(key, data.defaultValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private ArrayMap<String, TareFactorData> getMap(int factorPolicy) {
|
||||||
|
switch (factorPolicy) {
|
||||||
|
case POLICY_ALARM_MANAGER:
|
||||||
|
return mAlarmManagerMap;
|
||||||
|
case POLICY_JOB_SCHEDULER:
|
||||||
|
return mJobSchedulerMap;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid factor policy given");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a key and factor policy as input and grabs the title linked to it.
|
* Takes a key and factor policy as input and grabs the title linked to it.
|
||||||
*
|
*
|
||||||
* @param key the key of the factor you want to get the title of
|
* @param key the key of the factor you want to get the title of
|
||||||
* @param factorPolicy the policy you want the title of
|
* @param factorPolicy the policy you want the title of
|
||||||
*/
|
*/
|
||||||
private String getTitle(String key, int factorPolicy) {
|
private String getTitle(String key, int factorPolicy) {
|
||||||
@@ -507,21 +491,11 @@ public class TareFactorController {
|
|||||||
/**
|
/**
|
||||||
* Takes a key and factor policy as input and grabs the current value linked to it.
|
* Takes a key and factor policy as input and grabs the current value linked to it.
|
||||||
*
|
*
|
||||||
* @param key the key of the factor you want to get the default value of
|
* @param key the key of the factor you want to get the default value of
|
||||||
* @param factorPolicy the policy you want the current value of
|
* @param factorPolicy the policy you want the current value of
|
||||||
*/
|
*/
|
||||||
private int getCurrentValue(String key, int factorPolicy) {
|
private int getCurrentValue(String key, int factorPolicy) {
|
||||||
ArrayMap<String, TareFactorData> currentMap;
|
final ArrayMap<String, TareFactorData> currentMap = getMap(factorPolicy);
|
||||||
switch (factorPolicy) {
|
|
||||||
case POLICY_ALARM_MANAGER:
|
|
||||||
currentMap = mAlarmManagerMap;
|
|
||||||
break;
|
|
||||||
case POLICY_JOB_SCHEDULER:
|
|
||||||
currentMap = mJobSchedulerMap;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Invalid factor policy given");
|
|
||||||
}
|
|
||||||
return currentMap.get(key).currentValue;
|
return currentMap.get(key).currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,18 +525,14 @@ public class TareFactorController {
|
|||||||
* @param factorPolicy policy being updated
|
* @param factorPolicy policy being updated
|
||||||
*/
|
*/
|
||||||
public void updateValue(String key, int editedValue, int factorPolicy) {
|
public void updateValue(String key, int editedValue, int factorPolicy) {
|
||||||
switch (factorPolicy) {
|
final ArrayMap<String, TareFactorData> map = getMap(factorPolicy);
|
||||||
case POLICY_ALARM_MANAGER:
|
|
||||||
mAlarmManagerMap.get(key).currentValue = editedValue;
|
final TareFactorData data = map.get(key);
|
||||||
rebuildPolicyConstants(factorPolicy);
|
if (data.currentValue == editedValue) {
|
||||||
break;
|
return;
|
||||||
case POLICY_JOB_SCHEDULER:
|
|
||||||
mJobSchedulerMap.get(key).currentValue = editedValue;
|
|
||||||
rebuildPolicyConstants(factorPolicy);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Invalid factor policy given");
|
|
||||||
}
|
}
|
||||||
|
data.currentValue = editedValue;
|
||||||
|
rebuildPolicyConstants(factorPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -574,16 +544,14 @@ public class TareFactorController {
|
|||||||
private void rebuildPolicyConstants(int factorPolicy) {
|
private void rebuildPolicyConstants(int factorPolicy) {
|
||||||
switch (factorPolicy) {
|
switch (factorPolicy) {
|
||||||
case POLICY_ALARM_MANAGER:
|
case POLICY_ALARM_MANAGER:
|
||||||
writeConstantsToSettings(mAlarmManagerMap,
|
writeConstantsToSettings(mAlarmManagerMap, TARE_ALARM_MANAGER_CONSTANTS);
|
||||||
Settings.Global.TARE_ALARM_MANAGER_CONSTANTS);
|
|
||||||
|
|
||||||
mAlarmManagerConstants = Settings.Global
|
mAlarmManagerConstants = Settings.Global
|
||||||
.getString(mContentResolver, Settings.Global
|
.getString(mContentResolver, Settings.Global
|
||||||
.TARE_ALARM_MANAGER_CONSTANTS);
|
.TARE_ALARM_MANAGER_CONSTANTS);
|
||||||
break;
|
break;
|
||||||
case POLICY_JOB_SCHEDULER:
|
case POLICY_JOB_SCHEDULER:
|
||||||
writeConstantsToSettings(mJobSchedulerMap,
|
writeConstantsToSettings(mJobSchedulerMap, TARE_JOB_SCHEDULER_CONSTANTS);
|
||||||
Settings.Global.TARE_JOB_SCHEDULER_CONSTANTS);
|
|
||||||
|
|
||||||
mJobSchedulerConstants = Settings.Global
|
mJobSchedulerConstants = Settings.Global
|
||||||
.getString(mContentResolver, Settings.Global
|
.getString(mContentResolver, Settings.Global
|
||||||
@@ -623,7 +591,7 @@ public class TareFactorController {
|
|||||||
public TareFactorDialogFragment createDialog(String key) {
|
public TareFactorDialogFragment createDialog(String key) {
|
||||||
int policy = getFactorType(key);
|
int policy = getFactorType(key);
|
||||||
return new TareFactorDialogFragment(getTitle(key, policy), key,
|
return new TareFactorDialogFragment(getTitle(key, policy), key,
|
||||||
getCurrentValue(key, policy), policy , this);
|
getCurrentValue(key, policy), policy, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user