@Test public void testRestorePackage_whenPackageNullWhenCreated() throws Exception { ShadowBinder.setCallingUid(1); setUpPackage(PACKAGE_1, 1); when(mBackupManagerService.getAvailableRestoreToken(PACKAGE_1)).thenReturn(TOKEN_1); setUpTransport(mTransport); IRestoreSession restoreSession = createActiveRestoreSession(null, mTransport); int result = restoreSession.restorePackage(PACKAGE_1, mObserver, mMonitor); mShadowBackupLooper.runToEndOfTasks(); assertThat(result).isEqualTo(0); }
public synchronized int restorePackage(String packageName, IRestoreObserver observer, IBackupManagerMonitor monitor) { if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer + "monitor=" + monitor); } if (mEnded) { throw new IllegalStateException("Restore session already ended"); } if (mTimedOut) { Slog.i(TAG, "Session already timed out"); return -1; } if (mPackageName != null) { if (!mPackageName.equals(packageName)) { Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName + " on session for package " + mPackageName); return -1; } } final PackageInfo app; try { app = mBackupManagerService.getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException nnf) { Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); return -1; } int perm = mBackupManagerService.getContext().checkPermission( android.Manifest.permission.BACKUP, Binder.getCallingPid(), Binder.getCallingUid()); if ((perm == PackageManager.PERMISSION_DENIED) && (app.applicationInfo.uid != Binder.getCallingUid())) { Slog.w(TAG, "restorePackage: bad packageName=" + packageName + " or calling uid=" + Binder.getCallingUid()); throw new SecurityException("No permission to restore other packages"); } if (!mTransportManager.isTransportRegistered(mTransportName)) { Slog.e(TAG, "Transport " + mTransportName + " not registered"); return -1; } long oldId = Binder.clearCallingIdentity(); try { long token = mBackupManagerService.getAvailableRestoreToken(packageName); if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " token=" + Long.toHexString(token)); } if (token == 0) { if (DEBUG) { Slog.w(TAG, "No data available for this package; not restoring"); } return -1; } return sendRestoreToHandlerLocked( (transportClient, listener) -> RestoreParams.createForSinglePackage( transportClient, observer, monitor, token, app, listener), "RestoreSession.restorePackage(" + packageName + ")"); } finally { Binder.restoreCallingIdentity(oldId); } }
ActiveRestoreSession extends IRestoreSession.Stub { public synchronized int restorePackage(String packageName, IRestoreObserver observer, IBackupManagerMonitor monitor) { if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer + "monitor=" + monitor); } if (mEnded) { throw new IllegalStateException("Restore session already ended"); } if (mTimedOut) { Slog.i(TAG, "Session already timed out"); return -1; } if (mPackageName != null) { if (!mPackageName.equals(packageName)) { Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName + " on session for package " + mPackageName); return -1; } } final PackageInfo app; try { app = mBackupManagerService.getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException nnf) { Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); return -1; } int perm = mBackupManagerService.getContext().checkPermission( android.Manifest.permission.BACKUP, Binder.getCallingPid(), Binder.getCallingUid()); if ((perm == PackageManager.PERMISSION_DENIED) && (app.applicationInfo.uid != Binder.getCallingUid())) { Slog.w(TAG, "restorePackage: bad packageName=" + packageName + " or calling uid=" + Binder.getCallingUid()); throw new SecurityException("No permission to restore other packages"); } if (!mTransportManager.isTransportRegistered(mTransportName)) { Slog.e(TAG, "Transport " + mTransportName + " not registered"); return -1; } long oldId = Binder.clearCallingIdentity(); try { long token = mBackupManagerService.getAvailableRestoreToken(packageName); if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " token=" + Long.toHexString(token)); } if (token == 0) { if (DEBUG) { Slog.w(TAG, "No data available for this package; not restoring"); } return -1; } return sendRestoreToHandlerLocked( (transportClient, listener) -> RestoreParams.createForSinglePackage( transportClient, observer, monitor, token, app, listener), "RestoreSession.restorePackage(" + packageName + ")"); } finally { Binder.restoreCallingIdentity(oldId); } } }
ActiveRestoreSession extends IRestoreSession.Stub { public synchronized int restorePackage(String packageName, IRestoreObserver observer, IBackupManagerMonitor monitor) { if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer + "monitor=" + monitor); } if (mEnded) { throw new IllegalStateException("Restore session already ended"); } if (mTimedOut) { Slog.i(TAG, "Session already timed out"); return -1; } if (mPackageName != null) { if (!mPackageName.equals(packageName)) { Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName + " on session for package " + mPackageName); return -1; } } final PackageInfo app; try { app = mBackupManagerService.getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException nnf) { Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); return -1; } int perm = mBackupManagerService.getContext().checkPermission( android.Manifest.permission.BACKUP, Binder.getCallingPid(), Binder.getCallingUid()); if ((perm == PackageManager.PERMISSION_DENIED) && (app.applicationInfo.uid != Binder.getCallingUid())) { Slog.w(TAG, "restorePackage: bad packageName=" + packageName + " or calling uid=" + Binder.getCallingUid()); throw new SecurityException("No permission to restore other packages"); } if (!mTransportManager.isTransportRegistered(mTransportName)) { Slog.e(TAG, "Transport " + mTransportName + " not registered"); return -1; } long oldId = Binder.clearCallingIdentity(); try { long token = mBackupManagerService.getAvailableRestoreToken(packageName); if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " token=" + Long.toHexString(token)); } if (token == 0) { if (DEBUG) { Slog.w(TAG, "No data available for this package; not restoring"); } return -1; } return sendRestoreToHandlerLocked( (transportClient, listener) -> RestoreParams.createForSinglePackage( transportClient, observer, monitor, token, app, listener), "RestoreSession.restorePackage(" + packageName + ")"); } finally { Binder.restoreCallingIdentity(oldId); } } ActiveRestoreSession(
            BackupManagerService backupManagerService,
            @Nullable String packageName,
            String transportName); }
