Search in sources :

Example 41 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer 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 42 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class AppOpsService method writeState.

void writeState() {
    synchronized (mFile) {
        FileOutputStream stream;
        try {
            stream = mFile.startWrite();
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state: " + e);
            return;
        }
        SparseArray<UidState> outUidStates = null;
        synchronized (this) {
            final int uidStateCount = mUidStates.size();
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = mUidStates.valueAt(i);
                SparseIntArray opModes = uidState.opModes;
                if (opModes != null && opModes.size() > 0) {
                    UidState outUidState = new UidState(uidState.uid);
                    outUidState.opModes = opModes.clone();
                    if (outUidStates == null) {
                        outUidStates = new SparseArray<>();
                    }
                    outUidStates.put(mUidStates.keyAt(i), outUidState);
                }
            }
        }
        List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(stream, StandardCharsets.UTF_8.name());
            out.startDocument(null, true);
            out.startTag(null, "app-ops");
            final int uidStateCount = outUidStates != null ? outUidStates.size() : 0;
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = outUidStates.valueAt(i);
                if (uidState.opModes != null && uidState.opModes.size() > 0) {
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(uidState.uid));
                    SparseIntArray uidOpModes = uidState.opModes;
                    final int opCount = uidOpModes.size();
                    for (int j = 0; j < opCount; j++) {
                        final int op = uidOpModes.keyAt(j);
                        final int mode = uidOpModes.valueAt(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op));
                        out.attribute(null, "m", Integer.toString(mode));
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
            }
            if (allOps != null) {
                String lastPkg = null;
                for (int i = 0; i < allOps.size(); i++) {
                    AppOpsManager.PackageOps pkg = allOps.get(i);
                    if (!pkg.getPackageName().equals(lastPkg)) {
                        if (lastPkg != null) {
                            out.endTag(null, "pkg");
                        }
                        lastPkg = pkg.getPackageName();
                        out.startTag(null, "pkg");
                        out.attribute(null, "n", lastPkg);
                    }
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(pkg.getUid()));
                    synchronized (this) {
                        Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), false);
                        // from Ops.
                        if (ops != null) {
                            out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
                        } else {
                            out.attribute(null, "p", Boolean.toString(false));
                        }
                    }
                    List<AppOpsManager.OpEntry> ops = pkg.getOps();
                    for (int j = 0; j < ops.size(); j++) {
                        AppOpsManager.OpEntry op = ops.get(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op.getOp()));
                        if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
                            out.attribute(null, "m", Integer.toString(op.getMode()));
                        }
                        long time = op.getTime();
                        if (time != 0) {
                            out.attribute(null, "t", Long.toString(time));
                        }
                        time = op.getRejectTime();
                        if (time != 0) {
                            out.attribute(null, "r", Long.toString(time));
                        }
                        int dur = op.getDuration();
                        if (dur != 0) {
                            out.attribute(null, "d", Integer.toString(dur));
                        }
                        int proxyUid = op.getProxyUid();
                        if (proxyUid != -1) {
                            out.attribute(null, "pu", Integer.toString(proxyUid));
                        }
                        String proxyPackageName = op.getProxyPackageName();
                        if (proxyPackageName != null) {
                            out.attribute(null, "pp", proxyPackageName);
                        }
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
                if (lastPkg != null) {
                    out.endTag(null, "pkg");
                }
            }
            out.endTag(null, "app-ops");
            out.endDocument();
            mFile.finishWrite(stream);
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state, restoring backup.", e);
            mFile.failWrite(stream);
        }
    }
}
Also used : IOException(java.io.IOException) AppOpsManager(android.app.AppOpsManager) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) SparseIntArray(android.util.SparseIntArray) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 43 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class DevicePolicyManagerService method saveSettingsLocked.

