Created
February 11, 2016 16:56
-
-
Save DmitryVarennikov/4aef5dbd837a6216f6aa to your computer and use it in GitHub Desktop.
BackupAgent for Android Backup Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dvlab.birthdaynotes.services; | |
import android.app.backup.BackupDataInput; | |
import android.app.backup.BackupDataOutput; | |
import android.os.ParcelFileDescriptor; | |
import android.util.Log; | |
import com.dvlab.birthdaynotes.entities.Person; | |
import com.dvlab.birthdaynotes.repositories.PeopleRepo; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* cd ~/Android/Sdk/platform-tools | |
* ./adb shell bmgr backup com.dvlab.birthdaynotes | |
* ./adb shell bmgr run | |
* ./adb shell bmgr restore com.dvlab.birthdaynotes | |
* <p> | |
* ./adb shell bmgr list transports | |
* ./adb shell bmgr transport android/com.android.internal.backup.LocalTransport | |
* ./adb shell bmgr transport com.google.android.gms/.backup.BackupTransportService | |
*/ | |
public class BackupAgent extends android.app.backup.BackupAgent { | |
final public static String TAG = BackupAgent.class.getSimpleName(); | |
final private static String BACKUP_KEY = "people"; | |
@Override | |
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException { | |
Log.d(TAG, "onBackup"); | |
List<Person> people = PeopleRepo.findAll(); | |
JSONArray json = new JSONArray(); | |
for (Person person : people) { | |
JSONObject personJSON = person.toJSON(getFilesDir()); | |
json.put(personJSON); | |
} | |
Log.d(TAG, json.toString()); | |
byte[] buffer = json.toString().getBytes(); | |
int len = buffer.length; | |
data.writeEntityHeader(BACKUP_KEY, len); | |
data.writeEntityData(buffer, len); | |
} | |
@Override | |
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException { | |
Log.d(TAG, "onRestore"); | |
// There should be only one entity, but the safest | |
// way to consume it is using a while loop | |
while (data.readNextHeader()) { | |
String key = data.getKey(); | |
int dataSize = data.getDataSize(); | |
if (BACKUP_KEY.equals(key)) { | |
byte[] buffer = new byte[dataSize]; | |
data.readEntityData(buffer, 0, dataSize); | |
try { | |
JSONArray json = new JSONArray(new String(buffer)); | |
List<Person> people = new ArrayList<>(json.length()); | |
for (int i = 0; i < json.length(); i++) { | |
Person person = new Person(json.getJSONObject(i), getFilesDir()); | |
people.add(person); | |
} | |
PeopleRepo.restoreBackup(people); | |
} catch (JSONException e) { | |
Log.e(TAG, e.getMessage(), e); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment