其中涉及的部分类可以自行查询,如:AtomicFile.java FastXmlSerializer.java
private static final String APPCONFIG_FILENAME = "appPowerSaveConfig.xml";
private static final String XML_TAG_FILE = "app_powersave_config";
private static final String XML_TAG_PKG = "package";
private static final String XML_ATTRIBUTE_PKG_NAME = "name";
private static final String XML_ATTRIBUTE_PKG_OPTIMIZE = "optimize";
private static final String XML_ATTRIBUTE_PKG_ALARM = "alarm";
private static final String XML_ATTRIBUTE_PKG_WAKELOCK = "wakelock";
private static final String XML_ATTRIBUTE_PKG_NETWORK = "network";
private static final String XML_ATTRIBUTE_PKG_AUTOLAUNCH = "autolaunch";
private static final String XML_ATTRIBUTE_PKG_SECONDARYLAUNCH = "secondarylaunch";
private static final String XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP = "lockscreencleanup";
private static final String XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE = "consumertype";
//将一下MAP数据写入XML文件中
public static boolean writeConfig(Map appConfigMap) {
AtomicFile aFile = new AtomicFile(new File(new File(Environment.getDataDirectory(),
"system"), APPCONFIG_FILENAME));
FileOutputStream stream;
try {
stream = aFile.startWrite();
} catch (IOException e) {
Slog.e(TAG, "Failed to write state: " + e);
return false;
}
try {
XmlSerializer serializer = new FastXmlSerializer();
serializer.setOutput(stream, "utf-8");
serializer.startDocument(null, true);
serializer.startTag(null, XML_TAG_FILE);
if (appConfigMap != null) {
for (Map.Entry cur : appConfigMap.entrySet()) {
final String appName = cur.getKey();
final AppPowerSaveConfig config = cur.getValue();
if (config.isReset()) continue;
serializer.startTag(null, XML_TAG_PKG);
serializer.attribute(null, XML_ATTRIBUTE_PKG_NAME, appName);
serializer.attribute(null, XML_ATTRIBUTE_PKG_OPTIMIZE,
String.valueOf(config.optimize));
serializer.attribute(null, XML_ATTRIBUTE_PKG_ALARM,
String.valueOf(config.alarm));
serializer.attribute(null, XML_ATTRIBUTE_PKG_WAKELOCK,
String.valueOf(config.wakelock));
serializer.attribute(null, XML_ATTRIBUTE_PKG_NETWORK,
String.valueOf(config.network));
serializer.attribute(null, XML_ATTRIBUTE_PKG_AUTOLAUNCH,
String.valueOf(config.autoLaunch));
serializer.attribute(null, XML_ATTRIBUTE_PKG_SECONDARYLAUNCH,
String.valueOf(config.secondaryLaunch));
serializer.attribute(null, XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP,
String.valueOf(config.lockscreenCleanup));
serializer.attribute(null, XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE,
String.valueOf(config.powerConsumerType));
serializer.endTag(null, XML_TAG_PKG);
}
}
serializer.endTag(null, XML_TAG_FILE);
serializer.endDocument();
aFile.finishWrite(stream);
} catch (IOException e) {
Slog.e(TAG, "Failed to write state, restoring backup." + "exp:" + "\n" + e);
aFile.failWrite(stream);
return false;
}
return true;
}
//将XML文件写入一下MAP中
/**
* static API used to read the config from /data/system/appPowerSaveConfig.xml
* and save them in appConfigMap
* @param appConfigMap The configs read from config file will save to it
* @return Returns true for sucess.Return false for fail
*/
public static boolean readConfig(Map appConfigMap) {
AtomicFile aFile = new AtomicFile(new File(new File(Environment.getDataDirectory(),
"system"), APPCONFIG_FILENAME));
InputStream stream = null;
try {
stream = aFile.openRead();
} catch (FileNotFoundException exp) {
Slog.e(TAG, ">>>file not found," + exp);
}
if (null == stream) {
aFile = new AtomicFile(new File(new File(Environment.getRootDirectory(), "etc"),
APPCONFIG_FILENAME));
try {
stream = aFile.openRead();
} catch (FileNotFoundException exp) {
Slog.e(TAG, ">>>default file not found," + exp);
return false;
}
}
try {
String appName = null;
AppPowerSaveConfig appConfig = null;
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(stream, "UTF-8");
int event = pullParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
//retList = new ArrayList();
break;
case XmlPullParser.START_TAG:
if (XML_TAG_PKG.equals(pullParser.getName())) {
appConfig = new AppPowerSaveConfig();
appName = pullParser.getAttributeValue(null, XML_ATTRIBUTE_PKG_NAME);
appConfig.optimize = Integer.parseInt(pullParser.getAttributeValue(null,
XML_ATTRIBUTE_PKG_OPTIMIZE));
appConfig.alarm = Integer.parseInt(pullParser.getAttributeValue(null,
XML_ATTRIBUTE_PKG_ALARM));
appConfig.wakelock = Integer.parseInt(pullParser.getAttributeValue(null,
XML_ATTRIBUTE_PKG_WAKELOCK));
appConfig.network = Integer.parseInt(pullParser.getAttributeValue(null,
XML_ATTRIBUTE_PKG_NETWORK));
appConfig.autoLaunch = Integer.parseInt(pullParser.getAttributeValue(
null, XML_ATTRIBUTE_PKG_AUTOLAUNCH));
appConfig.secondaryLaunch =
Integer.parseInt(pullParser.getAttributeValue(
null, XML_ATTRIBUTE_PKG_SECONDARYLAUNCH));
appConfig.lockscreenCleanup =
Integer.parseInt(pullParser.getAttributeValue(
null, XML_ATTRIBUTE_PKG_LOCKSCREENCLEANUP));
appConfig.powerConsumerType =
Integer.parseInt(pullParser.getAttributeValue(
null, XML_ATTRIBUTE_PKG_POWERCONSUMERTYPE));
}
break;
case XmlPullParser.END_TAG:
if (XML_TAG_PKG.equals(pullParser.getName())) {
appConfigMap.put(appName, appConfig);
appConfig = null;
}
break;
}
event = pullParser.next();
}
} catch (IllegalStateException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} catch (NullPointerException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} catch (NumberFormatException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} catch (XmlPullParserException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} catch (IOException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} catch (IndexOutOfBoundsException e) {
Slog.e(TAG, "Failed parsing " + e);
return false;
} finally {
try {
stream.close();
} catch (IOException e) {
Slog.e(TAG, "Fail to close stream " + e);
return false;
} catch (Exception e) {
Slog.e(TAG, "exception at last,e: " + e);
return false;
}
}
return true;
}
XML文件格式:
xxxx:/data/system # cat appPowerSaveConfig.xml