OSDN Git Service

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