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.DEBUG;
20 import static com.android.server.backup.BackupManagerService.TAG;
21 
22 import android.app.backup.BackupProgress;
23 import android.app.backup.IBackupObserver;
24 import android.os.RemoteException;
25 import android.util.Slog;
26 
27 /**
28  * Utility methods to communicate with BackupObserver.
29  */
30 public class BackupObserverUtils {
31     /**
32      * Wraps {@link IBackupObserver#onUpdate(String, BackupProgress)} to handle RemoteException,
33      * so that the caller doesn't have to.
34      */
sendBackupOnUpdate(IBackupObserver observer, String packageName, BackupProgress progress)35     public static void sendBackupOnUpdate(IBackupObserver observer, String packageName,
36             BackupProgress progress) {
37         if (observer != null) {
38             try {
39                 observer.onUpdate(packageName, progress);
40             } catch (RemoteException e) {
41                 if (DEBUG) {
42                     Slog.w(TAG, "Backup observer went away: onUpdate");
43                 }
44             }
45         }
46     }
47 
48     /**
49      * Wraps {@link IBackupObserver#onResult(String, int)} to handle RemoteException, so that the
50      * caller doesn't have to.
51      */
sendBackupOnPackageResult(IBackupObserver observer, String packageName, int status)52     public static void sendBackupOnPackageResult(IBackupObserver observer, String packageName,
53             int status) {
54         if (observer != null) {
55             try {
56                 observer.onResult(packageName, status);
57             } catch (RemoteException e) {
58                 if (DEBUG) {
59                     Slog.w(TAG, "Backup observer went away: onResult");
60                 }
61             }
62         }
63     }
64 
65     /**
66      * Wraps {@link IBackupObserver#backupFinished(int)} to handle RemoteException, so that the
67      * caller doesn't have to.
68      */
sendBackupFinished(IBackupObserver observer, int status)69     public static void sendBackupFinished(IBackupObserver observer, int status) {
70         if (observer != null) {
71             try {
72                 observer.backupFinished(status);
73             } catch (RemoteException e) {
74                 if (DEBUG) {
75                     Slog.w(TAG, "Backup observer went away: backupFinished");
76                 }
77             }
78         }
79     }
80 }
81