• 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             // enable the activity before launching it
62             getPackageManager().setComponentEnabledSetting(intent.getComponent(),
63                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
64 
65             // use startActivityForResult to let the activity check the caller signature
66             startActivityForResult(intent, 1);
67             finish();
68         } else {
69             if (Log.isLoggable(TAG, Log.DEBUG)) {
70                 Log.d(TAG, "Manufacturer provided backup settings, showing the preference screen");
71             }
72             // mFragmentManager can be set by {@link #setFragmentManager()} for testing
73             if (mFragmentManager == null) {
74                 mFragmentManager = getFragmentManager();
75             }
76             mFragmentManager.beginTransaction()
77                     .replace(android.R.id.content, new BackupSettingsFragment())
78                     .commit();
79         }
80     }
81 
82     /**
83      * For Search.
84      */
85     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
86             new BaseSearchIndexProvider() {
87                 private static final String BACKUP_SEARCH_INDEX_KEY = "backup";
88 
89                 @Override
90                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
91                         boolean enabled) {
92 
93                     final List<SearchIndexableRaw> result = new ArrayList<>();
94 
95                     // Add the activity title
96                     SearchIndexableRaw data = new SearchIndexableRaw(context);
97                     data.title = context.getString(R.string.privacy_settings_title);
98                     data.screenTitle = context.getString(R.string.settings_label);
99                     data.keywords = context.getString(R.string.keywords_backup);
100                     data.intentTargetPackage = context.getPackageName();
101                     data.intentTargetClass = BackupSettingsActivity.class.getName();
102                     data.intentAction = Intent.ACTION_MAIN;
103                     data.key = BACKUP_SEARCH_INDEX_KEY;
104                     result.add(data);
105 
106                     return result;
107                 }
108 
109                 @Override
110                 public List<String> getNonIndexableKeys(Context context) {
111                     final List<String> keys = super.getNonIndexableKeys(context);
112 
113                     // For non-primary user, no backup is available, so don't show it in search
114                     // TODO: http://b/22388012
115                     if (UserHandle.myUserId() != UserHandle.USER_SYSTEM) {
116                         if (Log.isLoggable(TAG, Log.DEBUG)) {
117                             Log.d(TAG, "Not a system user, not indexing the screen");
118                         }
119                         keys.add(BACKUP_SEARCH_INDEX_KEY);
120                     }
121 
122                     return keys;
123                 }
124             };
125 
126     @VisibleForTesting
setFragmentManager(FragmentManager fragmentManager)127     void setFragmentManager(FragmentManager fragmentManager) {
128         mFragmentManager = fragmentManager;
129     }
130 }