• 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.FragmentActivity;
27 import androidx.fragment.app.FragmentManager;
28 
29 import com.android.settings.R;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.search.Indexable;
32 import com.android.settings.search.SearchIndexableRaw;
33 import com.android.settingslib.search.SearchIndexable;
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 FragmentActivity 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 
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             }
66             Intent intent = backupHelper.getIntentForBackupSettings();
67             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             }
78 
79             // use startActivityForResult to let the activity check the caller signature
80             startActivityForResult(intent, 1);
81             finish();
82         } else {
83             if (Log.isLoggable(TAG, Log.DEBUG)) {
84                 Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
85             }
86             // mFragmentManager can be set by {@link #setFragmentManager()} for testing
87             if (mFragmentManager == null) {
88                 mFragmentManager = getSupportFragmentManager();
89             }
90             mFragmentManager.beginTransaction()
91                     .replace(android.R.id.content, new BackupSettingsFragment())
92                     .commit();
93         }
94     }
95 
96     /**
97      * For Search.
98      */
99     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
100             new BaseSearchIndexProvider() {
101                 private static final String BACKUP_SEARCH_INDEX_KEY = "Backup";
102 
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);
111                     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);
114                     data.intentTargetPackage = context.getPackageName();
115                     data.intentTargetClass = UserBackupSettingsActivity.class.getName();
116                     data.intentAction = Intent.ACTION_MAIN;
117                     data.key = BACKUP_SEARCH_INDEX_KEY;
118                     result.add(data);
119 
120                     return result;
121                 }
122 
123                 @Override
124                 public List<String> getNonIndexableKeys(Context context) {
125                     final List<String> keys = super.getNonIndexableKeys(context);
126                     if (!new BackupSettingsHelper(context).isBackupServiceActive()) {
127                         keys.add(BACKUP_SEARCH_INDEX_KEY);
128                     }
129                     return keys;
130                 }
131             };
132 
133     @VisibleForTesting
setFragmentManager(FragmentManager fragmentManager)134     void setFragmentManager(FragmentManager fragmentManager) {
135         mFragmentManager = fragmentManager;
136     }
137 }