Home | History | Annotate | Download | only in dashboard
      1 /*
      2  * Copyright (C) 2015 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.dashboard;
     18 
     19 import android.app.Activity;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.PackageManager;
     24 import android.os.Bundle;
     25 import android.provider.Settings;
     26 import android.support.v7.preference.Preference;
     27 import android.text.TextUtils;
     28 import android.util.Log;
     29 
     30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     31 import com.android.settings.R;
     32 import com.android.settings.SettingsActivity;
     33 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
     34 import com.android.settings.overlay.FeatureFactory;
     35 import com.android.settingslib.drawer.CategoryManager;
     36 import com.android.settingslib.drawer.DashboardCategory;
     37 import com.android.settingslib.drawer.ProfileSelectDialog;
     38 import com.android.settingslib.drawer.SettingsDrawerActivity;
     39 import com.android.settingslib.drawer.Tile;
     40 
     41 import java.util.ArrayList;
     42 import java.util.List;
     43 
     44 /**
     45  * Impl for {@code DashboardFeatureProvider}.
     46  */
     47 public class DashboardFeatureProviderImpl implements DashboardFeatureProvider {
     48 
     49     private static final String TAG = "DashboardFeatureImpl";
     50 
     51     private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
     52     private static final String META_DATA_KEY_INTENT_ACTION = "com.android.settings.intent.action";
     53 
     54     protected final Context mContext;
     55 
     56     private final MetricsFeatureProvider mMetricsFeatureProvider;
     57     private final CategoryManager mCategoryManager;
     58     private final PackageManager mPackageManager;
     59 
     60     public DashboardFeatureProviderImpl(Context context) {
     61         mContext = context.getApplicationContext();
     62         mCategoryManager = CategoryManager.get(context, getExtraIntentAction());
     63         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
     64         mPackageManager = context.getPackageManager();
     65     }
     66 
     67     @Override
     68     public DashboardCategory getTilesForCategory(String key) {
     69         return mCategoryManager.getTilesByCategory(mContext, key);
     70     }
     71 
     72     @Override
     73     public List<Preference> getPreferencesForCategory(Activity activity, Context context,
     74             int sourceMetricsCategory, String key) {
     75         final DashboardCategory category = getTilesForCategory(key);
     76         if (category == null) {
     77             Log.d(TAG, "NO dashboard tiles for " + TAG);
     78             return null;
     79         }
     80         final List<Tile> tiles = category.tiles;
     81         if (tiles == null || tiles.isEmpty()) {
     82             Log.d(TAG, "tile list is empty, skipping category " + category.title);
     83             return null;
     84         }
     85         final List<Preference> preferences = new ArrayList<>();
     86         for (Tile tile : tiles) {
     87             final Preference pref = new Preference(context);
     88             bindPreferenceToTile(activity, sourceMetricsCategory, pref, tile, null /* key */,
     89                     Preference.DEFAULT_ORDER /* baseOrder */);
     90             preferences.add(pref);
     91         }
     92         return preferences;
     93     }
     94 
     95     @Override
     96     public List<DashboardCategory> getAllCategories() {
     97         return mCategoryManager.getCategories(mContext);
     98     }
     99 
    100     @Override
    101     public boolean shouldTintIcon() {
    102         return mContext.getResources().getBoolean(R.bool.config_tintSettingIcon);
    103     }
    104 
    105     @Override
    106     public String getDashboardKeyForTile(Tile tile) {
    107         if (tile == null || tile.intent == null) {
    108             return null;
    109         }
    110         if (!TextUtils.isEmpty(tile.key)) {
    111             return tile.key;
    112         }
    113         final StringBuilder sb = new StringBuilder(DASHBOARD_TILE_PREF_KEY_PREFIX);
    114         final ComponentName component = tile.intent.getComponent();
    115         sb.append(component.getClassName());
    116         return sb.toString();
    117     }
    118 
    119     @Override
    120     public void bindPreferenceToTile(Activity activity, int sourceMetricsCategory, Preference pref,
    121             Tile tile, String key, int baseOrder) {
    122         pref.setTitle(tile.title);
    123         if (!TextUtils.isEmpty(key)) {
    124             pref.setKey(key);
    125         } else {
    126             pref.setKey(getDashboardKeyForTile(tile));
    127         }
    128         if (tile.summary != null) {
    129             pref.setSummary(tile.summary);
    130         } else {
    131             pref.setSummary(R.string.summary_placeholder);
    132         }
    133         if (tile.icon != null) {
    134             pref.setIcon(tile.icon.loadDrawable(activity));
    135         }
    136         final Bundle metadata = tile.metaData;
    137         String clsName = null;
    138         String action = null;
    139         if (metadata != null) {
    140             clsName = metadata.getString(SettingsActivity.META_DATA_KEY_FRAGMENT_CLASS);
    141             action = metadata.getString(META_DATA_KEY_INTENT_ACTION);
    142         }
    143         if (!TextUtils.isEmpty(clsName)) {
    144             pref.setFragment(clsName);
    145         } else if (tile.intent != null) {
    146             final Intent intent = new Intent(tile.intent);
    147             intent.putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY, sourceMetricsCategory);
    148             if (action != null) {
    149                 intent.setAction(action);
    150             }
    151             pref.setOnPreferenceClickListener(preference -> {
    152                 launchIntentOrSelectProfile(activity, tile, intent, sourceMetricsCategory);
    153                 return true;
    154             });
    155         }
    156         final String skipOffsetPackageName = activity.getPackageName();
    157         // Use negated priority for order, because tile priority is based on intent-filter
    158         // (larger value has higher priority). However pref order defines smaller value has
    159         // higher priority.
    160         if (tile.priority != 0) {
    161             boolean shouldSkipBaseOrderOffset = false;
    162             if (tile.intent != null) {
    163                 shouldSkipBaseOrderOffset = TextUtils.equals(
    164                         skipOffsetPackageName, tile.intent.getComponent().getPackageName());
    165             }
    166             if (shouldSkipBaseOrderOffset || baseOrder == Preference.DEFAULT_ORDER) {
    167                 pref.setOrder(-tile.priority);
    168             } else {
    169                 pref.setOrder(-tile.priority + baseOrder);
    170             }
    171         }
    172     }
    173 
    174     @Override
    175     public ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
    176             DashboardFragment fragment, Bundle args) {
    177         boolean keepExpanded = false;
    178         if (args != null) {
    179             keepExpanded = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY) != null;
    180         }
    181         return new ProgressiveDisclosureMixin(context, fragment, keepExpanded);
    182     }
    183 
    184     @Override
    185     public String getExtraIntentAction() {
    186         return null;
    187     }
    188 
    189     @Override
    190     public void openTileIntent(Activity activity, Tile tile) {
    191         if (tile == null) {
    192             Intent intent = new Intent(Settings.ACTION_SETTINGS).addFlags(
    193                     Intent.FLAG_ACTIVITY_CLEAR_TASK);
    194             mContext.startActivity(intent);
    195             return;
    196         }
    197 
    198         if (tile.intent == null) {
    199             return;
    200         }
    201         final Intent intent = new Intent(tile.intent)
    202                 .putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY,
    203                         MetricsEvent.DASHBOARD_SUMMARY)
    204                 .putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true)
    205                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    206         launchIntentOrSelectProfile(activity, tile, intent, MetricsEvent.DASHBOARD_SUMMARY);
    207     }
    208 
    209     private void launchIntentOrSelectProfile(Activity activity, Tile tile, Intent intent,
    210             int sourceMetricCategory) {
    211         if (!isIntentResolvable(intent)) {
    212             Log.w(TAG, "Cannot resolve intent, skipping. " + intent);
    213             return;
    214         }
    215         ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
    216         if (tile.userHandle == null) {
    217             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
    218             activity.startActivityForResult(intent, 0);
    219         } else if (tile.userHandle.size() == 1) {
    220             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
    221             activity.startActivityForResultAsUser(intent, 0, tile.userHandle.get(0));
    222         } else {
    223             ProfileSelectDialog.show(activity.getFragmentManager(), tile);
    224         }
    225     }
    226 
    227     private boolean isIntentResolvable(Intent intent) {
    228         return mPackageManager.resolveActivity(intent, 0) != null;
    229     }
    230 }
    231