private void saveSettingsLocked(int userHandle) {
    DevicePolicyData policy = getUserData(userHandle);
    JournaledFile journal = makeJournaledFile(userHandle);
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(journal.chooseForWrite(), false);
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(stream, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, "policies");
        if (policy.mRestrictionsProvider != null) {
            out.attribute(null, ATTR_PERMISSION_PROVIDER, policy.mRestrictionsProvider.flattenToString());
        }
        if (policy.mUserSetupComplete) {
            out.attribute(null, ATTR_SETUP_COMPLETE, Boolean.toString(true));
        }
        if (policy.mPaired) {
            out.attribute(null, ATTR_DEVICE_PAIRED, Boolean.toString(true));
        }
        if (policy.mDeviceProvisioningConfigApplied) {
            out.attribute(null, ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED, Boolean.toString(true));
        }
        if (policy.mUserProvisioningState != DevicePolicyManager.STATE_USER_UNMANAGED) {
            out.attribute(null, ATTR_PROVISIONING_STATE, Integer.toString(policy.mUserProvisioningState));
        }
        if (policy.mPermissionPolicy != DevicePolicyManager.PERMISSION_POLICY_PROMPT) {
            out.attribute(null, ATTR_PERMISSION_POLICY, Integer.toString(policy.mPermissionPolicy));
        }
        if (policy.mDelegatedCertInstallerPackage != null) {
            out.attribute(null, ATTR_DELEGATED_CERT_INSTALLER, policy.mDelegatedCertInstallerPackage);
        }
        if (policy.mApplicationRestrictionsManagingPackage != null) {
            out.attribute(null, ATTR_APPLICATION_RESTRICTIONS_MANAGER, policy.mApplicationRestrictionsManagingPackage);
        }
        final int N = policy.mAdminList.size();
        for (int i = 0; i < N; i++) {
            ActiveAdmin ap = policy.mAdminList.get(i);
            if (ap != null) {
                out.startTag(null, "admin");
                out.attribute(null, "name", ap.info.getComponent().flattenToString());
                ap.writeToXml(out);
                out.endTag(null, "admin");
            }
        }
        if (policy.mPasswordOwner >= 0) {
            out.startTag(null, "password-owner");
            out.attribute(null, "value", Integer.toString(policy.mPasswordOwner));
            out.endTag(null, "password-owner");
        }
        if (policy.mFailedPasswordAttempts != 0) {
            out.startTag(null, "failed-password-attempts");
            out.attribute(null, "value", Integer.toString(policy.mFailedPasswordAttempts));
            out.endTag(null, "failed-password-attempts");
        }
        // Don't save metrics for FBE devices
        if (!mInjector.storageManagerIsFileBasedEncryptionEnabled() && (policy.mActivePasswordQuality != 0 || policy.mActivePasswordLength != 0 || policy.mActivePasswordUpperCase != 0 || policy.mActivePasswordLowerCase != 0 || policy.mActivePasswordLetters != 0 || policy.mActivePasswordNumeric != 0 || policy.mActivePasswordSymbols != 0 || policy.mActivePasswordNonLetter != 0)) {
            out.startTag(null, "active-password");
            out.attribute(null, "quality", Integer.toString(policy.mActivePasswordQuality));
            out.attribute(null, "length", Integer.toString(policy.mActivePasswordLength));
            out.attribute(null, "uppercase", Integer.toString(policy.mActivePasswordUpperCase));
            out.attribute(null, "lowercase", Integer.toString(policy.mActivePasswordLowerCase));
            out.attribute(null, "letters", Integer.toString(policy.mActivePasswordLetters));
            out.attribute(null, "numeric", Integer.toString(policy.mActivePasswordNumeric));
            out.attribute(null, "symbols", Integer.toString(policy.mActivePasswordSymbols));
            out.attribute(null, "nonletter", Integer.toString(policy.mActivePasswordNonLetter));
            out.endTag(null, "active-password");
        }
        for (int i = 0; i < policy.mAcceptedCaCertificates.size(); i++) {
            out.startTag(null, TAG_ACCEPTED_CA_CERTIFICATES);
            out.attribute(null, ATTR_NAME, policy.mAcceptedCaCertificates.valueAt(i));
            out.endTag(null, TAG_ACCEPTED_CA_CERTIFICATES);
        }
        for (int i = 0; i < policy.mLockTaskPackages.size(); i++) {
            String component = policy.mLockTaskPackages.get(i);
            out.startTag(null, TAG_LOCK_TASK_COMPONENTS);
            out.attribute(null, "name", component);
            out.endTag(null, TAG_LOCK_TASK_COMPONENTS);
        }
        if (policy.mStatusBarDisabled) {
            out.startTag(null, TAG_STATUS_BAR);
            out.attribute(null, ATTR_DISABLED, Boolean.toString(policy.mStatusBarDisabled));
            out.endTag(null, TAG_STATUS_BAR);
        }
        if (policy.doNotAskCredentialsOnBoot) {
            out.startTag(null, DO_NOT_ASK_CREDENTIALS_ON_BOOT_XML);
            out.endTag(null, DO_NOT_ASK_CREDENTIALS_ON_BOOT_XML);
        }
        for (String id : policy.mAffiliationIds) {
            out.startTag(null, TAG_AFFILIATION_ID);
            out.attribute(null, "id", id);
            out.endTag(null, TAG_AFFILIATION_ID);
        }
        if (policy.mAdminBroadcastPending) {
            out.startTag(null, TAG_ADMIN_BROADCAST_PENDING);
            out.attribute(null, ATTR_VALUE, Boolean.toString(policy.mAdminBroadcastPending));
            out.endTag(null, TAG_ADMIN_BROADCAST_PENDING);
        }
        if (policy.mInitBundle != null) {
            out.startTag(null, TAG_INITIALIZATION_BUNDLE);
            policy.mInitBundle.saveToXml(out);
            out.endTag(null, TAG_INITIALIZATION_BUNDLE);
        }
        out.endTag(null, "policies");
        out.endDocument();
        stream.flush();
        FileUtils.sync(stream);
        stream.close();
        journal.commit();
        sendChangedNotification(userHandle);
    } catch (XmlPullParserException | IOException e) {
        Slog.w(LOG_TAG, "failed writing file", e);
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ex) {
        // Ignore
        }
        journal.rollback();
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) JournaledFile(com.android.internal.util.JournaledFile) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParcelableString(com.android.internal.util.ParcelableString) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 44 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class AppIdleHistory method writeAppIdleTimesLocked.

