Android 将MAP格式数据写入XML 将XMP文件读MAP数据格式中

 其中涉及的部分类可以自行查询,如: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<String, AppPowerSaveConfig> 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<String, AppPowerSaveConfig> 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<String, AppPowerSaveConfig> 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<PowerGuruAlarmInfo>();
                        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
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<app_powersave_config>
<package name="com.ss.android.ugc.aweme" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
<package name="com.android.camera2" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="4" />
<package name="com.fadisu.cpurun" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
<package name="com.pp.assistant" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="0" />
<package name="com.sprd.sleepwakeuptest" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="sprdtest.message" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.xinlian.bitz" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="1" />
<package name="com.spreadtrum.itestapp" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.google.android.gms" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="0" consumertype="4" />
<package name="com.sprd.bmte.coulomb" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="0" secondarylaunch="0" lockscreencleanup="2" consumertype="0" />
<package name="com.comcat.activity" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.jio.emiddleware" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.tencent.qqlive" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="1" />
<package name="com.greenpoint.android.mc10086.activity" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="2" secondarylaunch="2" lockscreencleanup="2" consumertype="0" />
<package name="com.spreadst.validdate" optimize="1" alarm="0" wakelock="0" network="0" autolaunch="1" secondarylaunch="1" lockscreencleanup="1" consumertype="0" />
</app_powersave_config>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值