| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.backup; |
| 18 |
|
| 19 |
import android.os.ParcelFileDescriptor; |
| 20 |
import android.util.ArrayMap; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import java.io.ByteArrayInputStream; |
| 24 |
import java.io.ByteArrayOutputStream; |
| 25 |
import java.io.DataInputStream; |
| 26 |
import java.io.DataOutputStream; |
| 27 |
import java.io.EOFException; |
| 28 |
import java.io.FileInputStream; |
| 29 |
import java.io.FileOutputStream; |
| 30 |
import java.io.IOException; |
| 31 |
import java.util.zip.CRC32; |
| 32 |
import java.util.zip.DeflaterOutputStream; |
| 33 |
import java.util.zip.InflaterInputStream; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public abstract class BlobBackupHelper implements BackupHelper { |
| 43 |
private static final String TAG = "BlobBackupHelper"; |
| 44 |
private static final boolean DEBUG = false; |
| 45 |
|
| 46 |
private final int mCurrentBlobVersion; |
| 47 |
private final String[] mKeys; |
| 48 |
|
| 49 |
public BlobBackupHelper(int currentBlobVersion, String... keys) { |
| 50 |
mCurrentBlobVersion = currentBlobVersion; |
| 51 |
mKeys = keys; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
abstract protected byte[] getBackupPayload(String key); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
abstract protected void applyRestoredPayload(String key, byte[] payload); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@SuppressWarnings("resource") |
| 90 |
private ArrayMap<String, Long> readOldState(ParcelFileDescriptor oldStateFd) { |
| 91 |
final ArrayMap<String, Long> state = new ArrayMap<String, Long>(); |
| 92 |
|
| 93 |
FileInputStream fis = new FileInputStream(oldStateFd.getFileDescriptor()); |
| 94 |
DataInputStream in = new DataInputStream(fis); |
| 95 |
|
| 96 |
try { |
| 97 |
int version = in.readInt(); |
| 98 |
if (version <= mCurrentBlobVersion) { |
| 99 |
final int numKeys = in.readInt(); |
| 100 |
if (DEBUG) { |
| 101 |
Log.i(TAG, " " + numKeys + " keys in state record"); |
| 102 |
} |
| 103 |
for (int i = 0; i < numKeys; i++) { |
| 104 |
String key = in.readUTF(); |
| 105 |
long checksum = in.readLong(); |
| 106 |
if (DEBUG) { |
| 107 |
Log.i(TAG, " key '" + key + "' checksum is " + checksum); |
| 108 |
} |
| 109 |
state.put(key, checksum); |
| 110 |
} |
| 111 |
} else { |
| 112 |
Log.w(TAG, "Prior state from unrecognized version " + version); |
| 113 |
} |
| 114 |
} catch (EOFException e) { |
| 115 |
|
| 116 |
|
| 117 |
if (DEBUG) { |
| 118 |
Log.i(TAG, "Hit EOF reading prior state"); |
| 119 |
} |
| 120 |
state.clear(); |
| 121 |
} catch (Exception e) { |
| 122 |
Log.e(TAG, "Error examining prior backup state " + e.getMessage()); |
| 123 |
state.clear(); |
| 124 |
} |
| 125 |
|
| 126 |
return state; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private void writeBackupState(ArrayMap<String, Long> state, ParcelFileDescriptor stateFile) { |
| 133 |
try { |
| 134 |
FileOutputStream fos = new FileOutputStream(stateFile.getFileDescriptor()); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@SuppressWarnings("resource") |
| 139 |
DataOutputStream out = new DataOutputStream(fos); |
| 140 |
|
| 141 |
out.writeInt(mCurrentBlobVersion); |
| 142 |
|
| 143 |
final int N = (state != null) ? state.size() : 0; |
| 144 |
out.writeInt(N); |
| 145 |
for (int i = 0; i < N; i++) { |
| 146 |
final String key = state.keyAt(i); |
| 147 |
final long checksum = state.valueAt(i).longValue(); |
| 148 |
if (DEBUG) { |
| 149 |
Log.i(TAG, " writing key " + key + " checksum = " + checksum); |
| 150 |
} |
| 151 |
out.writeUTF(key); |
| 152 |
out.writeLong(checksum); |
| 153 |
} |
| 154 |
} catch (IOException e) { |
| 155 |
Log.e(TAG, "Unable to write updated state", e); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
private byte[] deflate(byte[] data) { |
| 161 |
byte[] result = null; |
| 162 |
if (data != null) { |
| 163 |
try { |
| 164 |
ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 165 |
DataOutputStream headerOut = new DataOutputStream(sink); |
| 166 |
|
| 167 |
|
| 168 |
headerOut.writeInt(mCurrentBlobVersion); |
| 169 |
|
| 170 |
DeflaterOutputStream out = new DeflaterOutputStream(sink); |
| 171 |
out.write(data); |
| 172 |
out.close(); |
| 173 |
result = sink.toByteArray(); |
| 174 |
if (DEBUG) { |
| 175 |
Log.v(TAG, "Deflated " + data.length + " bytes to " + result.length); |
| 176 |
} |
| 177 |
} catch (IOException e) { |
| 178 |
Log.w(TAG, "Unable to process payload: " + e.getMessage()); |
| 179 |
} |
| 180 |
} |
| 181 |
return result; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
private byte[] inflate(byte[] compressedData) { |
| 186 |
byte[] result = null; |
| 187 |
if (compressedData != null) { |
| 188 |
try { |
| 189 |
ByteArrayInputStream source = new ByteArrayInputStream(compressedData); |
| 190 |
DataInputStream headerIn = new DataInputStream(source); |
| 191 |
int version = headerIn.readInt(); |
| 192 |
if (version > mCurrentBlobVersion) { |
| 193 |
Log.w(TAG, "Saved payload from unrecognized version " + version); |
| 194 |
return null; |
| 195 |
} |
| 196 |
|
| 197 |
InflaterInputStream in = new InflaterInputStream(source); |
| 198 |
ByteArrayOutputStream inflated = new ByteArrayOutputStream(); |
| 199 |
byte[] buffer = new byte[4096]; |
| 200 |
int nRead; |
| 201 |
while ((nRead = in.read(buffer)) > 0) { |
| 202 |
inflated.write(buffer, 0, nRead); |
| 203 |
} |
| 204 |
in.close(); |
| 205 |
inflated.flush(); |
| 206 |
result = inflated.toByteArray(); |
| 207 |
if (DEBUG) { |
| 208 |
Log.v(TAG, "Inflated " + compressedData.length + " bytes to " + result.length); |
| 209 |
} |
| 210 |
} catch (IOException e) { |
| 211 |
|
| 212 |
Log.w(TAG, "Unable to process restored payload: " + e.getMessage()); |
| 213 |
} |
| 214 |
} |
| 215 |
return result; |
| 216 |
} |
| 217 |
|
| 218 |
private long checksum(byte[] buffer) { |
| 219 |
if (buffer != null) { |
| 220 |
try { |
| 221 |
CRC32 crc = new CRC32(); |
| 222 |
ByteArrayInputStream bis = new ByteArrayInputStream(buffer); |
| 223 |
byte[] buf = new byte[4096]; |
| 224 |
int nRead = 0; |
| 225 |
while ((nRead = bis.read(buf)) >= 0) { |
| 226 |
crc.update(buf, 0, nRead); |
| 227 |
} |
| 228 |
return crc.getValue(); |
| 229 |
} catch (Exception e) { |
| 230 |
|
| 231 |
} |
| 232 |
} |
| 233 |
return -1; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
@Override |
| 239 |
public void performBackup(ParcelFileDescriptor oldStateFd, BackupDataOutput data, |
| 240 |
ParcelFileDescriptor newStateFd) { |
| 241 |
if (DEBUG) { |
| 242 |
Log.i(TAG, "Performing backup for " + this.getClass().getName()); |
| 243 |
} |
| 244 |
|
| 245 |
final ArrayMap<String, Long> oldState = readOldState(oldStateFd); |
| 246 |
final ArrayMap<String, Long> newState = new ArrayMap<String, Long>(); |
| 247 |
|
| 248 |
try { |
| 249 |
for (String key : mKeys) { |
| 250 |
final byte[] payload = deflate(getBackupPayload(key)); |
| 251 |
final long checksum = checksum(payload); |
| 252 |
if (DEBUG) { |
| 253 |
Log.i(TAG, "Key " + key + " backup checksum is " + checksum); |
| 254 |
} |
| 255 |
newState.put(key, checksum); |
| 256 |
|
| 257 |
Long oldChecksum = oldState.get(key); |
| 258 |
if (oldChecksum == null || checksum != oldChecksum.longValue()) { |
| 259 |
if (DEBUG) { |
| 260 |
Log.i(TAG, "Checksum has changed from " + oldChecksum + " to " + checksum |
| 261 |
+ " for key " + key + ", writing"); |
| 262 |
} |
| 263 |
if (payload != null) { |
| 264 |
data.writeEntityHeader(key, payload.length); |
| 265 |
data.writeEntityData(payload, payload.length); |
| 266 |
} else { |
| 267 |
|
| 268 |
data.writeEntityHeader(key, -1); |
| 269 |
} |
| 270 |
} else { |
| 271 |
if (DEBUG) { |
| 272 |
Log.i(TAG, "No change under key " + key + " => not writing"); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} catch (Exception e) { |
| 277 |
Log.w(TAG, "Unable to record notification state: " + e.getMessage()); |
| 278 |
newState.clear(); |
| 279 |
} finally { |
| 280 |
|
| 281 |
writeBackupState(newState, newStateFd); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public void restoreEntity(BackupDataInputStream data) { |
| 287 |
final String key = data.getKey(); |
| 288 |
try { |
| 289 |
|
| 290 |
int which; |
| 291 |
for (which = 0; which < mKeys.length; which++) { |
| 292 |
if (key.equals(mKeys[which])) { |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
if (which >= mKeys.length) { |
| 297 |
Log.e(TAG, "Unrecognized key " + key + ", ignoring"); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
byte[] compressed = new byte[data.size()]; |
| 302 |
data.read(compressed); |
| 303 |
byte[] payload = inflate(compressed); |
| 304 |
applyRestoredPayload(key, payload); |
| 305 |
} catch (Exception e) { |
| 306 |
Log.e(TAG, "Exception restoring entity " + key + " : " + e.getMessage()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
public void writeNewStateDescription(ParcelFileDescriptor newState) { |
| 312 |
|
| 313 |
if (DEBUG) { |
| 314 |
Log.i(TAG, "Writing state description after restore"); |
| 315 |
} |
| 316 |
writeBackupState(null, newState); |
| 317 |
} |
| 318 |
} |