下面列出了怎么用android.app.backup.BackupDataInput的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
Log.i(TAG, "Restoring from backup");
while (data.readNextHeader()) {
String key = data.getKey();
Log.d(TAG, "Restoring entity " + key);
if (key.equals(PREFERENCES_ENTITY)) {
restorePreferences(data);
} else {
Log.e(TAG, "Found unknown backup entity: " + key);
data.skipEntityData();
}
}
Log.i(TAG, "Done restoring from backup");
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
// Let the restore operation go through
super.onRestore(data, appVersionCode, newState);
// Remove the preferences that we don't want restored.
final SharedPreferences.Editor prefEditor = getSharedPreferences(
getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit();
for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) {
prefEditor.remove(key);
}
// Flush the changes to disk.
prefEditor.commit();
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
// Let the restore operation go through
super.onRestore(data, appVersionCode, newState);
// Remove the preferences that we don't want restored.
final SharedPreferences.Editor prefEditor = getSharedPreferences(
getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit();
for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) {
prefEditor.remove(key);
}
// Flush the changes to disk.
prefEditor.commit();
}
/**
* Adding locking around the file rewrite that happens during restore is
* similarly straightforward.
*/
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
// Hold the lock while the FileBackupHelper restores the file from
// the data provided here.
synchronized (sDataLock) {
super.onRestore(data, appVersionCode, newState);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
Log.d(THIS_FILE, "App version code : " + appVersionCode);
super.onRestore(data, appVersionCode, newState);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
// Let the restore operation go through
super.onRestore(data, appVersionCode, newState);
// Remove the preferences that we don't want restored.
final SharedPreferences.Editor prefEditor = getSharedPreferences(
getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit();
for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) {
prefEditor.remove(key);
}
// Flush the changes to disk.
prefEditor.commit();
}
/**
* Restores all preferences from the backup.
*
* @param data the backup data to read from
* @throws IOException if there are any errors while reading
*/
private void restorePreferences(BackupDataInput data) throws IOException {
int dataSize = data.getDataSize();
byte[] dataBuffer = new byte[dataSize];
int read = data.readEntityData(dataBuffer, 0, dataSize);
if (read != dataSize) {
throw new IOException("Failed to read all the preferences data");
}
SharedPreferences preferences = this.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
PreferenceBackupHelper importer = createPreferenceBackupHelper();
importer.importPreferences(dataBuffer, preferences);
}
void incorporateWifiSupplicant(BackupDataInput data) {
restoredSupplicantData = new byte[data.getDataSize()];
if (restoredSupplicantData.length <= 0) return;
try {
data.readEntityData(restoredSupplicantData, 0, data.getDataSize());
} catch (IOException e) {
Log.w(TAG, "Unable to read supplicant data");
restoredSupplicantData = null;
}
}
void incorporateWifiConfigFile(BackupDataInput data) {
restoredWifiConfigFile = new byte[data.getDataSize()];
if (restoredWifiConfigFile.length <= 0) return;
try {
data.readEntityData(restoredWifiConfigFile, 0, data.getDataSize());
} catch (IOException e) {
Log.w(TAG, "Unable to read config file");
restoredWifiConfigFile = null;
}
}
private void restoreSettings(BackupDataInput data, Uri contentUri,
HashSet<String> movedToGlobal) {
byte[] settings = new byte[data.getDataSize()];
try {
data.readEntityData(settings, 0, settings.length);
} catch (IOException ioe) {
Log.e(TAG, "Couldn't read entity data");
return;
}
restoreSettings(settings, settings.length, contentUri, movedToGlobal);
}
private void restoreSettings(BackupDataInput data, Uri contentUri,
HashSet<String> movedToGlobal) {
byte[] settings = new byte[data.getDataSize()];
try {
data.readEntityData(settings, 0, settings.length);
} catch (IOException ioe) {
Log.e(TAG, "Couldn't read entity data");
return;
}
restoreSettings(settings, settings.length, contentUri, movedToGlobal);
}
private void restoreLockSettings(BackupDataInput data) {
final byte[] settings = new byte[data.getDataSize()];
try {
data.readEntityData(settings, 0, settings.length);
} catch (IOException ioe) {
Log.e(TAG, "Couldn't read entity data");
return;
}
restoreLockSettings(settings, settings.length);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
super.onRestore(data, appVersionCode, newState);
SharedPreferences.Editor editor = Util.getPreferences(this).edit();
editor.remove(Constants.PREFERENCES_KEY_CACHE_LOCATION);
editor.remove(Constants.CACHE_AUDIO_SESSION_ID);
editor.apply();
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
if (!Utilities.isLmpOrAbove()) {
// No restore for old devices.
Log.i(TAG, "You shall not pass!!!");
Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
return;
}
// Clear dB before restore
LauncherAppState.getLauncherProvider().createEmptyDB();
boolean hasData;
try {
super.onRestore(data, appVersionCode, newState);
// If no favorite was migrated, clear the data and start fresh.
final Cursor c = getContentResolver().query(
LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, null, null, null, null);
hasData = c.moveToNext();
c.close();
} catch (Exception e) {
// If the restore fails, we should do a fresh start.
Log.e(TAG, "Restore failed", e);
hasData = false;
}
if (hasData && mHelper.restoreSuccessful) {
LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
} else {
if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
LauncherAppState.getLauncherProvider().createEmptyDB();
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
// Let the restore operation go through
super.onRestore(data, appVersionCode, newState);
// Remove the preferences that we don't want restored.
final SharedPreferences.Editor prefEditor = getSharedPreferences(
getPackageName() + PREF_SUFFIX, MODE_PRIVATE).edit();
for (final String key : LocalSettingsConstants.PREFS_TO_SKIP_RESTORING) {
prefEditor.remove(key);
}
// Flush the changes to disk.
prefEditor.commit();
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
Log.i(TAG, "Restore from backup started");
super.onRestore(data, appVersionCode, newState);
PreferencesUtils.patchAfterRestore(this);
Log.i(TAG, "Restore from backup finished");
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
// Hold the lock while the FileBackupHelper restores the file
synchronized (UserData.sFileBackupLock) {
super.onRestore(data, appVersionCode, newState);
}
}
@Override
public void onRestore(
BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) {
// Doesn't do incremental backup/restore
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
synchronized (DatabaseHelper.DatabaseFileLock) {
super.onRestore(data, appVersionCode, newState);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
super.onRestore(data, appVersionCode, newState);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
// No implementation needed for Android 6.0 Auto Backup. Used only on older versions of
// Android Backup
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
synchronized (DbHelper.sLock) {
super.onRestore(data, appVersionCode, newState);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
super.onRestore(data, appVersionCode, newState);
Util.getPreferences(this).edit().remove(Constants.PREFERENCES_KEY_CACHE_LOCATION).apply();
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
HashSet<String> movedToGlobal = new HashSet<String>();
Settings.System.getMovedKeys(movedToGlobal);
Settings.Secure.getMovedKeys(movedToGlobal);
while (data.readNextHeader()) {
final String key = data.getKey();
final int size = data.getDataSize();
if (KEY_SYSTEM.equals(key)) {
restoreSettings(data, Settings.System.CONTENT_URI, movedToGlobal);
mSettingsHelper.applyAudioSettings();
} else if (KEY_SECURE.equals(key)) {
restoreSettings(data, Settings.Secure.CONTENT_URI, movedToGlobal);
} else if (KEY_GLOBAL.equals(key)) {
restoreSettings(data, Settings.Global.CONTENT_URI, null);
} else if (KEY_WIFI_SUPPLICANT.equals(key)) {
initWifiRestoreIfNecessary();
mWifiRestore.incorporateWifiSupplicant(data);
} else if (KEY_LOCALE.equals(key)) {
byte[] localeData = new byte[size];
data.readEntityData(localeData, 0, size);
mSettingsHelper.setLocaleData(localeData, size);
} else if (KEY_WIFI_CONFIG.equals(key)) {
initWifiRestoreIfNecessary();
mWifiRestore.incorporateWifiConfigFile(data);
} else {
data.skipEntityData();
}
}
// If we have wifi data to restore, post a runnable to perform the
// bounce-and-update operation a little ways in the future.
if (mWifiRestore != null) {
long wifiBounceDelayMillis = Settings.Global.getLong(
getContentResolver(),
Settings.Global.WIFI_BOUNCE_DELAY_OVERRIDE_MS,
WIFI_BOUNCE_DELAY_MILLIS);
new Handler(getMainLooper()).postDelayed(mWifiRestore, wifiBounceDelayMillis);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
HashSet<String> movedToGlobal = new HashSet<String>();
Settings.System.getMovedToGlobalSettings(movedToGlobal);
Settings.Secure.getMovedToGlobalSettings(movedToGlobal);
byte[] restoredWifiSupplicantData = null;
byte[] restoredWifiIpConfigData = null;
while (data.readNextHeader()) {
final String key = data.getKey();
final int size = data.getDataSize();
switch (key) {
case KEY_SYSTEM :
restoreSettings(data, Settings.System.CONTENT_URI, movedToGlobal);
mSettingsHelper.applyAudioSettings();
break;
case KEY_SECURE :
restoreSettings(data, Settings.Secure.CONTENT_URI, movedToGlobal);
break;
case KEY_GLOBAL :
restoreSettings(data, Settings.Global.CONTENT_URI, null);
break;
case KEY_WIFI_SUPPLICANT :
restoredWifiSupplicantData = new byte[size];
data.readEntityData(restoredWifiSupplicantData, 0, size);
break;
case KEY_LOCALE :
byte[] localeData = new byte[size];
data.readEntityData(localeData, 0, size);
mSettingsHelper.setLocaleData(localeData, size);
break;
case KEY_WIFI_CONFIG :
restoredWifiIpConfigData = new byte[size];
data.readEntityData(restoredWifiIpConfigData, 0, size);
break;
case KEY_LOCK_SETTINGS :
restoreLockSettings(data);
break;
case KEY_SOFTAP_CONFIG :
byte[] softapData = new byte[size];
data.readEntityData(softapData, 0, size);
restoreSoftApConfiguration(softapData);
break;
case KEY_NETWORK_POLICIES:
byte[] netPoliciesData = new byte[size];
data.readEntityData(netPoliciesData, 0, size);
restoreNetworkPolicies(netPoliciesData);
break;
case KEY_WIFI_NEW_CONFIG:
byte[] restoredWifiNewConfigData = new byte[size];
data.readEntityData(restoredWifiNewConfigData, 0, size);
restoreNewWifiConfigData(restoredWifiNewConfigData);
break;
default :
data.skipEntityData();
}
}
// Do this at the end so that we also pull in the ipconfig data.
if (restoredWifiSupplicantData != null) {
restoreSupplicantWifiConfigData(
restoredWifiSupplicantData, restoredWifiIpConfigData);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
if (!Utilities.ATLEAST_LOLLIPOP) {
// No restore for old devices.
Log.i(TAG, "You shall not pass!!!");
Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
return;
}
// Clear dB before restore
LauncherAppState.getLauncherProvider().createEmptyDB();
boolean hasData;
try {
super.onRestore(data, appVersionCode, newState);
// If no favorite was migrated, clear the data and start fresh.
final Cursor c = getContentResolver().query(
LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
hasData = c.moveToNext();
c.close();
} catch (Exception e) {
// If the restore fails, we should do a fresh start.
Log.e(TAG, "Restore failed", e);
hasData = false;
}
if (hasData && mHelper.restoreSuccessful) {
LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
// Rank was added in v4.
if (mHelper.restoredBackupVersion <= 3) {
LauncherAppState.getLauncherProvider().updateFolderItemsRank();
}
if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
MigrateFromRestoreTask.markForMigration(getApplicationContext(),
(int) mHelper.migrationCompatibleProfileData.desktopCols,
(int) mHelper.migrationCompatibleProfileData.desktopRows,
mHelper.widgetSizes);
}
LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
} else {
if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
LauncherAppState.getLauncherProvider().createEmptyDB();
}
}
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) {}