ActiveRestoreSession extends IRestoreSession.Stub { public synchronized int restorePackage(String packageName, IRestoreObserver observer, IBackupManagerMonitor monitor) { if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer + "monitor=" + monitor); } if (mEnded) { throw new IllegalStateException("Restore session already ended"); } if (mTimedOut) { Slog.i(TAG, "Session already timed out"); return -1; } if (mPackageName != null) { if (!mPackageName.equals(packageName)) { Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName + " on session for package " + mPackageName); return -1; } } final PackageInfo app; try { app = mBackupManagerService.getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException nnf) { Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); return -1; } int perm = mBackupManagerService.getContext().checkPermission( android.Manifest.permission.BACKUP, Binder.getCallingPid(), Binder.getCallingUid()); if ((perm == PackageManager.PERMISSION_DENIED) && (app.applicationInfo.uid != Binder.getCallingUid())) { Slog.w(TAG, "restorePackage: bad packageName=" + packageName + " or calling uid=" + Binder.getCallingUid()); throw new SecurityException("No permission to restore other packages"); } if (!mTransportManager.isTransportRegistered(mTransportName)) { Slog.e(TAG, "Transport " + mTransportName + " not registered"); return -1; } long oldId = Binder.clearCallingIdentity(); try { long token = mBackupManagerService.getAvailableRestoreToken(packageName); if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " token=" + Long.toHexString(token)); } if (token == 0) { if (DEBUG) { Slog.w(TAG, "No data available for this package; not restoring"); } return -1; } return sendRestoreToHandlerLocked( (transportClient, listener) -> RestoreParams.createForSinglePackage( transportClient, observer, monitor, token, app, listener), "RestoreSession.restorePackage(" + packageName + ")"); } finally { Binder.restoreCallingIdentity(oldId); } } ActiveRestoreSession(
            BackupManagerService backupManagerService,
            @Nullable String packageName,
            String transportName); void markTimedOut(); synchronized int getAvailableRestoreSets(IRestoreObserver observer,
            IBackupManagerMonitor monitor); synchronized int restoreAll(long token, IRestoreObserver observer,
            IBackupManagerMonitor monitor); synchronized int restoreSome(long token, IRestoreObserver observer,
            IBackupManagerMonitor monitor, String[] packages); synchronized int restorePackage(String packageName, IRestoreObserver observer,
            IBackupManagerMonitor monitor); void setRestoreSets(RestoreSet[] restoreSets); synchronized void endRestoreSession(); }
ActiveRestoreSession extends IRestoreSession.Stub { public synchronized int restorePackage(String packageName, IRestoreObserver observer, IBackupManagerMonitor monitor) { if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " obs=" + observer + "monitor=" + monitor); } if (mEnded) { throw new IllegalStateException("Restore session already ended"); } if (mTimedOut) { Slog.i(TAG, "Session already timed out"); return -1; } if (mPackageName != null) { if (!mPackageName.equals(packageName)) { Slog.e(TAG, "Ignoring attempt to restore pkg=" + packageName + " on session for package " + mPackageName); return -1; } } final PackageInfo app; try { app = mBackupManagerService.getPackageManager().getPackageInfo(packageName, 0); } catch (NameNotFoundException nnf) { Slog.w(TAG, "Asked to restore nonexistent pkg " + packageName); return -1; } int perm = mBackupManagerService.getContext().checkPermission( android.Manifest.permission.BACKUP, Binder.getCallingPid(), Binder.getCallingUid()); if ((perm == PackageManager.PERMISSION_DENIED) && (app.applicationInfo.uid != Binder.getCallingUid())) { Slog.w(TAG, "restorePackage: bad packageName=" + packageName + " or calling uid=" + Binder.getCallingUid()); throw new SecurityException("No permission to restore other packages"); } if (!mTransportManager.isTransportRegistered(mTransportName)) { Slog.e(TAG, "Transport " + mTransportName + " not registered"); return -1; } long oldId = Binder.clearCallingIdentity(); try { long token = mBackupManagerService.getAvailableRestoreToken(packageName); if (DEBUG) { Slog.v(TAG, "restorePackage pkg=" + packageName + " token=" + Long.toHexString(token)); } if (token == 0) { if (DEBUG) { Slog.w(TAG, "No data available for this package; not restoring"); } return -1; } return sendRestoreToHandlerLocked( (transportClient, listener) -> RestoreParams.createForSinglePackage( transportClient, observer, monitor, token, app, listener), "RestoreSession.restorePackage(" + packageName + ")"); } finally { Binder.restoreCallingIdentity(oldId); } } ActiveRestoreSession(
            BackupManagerService backupManagerService,
            @Nullable String packageName,
            String transportName); void markTimedOut(); synchronized int getAvailableRestoreSets(IRestoreObserver observer,
            IBackupManagerMonitor monitor); synchronized int restoreAll(long token, IRestoreObserver observer,
            IBackupManagerMonitor monitor); synchronized int restoreSome(long token, IRestoreObserver observer,
            IBackupManagerMonitor monitor, String[] packages); synchronized int restorePackage(String packageName, IRestoreObserver observer,
            IBackupManagerMonitor monitor); void setRestoreSets(RestoreSet[] restoreSets); synchronized void endRestoreSession(); public RestoreSet[] mRestoreSets; }