Java Code Examples for android.util.AtomicFile#failWrite()
The following examples show how to use
android.util.AtomicFile#failWrite() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackageDexUsage.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@Override protected void writeInternal(Void data) { AtomicFile file = getFile(); FileOutputStream f = null; try { f = file.startWrite(); OutputStreamWriter osw = new OutputStreamWriter(f); write(osw); osw.flush(); file.finishWrite(f); } catch (IOException e) { if (f != null) { file.failWrite(f); } Slog.e(TAG, "Failed to write usage for dex files", e); } }
Example 2
Source File: UserManagerService.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
private void writeUserLP(UserData userData) { if (DBG) { debug("writeUserLP " + userData); } FileOutputStream fos = null; AtomicFile userFile = new AtomicFile(new File(mUsersDir, userData.info.id + XML_SUFFIX)); try { fos = userFile.startWrite(); final BufferedOutputStream bos = new BufferedOutputStream(fos); writeUserLP(userData, bos); userFile.finishWrite(fos); } catch (Exception ioe) { Slog.e(LOG_TAG, "Error writing user info " + userData.info.id, ioe); userFile.failWrite(fos); } }
Example 3
Source File: UserManagerService.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@VisibleForTesting @GuardedBy("mAppRestrictionsLock") static void writeApplicationRestrictionsLAr(Bundle restrictions, AtomicFile restrictionsFile) { FileOutputStream fos = null; try { fos = restrictionsFile.startWrite(); final BufferedOutputStream bos = new BufferedOutputStream(fos); 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_RESTRICTIONS); writeBundle(restrictions, serializer); serializer.endTag(null, TAG_RESTRICTIONS); serializer.endDocument(); restrictionsFile.finishWrite(fos); } catch (Exception e) { restrictionsFile.failWrite(fos); Slog.e(LOG_TAG, "Error writing application restrictions list", e); } }
Example 4
Source File: CompilerStats.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@Override protected void writeInternal(Void data) { AtomicFile file = getFile(); FileOutputStream f = null; try { f = file.startWrite(); OutputStreamWriter osw = new OutputStreamWriter(f); write(osw); osw.flush(); file.finishWrite(f); } catch (IOException e) { if (f != null) { file.failWrite(f); } Log.e(PackageManagerService.TAG, "Failed to write compiler stats", e); } }
Example 5
Source File: ShortcutService.java From android_9.0.0_r45 with Apache License 2.0 | 6 votes |
@GuardedBy("mLock") private void saveUserLocked(@UserIdInt int userId) { final File path = getUserFile(userId); if (DEBUG) { Slog.d(TAG, "Saving to " + path); } mShortcutBitmapSaver.waitForAllSavesLocked(); path.getParentFile().mkdirs(); final AtomicFile file = new AtomicFile(path); FileOutputStream os = null; try { os = file.startWrite(); saveUserInternalLocked(userId, os, /* forBackup= */ false); file.finishWrite(os); // Remove all dangling bitmap files. cleanupDanglingBitmapDirectoriesLocked(userId); } catch (XmlPullParserException | IOException e) { Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); file.failWrite(os); } }
Example 6
Source File: BrightnessTracker.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private void writeEvents() { synchronized (mEventsLock) { if (!mEventsDirty) { // Nothing to write return; } final AtomicFile writeTo = mInjector.getFile(EVENTS_FILE); if (writeTo == null) { return; } if (mEvents.isEmpty()) { if (writeTo.exists()) { writeTo.delete(); } mEventsDirty = false; } else { FileOutputStream output = null; try { output = writeTo.startWrite(); writeEventsLocked(output); writeTo.finishWrite(output); mEventsDirty = false; } catch (IOException e) { writeTo.failWrite(output); Slog.e(TAG, "Failed to write change mEvents.", e); } } } }
Example 7
Source File: BrightnessTracker.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
private void writeAmbientBrightnessStats() { final AtomicFile writeTo = mInjector.getFile(AMBIENT_BRIGHTNESS_STATS_FILE); if (writeTo == null) { return; } FileOutputStream output = null; try { output = writeTo.startWrite(); mAmbientBrightnessStatsTracker.writeStats(output); writeTo.finishWrite(output); } catch (IOException e) { writeTo.failWrite(output); Slog.e(TAG, "Failed to write ambient brightness stats.", e); } }
Example 8
Source File: FileUpdater.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@GuardedBy("mLock") private void saveDefaultValuesLocked() { final AtomicFile file = new AtomicFile(injectDefaultValuesFilename()); FileOutputStream outs = null; try { file.getBaseFile().getParentFile().mkdirs(); outs = file.startWrite(); // Write to XML XmlSerializer out = new FastXmlSerializer(); out.setOutput(outs, StandardCharsets.UTF_8.name()); out.startDocument(null, true); out.startTag(null, TAG_DEFAULT_ROOT); XmlUtils.writeMapXml(mDefaultValues, out, null); // Epilogue. out.endTag(null, TAG_DEFAULT_ROOT); out.endDocument(); // Close. file.finishWrite(outs); } catch (IOException | XmlPullParserException | RuntimeException e) { Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); file.failWrite(outs); } }
Example 9
Source File: PackageUsage.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@Override protected void writeInternal(Map<String, PackageParser.Package> packages) { AtomicFile file = getFile(); FileOutputStream f = null; try { f = file.startWrite(); BufferedOutputStream out = new BufferedOutputStream(f); FileUtils.setPermissions(file.getBaseFile().getPath(), 0640, SYSTEM_UID, PACKAGE_INFO_GID); StringBuilder sb = new StringBuilder(); sb.append(USAGE_FILE_MAGIC_VERSION_1); sb.append('\n'); out.write(sb.toString().getBytes(StandardCharsets.US_ASCII)); for (PackageParser.Package pkg : packages.values()) { if (pkg.getLatestPackageUseTimeInMills() == 0L) { continue; } sb.setLength(0); sb.append(pkg.packageName); for (long usageTimeInMillis : pkg.mLastPackageUsageTimeInMills) { sb.append(' '); sb.append(usageTimeInMillis); } sb.append('\n'); out.write(sb.toString().getBytes(StandardCharsets.US_ASCII)); } out.flush(); file.finishWrite(f); } catch (IOException e) { if (f != null) { file.failWrite(f); } Log.e(PackageManagerService.TAG, "Failed to write package usage times", e); } }
Example 10
Source File: ShortcutService.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
@GuardedBy("mLock") @VisibleForTesting void saveBaseStateLocked() { final AtomicFile file = getBaseStateFile(); if (DEBUG) { Slog.d(TAG, "Saving to " + file.getBaseFile()); } FileOutputStream outs = null; try { outs = file.startWrite(); // Write to XML XmlSerializer out = new FastXmlSerializer(); out.setOutput(outs, StandardCharsets.UTF_8.name()); out.startDocument(null, true); out.startTag(null, TAG_ROOT); // Body. writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime); // Epilogue. out.endTag(null, TAG_ROOT); out.endDocument(); // Close. file.finishWrite(outs); } catch (IOException e) { Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); file.failWrite(outs); } }
Example 11
Source File: ProcessStatsService.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
void performWriteState(long initialTime) { if (DEBUG) Slog.d(TAG, "Performing write to " + mFile.getBaseFile()); Parcel data; AtomicFile file; synchronized (mPendingWriteLock) { data = mPendingWrite; file = mPendingWriteFile; mPendingWriteCommitted = false; if (data == null) { return; } mPendingWrite = null; mPendingWriteFile = null; mWriteLock.lock(); } final long startTime = SystemClock.uptimeMillis(); FileOutputStream stream = null; try { stream = file.startWrite(); stream.write(data.marshall()); stream.flush(); file.finishWrite(stream); com.android.internal.logging.EventLogTags.writeCommitSysConfigFile( "procstats", SystemClock.uptimeMillis() - startTime + initialTime); if (DEBUG) Slog.d(TAG, "Write completed successfully!"); } catch (IOException e) { Slog.w(TAG, "Error writing process statistics", e); file.failWrite(stream); } finally { data.recycle(); trimHistoricStatesWriteLocked(); mWriteLock.unlock(); } }
Example 12
Source File: RegisteredServicesCache.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Writes services of a specified user to the file. */ private void writePersistentServicesLocked(UserServices<V> user, int userId) { if (mSerializerAndParser == null) { return; } AtomicFile atomicFile = createFileForUser(userId); FileOutputStream fos = null; try { fos = atomicFile.startWrite(); XmlSerializer out = new FastXmlSerializer(); out.setOutput(fos, StandardCharsets.UTF_8.name()); out.startDocument(null, true); out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); out.startTag(null, "services"); for (Map.Entry<V, Integer> service : user.persistentServices.entrySet()) { out.startTag(null, "service"); out.attribute(null, "uid", Integer.toString(service.getValue())); mSerializerAndParser.writeAsXml(service.getKey(), out); out.endTag(null, "service"); } out.endTag(null, "services"); out.endDocument(); atomicFile.finishWrite(fos); } catch (IOException e1) { Log.w(TAG, "Error writing accounts", e1); if (fos != null) { atomicFile.failWrite(fos); } } }
Example 13
Source File: FingerprintsUserState.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
private void doWriteState() { AtomicFile destination = new AtomicFile(mFile); ArrayList<Fingerprint> fingerprints; synchronized (this) { fingerprints = getCopy(mFingerprints); } FileOutputStream out = null; try { out = destination.startWrite(); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "utf-8"); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startDocument(null, true); serializer.startTag(null, TAG_FINGERPRINTS); final int count = fingerprints.size(); for (int i = 0; i < count; i++) { Fingerprint fp = fingerprints.get(i); serializer.startTag(null, TAG_FINGERPRINT); serializer.attribute(null, ATTR_FINGER_ID, Integer.toString(fp.getFingerId())); serializer.attribute(null, ATTR_NAME, fp.getName().toString()); serializer.attribute(null, ATTR_GROUP_ID, Integer.toString(fp.getGroupId())); serializer.attribute(null, ATTR_DEVICE_ID, Long.toString(fp.getDeviceId())); serializer.endTag(null, TAG_FINGERPRINT); } serializer.endTag(null, TAG_FINGERPRINTS); serializer.endDocument(); destination.finishWrite(out); // Any error while writing is fatal. } catch (Throwable t) { Slog.wtf(TAG, "Failed to write settings, restoring backup", t); destination.failWrite(out); throw new IllegalStateException("Failed to write fingerprints", t); } finally { IoUtils.closeQuietly(out); } }
Example 14
Source File: InstantAppRegistry.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
private void writeUninstalledInstantAppMetadata( @NonNull InstantAppInfo instantApp, @UserIdInt int userId) { File appDir = getInstantApplicationDir(instantApp.getPackageName(), userId); if (!appDir.exists() && !appDir.mkdirs()) { return; } File metadataFile = new File(appDir, INSTANT_APP_METADATA_FILE); AtomicFile destination = new AtomicFile(metadataFile); FileOutputStream out = null; try { out = destination.startWrite(); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, StandardCharsets.UTF_8.name()); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startDocument(null, true); serializer.startTag(null, TAG_PACKAGE); serializer.attribute(null, ATTR_LABEL, instantApp.loadLabel( mService.mContext.getPackageManager()).toString()); serializer.startTag(null, TAG_PERMISSIONS); for (String permission : instantApp.getRequestedPermissions()) { serializer.startTag(null, TAG_PERMISSION); serializer.attribute(null, ATTR_NAME, permission); if (ArrayUtils.contains(instantApp.getGrantedPermissions(), permission)) { serializer.attribute(null, ATTR_GRANTED, String.valueOf(true)); } serializer.endTag(null, TAG_PERMISSION); } serializer.endTag(null, TAG_PERMISSIONS); serializer.endTag(null, TAG_PACKAGE); serializer.endDocument(); destination.finishWrite(out); } catch (Throwable t) { Slog.wtf(LOG_TAG, "Failed to write instant state, restoring backup", t); destination.failWrite(out); } finally { IoUtils.closeQuietly(out); } }
Example 15
Source File: InputMethodManagerService.java From TvRemoteControl with Apache License 2.0 | 4 votes |
private static void writeAdditionalInputMethodSubtypes( HashMap<String, List<InputMethodSubtype>> allSubtypes, AtomicFile subtypesFile, HashMap<String, InputMethodInfo> methodMap) { // Safety net for the case that this function is called before methodMap is set. final boolean isSetMethodMap = methodMap != null && methodMap.size() > 0; FileOutputStream fos = null; try { fos = subtypesFile.startWrite(); final XmlSerializer out = new FastXmlSerializer(); out.setOutput(fos, "utf-8"); out.startDocument(null, true); out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); out.startTag(null, NODE_SUBTYPES); for (String imiId : allSubtypes.keySet()) { if (isSetMethodMap && !methodMap.containsKey(imiId)) { Slog.w(TAG, "IME uninstalled or not valid.: " + imiId); continue; } out.startTag(null, NODE_IMI); out.attribute(null, ATTR_ID, imiId); final List<InputMethodSubtype> subtypesList = allSubtypes.get(imiId); final int N = subtypesList.size(); for (int i = 0; i < N; ++i) { final InputMethodSubtype subtype = subtypesList.get(i); out.startTag(null, NODE_SUBTYPE); out.attribute(null, ATTR_ICON, String.valueOf(subtype.getIconResId())); out.attribute(null, ATTR_LABEL, String.valueOf(subtype.getNameResId())); out.attribute(null, ATTR_IME_SUBTYPE_LOCALE, subtype.getLocale()); out.attribute(null, ATTR_IME_SUBTYPE_MODE, subtype.getMode()); out.attribute(null, ATTR_IME_SUBTYPE_EXTRA_VALUE, subtype.getExtraValue()); out.attribute(null, ATTR_IS_AUXILIARY, String.valueOf(subtype.isAuxiliary() ? 1 : 0)); out.endTag(null, NODE_SUBTYPE); } out.endTag(null, NODE_IMI); } out.endTag(null, NODE_SUBTYPES); out.endDocument(); subtypesFile.finishWrite(fos); } catch (IOException e) { Slog.w(TAG, "Error writing subtypes", e); if (fos != null) { subtypesFile.failWrite(fos); } } }
Example 16
Source File: SettingsState.java From Study_Android_Demo with Apache License 2.0 | 4 votes |
private void doWriteState() { boolean wroteState = false; final int version; final ArrayMap<String, Setting> settings; synchronized (mLock) { version = mVersion; settings = new ArrayMap<>(mSettings); mDirty = false; mWriteScheduled = false; } synchronized (mWriteLock) { if (DEBUG_PERSISTENCE) { Slog.i(LOG_TAG, "[PERSIST START]"); } AtomicFile destination = new AtomicFile(mStatePersistFile); FileOutputStream out = null; try { out = destination.startWrite(); XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, StandardCharsets.UTF_8.name()); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startDocument(null, true); serializer.startTag(null, TAG_SETTINGS); serializer.attribute(null, ATTR_VERSION, String.valueOf(version)); final int settingCount = settings.size(); for (int i = 0; i < settingCount; i++) { Setting setting = settings.valueAt(i); writeSingleSetting(mVersion, serializer, setting.getId(), setting.getName(), setting.getValue(), setting.getDefaultValue(), setting.getPackageName(), setting.getTag(), setting.isDefaultFromSystem()); if (DEBUG_PERSISTENCE) { Slog.i(LOG_TAG, "[PERSISTED]" + setting.getName() + "=" + setting.getValue()); } } serializer.endTag(null, TAG_SETTINGS); serializer.endDocument(); destination.finishWrite(out); wroteState = true; if (DEBUG_PERSISTENCE) { Slog.i(LOG_TAG, "[PERSIST END]"); } } catch (Throwable t) { Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", t); destination.failWrite(out); } finally { IoUtils.closeQuietly(out); } } if (wroteState) { synchronized (mLock) { addHistoricalOperationLocked(HISTORICAL_OPERATION_PERSIST, null); } } }