public void writeAppIdleTimesLocked(int userId) {
    FileOutputStream fos = null;
    AtomicFile appIdleFile = new AtomicFile(getUserFile(userId));
    try {
        fos = appIdleFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        FastXmlSerializer xml = new FastXmlSerializer();
        xml.setOutput(bos, StandardCharsets.UTF_8.name());
        xml.startDocument(null, true);
        xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        xml.startTag(null, TAG_PACKAGES);
        ArrayMap<String, PackageHistory> userHistory = getUserHistoryLocked(userId);
        final int N = userHistory.size();
        for (int i = 0; i < N; i++) {
            String packageName = userHistory.keyAt(i);
            PackageHistory history = userHistory.valueAt(i);
            xml.startTag(null, TAG_PACKAGE);
            xml.attribute(null, ATTR_NAME, packageName);
            xml.attribute(null, ATTR_ELAPSED_IDLE, Long.toString(history.lastUsedElapsedTime));
            xml.attribute(null, ATTR_SCREEN_IDLE, Long.toString(history.lastUsedScreenTime));
            xml.endTag(null, TAG_PACKAGE);
        }
        xml.endTag(null, TAG_PACKAGES);
        xml.endDocument();
        appIdleFile.finishWrite(fos);
    } catch (Exception e) {
        appIdleFile.failWrite(fos);
        Slog.e(TAG, "Error writing app idle file for user " + userId);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 45 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class UserManagerService method writeUserListLP.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLP() {
    if (DBG) {
        debug("writeUserList");
    }
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mGuestRestrictions) {
            UserRestrictionsUtils.writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS);
        }
        serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mRestrictionsLock) {
            UserRestrictionsUtils.writeRestrictions(serializer, mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS);
        }
        serializer.startTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        serializer.attribute(null, ATTR_ID, Integer.toString(mGlobalRestrictionOwnerUserId));
        serializer.endTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        int[] userIdsToWrite;
        synchronized (mUsersLock) {
            userIdsToWrite = new int[mUsers.size()];
            for (int i = 0; i < userIdsToWrite.length; i++) {
                UserInfo user = mUsers.valueAt(i).info;
                userIdsToWrite[i] = user.id;
            }
        }
        for (int id : userIdsToWrite) {
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        Slog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) UserInfo(android.content.pm.UserInfo) BufferedOutputStream(java.io.BufferedOutputStream) ErrnoException(android.system.ErrnoException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Aggregations

FastXmlSerializer (com.android.internal.util.FastXmlSerializer)169 XmlSerializer (org.xmlpull.v1.XmlSerializer)156 IOException (java.io.IOException)138 FileOutputStream (java.io.FileOutputStream)132 BufferedOutputStream (java.io.BufferedOutputStream)56 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)35 RemoteException (android.os.RemoteException)29 AtomicFile (android.util.AtomicFile)28 FileNotFoundException (java.io.FileNotFoundException)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Map (java.util.Map)22 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 File (java.io.File)13 HashMap (java.util.HashMap)11 UserInfo (android.content.pm.UserInfo)9 SendIntentException (android.content.IntentSender.SendIntentException)8 PackageParserException (android.content.pm.PackageParser.PackageParserException)8 Point (android.graphics.Point)8