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 
DashboardFeatureProviderImpl(Context context)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
getTilesForCategory(String key)68     public DashboardCategory getTilesForCategory(String key) {
69         return mCategoryManager.getTilesByCategory(mContext, key);
70     }
71 
72     @Override
getPreferencesForCategory(Activity activity, Context context, int sourceMetricsCategory, String key)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
getAllCategories()96     public List<DashboardCategory> getAllCategories() {
97         return mCategoryManager.getCategories(mContext);
98     }
99 
100     @Override
getDashboardKeyForTile(Tile tile)101     public String getDashboardKeyForTile(Tile tile) {
102         if (tile == null || tile.intent == null) {
103             return null;
104         }
105         if (!TextUtils.isEmpty(tile.key)) {
106             return tile.key;
107         }
108         final StringBuilder sb = new StringBuilder(DASHBOARD_TILE_PREF_KEY_PREFIX);
109         final ComponentName component = tile.intent.getComponent();
110         sb.append(component.getClassName());
111         return sb.toString();
112     }
113 
114     @Override
bindPreferenceToTile(Activity activity, int sourceMetricsCategory, Preference pref, Tile tile, String key, int baseOrder)115     public void bindPreferenceToTile(Activity activity, int sourceMetricsCategory, Preference pref,
116             Tile tile, String key, int baseOrder) {
117         pref.setTitle(tile.title);
118         if (!TextUtils.isEmpty(key)) {
119             pref.setKey(key);
120         } else {
121             pref.setKey(getDashboardKeyForTile(tile));
122         }
123         if (tile.summary != null) {
124             pref.setSummary(tile.summary);
125         } else {
126             pref.setSummary(R.string.summary_placeholder);
127         }
128         if (tile.icon != null) {
129             pref.setIcon(tile.icon.loadDrawable(activity));
130         }
131         final Bundle metadata = tile.metaData;
132         String clsName = null;
133         String action = null;
134         if (metadata != null) {
135             clsName = metadata.getString(SettingsActivity.META_DATA_KEY_FRAGMENT_CLASS);
136             action = metadata.getString(META_DATA_KEY_INTENT_ACTION);
137         }
138         if (!TextUtils.isEmpty(clsName)) {
139             pref.setFragment(clsName);
140         } else if (tile.intent != null) {
141             final Intent intent = new Intent(tile.intent);
142             intent.putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY, sourceMetricsCategory);
143             if (action != null) {
144                 intent.setAction(action);
145             }
146             pref.setOnPreferenceClickListener(preference -> {
147                 launchIntentOrSelectProfile(activity, tile, intent, sourceMetricsCategory);
148                 return true;
149             });
150         }
151         final String skipOffsetPackageName = activity.getPackageName();
152         // Use negated priority for order, because tile priority is based on intent-filter
153         // (larger value has higher priority). However pref order defines smaller value has
154         // higher priority.
155         if (tile.priority != 0) {
156             boolean shouldSkipBaseOrderOffset = false;
157             if (tile.intent != null) {
158                 shouldSkipBaseOrderOffset = TextUtils.equals(
159                         skipOffsetPackageName, tile.intent.getComponent().getPackageName());
160             }
161             if (shouldSkipBaseOrderOffset || baseOrder == Preference.DEFAULT_ORDER) {
162                 pref.setOrder(-tile.priority);
163             } else {
164                 pref.setOrder(-tile.priority + baseOrder);
165             }
166         }
167     }
168 
169     @Override
getProgressiveDisclosureMixin(Context context, DashboardFragment fragment, Bundle args)170     public ProgressiveDisclosureMixin getProgressiveDisclosureMixin(Context context,
171             DashboardFragment fragment, Bundle args) {
172         boolean keepExpanded = false;
173         if (args != null) {
174             keepExpanded = args.getString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY) != null;
175         }
176         return new ProgressiveDisclosureMixin(context, fragment, keepExpanded);
177     }
178 
179     @Override
getExtraIntentAction()180     public String getExtraIntentAction() {
181         return null;
182     }
183 
184     @Override
openTileIntent(Activity activity, Tile tile)185     public void openTileIntent(Activity activity, Tile tile) {
186         if (tile == null) {
187             Intent intent = new Intent(Settings.ACTION_SETTINGS).addFlags(
188                     Intent.FLAG_ACTIVITY_CLEAR_TASK);
189             mContext.startActivity(intent);
190             return;
191         }
192 
193         if (tile.intent == null) {
194             return;
195         }
196         final Intent intent = new Intent(tile.intent)
197                 .putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY,
198                         MetricsEvent.DASHBOARD_SUMMARY)
199                 .putExtra(SettingsDrawerActivity.EXTRA_SHOW_MENU, true)
200                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
201         launchIntentOrSelectProfile(activity, tile, intent, MetricsEvent.DASHBOARD_SUMMARY);
202     }
203 
launchIntentOrSelectProfile(Activity activity, Tile tile, Intent intent, int sourceMetricCategory)204     private void launchIntentOrSelectProfile(Activity activity, Tile tile, Intent intent,
205             int sourceMetricCategory) {
206         if (!isIntentResolvable(intent)) {
207             Log.w(TAG, "Cannot resolve intent, skipping. " + intent);
208             return;
209         }
210         ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
211         if (tile.userHandle == null) {
212             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
213             activity.startActivityForResult(intent, 0);
214         } else if (tile.userHandle.size() == 1) {
215             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
216             activity.startActivityForResultAsUser(intent, 0, tile.userHandle.get(0));
217         } else {
218             ProfileSelectDialog.show(activity.getFragmentManager(), tile);
219         }
220     }
221 
isIntentResolvable(Intent intent)222     private boolean isIntentResolvable(Intent intent) {
223         return mPackageManager.resolveActivity(intent, 0) != null;
224     }
225 }
226