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