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