@Test public void testSelectBackupTransportAsync_withoutPermission() throws Exception { setUpForSelectTransport(); mShadowContext.denyPermissions(android.Manifest.permission.BACKUP); BackupManagerService backupManagerService = createInitializedBackupManagerService(); ComponentName newTransportComponent = mNewTransport.getTransportComponent(); expectThrows( SecurityException.class, () -> backupManagerService.selectBackupTransportAsync( newTransportComponent, mock(ISelectBackupTransportCallback.class))); }
@Override public void selectBackupTransportAsync( ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener) { mContext.enforceCallingOrSelfPermission( android.Manifest.permission.BACKUP, "selectBackupTransportAsync"); if (!isAllowedByMandatoryBackupTransportPolicy(transportComponent)) { try { if (listener != null) { Slog.w(TAG, "Failed to select transport - disallowed by device owner policy."); listener.onFailure(BackupManager.ERROR_BACKUP_NOT_ALLOWED); } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } return; } final long oldId = Binder.clearCallingIdentity(); try { String transportString = transportComponent.flattenToShortString(); Slog.v(TAG, "selectBackupTransportAsync(transport = " + transportString + ")"); mBackupHandler.post( () -> { String transportName = null; int result = mTransportManager.registerAndSelectTransport(transportComponent); if (result == BackupManager.SUCCESS) { try { transportName = mTransportManager.getTransportName(transportComponent); updateStateForTransport(transportName); } catch (TransportNotRegisteredException e) { Slog.e(TAG, "Transport got unregistered"); result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE; } } try { if (listener != null) { if (transportName != null) { listener.onSuccess(transportName); } else { listener.onFailure(result); } } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } }); } finally { Binder.restoreCallingIdentity(oldId); } }
BackupManagerService implements BackupManagerServiceInterface { @Override public void selectBackupTransportAsync( ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener) { mContext.enforceCallingOrSelfPermission( android.Manifest.permission.BACKUP, "selectBackupTransportAsync"); if (!isAllowedByMandatoryBackupTransportPolicy(transportComponent)) { try { if (listener != null) { Slog.w(TAG, "Failed to select transport - disallowed by device owner policy."); listener.onFailure(BackupManager.ERROR_BACKUP_NOT_ALLOWED); } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } return; } final long oldId = Binder.clearCallingIdentity(); try { String transportString = transportComponent.flattenToShortString(); Slog.v(TAG, "selectBackupTransportAsync(transport = " + transportString + ")"); mBackupHandler.post( () -> { String transportName = null; int result = mTransportManager.registerAndSelectTransport(transportComponent); if (result == BackupManager.SUCCESS) { try { transportName = mTransportManager.getTransportName(transportComponent); updateStateForTransport(transportName); } catch (TransportNotRegisteredException e) { Slog.e(TAG, "Transport got unregistered"); result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE; } } try { if (listener != null) { if (transportName != null) { listener.onSuccess(transportName); } else { listener.onFailure(result); } } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } }); } finally { Binder.restoreCallingIdentity(oldId); } } }
BackupManagerService implements BackupManagerServiceInterface { @Override public void selectBackupTransportAsync( ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener) { mContext.enforceCallingOrSelfPermission( android.Manifest.permission.BACKUP, "selectBackupTransportAsync"); if (!isAllowedByMandatoryBackupTransportPolicy(transportComponent)) { try { if (listener != null) { Slog.w(TAG, "Failed to select transport - disallowed by device owner policy."); listener.onFailure(BackupManager.ERROR_BACKUP_NOT_ALLOWED); } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } return; } final long oldId = Binder.clearCallingIdentity(); try { String transportString = transportComponent.flattenToShortString(); Slog.v(TAG, "selectBackupTransportAsync(transport = " + transportString + ")"); mBackupHandler.post( () -> { String transportName = null; int result = mTransportManager.registerAndSelectTransport(transportComponent); if (result == BackupManager.SUCCESS) { try { transportName = mTransportManager.getTransportName(transportComponent); updateStateForTransport(transportName); } catch (TransportNotRegisteredException e) { Slog.e(TAG, "Transport got unregistered"); result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE; } } try { if (listener != null) { if (transportName != null) { listener.onSuccess(transportName); } else { listener.onFailure(result); } } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } }); } finally { Binder.restoreCallingIdentity(oldId); } } @VisibleForTesting  BackupManagerService(
            Context context,
            Trampoline parent,
            HandlerThread backupThread,
            File baseStateDir,
            File dataDir,
            TransportManager transportManager); }
