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 android.app.backup.BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_NAME; 20 21 import static com.android.server.backup.BackupManagerService.DEBUG; 22 import static com.android.server.backup.BackupManagerService.TAG; 23 24 import android.app.backup.BackupManagerMonitor; 25 import android.app.backup.IBackupManagerMonitor; 26 import android.content.pm.PackageInfo; 27 import android.os.Bundle; 28 import android.os.RemoteException; 29 import android.util.Slog; 30 31 /** 32 * Utility methods to communicate with BackupManagerMonitor. 33 */ 34 public class BackupManagerMonitorUtils { 35 /** 36 * Notifies monitor about the event. 37 * 38 * Calls {@link IBackupManagerMonitor#onEvent(Bundle)} with a bundle representing current event. 39 * 40 * @param monitor - implementation of {@link IBackupManagerMonitor} to notify. 41 * @param id - event id. 42 * @param pkg - package event is related to. 43 * @param category - event category. 44 * @param extras - additional event data. 45 * @return <code>monitor</code> if call succeeded and <code>null</code> otherwise. 46 */ 47 public static IBackupManagerMonitor monitorEvent(IBackupManagerMonitor monitor, int id, 48 PackageInfo pkg, int category, Bundle extras) { 49 if (monitor != null) { 50 try { 51 Bundle bundle = new Bundle(); 52 bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_ID, id); 53 bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_CATEGORY, category); 54 if (pkg != null) { 55 bundle.putString(EXTRA_LOG_EVENT_PACKAGE_NAME, 56 pkg.packageName); 57 bundle.putInt(BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_VERSION, 58 pkg.versionCode); 59 bundle.putLong(BackupManagerMonitor.EXTRA_LOG_EVENT_PACKAGE_LONG_VERSION, 60 pkg.getLongVersionCode()); 61 } 62 if (extras != null) { 63 bundle.putAll(extras); 64 } 65 monitor.onEvent(bundle); 66 return monitor; 67 } catch (RemoteException e) { 68 if (DEBUG) { 69 Slog.w(TAG, "backup manager monitor went away"); 70 } 71 } 72 } 73 return null; 74 } 75 76 /** 77 * Adds given key-value pair in the bundle and returns the bundle. If bundle was null it will 78 * be created. 79 * 80 * @param extras - bundle where to add key-value to, if null a new bundle will be created. 81 * @param key - key. 82 * @param value - value. 83 * @return extras if it was not null and new bundle otherwise. 84 */ 85 public static Bundle putMonitoringExtra(Bundle extras, String key, String value) { 86 if (extras == null) { 87 extras = new Bundle(); 88 } 89 extras.putString(key, value); 90 return extras; 91 } 92 93 /** 94 * Adds given key-value pair in the bundle and returns the bundle. If bundle was null it will 95 * be created. 96 * 97 * @param extras - bundle where to add key-value to, if null a new bundle will be created. 98 * @param key - key. 99 * @param value - value. 100 * @return extras if it was not null and new bundle otherwise. 101 */ 102 public static Bundle putMonitoringExtra(Bundle extras, String key, long value) { 103 if (extras == null) { 104 extras = new Bundle(); 105 } 106 extras.putLong(key, value); 107 return extras; 108 } 109 110 /** 111 * Adds given key-value pair in the bundle and returns the bundle. If bundle was null it will 112 * be created. 113 * 114 * @param extras - bundle where to add key-value to, if null a new bundle will be created. 115 * @param key - key. 116 * @param value - value. 117 * @return extras if it was not null and new bundle otherwise. 118 */ 119 public static Bundle putMonitoringExtra(Bundle extras, String key, boolean value) { 120 if (extras == null) { 121 extras = new Bundle(); 122 } 123 extras.putBoolean(key, value); 124 return extras; 125 } 126 } 127