Don\'t update conditions on background threads
am: 6cb280d146
* commit '6cb280d14641327b32378504544d125bbc3bcaf3':
Don't update conditions on background threads
Change-Id: I446e85dc5462e3c06a75e2e53f1990990bb06f48
This commit is contained in:
@@ -56,7 +56,7 @@ public class ConditionManager {
|
|||||||
|
|
||||||
private ConditionManager(Context context) {
|
private ConditionManager(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mConditions = new ArrayList<Condition>();
|
mConditions = new ArrayList<>();
|
||||||
new ConditionLoader().execute();
|
new ConditionLoader().execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,11 +67,11 @@ public class ConditionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void readFromXml() {
|
private void readFromXml(File xmlFile, ArrayList<Condition> conditions) {
|
||||||
if (DEBUG) Log.d(TAG, "Reading from " + mXmlFile.toString());
|
if (DEBUG) Log.d(TAG, "Reading from " + xmlFile.toString());
|
||||||
try {
|
try {
|
||||||
XmlPullParser parser = Xml.newPullParser();
|
XmlPullParser parser = Xml.newPullParser();
|
||||||
FileReader in = new FileReader(mXmlFile);
|
FileReader in = new FileReader(xmlFile);
|
||||||
parser.setInput(in);
|
parser.setInput(in);
|
||||||
int state = parser.getEventType();
|
int state = parser.getEventType();
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ public class ConditionManager {
|
|||||||
PersistableBundle bundle = PersistableBundle.restoreFromXml(parser);
|
PersistableBundle bundle = PersistableBundle.restoreFromXml(parser);
|
||||||
if (DEBUG) Log.d(TAG, "Reading " + clz + " -- " + bundle);
|
if (DEBUG) Log.d(TAG, "Reading " + clz + " -- " + bundle);
|
||||||
condition.restoreState(bundle);
|
condition.restoreState(bundle);
|
||||||
mConditions.add(condition);
|
conditions.add(condition);
|
||||||
while (parser.getDepth() > depth) {
|
while (parser.getDepth() > depth) {
|
||||||
parser.next();
|
parser.next();
|
||||||
}
|
}
|
||||||
@@ -129,21 +129,21 @@ public class ConditionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addMissingConditions() {
|
private void addMissingConditions(ArrayList<Condition> conditions) {
|
||||||
addIfMissing(AirplaneModeCondition.class);
|
addIfMissing(AirplaneModeCondition.class, conditions);
|
||||||
addIfMissing(HotspotCondition.class);
|
addIfMissing(HotspotCondition.class, conditions);
|
||||||
addIfMissing(DndCondition.class);
|
addIfMissing(DndCondition.class, conditions);
|
||||||
addIfMissing(BatterySaverCondition.class);
|
addIfMissing(BatterySaverCondition.class, conditions);
|
||||||
addIfMissing(CellularDataCondition.class);
|
addIfMissing(CellularDataCondition.class, conditions);
|
||||||
addIfMissing(BackgroundDataCondition.class);
|
addIfMissing(BackgroundDataCondition.class, conditions);
|
||||||
addIfMissing(WorkModeCondition.class);
|
addIfMissing(WorkModeCondition.class, conditions);
|
||||||
Collections.sort(mConditions, CONDITION_COMPARATOR);
|
Collections.sort(conditions, CONDITION_COMPARATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addIfMissing(Class<? extends Condition> clz) {
|
private void addIfMissing(Class<? extends Condition> clz, ArrayList<Condition> conditions) {
|
||||||
if (getCondition(clz) == null) {
|
if (getCondition(clz, conditions) == null) {
|
||||||
if (DEBUG) Log.d(TAG, "Adding missing " + clz.getName());
|
if (DEBUG) Log.d(TAG, "Adding missing " + clz.getName());
|
||||||
mConditions.add(createCondition(clz));
|
conditions.add(createCondition(clz));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,10 +171,14 @@ public class ConditionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Condition> T getCondition(Class<T> clz) {
|
public <T extends Condition> T getCondition(Class<T> clz) {
|
||||||
final int N = mConditions.size();
|
return getCondition(clz, mConditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends Condition> T getCondition(Class<T> clz, List<Condition> conditions) {
|
||||||
|
final int N = conditions.size();
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
if (clz.equals(mConditions.get(i).getClass())) {
|
if (clz.equals(conditions.get(i).getClass())) {
|
||||||
return (T) mConditions.get(i);
|
return (T) conditions.get(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -213,19 +217,22 @@ public class ConditionManager {
|
|||||||
mListeners.remove(listener);
|
mListeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ConditionLoader extends AsyncTask<Void, Void, Void> {
|
private class ConditionLoader extends AsyncTask<Void, Void, ArrayList<Condition>> {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected ArrayList<Condition> doInBackground(Void... params) {
|
||||||
|
ArrayList<Condition> conditions = new ArrayList<>();
|
||||||
mXmlFile = new File(mContext.getFilesDir(), FILE_NAME);
|
mXmlFile = new File(mContext.getFilesDir(), FILE_NAME);
|
||||||
if (mXmlFile.exists()) {
|
if (mXmlFile.exists()) {
|
||||||
readFromXml();
|
readFromXml(mXmlFile, conditions);
|
||||||
}
|
}
|
||||||
addMissingConditions();
|
addMissingConditions(conditions);
|
||||||
return null;
|
return conditions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid) {
|
protected void onPostExecute(ArrayList<Condition> conditions) {
|
||||||
|
mConditions.clear();
|
||||||
|
mConditions.addAll(conditions);
|
||||||
final int N = mListeners.size();
|
final int N = mListeners.size();
|
||||||
for (int i = 0; i < N; i++) {
|
for (int i = 0; i < N; i++) {
|
||||||
mListeners.get(i).onConditionsChanged();
|
mListeners.get(i).onConditionsChanged();
|
||||||
|
Reference in New Issue
Block a user