| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.hugebackup; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.app.backup.BackupManager; |
| 21 |
import android.app.backup.RestoreObserver; |
| 22 |
import android.os.Bundle; |
| 23 |
import android.util.Log; |
| 24 |
import android.view.View; |
| 25 |
import android.widget.CheckBox; |
| 26 |
import android.widget.CompoundButton; |
| 27 |
import android.widget.RadioGroup; |
| 28 |
|
| 29 |
import java.io.File; |
| 30 |
import java.io.IOException; |
| 31 |
import java.io.RandomAccessFile; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class HugeBackupActivity extends Activity { |
| 38 |
static final String TAG = "HugeBackupActivity"; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static final Object[] sDataLock = new Object[0]; |
| 50 |
|
| 51 |
|
| 52 |
static final String DATA_FILE_NAME = "saved_data"; |
| 53 |
|
| 54 |
|
| 55 |
RadioGroup mFillingGroup; |
| 56 |
CheckBox mAddMayoCheckbox; |
| 57 |
CheckBox mAddTomatoCheckbox; |
| 58 |
|
| 59 |
|
| 60 |
File mDataFile; |
| 61 |
|
| 62 |
|
| 63 |
BackupManager mBackupManager; |
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public void onCreate(Bundle savedInstanceState) { |
| 68 |
super.onCreate(savedInstanceState); |
| 69 |
|
| 70 |
|
| 71 |
setContentView(R.layout.backup_restore); |
| 72 |
|
| 73 |
|
| 74 |
mFillingGroup = findViewById(R.id.filling_group); |
| 75 |
mAddMayoCheckbox = findViewById(R.id.mayo); |
| 76 |
mAddTomatoCheckbox = findViewById(R.id.tomato); |
| 77 |
|
| 78 |
|
| 79 |
mDataFile = new File(getFilesDir(), HugeBackupActivity.DATA_FILE_NAME); |
| 80 |
|
| 81 |
|
| 82 |
mBackupManager = new BackupManager(this); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
populateUI(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
void populateUI() { |
| 95 |
RandomAccessFile file; |
| 96 |
|
| 97 |
|
| 98 |
int whichFilling = R.id.pastrami; |
| 99 |
boolean addMayo = false; |
| 100 |
boolean addTomato = false; |
| 101 |
|
| 102 |
|
| 103 |
synchronized (HugeBackupActivity.sDataLock) { |
| 104 |
boolean exists = mDataFile.exists(); |
| 105 |
try { |
| 106 |
file = new RandomAccessFile(mDataFile, "rw"); |
| 107 |
if (exists) { |
| 108 |
Log.v(TAG, "datafile exists"); |
| 109 |
whichFilling = file.readInt(); |
| 110 |
addMayo = file.readBoolean(); |
| 111 |
addTomato = file.readBoolean(); |
| 112 |
Log.v(TAG, " mayo=" + addMayo |
| 113 |
+ " tomato=" + addTomato |
| 114 |
+ " filling=" + whichFilling); |
| 115 |
} else { |
| 116 |
|
| 117 |
|
| 118 |
Log.v(TAG, "creating default datafile"); |
| 119 |
writeDataToFileLocked(file, |
| 120 |
addMayo, addTomato, whichFilling); |
| 121 |
|
| 122 |
|
| 123 |
mBackupManager.dataChanged(); |
| 124 |
} |
| 125 |
} catch (IOException ioe) { |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
mFillingGroup.check(whichFilling); |
| 131 |
mAddMayoCheckbox.setChecked(addMayo); |
| 132 |
mAddTomatoCheckbox.setChecked(addTomato); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mFillingGroup.setOnCheckedChangeListener( |
| 139 |
new RadioGroup.OnCheckedChangeListener() { |
| 140 |
public void onCheckedChanged(RadioGroup group, |
| 141 |
int checkedId) { |
| 142 |
|
| 143 |
|
| 144 |
Log.v(TAG, "New radio item selected: " + checkedId); |
| 145 |
recordNewUIState(); |
| 146 |
} |
| 147 |
}); |
| 148 |
|
| 149 |
CompoundButton.OnCheckedChangeListener checkListener |
| 150 |
= new CompoundButton.OnCheckedChangeListener() { |
| 151 |
public void onCheckedChanged(CompoundButton buttonView, |
| 152 |
boolean isChecked) { |
| 153 |
|
| 154 |
Log.v(TAG, "Checkbox toggled: " + buttonView); |
| 155 |
recordNewUIState(); |
| 156 |
} |
| 157 |
}; |
| 158 |
mAddMayoCheckbox.setOnCheckedChangeListener(checkListener); |
| 159 |
mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
void writeDataToFileLocked(RandomAccessFile file, |
| 166 |
boolean addMayo, boolean addTomato, int whichFilling) |
| 167 |
throws IOException { |
| 168 |
file.setLength(0L); |
| 169 |
file.writeInt(whichFilling); |
| 170 |
file.writeBoolean(addMayo); |
| 171 |
file.writeBoolean(addTomato); |
| 172 |
Log.v(TAG, "NEW STATE: mayo=" + addMayo |
| 173 |
+ " tomato=" + addTomato |
| 174 |
+ " filling=" + whichFilling); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
void recordNewUIState() { |
| 183 |
boolean addMayo = mAddMayoCheckbox.isChecked(); |
| 184 |
boolean addTomato = mAddTomatoCheckbox.isChecked(); |
| 185 |
int whichFilling = mFillingGroup.getCheckedRadioButtonId(); |
| 186 |
try { |
| 187 |
synchronized (HugeBackupActivity.sDataLock) { |
| 188 |
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw"); |
| 189 |
writeDataToFileLocked(file, addMayo, addTomato, whichFilling); |
| 190 |
} |
| 191 |
} catch (IOException e) { |
| 192 |
Log.e(TAG, "Unable to record new UI state"); |
| 193 |
} |
| 194 |
|
| 195 |
mBackupManager.dataChanged(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public void onRestoreButtonClick(View v) { |
| 203 |
Log.v(TAG, "Requesting restore of our most recent data"); |
| 204 |
mBackupManager.requestRestore( |
| 205 |
new RestoreObserver() { |
| 206 |
public void restoreFinished(int error) { |
| 207 |
|
| 208 |
Log.v(TAG, "Restore finished, error = " + error); |
| 209 |
populateUI(); |
| 210 |
} |
| 211 |
} |
| 212 |
); |
| 213 |
} |
| 214 |
} |