HugeBackupActivitypublic class HugeBackupActivity extends android.app.Activity Deliberately back up waaaaaaay too much data. Cloned with some alterations
from the Backup/Restore sample application. |
Fields Summary |
---|
static final String | TAG | static final Object[] | sDataLockWe serialize access to our persistent data through a global static
object. This ensures that in the unlikely event of the our backup/restore
agent running to perform a backup while our UI is updating the file, the
agent will not accidentally read partially-written data.
Curious but true: a zero-length array is slightly lighter-weight than
merely allocating an Object, and can still be synchronized on. | static final String | DATA_FILE_NAMEAlso supply a global standard file name for everyone to use | android.widget.RadioGroup | mFillingGroupThe various bits of UI that the user can manipulate | android.widget.CheckBox | mAddMayoCheckbox | android.widget.CheckBox | mAddTomatoCheckbox | File | mDataFileCache a reference to our persistent data file | android.app.backup.BackupManager | mBackupManagerAlso cache a reference to the Backup Manager |
Methods Summary |
---|
public void | onCreate(android.os.Bundle savedInstanceState)Set up the activity and populate its UI from the persistent data.
super.onCreate(savedInstanceState);
/** Establish the activity's UI */
setContentView(R.layout.backup_restore);
/** Once the UI has been inflated, cache the controls for later */
mFillingGroup = (RadioGroup) findViewById(R.id.filling_group);
mAddMayoCheckbox = (CheckBox) findViewById(R.id.mayo);
mAddTomatoCheckbox = (CheckBox) findViewById(R.id.tomato);
/** Set up our file bookkeeping */
mDataFile = new File(getFilesDir(), HugeBackupActivity.DATA_FILE_NAME);
/** It is handy to keep a BackupManager cached */
mBackupManager = new BackupManager(this);
/**
* Finally, build the UI from the persistent store
*/
populateUI();
| public void | onRestoreButtonClick(android.view.View v)Click handler, designated in the layout, that runs a restore of the app's
most recent data when the button is pressed.
Log.v(TAG, "Requesting restore of our most recent data");
mBackupManager.requestRestore(
new RestoreObserver() {
public void restoreFinished(int error) {
/** Done with the restore! Now draw the new state of our data */
Log.v(TAG, "Restore finished, error = " + error);
populateUI();
}
}
);
| void | populateUI()Configure the UI based on our persistent data, creating the
data file and establishing defaults if necessary.
RandomAccessFile file;
// Default values in case there's no data file yet
int whichFilling = R.id.pastrami;
boolean addMayo = false;
boolean addTomato = false;
/** Hold the data-access lock around access to the file */
synchronized (HugeBackupActivity.sDataLock) {
boolean exists = mDataFile.exists();
try {
file = new RandomAccessFile(mDataFile, "rw");
if (exists) {
Log.v(TAG, "datafile exists");
whichFilling = file.readInt();
addMayo = file.readBoolean();
addTomato = file.readBoolean();
Log.v(TAG, " mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
} else {
// The default values were configured above: write them
// to the newly-created file.
Log.v(TAG, "creating default datafile");
writeDataToFileLocked(file,
addMayo, addTomato, whichFilling);
// We also need to perform an initial backup; ask for one
mBackupManager.dataChanged();
}
} catch (IOException ioe) {
}
}
/** Now that we've processed the file, build the UI outside the lock */
mFillingGroup.check(whichFilling);
mAddMayoCheckbox.setChecked(addMayo);
mAddTomatoCheckbox.setChecked(addTomato);
/**
* We also want to record the new state when the user makes changes,
* so install simple observers that do this
*/
mFillingGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,
int checkedId) {
// As with the checkbox listeners, rewrite the
// entire state file
Log.v(TAG, "New radio item selected: " + checkedId);
recordNewUIState();
}
});
CompoundButton.OnCheckedChangeListener checkListener
= new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// Whichever one is altered, we rewrite the entire UI state
Log.v(TAG, "Checkbox toggled: " + buttonView);
recordNewUIState();
}
};
mAddMayoCheckbox.setOnCheckedChangeListener(checkListener);
mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener);
| void | recordNewUIState()Another helper; this one reads the current UI state and writes that
to the persistent store, then tells the backup manager that we need
a backup.
boolean addMayo = mAddMayoCheckbox.isChecked();
boolean addTomato = mAddTomatoCheckbox.isChecked();
int whichFilling = mFillingGroup.getCheckedRadioButtonId();
try {
synchronized (HugeBackupActivity.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
writeDataToFileLocked(file, addMayo, addTomato, whichFilling);
}
} catch (IOException e) {
Log.e(TAG, "Unable to record new UI state");
}
mBackupManager.dataChanged();
| void | writeDataToFileLocked(java.io.RandomAccessFile file, boolean addMayo, boolean addTomato, int whichFilling)Handy helper routine to write the UI data to a file.
file.setLength(0L);
file.writeInt(whichFilling);
file.writeBoolean(addMayo);
file.writeBoolean(addTomato);
Log.v(TAG, "NEW STATE: mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
|
|