Search in sources :

Example 96 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class FastXmlSerializerTest method checkPreserved.

private boolean checkPreserved(String description, String str) {
    boolean ok = true;
    byte[] data;
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final XmlSerializer out = new FastXmlSerializer();
        out.setOutput(baos, StandardCharsets.UTF_16.name());
        out.startDocument(null, true);
        out.startTag(null, ROOT_TAG);
        out.attribute(null, ATTR, str);
        out.text(str);
        out.endTag(null, ROOT_TAG);
        out.endDocument();
        baos.flush();
        data = baos.toByteArray();
    } catch (Exception e) {
        Log.e(TAG, "Unable to serialize: " + description, e);
        return false;
    }
    if (ENABLE_DUMP) {
        Log.d(TAG, "Dump:");
        Log.d(TAG, new String(data));
    }
    try (final ByteArrayInputStream baos = new ByteArrayInputStream(data)) {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(baos, StandardCharsets.UTF_16.name());
        int type;
        String tag = null;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                tag = parser.getName();
                if (ROOT_TAG.equals(tag)) {
                    String read = parser.getAttributeValue(null, ATTR);
                    if (!str.equals(read)) {
                        Log.e(TAG, "Attribute not preserved: " + description + " input=\"" + str + "\", but read=\"" + read + "\"");
                        ok = false;
                    }
                }
            }
            if (type == XmlPullParser.TEXT && ROOT_TAG.equals(tag)) {
                String read = parser.getText();
                if (!str.equals(parser.getText())) {
                    Log.e(TAG, "Text not preserved: " + description + " input=\"" + str + "\", but read=\"" + read + "\"");
                    ok = false;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to parse: " + description, e);
        return false;
    }
    return ok;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 97 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class BatteryStatsImpl method recordDailyStatsLocked.

public void recordDailyStatsLocked() {
    DailyItem item = new DailyItem();
    item.mStartTime = mDailyStartTime;
    item.mEndTime = System.currentTimeMillis();
    boolean hasData = false;
    if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
        hasData = true;
        item.mDischargeSteps = new LevelStepTracker(mDailyDischargeStepTracker.mNumStepDurations, mDailyDischargeStepTracker.mStepDurations);
    }
    if (mDailyChargeStepTracker.mNumStepDurations > 0) {
        hasData = true;
        item.mChargeSteps = new LevelStepTracker(mDailyChargeStepTracker.mNumStepDurations, mDailyChargeStepTracker.mStepDurations);
    }
    if (mDailyPackageChanges != null) {
        hasData = true;
        item.mPackageChanges = mDailyPackageChanges;
        mDailyPackageChanges = null;
    }
    mDailyDischargeStepTracker.init();
    mDailyChargeStepTracker.init();
    updateDailyDeadlineLocked();
    if (hasData) {
        mDailyItems.add(item);
        while (mDailyItems.size() > MAX_DAILY_ITEMS) {
            mDailyItems.remove(0);
        }
        final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(memStream, StandardCharsets.UTF_8.name());
            writeDailyItemsLocked(out);
            BackgroundThread.getHandler().post(new Runnable() {

                @Override
                public void run() {
                    synchronized (mCheckinFile) {
                        FileOutputStream stream = null;
                        try {
                            stream = mDailyFile.startWrite();
                            memStream.writeTo(stream);
                            stream.flush();
                            FileUtils.sync(stream);
                            stream.close();
                            mDailyFile.finishWrite(stream);
                        } catch (IOException e) {
                            Slog.w("BatteryStats", "Error writing battery daily items", e);
                            mDailyFile.failWrite(stream);
                        }
                    }
                }
            });
        } catch (IOException e) {
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 98 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class AccountManagerBackupHelper method backupAccountAccessPermissions.

public byte[] backupAccountAccessPermissions(int userId) {
    final AccountManagerService.UserAccounts accounts = mAccountManagerService.getUserAccounts(userId);
    synchronized (accounts.cacheLock) {
        SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
        try (Cursor cursor = db.rawQuery(ACCOUNT_ACCESS_GRANTS, null)) {
            if (cursor == null || !cursor.moveToFirst()) {
                return null;
            }
            final int nameColumnIdx = cursor.getColumnIndex(AccountManagerService.ACCOUNTS_NAME);
            final int uidColumnIdx = cursor.getColumnIndex(AccountManagerService.GRANTS_GRANTEE_UID);
            ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            try {
                final XmlSerializer serializer = new FastXmlSerializer();
                serializer.setOutput(dataStream, StandardCharsets.UTF_8.name());
                serializer.startDocument(null, true);
                serializer.startTag(null, TAG_PERMISSIONS);
                PackageManager packageManager = mAccountManagerService.mContext.getPackageManager();
                do {
                    final String accountName = cursor.getString(nameColumnIdx);
                    final int uid = cursor.getInt(uidColumnIdx);
                    final String[] packageNames = packageManager.getPackagesForUid(uid);
                    if (packageNames == null) {
                        continue;
                    }
                    for (String packageName : packageNames) {
                        String digest = PackageUtils.computePackageCertSha256Digest(packageManager, packageName, userId);
                        if (digest != null) {
                            serializer.startTag(null, TAG_PERMISSION);
                            serializer.attribute(null, ATTR_ACCOUNT_SHA_256, PackageUtils.computeSha256Digest(accountName.getBytes()));
                            serializer.attribute(null, ATTR_PACKAGE, packageName);
                            serializer.attribute(null, ATTR_DIGEST, digest);
                            serializer.endTag(null, TAG_PERMISSION);
                        }
                    }
                } while (cursor.moveToNext());
                serializer.endTag(null, TAG_PERMISSIONS);
                serializer.endDocument();
                serializer.flush();
            } catch (IOException e) {
                Log.e(TAG, "Error backing up account access grants", e);
                return null;
            }
            return dataStream.toByteArray();
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) PackageManager(android.content.pm.PackageManager) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Cursor(android.database.Cursor) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 99 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class AccessibilityNodeInfoDumper method dumpWindowToFile.

/**
     * Using {@link AccessibilityNodeInfo} this method will walk the layout hierarchy
     * and generates an xml dump to the location specified by <code>dumpFile</code>
     * @param root The root accessibility node.
     * @param dumpFile The file to dump to.
     * @param rotation The rotaion of current display
     * @param width The pixel width of current display
     * @param height The pixel height of current display
     */
public static void dumpWindowToFile(AccessibilityNodeInfo root, File dumpFile, int rotation, int width, int height) {
    if (root == null) {
        return;
    }
    final long startTime = SystemClock.uptimeMillis();
    try {
        FileWriter writer = new FileWriter(dumpFile);
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter stringWriter = new StringWriter();
        serializer.setOutput(stringWriter);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "hierarchy");
        serializer.attribute("", "rotation", Integer.toString(rotation));
        dumpNodeRec(root, serializer, 0, width, height);
        serializer.endTag("", "hierarchy");
        serializer.endDocument();
        writer.write(stringWriter.toString());
        writer.close();
    } catch (IOException e) {
        Log.e(LOGTAG, "failed to dump window to file", e);
    }
    final long endTime = SystemClock.uptimeMillis();
    Log.w(LOGTAG, "Fetch time: " + (endTime - startTime) + "ms");
}
Also used : StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 100 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project AntennaPod by AntennaPod.

the class OpmlWriter method writeDocument.

/**
	 * Takes a list of feeds and a writer and writes those into an OPML
	 * document.
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 * @throws IllegalArgumentException
	 */
@Override
public void writeDocument(List<Feed> feeds, Writer writer) throws IllegalArgumentException, IllegalStateException, IOException {
    Log.d(TAG, "Starting to write document");
    XmlSerializer xs = Xml.newSerializer();
    xs.setFeature(OpmlSymbols.XML_FEATURE_INDENT_OUTPUT, true);
    xs.setOutput(writer);
    xs.startDocument(ENCODING, false);
    xs.startTag(null, OpmlSymbols.OPML);
    xs.attribute(null, OpmlSymbols.VERSION, OPML_VERSION);
    xs.startTag(null, OpmlSymbols.HEAD);
    xs.startTag(null, OpmlSymbols.TITLE);
    xs.text(OPML_TITLE);
    xs.endTag(null, OpmlSymbols.TITLE);
    xs.startTag(null, OpmlSymbols.DATE_CREATED);
    xs.text(DateUtils.formatRFC822Date(new Date()));
    xs.endTag(null, OpmlSymbols.DATE_CREATED);
    xs.endTag(null, OpmlSymbols.HEAD);
    xs.startTag(null, OpmlSymbols.BODY);
    for (Feed feed : feeds) {
        xs.startTag(null, OpmlSymbols.OUTLINE);
        xs.attribute(null, OpmlSymbols.TEXT, feed.getTitle());
        xs.attribute(null, OpmlSymbols.TITLE, feed.getTitle());
        if (feed.getType() != null) {
            xs.attribute(null, OpmlSymbols.TYPE, feed.getType());
        }
        xs.attribute(null, OpmlSymbols.XMLURL, feed.getDownload_url());
        if (feed.getLink() != null) {
            xs.attribute(null, OpmlSymbols.HTMLURL, feed.getLink());
        }
        xs.endTag(null, OpmlSymbols.OUTLINE);
    }
    xs.endTag(null, OpmlSymbols.BODY);
    xs.endTag(null, OpmlSymbols.OPML);
    xs.endDocument();
    Log.d(TAG, "Finished writing document");
}
Also used : Date(java.util.Date) XmlSerializer(org.xmlpull.v1.XmlSerializer) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

XmlSerializer (org.xmlpull.v1.XmlSerializer)266 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)156 IOException (java.io.IOException)151 FileOutputStream (java.io.FileOutputStream)144 BufferedOutputStream (java.io.BufferedOutputStream)57 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 AtomicFile (android.util.AtomicFile)39 RemoteException (android.os.RemoteException)29 File (java.io.File)26 FileNotFoundException (java.io.FileNotFoundException)26 StringWriter (java.io.StringWriter)26 Map (java.util.Map)23 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 UserInfo (android.content.pm.UserInfo)9 HashMap (java.util.HashMap)9 SendIntentException (android.content.IntentSender.SendIntentException)8 PackageParserException (android.content.pm.PackageParser.PackageParserException)8