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