blob: c099ad4ee8c453de776d33820a0541e2eacb597a [file] [log] [blame]
Anton Philippovadfec552017-01-25 20:37:36 +00001/*
2 * Copyright (C) 2017 The Android Open Source Project
Torsten Grote381c12b2020-09-30 15:14:49 -03003 * Copyright (C) 2024 The LineageOS Project
Anton Philippovadfec552017-01-25 20:37:36 +00004 *
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
18package com.android.settings.backup;
19
Anton Philippov9a5220e2017-03-01 21:08:56 +000020import android.content.Context;
Anton Philippov18dd11f2017-02-03 16:01:52 +000021import android.content.Intent;
22import android.content.pm.PackageManager;
Anton Philippovadfec552017-01-25 20:37:36 +000023import android.os.Bundle;
24import android.util.Log;
25
Fan Zhang23f8d592018-08-28 15:11:40 -070026import androidx.annotation.VisibleForTesting;
Fan Zhang23f8d592018-08-28 15:11:40 -070027import androidx.fragment.app.FragmentManager;
28
Anton Philippov9a5220e2017-03-01 21:08:56 +000029import com.android.settings.R;
Jason Chiu9eab62a2024-11-06 14:44:17 +080030import com.android.settings.SettingsActivity;
Anton Philippov9a5220e2017-03-01 21:08:56 +000031import com.android.settings.search.BaseSearchIndexProvider;
Torsten Grote381c12b2020-09-30 15:14:49 -030032import com.android.settings.utils.InsetUtils;
Raff Tsai966fa012019-09-25 11:19:06 +080033import com.android.settingslib.search.Indexable;
Tony Mantler0fcd6cb2018-03-26 15:17:25 -070034import com.android.settingslib.search.SearchIndexable;
Yanting Yangb7e1ece2020-04-10 20:08:38 +080035import com.android.settingslib.search.SearchIndexableRaw;
Anton Philippov9a5220e2017-03-01 21:08:56 +000036
37import java.util.ArrayList;
38import java.util.List;
39
Anton Philippovadfec552017-01-25 20:37:36 +000040
41/**
42 * The activity used to launch the configured Backup activity or the preference screen
43 * if the manufacturer provided their backup settings.
Chandan Nath0bfef632019-01-28 21:15:13 +000044 * 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 Philippovadfec552017-01-25 20:37:36 +000049 */
Tony Mantler0fcd6cb2018-03-26 15:17:25 -070050@SearchIndexable
Jason Chiu9eab62a2024-11-06 14:44:17 +080051public class UserBackupSettingsActivity extends SettingsActivity implements Indexable {
Anton Philippovadfec552017-01-25 20:37:36 +000052 private static final String TAG = "BackupSettingsActivity";
Anton Philippovf8b31542017-02-14 10:41:20 +000053 private FragmentManager mFragmentManager;
Anton Philippovadfec552017-01-25 20:37:36 +000054
55 @Override
56 public void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
Jason Chiu9eab62a2024-11-06 14:44:17 +080058 if (isFinishing()) {
59 return;
60 }
Anton Philippovadfec552017-01-25 20:37:36 +000061
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 Philippov18dd11f2017-02-03 16:01:52 +000071 Intent intent = backupHelper.getIntentForBackupSettings();
Bernardo Rufino368b5a62018-03-06 16:07:13 +000072 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 Philippov18dd11f2017-02-03 16:01:52 +000083
Anton Philippovadfec552017-01-25 20:37:36 +000084 // use startActivityForResult to let the activity check the caller signature
Anton Philippov18dd11f2017-02-03 16:01:52 +000085 startActivityForResult(intent, 1);
Anton Philippovadfec552017-01-25 20:37:36 +000086 finish();
87 } else {
88 if (Log.isLoggable(TAG, Log.DEBUG)) {
89 Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
90 }
Torsten Grote381c12b2020-09-30 15:14:49 -030091
92 InsetUtils.applyWindowInsetsListener(findViewById(R.id.main_content));
93
Anton Philippovf8b31542017-02-14 10:41:20 +000094 // mFragmentManager can be set by {@link #setFragmentManager()} for testing
95 if (mFragmentManager == null) {
tmfang27c84de2018-06-28 11:39:05 +080096 mFragmentManager = getSupportFragmentManager();
Anton Philippovf8b31542017-02-14 10:41:20 +000097 }
98 mFragmentManager.beginTransaction()
Torsten Grote381c12b2020-09-30 15:14:49 -030099 .replace(R.id.main_content, new BackupSettingsFragment())
Anton Philippovadfec552017-01-25 20:37:36 +0000100 .commit();
101 }
102 }
Anton Philippovf8b31542017-02-14 10:41:20 +0000103
Anton Philippov9a5220e2017-03-01 21:08:56 +0000104 /**
105 * For Search.
106 */
Raff Tsaiac3e0d02019-09-19 17:06:45 +0800107 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Anton Philippov9a5220e2017-03-01 21:08:56 +0000108 new BaseSearchIndexProvider() {
Chandan Nath826a91c2019-04-10 17:02:02 +0100109 private static final String BACKUP_SEARCH_INDEX_KEY = "Backup";
Anton Philippov9a5220e2017-03-01 21:08:56 +0000110
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 Zhangb4737302017-05-31 12:43:48 -0700119 data.title = context.getString(R.string.privacy_settings_title);
Yanting Yangb7e1ece2020-04-10 20:08:38 +0800120 data.screenTitle = context.getString(R.string.privacy_settings_title);
Fan Zhangb4737302017-05-31 12:43:48 -0700121 data.keywords = context.getString(R.string.keywords_backup);
Anton Philippov9a5220e2017-03-01 21:08:56 +0000122 data.intentTargetPackage = context.getPackageName();
Fan Zhang03dd39c2019-04-02 16:41:01 -0700123 data.intentTargetClass = UserBackupSettingsActivity.class.getName();
Fan Zhangb4737302017-05-31 12:43:48 -0700124 data.intentAction = Intent.ACTION_MAIN;
Anton Philippov9a5220e2017-03-01 21:08:56 +0000125 data.key = BACKUP_SEARCH_INDEX_KEY;
126 result.add(data);
127
128 return result;
129 }
Chandan Nath826a91c2019-04-10 17:02:02 +0100130
131 @Override
132 public List<String> getNonIndexableKeys(Context context) {
133 final List<String> keys = super.getNonIndexableKeys(context);
Chandan Nathf94033d2019-04-24 18:58:50 +0100134 if (!new BackupSettingsHelper(context).isBackupServiceActive()) {
Chandan Nath826a91c2019-04-10 17:02:02 +0100135 keys.add(BACKUP_SEARCH_INDEX_KEY);
136 }
137 return keys;
138 }
Anton Philippov9a5220e2017-03-01 21:08:56 +0000139 };
140
Anton Philippovf8b31542017-02-14 10:41:20 +0000141 @VisibleForTesting
142 void setFragmentManager(FragmentManager fragmentManager) {
143 mFragmentManager = fragmentManager;
144 }
Anton Philippovadfec552017-01-25 20:37:36 +0000145}