1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.server.backup.utils;
18 
19 import static com.android.server.backup.BackupManagerService.TAG;
20 
21 import android.app.backup.IFullBackupRestoreObserver;
22 import android.os.RemoteException;
23 import android.util.Slog;
24 
25 /**
26  * Utility methods to communicate with FullBackupRestoreObserver.
27  */
28 public class FullBackupRestoreObserverUtils {
29     /**
30      * Wraps {@link IFullBackupRestoreObserver#onStartRestore()} to handle RemoteException, so that
31      * the caller doesn't have to.
32      *
33      * @param observer - IFullBackupRestoreObserver to communicate with.
34      * @return observer if the call worked and null if there was a communication problem.
35      */
sendStartRestore(IFullBackupRestoreObserver observer)36     public static IFullBackupRestoreObserver sendStartRestore(IFullBackupRestoreObserver observer) {
37         if (observer != null) {
38             try {
39                 observer.onStartRestore();
40             } catch (RemoteException e) {
41                 Slog.w(TAG, "full restore observer went away: startRestore");
42                 observer = null;
43             }
44         }
45         return observer;
46     }
47 
48     /**
49      * Wraps {@link IFullBackupRestoreObserver#onRestorePackage(String)} to handle RemoteException,
50      * so that the caller doesn't have to.
51      *
52      * @param observer - IFullBackupRestoreObserver to communicate with.
53      * @param name - package name.
54      * @return observer if the call worked and null if there was a communication problem.
55      */
sendOnRestorePackage( IFullBackupRestoreObserver observer, String name)56     public static IFullBackupRestoreObserver sendOnRestorePackage(
57             IFullBackupRestoreObserver observer, String name) {
58         if (observer != null) {
59             try {
60                 // TODO: use a more user-friendly name string
61                 observer.onRestorePackage(name);
62             } catch (RemoteException e) {
63                 Slog.w(TAG, "full restore observer went away: restorePackage");
64                 observer = null;
65             }
66         }
67         return observer;
68     }
69 
70     /**
71      * Wraps {@link IFullBackupRestoreObserver#onEndRestore()} ()} to handle RemoteException, so
72      * that the caller doesn't have to.
73      *
74      * @param observer - IFullBackupRestoreObserver to communicate with.
75      * @return observer if the call worked and null if there was a communication problem.
76      */
sendEndRestore(IFullBackupRestoreObserver observer)77     public static IFullBackupRestoreObserver sendEndRestore(IFullBackupRestoreObserver observer) {
78         if (observer != null) {
79             try {
80                 observer.onEndRestore();
81             } catch (RemoteException e) {
82                 Slog.w(TAG, "full restore observer went away: endRestore");
83                 observer = null;
84             }
85         }
86         return observer;
87     }
88 }
89