| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| Torsten Grote | 381c12b | 2020-09-30 15:14:49 -0300 | [diff] [blame] | 3 | * Copyright (C) 2024 The LineageOS Project |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License |
| 16 | */ |
| 17 | |
| 18 | package com.android.settings.backup; |
| 19 | |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 20 | import android.content.Context; |
| Anton Philippov | 18dd11f | 2017-02-03 16:01:52 +0000 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.content.pm.PackageManager; |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 23 | import android.os.Bundle; |
| 24 | import android.util.Log; |
| 25 | |
| Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 26 | import androidx.annotation.VisibleForTesting; |
| Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.fragment.app.FragmentManager; |
| 28 | |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 29 | import com.android.settings.R; |
| Jason Chiu | 9eab62a | 2024-11-06 14:44:17 +0800 | [diff] [blame] | 30 | import com.android.settings.SettingsActivity; |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 31 | import com.android.settings.search.BaseSearchIndexProvider; |
| Torsten Grote | 381c12b | 2020-09-30 15:14:49 -0300 | [diff] [blame] | 32 | import com.android.settings.utils.InsetUtils; |
| Raff Tsai | 966fa01 | 2019-09-25 11:19:06 +0800 | [diff] [blame] | 33 | import com.android.settingslib.search.Indexable; |
| Tony Mantler | 0fcd6cb | 2018-03-26 15:17:25 -0700 | [diff] [blame] | 34 | import com.android.settingslib.search.SearchIndexable; |
| Yanting Yang | b7e1ece | 2020-04-10 20:08:38 +0800 | [diff] [blame] | 35 | import com.android.settingslib.search.SearchIndexableRaw; |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 36 | |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * The activity used to launch the configured Backup activity or the preference screen |
| 43 | * if the manufacturer provided their backup settings. |
| Chandan Nath | 0bfef63 | 2019-01-28 21:15:13 +0000 | [diff] [blame] | 44 | * Pre-Q, BackupSettingsActivity was disabled for non-system users. Therefore, for phones which |
| 45 | * upgrade to Q, BackupSettingsActivity is disabled for those users. However, we cannot simply |
| 46 | * enable it in Q since component enable can only be done by the user itself; which is not |
| 47 | * enough in Q we want it to be enabled for all profile users of the user. |
| 48 | * Therefore, as a simple workaround, we use a new class which is enabled by default. |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 49 | */ |
| Tony Mantler | 0fcd6cb | 2018-03-26 15:17:25 -0700 | [diff] [blame] | 50 | @SearchIndexable |
| Jason Chiu | 9eab62a | 2024-11-06 14:44:17 +0800 | [diff] [blame] | 51 | public class UserBackupSettingsActivity extends SettingsActivity implements Indexable { |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 52 | private static final String TAG = "BackupSettingsActivity"; |
| Anton Philippov | f8b3154 | 2017-02-14 10:41:20 +0000 | [diff] [blame] | 53 | private FragmentManager mFragmentManager; |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public void onCreate(Bundle savedInstanceState) { |
| 57 | super.onCreate(savedInstanceState); |
| Jason Chiu | 9eab62a | 2024-11-06 14:44:17 +0800 | [diff] [blame] | 58 | if (isFinishing()) { |
| 59 | return; |
| 60 | } |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 61 | |
| 62 | BackupSettingsHelper backupHelper = new BackupSettingsHelper(this); |
| 63 | |
| 64 | if (!backupHelper.isBackupProvidedByManufacturer()) { |
| 65 | // If manufacturer specific backup settings are not provided then launch |
| 66 | // the backup settings provided by backup transport or the default one directly. |
| 67 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 68 | Log.d(TAG, |
| 69 | "No manufacturer settings found, launching the backup settings directly"); |
| 70 | } |
| Anton Philippov | 18dd11f | 2017-02-03 16:01:52 +0000 | [diff] [blame] | 71 | Intent intent = backupHelper.getIntentForBackupSettings(); |
| Bernardo Rufino | 368b5a6 | 2018-03-06 16:07:13 +0000 | [diff] [blame] | 72 | try { |
| 73 | // enable the activity before launching it |
| 74 | getPackageManager().setComponentEnabledSetting( |
| 75 | intent.getComponent(), |
| 76 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, |
| 77 | PackageManager.DONT_KILL_APP); |
| 78 | } catch (SecurityException e) { |
| 79 | Log.w(TAG, "Trying to enable activity " + intent.getComponent() + " but couldn't: " |
| 80 | + e.getMessage()); |
| 81 | // the activity may already be enabled |
| 82 | } |
| Anton Philippov | 18dd11f | 2017-02-03 16:01:52 +0000 | [diff] [blame] | 83 | |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 84 | // use startActivityForResult to let the activity check the caller signature |
| Anton Philippov | 18dd11f | 2017-02-03 16:01:52 +0000 | [diff] [blame] | 85 | startActivityForResult(intent, 1); |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 86 | finish(); |
| 87 | } else { |
| 88 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 89 | Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen"); |
| 90 | } |
| Torsten Grote | 381c12b | 2020-09-30 15:14:49 -0300 | [diff] [blame] | 91 | |
| 92 | InsetUtils.applyWindowInsetsListener(findViewById(R.id.main_content)); |
| 93 | |
| Anton Philippov | f8b3154 | 2017-02-14 10:41:20 +0000 | [diff] [blame] | 94 | // mFragmentManager can be set by {@link #setFragmentManager()} for testing |
| 95 | if (mFragmentManager == null) { |
| tmfang | 27c84de | 2018-06-28 11:39:05 +0800 | [diff] [blame] | 96 | mFragmentManager = getSupportFragmentManager(); |
| Anton Philippov | f8b3154 | 2017-02-14 10:41:20 +0000 | [diff] [blame] | 97 | } |
| 98 | mFragmentManager.beginTransaction() |
| Torsten Grote | 381c12b | 2020-09-30 15:14:49 -0300 | [diff] [blame] | 99 | .replace(R.id.main_content, new BackupSettingsFragment()) |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 100 | .commit(); |
| 101 | } |
| 102 | } |
| Anton Philippov | f8b3154 | 2017-02-14 10:41:20 +0000 | [diff] [blame] | 103 | |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 104 | /** |
| 105 | * For Search. |
| 106 | */ |
| Raff Tsai | ac3e0d0 | 2019-09-19 17:06:45 +0800 | [diff] [blame] | 107 | public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 108 | new BaseSearchIndexProvider() { |
| Chandan Nath | 826a91c | 2019-04-10 17:02:02 +0100 | [diff] [blame] | 109 | private static final String BACKUP_SEARCH_INDEX_KEY = "Backup"; |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 110 | |
| 111 | @Override |
| 112 | public List<SearchIndexableRaw> getRawDataToIndex(Context context, |
| 113 | boolean enabled) { |
| 114 | |
| 115 | final List<SearchIndexableRaw> result = new ArrayList<>(); |
| 116 | |
| 117 | // Add the activity title |
| 118 | SearchIndexableRaw data = new SearchIndexableRaw(context); |
| Fan Zhang | b473730 | 2017-05-31 12:43:48 -0700 | [diff] [blame] | 119 | data.title = context.getString(R.string.privacy_settings_title); |
| Yanting Yang | b7e1ece | 2020-04-10 20:08:38 +0800 | [diff] [blame] | 120 | data.screenTitle = context.getString(R.string.privacy_settings_title); |
| Fan Zhang | b473730 | 2017-05-31 12:43:48 -0700 | [diff] [blame] | 121 | data.keywords = context.getString(R.string.keywords_backup); |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 122 | data.intentTargetPackage = context.getPackageName(); |
| Fan Zhang | 03dd39c | 2019-04-02 16:41:01 -0700 | [diff] [blame] | 123 | data.intentTargetClass = UserBackupSettingsActivity.class.getName(); |
| Fan Zhang | b473730 | 2017-05-31 12:43:48 -0700 | [diff] [blame] | 124 | data.intentAction = Intent.ACTION_MAIN; |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 125 | data.key = BACKUP_SEARCH_INDEX_KEY; |
| 126 | result.add(data); |
| 127 | |
| 128 | return result; |
| 129 | } |
| Chandan Nath | 826a91c | 2019-04-10 17:02:02 +0100 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public List<String> getNonIndexableKeys(Context context) { |
| 133 | final List<String> keys = super.getNonIndexableKeys(context); |
| Chandan Nath | f94033d | 2019-04-24 18:58:50 +0100 | [diff] [blame] | 134 | if (!new BackupSettingsHelper(context).isBackupServiceActive()) { |
| Chandan Nath | 826a91c | 2019-04-10 17:02:02 +0100 | [diff] [blame] | 135 | keys.add(BACKUP_SEARCH_INDEX_KEY); |
| 136 | } |
| 137 | return keys; |
| 138 | } |
| Anton Philippov | 9a5220e | 2017-03-01 21:08:56 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| Anton Philippov | f8b3154 | 2017-02-14 10:41:20 +0000 | [diff] [blame] | 141 | @VisibleForTesting |
| 142 | void setFragmentManager(FragmentManager fragmentManager) { |
| 143 | mFragmentManager = fragmentManager; |
| 144 | } |
| Anton Philippov | adfec55 | 2017-01-25 20:37:36 +0000 | [diff] [blame] | 145 | } |