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.settings.backup; 18 19 20 import android.app.backup.BackupManager; 21 import android.app.backup.IBackupManager; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.RemoteException; 25 import android.os.ServiceManager; 26 import android.os.UserHandle; 27 import android.text.TextUtils; 28 import android.util.Log; 29 30 import androidx.annotation.VisibleForTesting; 31 32 import com.android.settings.R; 33 import com.android.settings.Settings.PrivacySettingsActivity; 34 35 import java.net.URISyntaxException; 36 37 /** 38 * Helper class for {@link UserBackupSettingsActivity} that interacts with {@link IBackupManager}. 39 */ 40 public class BackupSettingsHelper { 41 private static final String TAG = "BackupSettingsHelper"; 42 43 private IBackupManager mBackupManager = IBackupManager.Stub.asInterface( 44 ServiceManager.getService(Context.BACKUP_SERVICE)); 45 46 private Context mContext; 47 BackupSettingsHelper(Context context)48 public BackupSettingsHelper(Context context) { 49 mContext = context; 50 } 51 52 /** 53 * Returns an intent to launch backup settings from backup transport if the intent was provided 54 * by the transport. Otherwise returns the intent to launch the default backup settings screen. 55 * 56 * @return Intent for launching backup settings 57 */ getIntentForBackupSettings()58 public Intent getIntentForBackupSettings() { 59 Intent intent; 60 if (isIntentProvidedByTransport()) { 61 intent = getIntentForBackupSettingsFromTransport(); 62 } else { 63 Log.e(TAG, "Backup transport has not provided an intent" 64 + " or the component for the intent is not found!"); 65 intent = getIntentForDefaultBackupSettings(); 66 } 67 return intent; 68 } 69 70 /** 71 * Returns a label for the settings item that will point to the backup settings provided by 72 * the transport. If no label was provided by transport, returns the default string. 73 * 74 * @return Label for the backup settings item. 75 */ getLabelForBackupSettings()76 public CharSequence getLabelForBackupSettings() { 77 CharSequence label = getLabelFromBackupTransport(); 78 if (TextUtils.isEmpty(label)) { 79 label = mContext.getString(R.string.privacy_settings_title); 80 } 81 return label; 82 } 83 84 /** 85 * Returns a summary string for the settings item that will point to the backup settings 86 * provided by the transport. If no summary was provided by transport, returns the default 87 * string. 88 * 89 * @return Summary for the backup settings item. 90 */ getSummaryForBackupSettings()91 public String getSummaryForBackupSettings() { 92 String summary = getSummaryFromBackupTransport(); 93 if (summary == null) { 94 summary = mContext.getString(R.string.backup_configure_account_default_summary); 95 } 96 return summary; 97 } 98 99 100 /** 101 * Checks if the manufacturer provided an intent to launch its backup settings screen 102 * in the config file. 103 */ isBackupProvidedByManufacturer()104 public boolean isBackupProvidedByManufacturer() { 105 if (Log.isLoggable(TAG, Log.DEBUG)) { 106 Log.d(TAG, "Checking if intent provided by manufacturer"); 107 } 108 String intentString = 109 mContext.getResources().getString(R.string.config_backup_settings_intent); 110 111 return intentString != null && !intentString.isEmpty(); 112 } 113 114 /** 115 * Returns the label for the backup settings item provided by the manufacturer. 116 */ getLabelProvidedByManufacturer()117 public String getLabelProvidedByManufacturer() { 118 return mContext.getResources().getString(R.string.config_backup_settings_label); 119 } 120 121 /** 122 * Returns the intent to the backup settings screen provided by the manufacturer. 123 */ getIntentProvidedByManufacturer()124 public Intent getIntentProvidedByManufacturer() { 125 if (Log.isLoggable(TAG, Log.DEBUG)) { 126 Log.d(TAG, "Getting a backup settings intent provided by manufacturer"); 127 } 128 String intentString = 129 mContext.getResources().getString(R.string.config_backup_settings_intent); 130 if (intentString != null && !intentString.isEmpty()) { 131 try { 132 return Intent.parseUri(intentString, 0); 133 } catch (URISyntaxException e) { 134 Log.e(TAG, "Invalid intent provided by the manufacturer.", e); 135 } 136 } 137 return null; 138 } 139 140 /** 141 * Gets the intent from Backup transport and adds the extra depending on whether the user has 142 * rights to see backup settings. 143 * 144 * @return Intent to launch Backup settings provided by the Backup transport. 145 */ 146 @VisibleForTesting getIntentForBackupSettingsFromTransport()147 Intent getIntentForBackupSettingsFromTransport() { 148 Intent intent = getIntentFromBackupTransport(); 149 if (intent != null) { 150 intent.putExtra(BackupManager.EXTRA_BACKUP_SERVICES_AVAILABLE, isBackupServiceActive()); 151 } 152 return intent; 153 } 154 getIntentForDefaultBackupSettings()155 private Intent getIntentForDefaultBackupSettings() { 156 return new Intent(mContext, PrivacySettingsActivity.class); 157 } 158 159 /** 160 * Checks if the transport provided the intent to launch the backup settings and if that 161 * intent resolves to an activity. 162 */ 163 @VisibleForTesting isIntentProvidedByTransport()164 boolean isIntentProvidedByTransport() { 165 Intent intent = getIntentFromBackupTransport(); 166 return intent != null && intent.resolveActivity(mContext.getPackageManager()) != null; 167 } 168 169 /** 170 * Gets an intent to launch the backup settings from the current transport using 171 * {@link com.android.internal.backup.IBackupTransport#dataManagementIntent()} API. 172 * 173 * @return intent provided by transport or null if no intent was provided. 174 */ getIntentFromBackupTransport()175 private Intent getIntentFromBackupTransport() { 176 try { 177 Intent intent = 178 mBackupManager.getDataManagementIntent(mBackupManager.getCurrentTransport()); 179 if (Log.isLoggable(TAG, Log.DEBUG)) { 180 if (intent != null) { 181 Log.d(TAG, "Parsed intent from backup transport: " + intent.toString()); 182 } else { 183 Log.d(TAG, "Received a null intent from backup transport"); 184 } 185 } 186 return intent; 187 } catch (RemoteException e) { 188 Log.e(TAG, "Error getting data management intent", e); 189 } 190 return null; 191 } 192 193 /** Checks if backup service is enabled for this user. */ isBackupServiceActive()194 public boolean isBackupServiceActive() { 195 boolean backupOkay; 196 try { 197 backupOkay = mBackupManager.isBackupServiceActive(UserHandle.myUserId()); 198 } catch (Exception e) { 199 // things go wrong talking to the backup system => ignore and 200 // pass the default 'false' as the "backup is a thing?" state. 201 backupOkay = false; 202 } 203 return backupOkay; 204 } 205 206 @VisibleForTesting getLabelFromBackupTransport()207 CharSequence getLabelFromBackupTransport() { 208 try { 209 CharSequence label = 210 mBackupManager.getDataManagementLabelForUser( 211 UserHandle.myUserId(), mBackupManager.getCurrentTransport()); 212 if (Log.isLoggable(TAG, Log.DEBUG)) { 213 Log.d(TAG, "Received the backup settings label from backup transport: " + label); 214 } 215 return label; 216 } catch (RemoteException e) { 217 Log.e(TAG, "Error getting data management label", e); 218 } 219 return null; 220 } 221 222 @VisibleForTesting getSummaryFromBackupTransport()223 String getSummaryFromBackupTransport() { 224 try { 225 String summary = 226 mBackupManager.getDestinationString(mBackupManager.getCurrentTransport()); 227 if (Log.isLoggable(TAG, Log.DEBUG)) { 228 Log.d(TAG, 229 "Received the backup settings summary from backup transport: " + summary); 230 } 231 return summary; 232 } catch (RemoteException e) { 233 Log.e(TAG, "Error getting data management summary", e); 234 } 235 return null; 236 } 237 } 238