2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.ActivityManager;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.SharedPreferences;
27 import android.content.pm.ActivityInfo;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageInfo;
30 import android.content.pm.PackageManager;
31 import android.content.pm.ResolveInfo;
32 import android.content.pm.UserInfo;
33 import android.content.res.Resources;
34 import android.graphics.ColorFilter;
35 import android.graphics.ColorMatrix;
36 import android.graphics.ColorMatrixColorFilter;
37 import android.graphics.drawable.Drawable;
38 import android.net.Uri;
39 import android.os.Build;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.os.UserManager;
43 import android.support.v7.preference.Preference;
44 import android.support.v7.preference.PreferenceGroup;
45 import android.support.v7.preference.PreferenceViewHolder;
46 import android.text.TextUtils;
47 import android.util.Log;
48 import android.view.View;
49 import android.view.View.OnClickListener;
50 import android.widget.ImageView;
51 import android.widget.RadioButton;
53 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
54 import com.android.settings.search.BaseSearchIndexProvider;
55 import com.android.settings.search.Index;
56 import com.android.settings.search.Indexable;
57 import com.android.settings.search.SearchIndexableRaw;
59 import java.util.ArrayList;
60 import java.util.List;
62 public class HomeSettings extends SettingsPreferenceFragment implements Indexable {
63 static final String TAG = "HomeSettings";
65 // Boolean extra, indicates only launchers that support managed profiles should be shown.
66 // Note: must match the constant defined in ManagedProvisioning
67 private static final String EXTRA_SUPPORT_MANAGED_PROFILES = "support_managed_profiles";
69 static final int REQUESTING_UNINSTALL = 10;
71 public static final String HOME_PREFS = "home_prefs";
72 public static final String HOME_PREFS_DO_SHOW = "do_show";
74 public static final String HOME_SHOW_NOTICE = "show";
76 private class HomePackageReceiver extends BroadcastReceiver {
78 public void onReceive(Context context, Intent intent) {
79 buildHomeActivitiesList();
80 Index.getInstance(context).updateFromClassNameResource(
81 HomeSettings.class.getName(), true, true);
85 private PreferenceGroup mPrefGroup;
86 private PackageManager mPm;
87 private ComponentName[] mHomeComponentSet;
88 private ArrayList<HomeAppPreference> mPrefs;
89 private HomeAppPreference mCurrentHome = null;
90 private final IntentFilter mHomeFilter;
91 private boolean mShowNotice;
92 private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver();
94 public HomeSettings() {
95 mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
96 mHomeFilter.addCategory(Intent.CATEGORY_HOME);
97 mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
100 OnClickListener mHomeClickListener = new OnClickListener() {
102 public void onClick(View v) {
103 int index = (Integer)v.getTag();
104 HomeAppPreference pref = mPrefs.get(index);
105 if (!pref.isChecked) {
106 makeCurrentHome(pref);
111 OnClickListener mDeleteClickListener = new OnClickListener() {
113 public void onClick(View v) {
114 int index = (Integer)v.getTag();
115 uninstallApp(mPrefs.get(index));
119 void makeCurrentHome(HomeAppPreference newHome) {
120 if (mCurrentHome != null) {
121 mCurrentHome.setChecked(false);
123 newHome.setChecked(true);
124 mCurrentHome = newHome;
126 mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
127 mHomeComponentSet, newHome.activityName);
129 getActivity().setResult(Activity.RESULT_OK);
132 void uninstallApp(HomeAppPreference pref) {
133 // Uninstallation is done by asking the OS to do it
134 Uri packageURI = Uri.parse("package:" + pref.uninstallTarget);
135 Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
136 uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, false);
137 int requestCode = REQUESTING_UNINSTALL + (pref.isChecked ? 1 : 0);
138 startActivityForResult(uninstallIntent, requestCode);
142 public void onActivityResult(int requestCode, int resultCode, Intent data) {
143 super.onActivityResult(requestCode, resultCode, data);
145 // Rebuild the list now that we might have nuked something
146 buildHomeActivitiesList();
148 // if the previous home app is now gone, fall back to the system one
149 if (requestCode > REQUESTING_UNINSTALL) {
150 // if mCurrentHome has gone null, it means we didn't find the previously-
151 // default home app when rebuilding the list, i.e. it was the one we
152 // just uninstalled. When that happens we make the system-bundled
153 // home app the active default.
154 if (mCurrentHome == null) {
155 for (int i = 0; i < mPrefs.size(); i++) {
156 HomeAppPreference pref = mPrefs.get(i);
158 makeCurrentHome(pref);
166 private void buildHomeActivitiesList() {
167 ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
168 ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);
170 Context context = getPrefContext();
172 mPrefGroup.removeAll();
173 mPrefs = new ArrayList<HomeAppPreference>();
174 mHomeComponentSet = new ComponentName[homeActivities.size()];
176 boolean supportManagedProfilesExtra =
177 getActivity().getIntent().getBooleanExtra(EXTRA_SUPPORT_MANAGED_PROFILES, false);
178 boolean mustSupportManagedProfile = hasManagedProfile()
179 || supportManagedProfilesExtra;
180 for (int i = 0; i < homeActivities.size(); i++) {
181 final ResolveInfo candidate = homeActivities.get(i);
182 final ActivityInfo info = candidate.activityInfo;
183 ComponentName activityName = new ComponentName(info.packageName, info.name);
184 mHomeComponentSet[i] = activityName;
186 Drawable icon = info.loadIcon(mPm);
187 CharSequence name = info.loadLabel(mPm);
188 HomeAppPreference pref;
190 if (mustSupportManagedProfile && !launcherHasManagedProfilesFeature(candidate)) {
191 pref = new HomeAppPreference(context, activityName, prefIndex,
192 icon, name, this, info, false /* not enabled */,
193 getResources().getString(R.string.home_work_profile_not_supported));
195 pref = new HomeAppPreference(context, activityName, prefIndex,
196 icon, name, this, info, true /* enabled */, null);
200 mPrefGroup.addPreference(pref);
201 if (activityName.equals(currentDefaultHome)) {
205 } catch (Exception e) {
206 Log.v(TAG, "Problem dealing with activity " + activityName, e);
210 if (mCurrentHome != null) {
211 if (mCurrentHome.isEnabled()) {
212 getActivity().setResult(Activity.RESULT_OK);
215 new Handler().post(new Runnable() {
217 mCurrentHome.setChecked(true);
223 private boolean hasManagedProfile() {
224 Context context = getActivity();
225 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
226 List<UserInfo> profiles = userManager.getProfiles(context.getUserId());
227 for (UserInfo userInfo : profiles) {
228 if (userInfo.isManagedProfile()) return true;
233 private boolean launcherHasManagedProfilesFeature(ResolveInfo resolveInfo) {
235 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
236 resolveInfo.activityInfo.packageName, 0 /* default flags */);
237 return versionNumberAtLeastL(appInfo.targetSdkVersion);
238 } catch (PackageManager.NameNotFoundException e) {
243 private boolean versionNumberAtLeastL(int versionNumber) {
244 return versionNumber >= Build.VERSION_CODES.LOLLIPOP;
248 public int getMetricsCategory() {
249 return MetricsEvent.HOME;
253 public void onCreate(Bundle savedInstanceState) {
254 super.onCreate(savedInstanceState);
255 addPreferencesFromResource(R.xml.home_selection);
257 mPm = getPackageManager();
258 mPrefGroup = (PreferenceGroup) findPreference("home");
260 Bundle args = getArguments();
261 mShowNotice = (args != null) && args.getBoolean(HOME_SHOW_NOTICE, false);
265 public void onResume() {
268 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
269 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
270 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
271 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
272 filter.addDataScheme("package");
273 getActivity().registerReceiver(mHomePackageReceiver, filter);
275 buildHomeActivitiesList();
279 public void onPause() {
281 getActivity().unregisterReceiver(mHomePackageReceiver);
284 private class HomeAppPreference extends Preference {
285 ComponentName activityName;
287 HomeSettings fragment;
288 final ColorFilter grayscaleFilter;
292 String uninstallTarget;
294 public HomeAppPreference(Context context, ComponentName activity,
295 int i, Drawable icon, CharSequence title, HomeSettings parent, ActivityInfo info,
296 boolean enabled, CharSequence summary) {
298 setLayoutResource(R.layout.preference_home_app);
303 activityName = activity;
307 ColorMatrix colorMatrix = new ColorMatrix();
308 colorMatrix.setSaturation(0f);
309 float[] matrix = colorMatrix.getArray();
311 grayscaleFilter = new ColorMatrixColorFilter(colorMatrix);
313 determineTargets(info);
316 // Check whether this activity is bundled on the system, with awareness
317 // of the META_HOME_ALTERNATE mechanism.
318 private void determineTargets(ActivityInfo info) {
319 final Bundle meta = info.metaData;
321 final String altHomePackage = meta.getString(ActivityManager.META_HOME_ALTERNATE);
322 if (altHomePackage != null) {
324 final int match = mPm.checkSignatures(info.packageName, altHomePackage);
325 if (match >= PackageManager.SIGNATURE_MATCH) {
326 PackageInfo altInfo = mPm.getPackageInfo(altHomePackage, 0);
327 final int altFlags = altInfo.applicationInfo.flags;
328 isSystem = (altFlags & ApplicationInfo.FLAG_SYSTEM) != 0;
329 uninstallTarget = altInfo.packageName;
332 } catch (Exception e) {
333 // e.g. named alternate package not found during lookup
334 Log.w(TAG, "Unable to compare/resolve alternate", e);
338 // No suitable metadata redirect, so use the package's own info
339 isSystem = (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
340 uninstallTarget = info.packageName;
344 public void onBindViewHolder(PreferenceViewHolder view) {
345 super.onBindViewHolder(view);
347 RadioButton radio = (RadioButton) view.findViewById(R.id.home_radio);
348 radio.setChecked(isChecked);
350 Integer indexObj = new Integer(index);
352 ImageView icon = (ImageView) view.findViewById(R.id.home_app_uninstall);
354 icon.setEnabled(false);
355 icon.setColorFilter(grayscaleFilter);
357 icon.setEnabled(true);
358 icon.setOnClickListener(mDeleteClickListener);
359 icon.setTag(indexObj);
362 View v = view.findViewById(R.id.home_app_pref);
365 v.setOnClickListener(mHomeClickListener);
368 void setChecked(boolean state) {
369 if (state != isChecked) {
379 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
380 new BaseSearchIndexProvider() {
382 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
383 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
385 final PackageManager pm = context.getPackageManager();
386 final ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
387 pm.getHomeActivities(homeActivities);
389 final SharedPreferences sp = context.getSharedPreferences(
390 HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
391 final boolean doShowHome = sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false);
393 // We index Home Launchers only if there are more than one or if we are showing the
394 // Home tile into the Dashboard
395 if (homeActivities.size() > 1 || doShowHome) {
396 final Resources res = context.getResources();
398 // Add fragment title
399 SearchIndexableRaw data = new SearchIndexableRaw(context);
400 data.title = res.getString(R.string.home_settings);
401 data.screenTitle = res.getString(R.string.home_settings);
402 data.keywords = res.getString(R.string.keywords_home);
405 for (int i = 0; i < homeActivities.size(); i++) {
406 final ResolveInfo resolveInfo = homeActivities.get(i);
407 final ActivityInfo activityInfo = resolveInfo.activityInfo;
411 name = activityInfo.loadLabel(pm);
412 if (TextUtils.isEmpty(name)) {
415 } catch (Exception e) {
416 Log.v(TAG, "Problem dealing with Home " + activityInfo.name, e);
420 data = new SearchIndexableRaw(context);
421 data.title = name.toString();
422 data.screenTitle = res.getString(R.string.home_settings);