OSDN Git Service

Don't update conditions on background threads
[android-x86/packages-apps-Settings.git] / src / com / android / settings / dashboard / conditional / ConditionManager.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.dashboard.conditional;
17
18 import android.content.Context;
19 import android.os.AsyncTask;
20 import android.os.PersistableBundle;
21 import android.util.Log;
22 import android.util.Xml;
23 import org.xmlpull.v1.XmlPullParser;
24 import org.xmlpull.v1.XmlPullParserException;
25 import org.xmlpull.v1.XmlSerializer;
26
27 import java.io.File;
28 import java.io.FileReader;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.List;
35
36 public class ConditionManager {
37
38     private static final String TAG = "ConditionManager";
39
40     private static final boolean DEBUG = false;
41
42     private static final String PKG = "com.android.settings.dashboard.conditional.";
43
44     private static final String FILE_NAME = "condition_state.xml";
45     private static final String TAG_CONDITIONS = "cs";
46     private static final String TAG_CONDITION = "c";
47     private static final String ATTR_CLASS = "cls";
48
49     private static ConditionManager sInstance;
50
51     private final Context mContext;
52     private final ArrayList<Condition> mConditions;
53     private File mXmlFile;
54
55     private final ArrayList<ConditionListener> mListeners = new ArrayList<>();
56
57     private ConditionManager(Context context) {
58         mContext = context;
59         mConditions = new ArrayList<>();
60         new ConditionLoader().execute();
61     }
62
63     public void refreshAll() {
64         final int N = mConditions.size();
65         for (int i = 0; i < N; i++) {
66             mConditions.get(i).refreshState();
67         }
68     }
69
70     private void readFromXml(File xmlFile, ArrayList<Condition> conditions) {
71         if (DEBUG) Log.d(TAG, "Reading from " + xmlFile.toString());
72         try {
73             XmlPullParser parser = Xml.newPullParser();
74             FileReader in = new FileReader(xmlFile);
75             parser.setInput(in);
76             int state = parser.getEventType();
77
78             while (state != XmlPullParser.END_DOCUMENT) {
79                 if (TAG_CONDITION.equals(parser.getName())) {
80                     int depth = parser.getDepth();
81                     String clz = parser.getAttributeValue("", ATTR_CLASS);
82                     if (!clz.startsWith(PKG)) {
83                         clz = PKG + clz;
84                     }
85                     Condition condition = createCondition(Class.forName(clz));
86                     PersistableBundle bundle = PersistableBundle.restoreFromXml(parser);
87                     if (DEBUG) Log.d(TAG, "Reading " + clz + " -- " + bundle);
88                     condition.restoreState(bundle);
89                     conditions.add(condition);
90                     while (parser.getDepth() > depth) {
91                         parser.next();
92                     }
93                 }
94                 state = parser.next();
95             }
96             in.close();
97         } catch (XmlPullParserException | IOException | ClassNotFoundException e) {
98             Log.w(TAG, "Problem reading " + FILE_NAME, e);
99         }
100     }
101
102     private void saveToXml() {
103         if (DEBUG) Log.d(TAG, "Writing to " + mXmlFile.toString());
104         try {
105             XmlSerializer serializer = Xml.newSerializer();
106             FileWriter writer = new FileWriter(mXmlFile);
107             serializer.setOutput(writer);
108
109             serializer.startDocument("UTF-8", true);
110             serializer.startTag("", TAG_CONDITIONS);
111
112             final int N = mConditions.size();
113             for (int i = 0; i < N; i++) {
114                 PersistableBundle bundle = new PersistableBundle();
115                 if (mConditions.get(i).saveState(bundle)) {
116                     serializer.startTag("", TAG_CONDITION);
117                     final String clz = mConditions.get(i).getClass().getSimpleName();
118                     serializer.attribute("", ATTR_CLASS, clz);
119                     bundle.saveToXml(serializer);
120                     serializer.endTag("", TAG_CONDITION);
121                 }
122             }
123
124             serializer.endTag("", TAG_CONDITIONS);
125             serializer.flush();
126             writer.close();
127         } catch (XmlPullParserException | IOException e) {
128             Log.w(TAG, "Problem writing " + FILE_NAME, e);
129         }
130     }
131
132     private void addMissingConditions(ArrayList<Condition> conditions) {
133         addIfMissing(AirplaneModeCondition.class, conditions);
134         addIfMissing(HotspotCondition.class, conditions);
135         addIfMissing(DndCondition.class, conditions);
136         addIfMissing(BatterySaverCondition.class, conditions);
137         addIfMissing(CellularDataCondition.class, conditions);
138         addIfMissing(BackgroundDataCondition.class, conditions);
139         addIfMissing(WorkModeCondition.class, conditions);
140         Collections.sort(conditions, CONDITION_COMPARATOR);
141     }
142
143     private void addIfMissing(Class<? extends Condition> clz, ArrayList<Condition> conditions) {
144         if (getCondition(clz, conditions) == null) {
145             if (DEBUG) Log.d(TAG, "Adding missing " + clz.getName());
146             conditions.add(createCondition(clz));
147         }
148     }
149
150     private Condition createCondition(Class<?> clz) {
151         if (AirplaneModeCondition.class == clz) {
152             return new AirplaneModeCondition(this);
153         } else if (HotspotCondition.class == clz) {
154             return new HotspotCondition(this);
155         } else if (DndCondition.class == clz) {
156             return new DndCondition(this);
157         } else if (BatterySaverCondition.class == clz) {
158             return new BatterySaverCondition(this);
159         } else if (CellularDataCondition.class == clz) {
160             return new CellularDataCondition(this);
161         } else if (BackgroundDataCondition.class == clz) {
162             return new BackgroundDataCondition(this);
163         } else if (WorkModeCondition.class == clz) {
164             return new WorkModeCondition(this);
165         }
166         throw new RuntimeException("Unexpected Condition " + clz);
167     }
168
169     Context getContext() {
170         return mContext;
171     }
172
173     public <T extends Condition> T getCondition(Class<T> clz) {
174         return getCondition(clz, mConditions);
175     }
176
177     private <T extends Condition> T getCondition(Class<T> clz, List<Condition> conditions) {
178         final int N = conditions.size();
179         for (int i = 0; i < N; i++) {
180             if (clz.equals(conditions.get(i).getClass())) {
181                 return (T) conditions.get(i);
182             }
183         }
184         return null;
185     }
186
187     public List<Condition> getConditions() {
188         return mConditions;
189     }
190
191     public List<Condition> getVisibleConditions() {
192         List<Condition> conditions = new ArrayList<>();
193         final int N = mConditions.size();
194         for (int i = 0; i < N; i++) {
195             if (mConditions.get(i).shouldShow()) {
196                 conditions.add(mConditions.get(i));
197             }
198         }
199         return conditions;
200     }
201
202     public void notifyChanged(Condition condition) {
203         saveToXml();
204         Collections.sort(mConditions, CONDITION_COMPARATOR);
205         final int N = mListeners.size();
206         for (int i = 0; i < N; i++) {
207             mListeners.get(i).onConditionsChanged();
208         }
209     }
210
211     public void addListener(ConditionListener listener) {
212         mListeners.add(listener);
213         listener.onConditionsChanged();
214     }
215
216     public void remListener(ConditionListener listener) {
217         mListeners.remove(listener);
218     }
219
220     private class ConditionLoader extends AsyncTask<Void, Void, ArrayList<Condition>> {
221         @Override
222         protected ArrayList<Condition> doInBackground(Void... params) {
223             ArrayList<Condition> conditions = new ArrayList<>();
224             mXmlFile = new File(mContext.getFilesDir(), FILE_NAME);
225             if (mXmlFile.exists()) {
226                 readFromXml(mXmlFile, conditions);
227             }
228             addMissingConditions(conditions);
229             return conditions;
230         }
231
232         @Override
233         protected void onPostExecute(ArrayList<Condition> conditions) {
234             mConditions.clear();
235             mConditions.addAll(conditions);
236             final int N = mListeners.size();
237             for (int i = 0; i < N; i++) {
238                 mListeners.get(i).onConditionsChanged();
239             }
240         }
241     }
242
243     public static ConditionManager get(Context context) {
244         if (sInstance == null) {
245             sInstance = new ConditionManager(context.getApplicationContext());
246         }
247         return sInstance;
248     }
249
250     public interface ConditionListener {
251         void onConditionsChanged();
252     }
253
254     private static final Comparator<Condition> CONDITION_COMPARATOR = new Comparator<Condition>() {
255         @Override
256         public int compare(Condition lhs, Condition rhs) {
257             return Long.compare(lhs.getLastChange(), rhs.getLastChange());
258         }
259     };
260 }