blob: dbbb1358e1c94436c49e3177282f3607a8977942 [file] [log] [blame]
Anton Philippovadfec552017-01-25 20:37:36 +00001/*
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
17package com.android.settings.backup;
18
Anton Philippov9a5220e2017-03-01 21:08:56 +000019import android.content.Context;
Anton Philippov18dd11f2017-02-03 16:01:52 +000020import android.content.Intent;
21import android.content.pm.PackageManager;
Anton Philippovadfec552017-01-25 20:37:36 +000022import android.os.Bundle;
23import android.util.Log;
24
Fan Zhang23f8d592018-08-28 15:11:40 -070025import androidx.annotation.VisibleForTesting;
26import androidx.fragment.app.FragmentActivity;
27import androidx.fragment.app.FragmentManager;
28
Anton Philippov9a5220e2017-03-01 21:08:56 +000029import com.android.settings.R;
Anton Philippov9a5220e2017-03-01 21:08:56 +000030import com.android.settings.search.BaseSearchIndexProvider;
31import com.android.settings.search.Indexable;
32import com.android.settings.search.SearchIndexableRaw;
Tony Mantler0fcd6cb2018-03-26 15:17:25 -070033import com.android.settingslib.search.SearchIndexable;
Anton Philippov9a5220e2017-03-01 21:08:56 +000034
35import java.util.ArrayList;
36import java.util.List;
37
Anton Philippovadfec552017-01-25 20:37:36 +000038
39/**
40 * The activity used to launch the configured Backup activity or the preference screen
41 * if the manufacturer provided their backup settings.
Chandan Nath0bfef632019-01-28 21:15:13 +000042 * Pre-Q, BackupSettingsActivity was disabled for non-system users. Therefore, for phones which
43 * upgrade to Q, BackupSettingsActivity is disabled for those users. However, we cannot simply
44 * enable it in Q since component enable can only be done by the user itself; which is not
45 * enough in Q we want it to be enabled for all profile users of the user.
46 * Therefore, as a simple workaround, we use a new class which is enabled by default.
Anton Philippovadfec552017-01-25 20:37:36 +000047 */
Tony Mantler0fcd6cb2018-03-26 15:17:25 -070048@SearchIndexable
Chandan Nath0bfef632019-01-28 21:15:13 +000049public class UserBackupSettingsActivity extends FragmentActivity implements Indexable {
Anton Philippovadfec552017-01-25 20:37:36 +000050 private static final String TAG = "BackupSettingsActivity";
Anton Philippovf8b31542017-02-14 10:41:20 +000051 private FragmentManager mFragmentManager;
Anton Philippovadfec552017-01-25 20:37:36 +000052
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56
57 BackupSettingsHelper backupHelper = new BackupSettingsHelper(this);
58
59 if (!backupHelper.isBackupProvidedByManufacturer()) {
60 // If manufacturer specific backup settings are not provided then launch
61 // the backup settings provided by backup transport or the default one directly.
62 if (Log.isLoggable(TAG, Log.DEBUG)) {
63 Log.d(TAG,
64 "No manufacturer settings found, launching the backup settings directly");
65 }
Anton Philippov18dd11f2017-02-03 16:01:52 +000066 Intent intent = backupHelper.getIntentForBackupSettings();
Bernardo Rufino368b5a62018-03-06 16:07:13 +000067 try {
68 // enable the activity before launching it
69 getPackageManager().setComponentEnabledSetting(
70 intent.getComponent(),
71 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
72 PackageManager.DONT_KILL_APP);
73 } catch (SecurityException e) {
74 Log.w(TAG, "Trying to enable activity " + intent.getComponent() + " but couldn't: "
75 + e.getMessage());
76 // the activity may already be enabled
77 }
Anton Philippov18dd11f2017-02-03 16:01:52 +000078
Anton Philippovadfec552017-01-25 20:37:36 +000079 // use startActivityForResult to let the activity check the caller signature
Anton Philippov18dd11f2017-02-03 16:01:52 +000080 startActivityForResult(intent, 1);
Anton Philippovadfec552017-01-25 20:37:36 +000081 finish();
82 } else {
83 if (Log.isLoggable(TAG, Log.DEBUG)) {
84 Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
85 }
Anton Philippovf8b31542017-02-14 10:41:20 +000086 // mFragmentManager can be set by {@link #setFragmentManager()} for testing
87 if (mFragmentManager == null) {
tmfang27c84de2018-06-28 11:39:05 +080088 mFragmentManager = getSupportFragmentManager();
Anton Philippovf8b31542017-02-14 10:41:20 +000089 }
90 mFragmentManager.beginTransaction()
Anton Philippovadfec552017-01-25 20:37:36 +000091 .replace(android.R.id.content, new BackupSettingsFragment())
92 .commit();
93 }
94 }
Anton Philippovf8b31542017-02-14 10:41:20 +000095
Anton Philippov9a5220e2017-03-01 21:08:56 +000096 /**
97 * For Search.
98 */
99 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
100 new BaseSearchIndexProvider() {
Chandan Nath826a91c2019-04-10 17:02:02 +0100101 private static final String BACKUP_SEARCH_INDEX_KEY = "Backup";
Anton Philippov9a5220e2017-03-01 21:08:56 +0000102
103 @Override
104 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
105 boolean enabled) {
106
107 final List<SearchIndexableRaw> result = new ArrayList<>();
108
109 // Add the activity title
110 SearchIndexableRaw data = new SearchIndexableRaw(context);
Fan Zhangb4737302017-05-31 12:43:48 -0700111 data.title = context.getString(R.string.privacy_settings_title);
112 data.screenTitle = context.getString(R.string.settings_label);
113 data.keywords = context.getString(R.string.keywords_backup);
Anton Philippov9a5220e2017-03-01 21:08:56 +0000114 data.intentTargetPackage = context.getPackageName();
Fan Zhang03dd39c2019-04-02 16:41:01 -0700115 data.intentTargetClass = UserBackupSettingsActivity.class.getName();
Fan Zhangb4737302017-05-31 12:43:48 -0700116 data.intentAction = Intent.ACTION_MAIN;
Anton Philippov9a5220e2017-03-01 21:08:56 +0000117 data.key = BACKUP_SEARCH_INDEX_KEY;
118 result.add(data);
119
120 return result;
121 }
Chandan Nath826a91c2019-04-10 17:02:02 +0100122
123 @Override
124 public List<String> getNonIndexableKeys(Context context) {
125 final List<String> keys = super.getNonIndexableKeys(context);
126 if (!new BackupSettingsHelper(context).showBackupSettingsForUser()) {
127 keys.add(BACKUP_SEARCH_INDEX_KEY);
128 }
129 return keys;
130 }
Anton Philippov9a5220e2017-03-01 21:08:56 +0000131 };
132
Anton Philippovf8b31542017-02-14 10:41:20 +0000133 @VisibleForTesting
134 void setFragmentManager(FragmentManager fragmentManager) {
135 mFragmentManager = fragmentManager;
136 }
Anton Philippovadfec552017-01-25 20:37:36 +0000137}