BackupManagerService implements BackupManagerServiceInterface { @Override public void selectBackupTransportAsync( ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener) { mContext.enforceCallingOrSelfPermission( android.Manifest.permission.BACKUP, "selectBackupTransportAsync"); if (!isAllowedByMandatoryBackupTransportPolicy(transportComponent)) { try { if (listener != null) { Slog.w(TAG, "Failed to select transport - disallowed by device owner policy."); listener.onFailure(BackupManager.ERROR_BACKUP_NOT_ALLOWED); } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } return; } final long oldId = Binder.clearCallingIdentity(); try { String transportString = transportComponent.flattenToShortString(); Slog.v(TAG, "selectBackupTransportAsync(transport = " + transportString + ")"); mBackupHandler.post( () -> { String transportName = null; int result = mTransportManager.registerAndSelectTransport(transportComponent); if (result == BackupManager.SUCCESS) { try { transportName = mTransportManager.getTransportName(transportComponent); updateStateForTransport(transportName); } catch (TransportNotRegisteredException e) { Slog.e(TAG, "Transport got unregistered"); result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE; } } try { if (listener != null) { if (transportName != null) { listener.onSuccess(transportName); } else { listener.onFailure(result); } } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } }); } finally { Binder.restoreCallingIdentity(oldId); } } @VisibleForTesting  BackupManagerService(
            Context context,
            Trampoline parent,
            HandlerThread backupThread,
            File baseStateDir,
            File dataDir,
            TransportManager transportManager); BackupManagerConstants getConstants(); BackupAgentTimeoutParameters getAgentTimeoutParameters(); Context getContext(); void setContext(Context context); PackageManager getPackageManager(); void setPackageManager(PackageManager packageManager); IPackageManager getPackageManagerBinder(); void setPackageManagerBinder(IPackageManager packageManagerBinder); IActivityManager getActivityManager(); void setActivityManager(IActivityManager activityManager); AlarmManager getAlarmManager(); void setAlarmManager(AlarmManager alarmManager); void setBackupManagerBinder(IBackupManager backupManagerBinder); TransportManager getTransportManager(); boolean isEnabled(); void setEnabled(boolean enabled); boolean isProvisioned(); void setProvisioned(boolean provisioned); PowerManager.WakeLock getWakelock(); void setWakelock(PowerManager.WakeLock wakelock); Handler getBackupHandler(); void setBackupHandler(BackupHandler backupHandler); PendingIntent getRunInitIntent(); void setRunInitIntent(PendingIntent runInitIntent); HashMap<String, BackupRequest> getPendingBackups(); void setPendingBackups(
            HashMap<String, BackupRequest> pendingBackups); Object getQueueLock(); boolean isBackupRunning(); void setBackupRunning(boolean backupRunning); long getLastBackupPass(); void setLastBackupPass(long lastBackupPass); Object getClearDataLock(); boolean isClearingData(); void setClearingData(boolean clearingData); boolean isRestoreInProgress(); void setRestoreInProgress(boolean restoreInProgress); Queue<PerformUnifiedRestoreTask> getPendingRestores(); ActiveRestoreSession getActiveRestoreSession(); void setActiveRestoreSession(
            ActiveRestoreSession activeRestoreSession); SparseArray<Operation> getCurrentOperations(); Object getCurrentOpLock(); SparseArray<AdbParams> getAdbBackupRestoreConfirmations(); File getBaseStateDir(); void setBaseStateDir(File baseStateDir); File getDataDir(); void setDataDir(File dataDir); DataChangedJournal getJournal(); void setJournal(@Nullable DataChangedJournal journal); SecureRandom getRng(); Set<String> getAncestralPackages(); void setAncestralPackages(Set<String> ancestralPackages); long getAncestralToken(); void setAncestralToken(long ancestralToken); long getCurrentToken(); void setCurrentToken(long currentToken); ArraySet<String> getPendingInits(); void clearPendingInits(); PerformFullTransportBackupTask getRunningFullBackupTask(); void setRunningFullBackupTask(
            PerformFullTransportBackupTask runningFullBackupTask); void unlockSystemUser(); @Override int generateRandomIntegerToken(); PackageManagerBackupAgent makeMetadataAgent(); PackageManagerBackupAgent makeMetadataAgent(List<PackageInfo> packages); void addBackupTrace(String s); void clearBackupTrace(); static BackupManagerService create(
            Context context,
            Trampoline parent,
            HandlerThread backupThread); byte[] randomBytes(int bits); @Override boolean setBackupPassword(String currentPw, String newPw); @Override boolean hasBackupPassword(); boolean backupPasswordMatches(String currentPw); void recordInitPending(
            boolean isPending, String transportName, String transportDirName); void resetBackupState(File stateFileDir); void logBackupComplete(String packageName); void writeRestoreTokens(); @Override IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode); void clearApplicationDataSynchronous(String packageName, boolean keepSystemState); @Override long getAvailableRestoreToken(String packageName); @Override int requestBackup(String[] packages, IBackupObserver observer, int flags); @Override int requestBackup(String[] packages, IBackupObserver observer,
            IBackupManagerMonitor monitor, int flags); @Override void cancelBackups(); @Override void prepareOperationTimeout(int token, long interval, BackupRestoreTask callback,
            int operationType); void removeOperation(int token); @Override boolean waitUntilOperationComplete(int token); void handleCancel(int token, boolean cancelAll); boolean isBackupOperationInProgress(); @Override void tearDownAgentAndKill(ApplicationInfo app); boolean deviceIsEncrypted(); void scheduleNextFullBackupJob(long transportMinLatency); void enqueueFullBackup(String packageName, long lastBackedUp); @Override boolean beginFullBackup(FullBackupJob scheduledJob); @Override void endFullBackup(); void restoreWidgetData(String packageName, byte[] widgetData); void dataChangedImpl(String packageName); @Override void dataChanged(final String packageName); @Override void initializeTransports(String[] transportNames, IBackupObserver observer); @Override void clearBackupData(String transportName, String packageName); @Override void backupNow(); boolean deviceIsProvisioned(); @Override void adbBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeObbs,
            boolean includeShared, boolean doWidgets, boolean doAllApps, boolean includeSystem,
            boolean compress, boolean doKeyValue, String[] pkgList); @Override void fullTransportBackup(String[] pkgNames); @Override void adbRestore(ParcelFileDescriptor fd); void signalAdbBackupRestoreCompletion(AdbParams params); @Override void acknowledgeAdbBackupOrRestore(int token, boolean allow,
            String curPassword, String encPpassword, IFullBackupRestoreObserver observer); @Override void setBackupEnabled(boolean enable); @Override void setAutoRestore(boolean doAutoRestore); @Override void setBackupProvisioned(boolean available); @Override boolean isBackupEnabled(); @Override String getCurrentTransport(); @Override String[] listAllTransports(); @Override ComponentName[] listAllTransportComponents(); @Override String[] getTransportWhitelist(); @Override void updateTransportAttributes(
            ComponentName transportComponent,
            String name,
            @Nullable Intent configurationIntent,
            String currentDestinationString,
            @Nullable Intent dataManagementIntent,
            @Nullable String dataManagementLabel); @Override @Deprecated @Nullable String selectBackupTransport(String transportName); @Override void selectBackupTransportAsync(
            ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener); @Override Intent getConfigurationIntent(String transportName); @Override String getDestinationString(String transportName); @Override Intent getDataManagementIntent(String transportName); @Override String getDataManagementLabel(String transportName); @Override void agentConnected(String packageName, IBinder agentBinder); @Override void agentDisconnected(String packageName); @Override void restoreAtInstall(String packageName, int token); @Override IRestoreSession beginRestoreSession(String packageName, String transport); void clearRestoreSession(ActiveRestoreSession currentSession); @Override void opComplete(int token, long result); @Override boolean isAppEligibleForBackup(String packageName); @Override String[] filterAppsEligibleForBackup(String[] packages); @Override void dump(FileDescriptor fd, PrintWriter pw, String[] args); @Override IBackupManager getBackupManagerBinder(); }
