• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.os.Bundle;
23 import android.util.Log;
24 
25 import androidx.annotation.VisibleForTesting;
26 import androidx.fragment.app.FragmentManager;
27 
28 import com.android.settings.R;
29 import com.android.settings.SettingsActivity;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settingslib.search.Indexable;
32 import com.android.settingslib.search.SearchIndexable;
33 import com.android.settingslib.search.SearchIndexableRaw;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 
39 /**
40  * The activity used to launch the configured Backup activity or the preference screen
41  * if the manufacturer provided their backup settings.
42  * 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.
47  */
48 @SearchIndexable
49 public class UserBackupSettingsActivity extends SettingsActivity implements Indexable {
50     private static final String TAG = "BackupSettingsActivity";
51     private FragmentManager mFragmentManager;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     public void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         if (isFinishing()) {
57             return;
58         }
59 
60         BackupSettingsHelper backupHelper = new BackupSettingsHelper(this);
61 
62         if (!backupHelper.isBackupProvidedByManufacturer()) {
63             // If manufacturer specific backup settings are not provided then launch
64             // the backup settings provided by backup transport or the default one directly.
65             if (Log.isLoggable(TAG, Log.DEBUG)) {
66                 Log.d(TAG,
67                         "No manufacturer settings found, launching the backup settings directly");
68             }
69             Intent intent = backupHelper.getIntentForBackupSettings();
70             try {
71                 // enable the activity before launching it
72                 getPackageManager().setComponentEnabledSetting(
73                         intent.getComponent(),
74                         PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
75                         PackageManager.DONT_KILL_APP);
76             } catch (SecurityException e) {
77                 Log.w(TAG, "Trying to enable activity " + intent.getComponent() + " but couldn't: "
78                         + e.getMessage());
79                 // the activity may already be enabled
80             }
81 
82             // use startActivityForResult to let the activity check the caller signature
83             startActivityForResult(intent, 1);
84             finish();
85         } else {
86             if (Log.isLoggable(TAG, Log.DEBUG)) {
87                 Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
88             }
89             // mFragmentManager can be set by {@link #setFragmentManager()} for testing
90             if (mFragmentManager == null) {
91                 mFragmentManager = getSupportFragmentManager();
92             }
93             mFragmentManager.beginTransaction()
94                     .replace(android.R.id.content, new BackupSettingsFragment())
95                     .commit();
96         }
97     }
98 
99     /**
100      * For Search.
101      */
102     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
103             new BaseSearchIndexProvider() {
104                 private static final String BACKUP_SEARCH_INDEX_KEY = "Backup";
105 
106                 @Override
107                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
108                         boolean enabled) {
109 
110                     final List<SearchIndexableRaw> result = new ArrayList<>();
111 
112                     // Add the activity title
113                     SearchIndexableRaw data = new SearchIndexableRaw(context);
114                     data.title = context.getString(R.string.privacy_settings_title);
115                     data.screenTitle = context.getString(R.string.privacy_settings_title);
116                     data.keywords = context.getString(R.string.keywords_backup);
117                     data.intentTargetPackage = context.getPackageName();
118                     data.intentTargetClass = UserBackupSettingsActivity.class.getName();
119                     data.intentAction = Intent.ACTION_MAIN;
120                     data.key = BACKUP_SEARCH_INDEX_KEY;
121                     result.add(data);
122 
123                     return result;
124                 }
125 
126                 @Override
127                 public List<String> getNonIndexableKeys(Context context) {
128                     final List<String> keys = super.getNonIndexableKeys(context);
129                     if (!new BackupSettingsHelper(context).isBackupServiceActive()) {
130                         keys.add(BACKUP_SEARCH_INDEX_KEY);
131                     }
132                     return keys;
133                 }
134             };
135 
136     @VisibleForTesting
setFragmentManager(FragmentManager fragmentManager)137     void setFragmentManager(FragmentManager fragmentManager) {
138         mFragmentManager = fragmentManager;
139     }
140 }