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