2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.dashboard.conditional;
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;
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;
36 public class ConditionManager {
38 private static final String TAG = "ConditionManager";
40 private static final boolean DEBUG = false;
42 private static final String PKG = "com.android.settings.dashboard.conditional.";
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";
49 private static ConditionManager sInstance;
51 private final Context mContext;
52 private final ArrayList<Condition> mConditions;
53 private File mXmlFile;
55 private final ArrayList<ConditionListener> mListeners = new ArrayList<>();
57 private ConditionManager(Context context) {
59 mConditions = new ArrayList<>();
60 new ConditionLoader().execute();
63 public void refreshAll() {
64 final int N = mConditions.size();
65 for (int i = 0; i < N; i++) {
66 mConditions.get(i).refreshState();
70 private void readFromXml(File xmlFile, ArrayList<Condition> conditions) {
71 if (DEBUG) Log.d(TAG, "Reading from " + xmlFile.toString());
73 XmlPullParser parser = Xml.newPullParser();
74 FileReader in = new FileReader(xmlFile);
76 int state = parser.getEventType();
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)) {
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) {
94 state = parser.next();
97 } catch (XmlPullParserException | IOException | ClassNotFoundException e) {
98 Log.w(TAG, "Problem reading " + FILE_NAME, e);
102 private void saveToXml() {
103 if (DEBUG) Log.d(TAG, "Writing to " + mXmlFile.toString());
105 XmlSerializer serializer = Xml.newSerializer();
106 FileWriter writer = new FileWriter(mXmlFile);
107 serializer.setOutput(writer);
109 serializer.startDocument("UTF-8", true);
110 serializer.startTag("", TAG_CONDITIONS);
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);
124 serializer.endTag("", TAG_CONDITIONS);
127 } catch (XmlPullParserException | IOException e) {
128 Log.w(TAG, "Problem writing " + FILE_NAME, e);
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);
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));
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);
166 throw new RuntimeException("Unexpected Condition " + clz);
169 Context getContext() {
173 public <T extends Condition> T getCondition(Class<T> clz) {
174 return getCondition(clz, mConditions);
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);
187 public List<Condition> getConditions() {
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));
202 public void notifyChanged(Condition condition) {
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();
211 public void addListener(ConditionListener listener) {
212 mListeners.add(listener);
213 listener.onConditionsChanged();
216 public void remListener(ConditionListener listener) {
217 mListeners.remove(listener);
220 private class ConditionLoader extends AsyncTask<Void, Void, ArrayList<Condition>> {
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);
228 addMissingConditions(conditions);
233 protected void onPostExecute(ArrayList<Condition> conditions) {
235 mConditions.addAll(conditions);
236 final int N = mListeners.size();
237 for (int i = 0; i < N; i++) {
238 mListeners.get(i).onConditionsChanged();
243 public static ConditionManager get(Context context) {
244 if (sInstance == null) {
245 sInstance = new ConditionManager(context.getApplicationContext());
250 public interface ConditionListener {
251 void onConditionsChanged();
254 private static final Comparator<Condition> CONDITION_COMPARATOR = new Comparator<Condition>() {
256 public int compare(Condition lhs, Condition rhs) {
257 return Long.compare(lhs.getLastChange(), rhs.getLastChange());