OSDN Git Service

Disable changing lock when device is not provisioned.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / SettingsActivity.java
1 /*
2  * Copyright (C) 2014 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;
18
19 import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO;
20
21 import android.app.ActionBar;
22 import android.app.ActivityManager;
23 import android.app.Fragment;
24 import android.app.FragmentManager;
25 import android.app.FragmentTransaction;
26 import android.content.BroadcastReceiver;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.content.IntentFilter;
31 import android.content.SharedPreferences;
32 import android.content.pm.ActivityInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.PackageManager.NameNotFoundException;
35 import android.graphics.Bitmap;
36 import android.graphics.Canvas;
37 import android.graphics.drawable.Drawable;
38 import android.os.AsyncTask;
39 import android.os.Bundle;
40 import android.os.UserHandle;
41 import android.os.UserManager;
42 import android.support.annotation.VisibleForTesting;
43 import android.support.v14.preference.PreferenceFragment;
44 import android.support.v4.content.LocalBroadcastManager;
45 import android.support.v7.preference.Preference;
46 import android.support.v7.preference.PreferenceManager;
47 import android.text.TextUtils;
48 import android.transition.TransitionManager;
49 import android.util.FeatureFlagUtils;
50 import android.util.Log;
51 import android.view.View;
52 import android.view.View.OnClickListener;
53 import android.view.ViewGroup;
54 import android.widget.Button;
55 import android.widget.Toolbar;
56
57 import com.android.internal.util.ArrayUtils;
58 import com.android.settings.Settings.WifiSettingsActivity;
59 import com.android.settings.applications.manageapplications.ManageApplications;
60 import com.android.settings.backup.BackupSettingsActivity;
61 import com.android.settings.core.FeatureFlags;
62 import com.android.settings.core.SubSettingLauncher;
63 import com.android.settings.core.gateway.SettingsGateway;
64 import com.android.settings.dashboard.DashboardFeatureProvider;
65 import com.android.settings.dashboard.DashboardSummary;
66 import com.android.settings.overlay.FeatureFactory;
67 import com.android.settings.search.DeviceIndexFeatureProvider;
68 import com.android.settings.wfd.WifiDisplaySettings;
69 import com.android.settings.widget.SwitchBar;
70 import com.android.settingslib.core.instrumentation.Instrumentable;
71 import com.android.settingslib.core.instrumentation.SharedPreferencesLogger;
72 import com.android.settingslib.development.DevelopmentSettingsEnabler;
73 import com.android.settingslib.drawer.DashboardCategory;
74 import com.android.settingslib.drawer.SettingsDrawerActivity;
75 import com.android.settingslib.utils.ThreadUtils;
76
77 import java.util.ArrayList;
78 import java.util.List;
79
80 public class SettingsActivity extends SettingsDrawerActivity
81         implements PreferenceManager.OnPreferenceTreeClickListener,
82         PreferenceFragment.OnPreferenceStartFragmentCallback,
83         ButtonBarHandler, FragmentManager.OnBackStackChangedListener {
84
85     private static final String LOG_TAG = "SettingsActivity";
86
87     // Constants for state save/restore
88     private static final String SAVE_KEY_CATEGORIES = ":settings:categories";
89
90     /**
91      * When starting this activity, the invoking Intent can contain this extra
92      * string to specify which fragment should be initially displayed.
93      * <p/>Starting from Key Lime Pie, when this argument is passed in, the activity
94      * will call isValidFragment() to confirm that the fragment class name is valid for this
95      * activity.
96      */
97     public static final String EXTRA_SHOW_FRAGMENT = ":settings:show_fragment";
98
99     /**
100      * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
101      * this extra can also be specified to supply a Bundle of arguments to pass
102      * to that fragment when it is instantiated during the initial creation
103      * of the activity.
104      */
105     public static final String EXTRA_SHOW_FRAGMENT_ARGUMENTS = ":settings:show_fragment_args";
106
107     /**
108      * Fragment "key" argument passed thru {@link #EXTRA_SHOW_FRAGMENT_ARGUMENTS}
109      */
110     public static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
111
112     public static final String BACK_STACK_PREFS = ":settings:prefs";
113
114     // extras that allow any preference activity to be launched as part of a wizard
115
116     // show Back and Next buttons? takes boolean parameter
117     // Back will then return RESULT_CANCELED and Next RESULT_OK
118     protected static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
119
120     // add a Skip button?
121     private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
122
123     // specify custom text for the Back or Next buttons, or cause a button to not appear
124     // at all by setting it to null
125     protected static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
126     protected static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
127
128     /**
129      * When starting this activity and using {@link #EXTRA_SHOW_FRAGMENT},
130      * those extra can also be specify to supply the title or title res id to be shown for
131      * that fragment.
132      */
133     public static final String EXTRA_SHOW_FRAGMENT_TITLE = ":settings:show_fragment_title";
134     /**
135      * The package name used to resolve the title resource id.
136      */
137     public static final String EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME =
138             ":settings:show_fragment_title_res_package_name";
139     public static final String EXTRA_SHOW_FRAGMENT_TITLE_RESID =
140             ":settings:show_fragment_title_resid";
141     public static final String EXTRA_SHOW_FRAGMENT_AS_SHORTCUT =
142             ":settings:show_fragment_as_shortcut";
143
144     public static final String EXTRA_SHOW_FRAGMENT_AS_SUBSETTING =
145             ":settings:show_fragment_as_subsetting";
146
147     @Deprecated
148     public static final String EXTRA_HIDE_DRAWER = ":settings:hide_drawer";
149
150     public static final String META_DATA_KEY_FRAGMENT_CLASS =
151             "com.android.settings.FRAGMENT_CLASS";
152
153     private static final String EXTRA_UI_OPTIONS = "settings:ui_options";
154
155     private String mFragmentClass;
156
157     private CharSequence mInitialTitle;
158     private int mInitialTitleResId;
159
160     private BroadcastReceiver mDevelopmentSettingsListener;
161
162     private boolean mBatteryPresent = true;
163     private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
164         @Override
165         public void onReceive(Context context, Intent intent) {
166             String action = intent.getAction();
167             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
168                 boolean batteryPresent = Utils.isBatteryPresent(intent);
169
170                 if (mBatteryPresent != batteryPresent) {
171                     mBatteryPresent = batteryPresent;
172                     updateTilesList();
173                 }
174             }
175         }
176     };
177
178     private SwitchBar mSwitchBar;
179
180     private Button mNextButton;
181
182     private boolean mIsShowingDashboard;
183
184     private ViewGroup mContent;
185
186     // Categories
187     private ArrayList<DashboardCategory> mCategories = new ArrayList<>();
188
189     private DashboardFeatureProvider mDashboardFeatureProvider;
190
191     public SwitchBar getSwitchBar() {
192         return mSwitchBar;
193     }
194
195     @Override
196     public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
197         new SubSettingLauncher(this)
198                 .setDestination(pref.getFragment())
199                 .setArguments(pref.getExtras())
200                 .setSourceMetricsCategory(caller instanceof Instrumentable
201                         ? ((Instrumentable) caller).getMetricsCategory()
202                         : Instrumentable.METRICS_CATEGORY_UNKNOWN)
203                 .setTitle(-1)
204                 .launch();
205         return true;
206     }
207
208     @Override
209     public boolean onPreferenceTreeClick(Preference preference) {
210         return false;
211     }
212
213     @Override
214     public SharedPreferences getSharedPreferences(String name, int mode) {
215         if (name.equals(getPackageName() + "_preferences")) {
216             return new SharedPreferencesLogger(this, getMetricsTag(),
217                     FeatureFactory.getFactory(this).getMetricsFeatureProvider());
218         }
219         return super.getSharedPreferences(name, mode);
220     }
221
222     private String getMetricsTag() {
223         String tag = getClass().getName();
224         if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
225             tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
226         }
227         if (tag.startsWith("com.android.settings.")) {
228             tag = tag.replace("com.android.settings.", "");
229         }
230         return tag;
231     }
232
233     @Override
234     protected void onCreate(Bundle savedState) {
235         super.onCreate(savedState);
236         Log.d(LOG_TAG, "Starting onCreate");
237         long startTime = System.currentTimeMillis();
238
239         final FeatureFactory factory = FeatureFactory.getFactory(this);
240
241         mDashboardFeatureProvider = factory.getDashboardFeatureProvider(this);
242
243         // Should happen before any call to getIntent()
244         getMetaData();
245
246         final Intent intent = getIntent();
247         if (intent.hasExtra(EXTRA_UI_OPTIONS)) {
248             getWindow().setUiOptions(intent.getIntExtra(EXTRA_UI_OPTIONS, 0));
249         }
250
251         // Getting Intent properties can only be done after the super.onCreate(...)
252         final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
253
254         final ComponentName cn = intent.getComponent();
255         final String className = cn.getClassName();
256
257         mIsShowingDashboard = className.equals(Settings.class.getName());
258
259         // This is a "Sub Settings" when:
260         // - this is a real SubSettings
261         // - or :settings:show_fragment_as_subsetting is passed to the Intent
262         final boolean isSubSettings = this instanceof SubSettings ||
263                 intent.getBooleanExtra(EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, false);
264
265         // If this is a sub settings, then apply the SubSettings Theme for the ActionBar content
266         // insets
267         if (isSubSettings) {
268             setTheme(R.style.Theme_SubSettings);
269         }
270
271         setContentView(mIsShowingDashboard ?
272                 R.layout.settings_main_dashboard : R.layout.settings_main_prefs);
273
274         mContent = findViewById(R.id.main_content);
275
276         getFragmentManager().addOnBackStackChangedListener(this);
277
278         if (savedState != null) {
279             // We are restarting from a previous saved state; used that to initialize, instead
280             // of starting fresh.
281             setTitleFromIntent(intent);
282
283             ArrayList<DashboardCategory> categories =
284                     savedState.getParcelableArrayList(SAVE_KEY_CATEGORIES);
285             if (categories != null) {
286                 mCategories.clear();
287                 mCategories.addAll(categories);
288                 setTitleFromBackStack();
289             }
290         } else {
291             launchSettingFragment(initialFragmentName, isSubSettings, intent);
292         }
293
294         final boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
295         if (mIsShowingDashboard) {
296             findViewById(R.id.search_bar).setVisibility(
297                     deviceProvisioned ? View.VISIBLE : View.INVISIBLE);
298             findViewById(R.id.action_bar).setVisibility(View.GONE);
299             final Toolbar toolbar = findViewById(R.id.search_action_bar);
300             FeatureFactory.getFactory(this).getSearchFeatureProvider()
301                     .initSearchToolbar(this, toolbar);
302             setActionBar(toolbar);
303
304             // Please forgive me for what I am about to do.
305             //
306             // Need to make the navigation icon non-clickable so that the entire card is clickable
307             // and goes to the search UI. Also set the background to null so there's no ripple.
308             View navView = toolbar.getNavigationView();
309             navView.setClickable(false);
310             navView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
311             navView.setBackground(null);
312         }
313
314         ActionBar actionBar = getActionBar();
315         if (actionBar != null) {
316             actionBar.setDisplayHomeAsUpEnabled(deviceProvisioned);
317             actionBar.setHomeButtonEnabled(deviceProvisioned);
318             actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
319         }
320         mSwitchBar = findViewById(R.id.switch_bar);
321         if (mSwitchBar != null) {
322             mSwitchBar.setMetricsTag(getMetricsTag());
323         }
324
325         // see if we should show Back/Next buttons
326         if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false)) {
327
328             View buttonBar = findViewById(R.id.button_bar);
329             if (buttonBar != null) {
330                 buttonBar.setVisibility(View.VISIBLE);
331
332                 Button backButton = (Button) findViewById(R.id.back_button);
333                 backButton.setOnClickListener(new OnClickListener() {
334                     public void onClick(View v) {
335                         setResult(RESULT_CANCELED, null);
336                         finish();
337                     }
338                 });
339                 Button skipButton = (Button) findViewById(R.id.skip_button);
340                 skipButton.setOnClickListener(new OnClickListener() {
341                     public void onClick(View v) {
342                         setResult(RESULT_OK, null);
343                         finish();
344                     }
345                 });
346                 mNextButton = (Button) findViewById(R.id.next_button);
347                 mNextButton.setOnClickListener(new OnClickListener() {
348                     public void onClick(View v) {
349                         setResult(RESULT_OK, null);
350                         finish();
351                     }
352                 });
353
354                 // set our various button parameters
355                 if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
356                     String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
357                     if (TextUtils.isEmpty(buttonText)) {
358                         mNextButton.setVisibility(View.GONE);
359                     } else {
360                         mNextButton.setText(buttonText);
361                     }
362                 }
363                 if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
364                     String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
365                     if (TextUtils.isEmpty(buttonText)) {
366                         backButton.setVisibility(View.GONE);
367                     } else {
368                         backButton.setText(buttonText);
369                     }
370                 }
371                 if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
372                     skipButton.setVisibility(View.VISIBLE);
373                 }
374             }
375         }
376
377         if (DEBUG_TIMING) {
378             Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime) + " ms");
379         }
380     }
381
382     @VisibleForTesting
383     void launchSettingFragment(String initialFragmentName, boolean isSubSettings, Intent intent) {
384         if (!mIsShowingDashboard && initialFragmentName != null) {
385             setTitleFromIntent(intent);
386
387             Bundle initialArguments = intent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
388             switchToFragment(initialFragmentName, initialArguments, true, false,
389                     mInitialTitleResId, mInitialTitle, false);
390         } else {
391             // Show search icon as up affordance if we are displaying the main Dashboard
392             mInitialTitleResId = R.string.dashboard_title;
393
394             switchToFragment(DashboardSummary.class.getName(), null /* args */, false, false,
395                     mInitialTitleResId, mInitialTitle, false);
396         }
397     }
398
399     private void setTitleFromIntent(Intent intent) {
400         Log.d(LOG_TAG, "Starting to set activity title");
401         final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
402         if (initialTitleResId > 0) {
403             mInitialTitle = null;
404             mInitialTitleResId = initialTitleResId;
405
406             final String initialTitleResPackageName = intent.getStringExtra(
407                     EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME);
408             if (initialTitleResPackageName != null) {
409                 try {
410                     Context authContext = createPackageContextAsUser(initialTitleResPackageName,
411                             0 /* flags */, new UserHandle(UserHandle.myUserId()));
412                     mInitialTitle = authContext.getResources().getText(mInitialTitleResId);
413                     setTitle(mInitialTitle);
414                     mInitialTitleResId = -1;
415                     return;
416                 } catch (NameNotFoundException e) {
417                     Log.w(LOG_TAG, "Could not find package" + initialTitleResPackageName);
418                 }
419             } else {
420                 setTitle(mInitialTitleResId);
421             }
422         } else {
423             mInitialTitleResId = -1;
424             final String initialTitle = intent.getStringExtra(EXTRA_SHOW_FRAGMENT_TITLE);
425             mInitialTitle = (initialTitle != null) ? initialTitle : getTitle();
426             setTitle(mInitialTitle);
427         }
428         Log.d(LOG_TAG, "Done setting title");
429     }
430
431     @Override
432     public void onBackStackChanged() {
433         setTitleFromBackStack();
434     }
435
436     private void setTitleFromBackStack() {
437         final int count = getFragmentManager().getBackStackEntryCount();
438
439         if (count == 0) {
440             if (mInitialTitleResId > 0) {
441                 setTitle(mInitialTitleResId);
442             } else {
443                 setTitle(mInitialTitle);
444             }
445             return;
446         }
447
448         FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1);
449         setTitleFromBackStackEntry(bse);
450     }
451
452     private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) {
453         final CharSequence title;
454         final int titleRes = bse.getBreadCrumbTitleRes();
455         if (titleRes > 0) {
456             title = getText(titleRes);
457         } else {
458             title = bse.getBreadCrumbTitle();
459         }
460         if (title != null) {
461             setTitle(title);
462         }
463     }
464
465     @Override
466     protected void onSaveInstanceState(Bundle outState) {
467         super.onSaveInstanceState(outState);
468         saveState(outState);
469     }
470
471     /**
472      * For testing purposes to avoid crashes from final variables in Activity's onSaveInstantState.
473      */
474     @VisibleForTesting
475     void saveState(Bundle outState) {
476         if (mCategories.size() > 0) {
477             outState.putParcelableArrayList(SAVE_KEY_CATEGORIES, mCategories);
478         }
479     }
480
481     @Override
482     protected void onResume() {
483         super.onResume();
484
485         mDevelopmentSettingsListener = new BroadcastReceiver() {
486             @Override
487             public void onReceive(Context context, Intent intent) {
488                 updateTilesList();
489             }
490         };
491         LocalBroadcastManager.getInstance(this).registerReceiver(mDevelopmentSettingsListener,
492                 new IntentFilter(DevelopmentSettingsEnabler.DEVELOPMENT_SETTINGS_CHANGED_ACTION));
493
494         registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
495
496         updateTilesList();
497         updateDeviceIndex();
498     }
499
500     @Override
501     protected void onPause() {
502         super.onPause();
503         LocalBroadcastManager.getInstance(this).unregisterReceiver(mDevelopmentSettingsListener);
504         mDevelopmentSettingsListener = null;
505         unregisterReceiver(mBatteryInfoReceiver);
506     }
507
508     @Override
509     public void setTaskDescription(ActivityManager.TaskDescription taskDescription) {
510         final Bitmap icon = getBitmapFromXmlResource(R.drawable.ic_launcher_settings);
511         taskDescription.setIcon(icon);
512         super.setTaskDescription(taskDescription);
513     }
514
515     protected boolean isValidFragment(String fragmentName) {
516         // Almost all fragments are wrapped in this,
517         // except for a few that have their own activities.
518         for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
519             if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
520         }
521         return false;
522     }
523
524     @Override
525     public Intent getIntent() {
526         Intent superIntent = super.getIntent();
527         String startingFragment = getStartingFragmentClass(superIntent);
528         // This is called from super.onCreate, isMultiPane() is not yet reliable
529         // Do not use onIsHidingHeaders either, which relies itself on this method
530         if (startingFragment != null) {
531             Intent modIntent = new Intent(superIntent);
532             modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
533             Bundle args = superIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
534             if (args != null) {
535                 args = new Bundle(args);
536             } else {
537                 args = new Bundle();
538             }
539             args.putParcelable("intent", superIntent);
540             modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
541             return modIntent;
542         }
543         return superIntent;
544     }
545
546     /**
547      * Checks if the component name in the intent is different from the Settings class and
548      * returns the class name to load as a fragment.
549      */
550     private String getStartingFragmentClass(Intent intent) {
551         if (mFragmentClass != null) return mFragmentClass;
552
553         String intentClass = intent.getComponent().getClassName();
554         if (intentClass.equals(getClass().getName())) return null;
555
556         if ("com.android.settings.RunningServices".equals(intentClass)
557                 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
558             // Old names of manage apps.
559             intentClass = ManageApplications.class.getName();
560         }
561
562         return intentClass;
563     }
564
565     /**
566      * Called by a preference panel fragment to finish itself.
567      *
568      * @param resultCode Optional result code to send back to the original
569      *                   launching fragment.
570      * @param resultData Optional result data to send back to the original
571      *                   launching fragment.
572      */
573     public void finishPreferencePanel(int resultCode, Intent resultData) {
574         setResult(resultCode, resultData);
575         finish();
576     }
577
578     /**
579      * Switch to a specific Fragment with taking care of validation, Title and BackStack
580      */
581     private Fragment switchToFragment(String fragmentName, Bundle args, boolean validate,
582             boolean addToBackStack, int titleResId, CharSequence title, boolean withTransition) {
583         Log.d(LOG_TAG, "Switching to fragment " + fragmentName);
584         if (validate && !isValidFragment(fragmentName)) {
585             throw new IllegalArgumentException("Invalid fragment for this activity: "
586                     + fragmentName);
587         }
588         Fragment f = Fragment.instantiate(this, fragmentName, args);
589         FragmentTransaction transaction = getFragmentManager().beginTransaction();
590         transaction.replace(R.id.main_content, f);
591         if (withTransition) {
592             TransitionManager.beginDelayedTransition(mContent);
593         }
594         if (addToBackStack) {
595             transaction.addToBackStack(SettingsActivity.BACK_STACK_PREFS);
596         }
597         if (titleResId > 0) {
598             transaction.setBreadCrumbTitle(titleResId);
599         } else if (title != null) {
600             transaction.setBreadCrumbTitle(title);
601         }
602         transaction.commitAllowingStateLoss();
603         getFragmentManager().executePendingTransactions();
604         Log.d(LOG_TAG, "Executed frag manager pendingTransactions");
605         return f;
606     }
607
608     private void updateTilesList() {
609         // Generally the items that are will be changing from these updates will
610         // not be in the top list of tiles, so run it in the background and the
611         // SettingsDrawerActivity will pick up on the updates automatically.
612         AsyncTask.execute(new Runnable() {
613             @Override
614             public void run() {
615                 doUpdateTilesList();
616             }
617         });
618     }
619
620     private void updateDeviceIndex() {
621         DeviceIndexFeatureProvider indexProvider = FeatureFactory.getFactory(
622                 this).getDeviceIndexFeatureProvider();
623
624         ThreadUtils.postOnBackgroundThread(
625                 () -> indexProvider.updateIndex(SettingsActivity.this, false /* force */));
626     }
627
628     private void doUpdateTilesList() {
629         PackageManager pm = getPackageManager();
630         final UserManager um = UserManager.get(this);
631         final boolean isAdmin = um.isAdminUser();
632         final FeatureFactory featureFactory = FeatureFactory.getFactory(this);
633         boolean somethingChanged = false;
634         final String packageName = getPackageName();
635         final StringBuilder changedList = new StringBuilder();
636         somethingChanged = setTileEnabled(changedList,
637                 new ComponentName(packageName, WifiSettingsActivity.class.getName()),
638                 pm.hasSystemFeature(PackageManager.FEATURE_WIFI), isAdmin) || somethingChanged;
639
640         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
641                         Settings.BluetoothSettingsActivity.class.getName()),
642                 pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH), isAdmin)
643                 || somethingChanged;
644
645
646         // Enable DataUsageSummaryActivity if the data plan feature flag is turned on otherwise
647         // enable DataPlanUsageSummaryActivity.
648         somethingChanged = setTileEnabled(changedList,
649                 new ComponentName(packageName, Settings.DataUsageSummaryActivity.class.getName()),
650                 Utils.isBandwidthControlEnabled() /* enabled */,
651                 isAdmin) || somethingChanged;
652
653         somethingChanged = setTileEnabled(changedList,
654                 new ComponentName(packageName,
655                         Settings.ConnectedDeviceDashboardActivity.class.getName()),
656                 !UserManager.isDeviceInDemoMode(this) /* enabled */,
657                 isAdmin) || somethingChanged;
658
659         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
660                         Settings.SimSettingsActivity.class.getName()),
661                 Utils.showSimCardTile(this), isAdmin)
662                 || somethingChanged;
663
664         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
665                         Settings.PowerUsageSummaryActivity.class.getName()),
666                 mBatteryPresent, isAdmin) || somethingChanged;
667
668         final boolean isDataUsageSettingsV2Enabled =
669                 FeatureFlagUtils.isEnabled(this, FeatureFlags.DATA_USAGE_SETTINGS_V2);
670         // Enable new data usage page if v2 enabled
671         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
672                         Settings.DataUsageSummaryActivity.class.getName()),
673                 Utils.isBandwidthControlEnabled() && isDataUsageSettingsV2Enabled, isAdmin)
674                 || somethingChanged;
675         // Enable legacy data usage page if v2 disabled
676         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
677                         Settings.DataUsageSummaryLegacyActivity.class.getName()),
678                 Utils.isBandwidthControlEnabled() && !isDataUsageSettingsV2Enabled, isAdmin)
679                 || somethingChanged;
680
681         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
682                         Settings.UserSettingsActivity.class.getName()),
683                 UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
684                         && !Utils.isMonkeyRunning(), isAdmin)
685                 || somethingChanged;
686
687         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
688                         Settings.NetworkDashboardActivity.class.getName()),
689                 !UserManager.isDeviceInDemoMode(this), isAdmin)
690                 || somethingChanged;
691
692         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
693                         Settings.DateTimeSettingsActivity.class.getName()),
694                 !UserManager.isDeviceInDemoMode(this), isAdmin)
695                 || somethingChanged;
696
697         final boolean showDev = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(this)
698                 && !Utils.isMonkeyRunning();
699         final boolean isAdminOrDemo = um.isAdminUser() || um.isDemoUser();
700         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
701                         Settings.DevelopmentSettingsDashboardActivity.class.getName()),
702                 showDev, isAdminOrDemo)
703                 || somethingChanged;
704
705         // Enable/disable backup settings depending on whether the user is admin.
706         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
707                 BackupSettingsActivity.class.getName()), true, isAdmin)
708                 || somethingChanged;
709
710         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
711                         Settings.WifiDisplaySettingsActivity.class.getName()),
712                 WifiDisplaySettings.isAvailable(this), isAdmin)
713                 || somethingChanged;
714
715         // Enable/disable the Me Card page.
716         final boolean aboutPhoneV2Enabled = featureFactory
717                 .getAccountFeatureProvider()
718                 .isAboutPhoneV2Enabled(this);
719         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
720                         Settings.MyDeviceInfoActivity.class.getName()),
721                 aboutPhoneV2Enabled, isAdmin)
722                 || somethingChanged;
723         somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
724                         Settings.DeviceInfoSettingsActivity.class.getName()),
725                 !aboutPhoneV2Enabled, isAdmin)
726                 || somethingChanged;
727
728         if (UserHandle.MU_ENABLED && !isAdmin) {
729
730             // When on restricted users, disable all extra categories (but only the settings ones).
731             final List<DashboardCategory> categories = mDashboardFeatureProvider.getAllCategories();
732             synchronized (categories) {
733                 for (DashboardCategory category : categories) {
734                     final int tileCount = category.getTilesCount();
735                     for (int i = 0; i < tileCount; i++) {
736                         final ComponentName component = category.getTile(i).intent.getComponent();
737                         final String name = component.getClassName();
738                         final boolean isEnabledForRestricted = ArrayUtils.contains(
739                                 SettingsGateway.SETTINGS_FOR_RESTRICTED, name) || (isAdminOrDemo
740                                 && Settings.DevelopmentSettingsDashboardActivity.class.getName()
741                                 .equals(name));
742                         if (packageName.equals(component.getPackageName())
743                                 && !isEnabledForRestricted) {
744                             somethingChanged =
745                                     setTileEnabled(changedList, component, false, isAdmin)
746                                             || somethingChanged;
747                         }
748                     }
749                 }
750             }
751         }
752
753         // Final step, refresh categories.
754         if (somethingChanged) {
755             Log.d(LOG_TAG, "Enabled state changed for some tiles, reloading all categories "
756                     + changedList.toString());
757             updateCategories();
758         } else {
759             Log.d(LOG_TAG, "No enabled state changed, skipping updateCategory call");
760         }
761     }
762
763     /**
764      * @return whether or not the enabled state actually changed.
765      */
766     private boolean setTileEnabled(StringBuilder changedList, ComponentName component,
767             boolean enabled, boolean isAdmin) {
768         if (UserHandle.MU_ENABLED && !isAdmin && getPackageName().equals(component.getPackageName())
769                 && !ArrayUtils.contains(SettingsGateway.SETTINGS_FOR_RESTRICTED,
770                 component.getClassName())) {
771             enabled = false;
772         }
773         boolean changed = setTileEnabled(component, enabled);
774         if (changed) {
775             changedList.append(component.toShortString()).append(",");
776         }
777         return changed;
778     }
779
780     private void getMetaData() {
781         try {
782             ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
783                     PackageManager.GET_META_DATA);
784             if (ai == null || ai.metaData == null) return;
785             mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
786         } catch (NameNotFoundException nnfe) {
787             // No recovery
788             Log.d(LOG_TAG, "Cannot get Metadata for: " + getComponentName().toString());
789         }
790     }
791
792     // give subclasses access to the Next button
793     public boolean hasNextButton() {
794         return mNextButton != null;
795     }
796
797     public Button getNextButton() {
798         return mNextButton;
799     }
800
801     @VisibleForTesting
802     Bitmap getBitmapFromXmlResource(int drawableRes) {
803         Drawable drawable = getResources().getDrawable(drawableRes, getTheme());
804         Canvas canvas = new Canvas();
805         Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
806                 drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
807         canvas.setBitmap(bitmap);
808         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
809         drawable.draw(canvas);
810
811         return bitmap;
812     }
813 }