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.getResources().getString(R.string.privacy_settings_title);
98                     data.screenTitle = context.getResources().getString(
99                             R.string.privacy_settings_title);
100                     data.keywords = context.getResources().getString(
101                             R.string.keywords_backup);
102                     data.intentTargetPackage = context.getPackageName();
103                     data.intentTargetClass = BackupSettingsActivity.class.getName();
104                     data.intentAction = "android.intent.action.MAIN";
105                     data.key = BACKUP_SEARCH_INDEX_KEY;
106                     result.add(data);
107 
108                     return result;
109                 }
110 
111                 @Override
112                 public List<String> getNonIndexableKeys(Context context) {
113                     final List<String> keys = new ArrayList<String>();
114 
115                     // For non-primary user, no backup is available, so don't show it in search
116                     // TODO: http://b/22388012
117                     if (UserHandle.myUserId() != UserHandle.USER_SYSTEM) {
118                         if (Log.isLoggable(TAG, Log.DEBUG)) {
119                             Log.d(TAG, "Not a system user, not indexing the screen");
120                         }
121                         keys.add(BACKUP_SEARCH_INDEX_KEY);
122                     }
123 
124                     return keys;
125                 }
126             };
127 
128     @VisibleForTesting
setFragmentManager(FragmentManager fragmentManager)129     void setFragmentManager(FragmentManager fragmentManager) {
130         mFragmentManager = fragmentManager;
131     }
132 }