BackupManagerService implements BackupManagerServiceInterface { @Override public void selectBackupTransportAsync( ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener) { mContext.enforceCallingOrSelfPermission( android.Manifest.permission.BACKUP, "selectBackupTransportAsync"); if (!isAllowedByMandatoryBackupTransportPolicy(transportComponent)) { try { if (listener != null) { Slog.w(TAG, "Failed to select transport - disallowed by device owner policy."); listener.onFailure(BackupManager.ERROR_BACKUP_NOT_ALLOWED); } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } return; } final long oldId = Binder.clearCallingIdentity(); try { String transportString = transportComponent.flattenToShortString(); Slog.v(TAG, "selectBackupTransportAsync(transport = " + transportString + ")"); mBackupHandler.post( () -> { String transportName = null; int result = mTransportManager.registerAndSelectTransport(transportComponent); if (result == BackupManager.SUCCESS) { try { transportName = mTransportManager.getTransportName(transportComponent); updateStateForTransport(transportName); } catch (TransportNotRegisteredException e) { Slog.e(TAG, "Transport got unregistered"); result = BackupManager.ERROR_TRANSPORT_UNAVAILABLE; } } try { if (listener != null) { if (transportName != null) { listener.onSuccess(transportName); } else { listener.onFailure(result); } } } catch (RemoteException e) { Slog.e(TAG, "ISelectBackupTransportCallback listener not available"); } }); } finally { Binder.restoreCallingIdentity(oldId); } } @VisibleForTesting  BackupManagerService(
            Context context,
            Trampoline parent,
            HandlerThread backupThread,
            File baseStateDir,
            File dataDir,
            TransportManager transportManager); BackupManagerConstants getConstants(); BackupAgentTimeoutParameters getAgentTimeoutParameters(); Context getContext(); void setContext(Context context); PackageManager getPackageManager(); void setPackageManager(PackageManager packageManager); IPackageManager getPackageManagerBinder(); void setPackageManagerBinder(IPackageManager packageManagerBinder); IActivityManager getActivityManager(); void setActivityManager(IActivityManager activityManager); AlarmManager getAlarmManager(); void setAlarmManager(AlarmManager alarmManager); void setBackupManagerBinder(IBackupManager backupManagerBinder); TransportManager getTransportManager(); boolean isEnabled(); void setEnabled(boolean enabled); boolean isProvisioned(); void setProvisioned(boolean provisioned); PowerManager.WakeLock getWakelock(); void setWakelock(PowerManager.WakeLock wakelock); Handler getBackupHandler(); void setBackupHandler(BackupHandler backupHandler); PendingIntent getRunInitIntent(); void setRunInitIntent(PendingIntent runInitIntent); HashMap<String, BackupRequest> getPendingBackups(); void setPendingBackups(
            HashMap<String, BackupRequest> pendingBackups); Object getQueueLock(); boolean isBackupRunning(); void setBackupRunning(boolean backupRunning); long getLastBackupPass(); void setLastBackupPass(long lastBackupPass); Object getClearDataLock(); boolean isClearingData(); void setClearingData(boolean clearingData); boolean isRestoreInProgress(); void setRestoreInProgress(boolean restoreInProgress); Queue<PerformUnifiedRestoreTask> getPendingRestores(); ActiveRestoreSession getActiveRestoreSession(); void setActiveRestoreSession(
            ActiveRestoreSession activeRestoreSession); SparseArray<Operation> getCurrentOperations(); Object getCurrentOpLock(); SparseArray<AdbParams> getAdbBackupRestoreConfirmations(); File getBaseStateDir(); void setBaseStateDir(File baseStateDir); File getDataDir(); void setDataDir(File dataDir); DataChangedJournal getJournal(); void setJournal(@Nullable DataChangedJournal journal); SecureRandom getRng(); Set<String> getAncestralPackages(); void setAncestralPackages(Set<String> ancestralPackages); long getAncestralToken(); void setAncestralToken(long ancestralToken); long getCurrentToken(); void setCurrentToken(long currentToken); ArraySet<String> getPendingInits(); void clearPendingInits(); PerformFullTransportBackupTask getRunningFullBackupTask(); void setRunningFullBackupTask(
            PerformFullTransportBackupTask runningFullBackupTask); void unlockSystemUser(); @Override int generateRandomIntegerToken(); PackageManagerBackupAgent makeMetadataAgent(); PackageManagerBackupAgent makeMetadataAgent(List<PackageInfo> packages); void addBackupTrace(String s); void clearBackupTrace(); static BackupManagerService create(
            Context context,
            Trampoline parent,
            HandlerThread backupThread); byte[] randomBytes(int bits); @Override boolean setBackupPassword(String currentPw, String newPw); @Override boolean hasBackupPassword(); boolean backupPasswordMatches(String currentPw); void recordInitPending(
            boolean isPending, String transportName, String transportDirName); void resetBackupState(File stateFileDir); void logBackupComplete(String packageName); void writeRestoreTokens(); @Override IBackupAgent bindToAgentSynchronous(ApplicationInfo app, int mode); void clearApplicationDataSynchronous(String packageName, boolean keepSystemState); @Override long getAvailableRestoreToken(String packageName); @Override int requestBackup(String[] packages, IBackupObserver observer, int flags); @Override int requestBackup(String[] packages, IBackupObserver observer,
            IBackupManagerMonitor monitor, int flags); @Override void cancelBackups(); @Override void prepareOperationTimeout(int token, long interval, BackupRestoreTask callback,
            int operationType); void removeOperation(int token); @Override boolean waitUntilOperationComplete(int token); void handleCancel(int token, boolean cancelAll); boolean isBackupOperationInProgress(); @Override void tearDownAgentAndKill(ApplicationInfo app); boolean deviceIsEncrypted(); void scheduleNextFullBackupJob(long transportMinLatency); void enqueueFullBackup(String packageName, long lastBackedUp); @Override boolean beginFullBackup(FullBackupJob scheduledJob); @Override void endFullBackup(); void restoreWidgetData(String packageName, byte[] widgetData); void dataChangedImpl(String packageName); @Override void dataChanged(final String packageName); @Override void initializeTransports(String[] transportNames, IBackupObserver observer); @Override void clearBackupData(String transportName, String packageName); @Override void backupNow(); boolean deviceIsProvisioned(); @Override void adbBackup(ParcelFileDescriptor fd, boolean includeApks, boolean includeObbs,
            boolean includeShared, boolean doWidgets, boolean doAllApps, boolean includeSystem,
            boolean compress, boolean doKeyValue, String[] pkgList); @Override void fullTransportBackup(String[] pkgNames); @Override void adbRestore(ParcelFileDescriptor fd); void signalAdbBackupRestoreCompletion(AdbParams params); @Override void acknowledgeAdbBackupOrRestore(int token, boolean allow,
            String curPassword, String encPpassword, IFullBackupRestoreObserver observer); @Override void setBackupEnabled(boolean enable); @Override void setAutoRestore(boolean doAutoRestore); @Override void setBackupProvisioned(boolean available); @Override boolean isBackupEnabled(); @Override String getCurrentTransport(); @Override String[] listAllTransports(); @Override ComponentName[] listAllTransportComponents(); @Override String[] getTransportWhitelist(); @Override void updateTransportAttributes(
            ComponentName transportComponent,
            String name,
            @Nullable Intent configurationIntent,
            String currentDestinationString,
            @Nullable Intent dataManagementIntent,
            @Nullable String dataManagementLabel); @Override @Deprecated @Nullable String selectBackupTransport(String transportName); @Override void selectBackupTransportAsync(
            ComponentName transportComponent, @Nullable ISelectBackupTransportCallback listener); @Override Intent getConfigurationIntent(String transportName); @Override String getDestinationString(String transportName); @Override Intent getDataManagementIntent(String transportName); @Override String getDataManagementLabel(String transportName); @Override void agentConnected(String packageName, IBinder agentBinder); @Override void agentDisconnected(String packageName); @Override void restoreAtInstall(String packageName, int token); @Override IRestoreSession beginRestoreSession(String packageName, String transport); void clearRestoreSession(ActiveRestoreSession currentSession); @Override void opComplete(int token, long result); @Override boolean isAppEligibleForBackup(String packageName); @Override String[] filterAppsEligibleForBackup(String[] packages); @Override void dump(FileDescriptor fd, PrintWriter pw, String[] args); @Override IBackupManager getBackupManagerBinder(); static final String TAG; static final boolean DEBUG; static final boolean MORE_DEBUG; static final boolean DEBUG_SCHEDULING; static final String KEY_WIDGET_STATE; static final String BACKUP_MANIFEST_FILENAME; static final int BACKUP_MANIFEST_VERSION; static final int BACKUP_FILE_VERSION; static final String BACKUP_FILE_HEADER_MAGIC; static final String BACKUP_METADATA_FILENAME; static final int BACKUP_METADATA_VERSION; static final int BACKUP_WIDGET_METADATA_TOKEN; static final String SETTINGS_PACKAGE; static final String SHARED_BACKUP_AGENT_PACKAGE; static final String RUN_BACKUP_ACTION; static final String RUN_INITIALIZE_ACTION; static final String BACKUP_FINISHED_ACTION; static final String BACKUP_FINISHED_PACKAGE_EXTRA; static final String PACKAGE_MANAGER_SENTINEL; static final boolean DEBUG_BACKUP_TRACE; static final int OP_PENDING; static final int OP_TYPE_BACKUP_WAIT; static final int OP_TYPE_RESTORE_WAIT; static final int OP_TYPE_BACKUP; }