Hold the current transport instantiated all the time.
We no longer instantiate the transport just for the duration of handling a backup or restore operation. Instead, we hold the object forever (replacing it if instructed to do so). This makes it easier for transports to watch system state and help set backup timing policy. Also fixes up the IBackupTransport documentation a bit.
This commit is contained in:
@@ -42,11 +42,14 @@ interface IBackupTransport {
|
|||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Verify that this is a suitable time for a backup pass. This should return zero
|
* Verify that this is a suitable time for a backup pass. This should return zero
|
||||||
* if a backup is reasonable right now, false otherwise. This method will be called
|
* if a backup is reasonable right now, some positive value otherwise. This method
|
||||||
* outside of the {@link #startSession}/{@link #endSession} pair.
|
* will be called outside of the {@link #startSession}/{@link #endSession} pair.
|
||||||
*
|
*
|
||||||
* <p>If this is not a suitable time for a backup, the transport should suggest a
|
* <p>If this is not a suitable time for a backup, the transport should return a
|
||||||
* backoff delay, in milliseconds, after which the Backup Manager should try again.
|
* backoff delay, in milliseconds, after which the Backup Manager should try again.
|
||||||
|
*
|
||||||
|
* @return Zero if this is a suitable time for a backup pass, or a positive time delay
|
||||||
|
* in milliseconds to suggest deferring the backup pass for a while.
|
||||||
*/
|
*/
|
||||||
long requestBackupTime();
|
long requestBackupTime();
|
||||||
|
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ import java.util.List;
|
|||||||
class BackupManagerService extends IBackupManager.Stub {
|
class BackupManagerService extends IBackupManager.Stub {
|
||||||
private static final String TAG = "BackupManagerService";
|
private static final String TAG = "BackupManagerService";
|
||||||
private static final boolean DEBUG = true;
|
private static final boolean DEBUG = true;
|
||||||
|
|
||||||
private static final long COLLECTION_INTERVAL = 1000;
|
private static final long COLLECTION_INTERVAL = 1000;
|
||||||
//private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
|
//private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
private class BackupRequest {
|
private class BackupRequest {
|
||||||
public ApplicationInfo appInfo;
|
public ApplicationInfo appInfo;
|
||||||
public boolean fullBackup;
|
public boolean fullBackup;
|
||||||
|
|
||||||
BackupRequest(ApplicationInfo app, boolean isFull) {
|
BackupRequest(ApplicationInfo app, boolean isFull) {
|
||||||
appInfo = app;
|
appInfo = app;
|
||||||
fullBackup = isFull;
|
fullBackup = isFull;
|
||||||
@@ -120,7 +120,9 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
private final Object mClearDataLock = new Object();
|
private final Object mClearDataLock = new Object();
|
||||||
private volatile boolean mClearingData;
|
private volatile boolean mClearingData;
|
||||||
|
|
||||||
|
// Current active transport & restore session
|
||||||
private int mTransportId;
|
private int mTransportId;
|
||||||
|
private IBackupTransport mTransport;
|
||||||
private RestoreSession mActiveRestoreSession;
|
private RestoreSession mActiveRestoreSession;
|
||||||
|
|
||||||
private File mStateDir;
|
private File mStateDir;
|
||||||
@@ -128,7 +130,7 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
private File mJournalDir;
|
private File mJournalDir;
|
||||||
private File mJournal;
|
private File mJournal;
|
||||||
private RandomAccessFile mJournalStream;
|
private RandomAccessFile mJournalStream;
|
||||||
|
|
||||||
public BackupManagerService(Context context) {
|
public BackupManagerService(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mPackageManager = context.getPackageManager();
|
mPackageManager = context.getPackageManager();
|
||||||
@@ -144,14 +146,16 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
mJournalDir.mkdirs();
|
mJournalDir.mkdirs();
|
||||||
makeJournalLocked(); // okay because no other threads are running yet
|
makeJournalLocked(); // okay because no other threads are running yet
|
||||||
|
|
||||||
//!!! TODO: default to cloud transport, not local
|
|
||||||
mTransportId = BackupManager.TRANSPORT_LOCAL;
|
|
||||||
|
|
||||||
// Build our mapping of uid to backup client services
|
// Build our mapping of uid to backup client services
|
||||||
synchronized (mBackupParticipants) {
|
synchronized (mBackupParticipants) {
|
||||||
addPackageParticipantsLocked(null);
|
addPackageParticipantsLocked(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stand up our default transport
|
||||||
|
//!!! TODO: default to cloud transport, not local
|
||||||
|
mTransportId = BackupManager.TRANSPORT_LOCAL;
|
||||||
|
mTransport = createTransport(mTransportId);
|
||||||
|
|
||||||
// Now that we know about valid backup participants, parse any
|
// Now that we know about valid backup participants, parse any
|
||||||
// leftover journal files and schedule a new backup pass
|
// leftover journal files and schedule a new backup pass
|
||||||
parseLeftoverJournals();
|
parseLeftoverJournals();
|
||||||
@@ -284,7 +288,7 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
// deleted. If we crash prior to that, the old journal is parsed
|
// deleted. If we crash prior to that, the old journal is parsed
|
||||||
// at next boot and the journaled requests fulfilled.
|
// at next boot and the journaled requests fulfilled.
|
||||||
}
|
}
|
||||||
(new PerformBackupThread(mTransportId, mBackupQueue, oldJournal)).run();
|
(new PerformBackupThread(mTransport, mBackupQueue, oldJournal)).run();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MSG_RUN_FULL_BACKUP:
|
case MSG_RUN_FULL_BACKUP:
|
||||||
@@ -396,7 +400,7 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
}
|
}
|
||||||
return allApps;
|
return allApps;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the given package's known backup participants. Unlike add/remove, the update
|
// Reset the given package's known backup participants. Unlike add/remove, the update
|
||||||
// action cannot be passed a null package name.
|
// action cannot be passed a null package name.
|
||||||
void updatePackageParticipantsLocked(String packageName) {
|
void updatePackageParticipantsLocked(String packageName) {
|
||||||
@@ -505,13 +509,13 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
|
|
||||||
class PerformBackupThread extends Thread {
|
class PerformBackupThread extends Thread {
|
||||||
private static final String TAG = "PerformBackupThread";
|
private static final String TAG = "PerformBackupThread";
|
||||||
int mTransport;
|
IBackupTransport mTransport;
|
||||||
ArrayList<BackupRequest> mQueue;
|
ArrayList<BackupRequest> mQueue;
|
||||||
File mJournal;
|
File mJournal;
|
||||||
|
|
||||||
public PerformBackupThread(int transportId, ArrayList<BackupRequest> queue,
|
public PerformBackupThread(IBackupTransport transport, ArrayList<BackupRequest> queue,
|
||||||
File journal) {
|
File journal) {
|
||||||
mTransport = transportId;
|
mTransport = transport;
|
||||||
mQueue = queue;
|
mQueue = queue;
|
||||||
mJournal = journal;
|
mJournal = journal;
|
||||||
}
|
}
|
||||||
@@ -520,15 +524,9 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
public void run() {
|
public void run() {
|
||||||
if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
|
if (DEBUG) Log.v(TAG, "Beginning backup of " + mQueue.size() + " targets");
|
||||||
|
|
||||||
// stand up the current transport
|
|
||||||
IBackupTransport transport = createTransport(mTransport);
|
|
||||||
if (transport == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// start up the transport
|
// start up the transport
|
||||||
try {
|
try {
|
||||||
transport.startSession();
|
mTransport.startSession();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error session transport");
|
Log.e(TAG, "Error session transport");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -536,11 +534,11 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The transport is up and running; now run all the backups in our queue
|
// The transport is up and running; now run all the backups in our queue
|
||||||
doQueuedBackups(transport);
|
doQueuedBackups(mTransport);
|
||||||
|
|
||||||
// Finally, tear down the transport
|
// Finally, tear down the transport
|
||||||
try {
|
try {
|
||||||
transport.endSession();
|
mTransport.endSession();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error ending transport");
|
Log.e(TAG, "Error ending transport");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -939,8 +937,14 @@ class BackupManagerService extends IBackupManager.Stub {
|
|||||||
public int selectBackupTransport(int transportId) {
|
public int selectBackupTransport(int transportId) {
|
||||||
mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
|
mContext.enforceCallingPermission("android.permission.BACKUP", "selectBackupTransport");
|
||||||
|
|
||||||
int prevTransport = mTransportId;
|
int prevTransport = -1;
|
||||||
mTransportId = transportId;
|
IBackupTransport newTransport = createTransport(transportId);
|
||||||
|
if (newTransport != null) {
|
||||||
|
// !!! TODO: a method on the old transport that says it's being deactivated?
|
||||||
|
mTransport = newTransport;
|
||||||
|
prevTransport = mTransportId;
|
||||||
|
mTransportId = transportId;
|
||||||
|
}
|
||||||
return prevTransport;
|
return prevTransport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user