Move Home settings to Default apps
Change-Id: I2ac7f9dd2c1c505ff3163b2a90c574a360b9409c
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b661a1e..90d8793 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -864,30 +864,6 @@
android:value="true" />
</activity>
- <activity android:name="Settings$HomeSettingsActivity"
- android:label="@string/home_settings"
- android:icon="@drawable/ic_settings_home"
- android:taskAffinity="">
- <intent-filter android:priority="1">
- <action android:name="android.settings.HOME_SETTINGS" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.VOICE_LAUNCH" />
- </intent-filter>
- <intent-filter android:priority="10">
- <action android:name="com.android.settings.action.SETTINGS" />
- </intent-filter>
- <meta-data android:name="com.android.settings.category"
- android:value="com.android.settings.category.device" />
- <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
- android:value="com.android.settings.HomeSettings" />
- <meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
- android:value="true" />
- </activity>
-
<activity android:name="Settings$DisplaySettingsActivity"
android:label="@string/display_settings"
android:icon="@drawable/ic_settings_display"
@@ -2711,6 +2687,10 @@
<action android:name="android.settings.MANAGE_DEFAULT_APPS_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
+ <intent-filter>
+ <action android:name="android.settings.HOME_SETTINGS" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.applications.ManageDefaultApps" />
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
diff --git a/res/layout/app_preference_item.xml b/res/layout/app_preference_item.xml
index b02f835..0ad08cc 100755
--- a/res/layout/app_preference_item.xml
+++ b/res/layout/app_preference_item.xml
@@ -18,35 +18,55 @@
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
- android:layout_height="?android:attr/listPreferredItemHeight"
+ android:layout_height="wrap_content"
+ android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
- android:id="@+id/app_image"
+ android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:padding="6dip"
- />
- <TextView
- android:id="@+id/app_label"
+ android:duplicateParentState="true"
+ />
+ <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:minHeight="?android:attr/listPreferredItemHeight"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:textColor="?android:attr/textColorAlertDialogListItem"
- android:gravity="center_vertical"
- android:paddingEnd="7dip"
- android:ellipsize="marquee"
- />
+ android:layout_gravity="center_vertical"
+ android:duplicateParentState="true"
+ android:orientation="vertical">
+ <TextView
+ android:id="@android:id/title"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:textColor="?android:attr/textColorAlertDialogListItem"
+ android:gravity="center_vertical"
+ android:paddingEnd="7dip"
+ android:ellipsize="marquee"
+ android:duplicateParentState="true"
+ />
+ <TextView
+ android:id="@android:id/summary"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textAppearance="?android:attr/textAppearanceSmall"
+ android:gravity="center_vertical"
+ android:paddingEnd="7dip"
+ android:ellipsize="marquee"
+ android:visibility="gone"
+ android:duplicateParentState="true"
+ />
+ </LinearLayout>
<TextView
android:id="@+id/default_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceSmall"
- android:gravity="center_vertical"
+ android:layout_gravity="center_vertical"
android:text="@string/default_app"
android:visibility="gone"
android:paddingEnd="7dip"
- />
+ android:duplicateParentState="true"
+ />
</LinearLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 385034f..096514f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -7005,4 +7005,10 @@
<!-- Title for switch to allow app unrestricted data usage [CHAR LIMIT=30] -->
<string name="unrestricted_app_summary">Allow unrestricted data access when Data Saver is on</string>
+ <!-- Button to switch the default home app [CHAR LIMIT=60] -->
+ <string name="home_app">Home app</string>
+
+ <!-- No default home set summary [CHAR LIMIT=NONE] -->
+ <string name="no_default_home">No default Home</string>
+
</resources>
diff --git a/res/xml/default_apps.xml b/res/xml/default_apps.xml
index a38ef4a..76d0e8c 100644
--- a/res/xml/default_apps.xml
+++ b/res/xml/default_apps.xml
@@ -25,6 +25,12 @@
android:title="@string/assist_and_voice_input_title"
/>
+ <com.android.settings.applications.DefaultHomePreference
+ android:key="default_home"
+ android:title="@string/home_app"
+ android:summary="@string/no_default_home"
+ />
+
<com.android.settings.applications.DefaultBrowserPreference
android:key="default_browser"
android:title="@string/default_browser_title"
diff --git a/src/com/android/settings/AppListPreference.java b/src/com/android/settings/AppListPreference.java
index 4d24d7f..1c1ccd7 100644
--- a/src/com/android/settings/AppListPreference.java
+++ b/src/com/android/settings/AppListPreference.java
@@ -16,7 +16,6 @@
package com.android.settings;
-import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
@@ -50,6 +49,7 @@
private Drawable[] mEntryDrawables;
private boolean mShowItemNone = false;
+ private CharSequence[] mSummaries;
public class AppArrayAdapter extends ArrayAdapter<CharSequence> {
private Drawable[] mImageDrawables = null;
@@ -63,16 +63,30 @@
}
@Override
+ public boolean isEnabled(int position) {
+ return mSummaries == null || mSummaries[position] == null;
+ }
+
+ @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.app_preference_item, parent, false);
- TextView textView = (TextView) view.findViewById(R.id.app_label);
+ TextView textView = (TextView) view.findViewById(android.R.id.title);
textView.setText(getItem(position));
if (position == mSelectedIndex) {
view.findViewById(R.id.default_label).setVisibility(View.VISIBLE);
}
- ImageView imageView = (ImageView)view.findViewById(R.id.app_image);
+ ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
imageView.setImageDrawable(mImageDrawables[position]);
+ // Summaries are describing why a item is disabled, so anything with a summary
+ // is not enabled.
+ boolean enabled = mSummaries == null || mSummaries[position] == null;
+ view.setEnabled(enabled);
+ if (!enabled) {
+ TextView summary = (TextView) view.findViewById(android.R.id.summary);
+ summary.setText(mSummaries[position]);
+ summary.setVisibility(View.VISIBLE);
+ }
return view;
}
}
@@ -133,6 +147,12 @@
}
public void setComponentNames(ComponentName[] componentNames, ComponentName defaultCN) {
+ setComponentNames(componentNames, defaultCN, null);
+ }
+
+ public void setComponentNames(ComponentName[] componentNames, ComponentName defaultCN,
+ CharSequence[] summaries) {
+ mSummaries = summaries;
// Look up all package names in PackageManager. Skip ones we can't find.
PackageManager pm = getContext().getPackageManager();
final int entryCount = componentNames.length + (mShowItemNone ? 1 : 0);
@@ -192,7 +212,7 @@
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
- return new SavedState(getEntryValues(), getValue(), mShowItemNone, superState);
+ return new SavedState(getEntryValues(), getValue(), mSummaries, mShowItemNone, superState);
}
@Override
@@ -201,6 +221,7 @@
SavedState savedState = (SavedState) state;
mShowItemNone = savedState.showItemNone;
setPackageNames(savedState.entryValues, savedState.value);
+ mSummaries = savedState.summaries;
super.onRestoreInstanceState(savedState.superState);
} else {
super.onRestoreInstanceState(state);
@@ -213,13 +234,15 @@
public final CharSequence value;
public final boolean showItemNone;
public final Parcelable superState;
+ public final CharSequence[] summaries;
- public SavedState(CharSequence[] entryValues, CharSequence value, boolean showItemNone,
- Parcelable superState) {
+ public SavedState(CharSequence[] entryValues, CharSequence value, CharSequence[] summaries,
+ boolean showItemNone, Parcelable superState) {
this.entryValues = entryValues;
this.value = value;
this.showItemNone = showItemNone;
this.superState = superState;
+ this.summaries = summaries;
}
@Override
@@ -233,6 +256,7 @@
dest.writeCharSequence(value);
dest.writeInt(showItemNone ? 1 : 0);
dest.writeParcelable(superState, flags);
+ dest.writeCharSequenceArray(summaries);
}
public static Creator<SavedState> CREATOR = new Creator<SavedState>() {
@@ -242,7 +266,8 @@
CharSequence value = source.readCharSequence();
boolean showItemNone = source.readInt() != 0;
Parcelable superState = source.readParcelable(getClass().getClassLoader());
- return new SavedState(entryValues, value, showItemNone, superState);
+ CharSequence[] summaries = source.readCharSequenceArray();
+ return new SavedState(entryValues, value, summaries, showItemNone, superState);
}
@Override
diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java
index dc134ff..db9d9c3 100644
--- a/src/com/android/settings/SettingsActivity.java
+++ b/src/com/android/settings/SettingsActivity.java
@@ -29,7 +29,6 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.nfc.NfcAdapter;
import android.os.AsyncTask;
@@ -114,8 +113,8 @@
import com.android.settings.wifi.WifiSettings;
import com.android.settings.wifi.p2p.WifiP2pSettings;
import com.android.settingslib.drawer.DashboardCategory;
-import com.android.settingslib.drawer.Tile;
import com.android.settingslib.drawer.SettingsDrawerActivity;
+import com.android.settingslib.drawer.Tile;
import java.util.ArrayList;
import java.util.List;
@@ -380,7 +379,6 @@
private static final String MSG_DATA_FORCE_REFRESH = "msg_data_force_refresh";
private boolean mNeedToRevertToInitialFragment = false;
- private int mHomeActivitiesCount = 1;
private Intent mResultIntentData;
private ComponentName mCurrentSuggestion;
@@ -561,8 +559,6 @@
mDisplayHomeAsUpEnabled = savedState.getBoolean(SAVE_KEY_SHOW_HOME_AS_UP);
mDisplaySearch = savedState.getBoolean(SAVE_KEY_SHOW_SEARCH);
- mHomeActivitiesCount = savedState.getInt(SAVE_KEY_HOME_ACTIVITIES_COUNT,
- 1 /* one home activity by default */);
} else {
if (!mIsShowingDashboard) {
mDisplaySearch = false;
@@ -651,17 +647,10 @@
}
}
- mHomeActivitiesCount = getHomeActivitiesCount();
if (DEBUG_TIMING) Log.d(LOG_TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
+ " ms");
}
- private int getHomeActivitiesCount() {
- final ArrayList<ResolveInfo> homeApps = new ArrayList<ResolveInfo>();
- getPackageManager().getHomeActivities(homeApps);
- return homeApps.size();
- }
-
private void setTitleFromIntent(Intent intent) {
final int initialTitleResId = intent.getIntExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, -1);
if (initialTitleResId > 0) {
@@ -751,8 +740,6 @@
String query = (mSearchView != null) ? mSearchView.getQuery().toString() : EMPTY_QUERY;
outState.putString(SAVE_KEY_SEARCH_QUERY, query);
}
-
- outState.putInt(SAVE_KEY_HOME_ACTIVITIES_COUNT, mHomeActivitiesCount);
}
@Override
@@ -1022,10 +1009,6 @@
mBatteryPresent, isAdmin, pm);
setTileEnabled(new ComponentName(packageName,
- Settings.HomeSettingsActivity.class.getName()),
- updateHomeSettingTiles(), isAdmin, pm);
-
- setTileEnabled(new ComponentName(packageName,
Settings.UserSettingsActivity.class.getName()),
UserHandle.MU_ENABLED && UserManager.supportsMultipleUsers()
&& !Utils.isMonkeyRunning(), isAdmin, pm);
@@ -1081,27 +1064,6 @@
}
}
- private boolean updateHomeSettingTiles() {
- // Once we decide to show Home settings, keep showing it forever
- SharedPreferences sp = getSharedPreferences(HomeSettings.HOME_PREFS, Context.MODE_PRIVATE);
- if (sp.getBoolean(HomeSettings.HOME_PREFS_DO_SHOW, false)) {
- return true;
- }
-
- try {
- mHomeActivitiesCount = getHomeActivitiesCount();
- if (mHomeActivitiesCount < 2) {
- return false;
- }
- } catch (Exception e) {
- // Can't look up the home activity; bail on configuring the icon
- Log.w(LOG_TAG, "Problem looking up home activity!", e);
- }
-
- sp.edit().putBoolean(HomeSettings.HOME_PREFS_DO_SHOW, true).apply();
- return true;
- }
-
private void getMetaData() {
try {
ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
diff --git a/src/com/android/settings/SmsDefaultDialog.java b/src/com/android/settings/SmsDefaultDialog.java
index a1af021..e38c9b8 100644
--- a/src/com/android/settings/SmsDefaultDialog.java
+++ b/src/com/android/settings/SmsDefaultDialog.java
@@ -187,14 +187,14 @@
Item item = ((Item) getItem(position));
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.app_preference_item, parent, false);
- TextView textView = (TextView) view.findViewById(R.id.app_label);
+ TextView textView = (TextView) view.findViewById(android.R.id.title);
textView.setText(item.label);
if (position == mSelectedIndex) {
view.findViewById(R.id.default_label).setVisibility(View.VISIBLE);
} else {
view.findViewById(R.id.default_label).setVisibility(View.GONE);
}
- ImageView imageView = (ImageView)view.findViewById(R.id.app_image);
+ ImageView imageView = (ImageView)view.findViewById(android.R.id.icon);
imageView.setImageDrawable(item.icon);
return view;
}
diff --git a/src/com/android/settings/applications/DefaultHomePreference.java b/src/com/android/settings/applications/DefaultHomePreference.java
new file mode 100644
index 0000000..7426ce6
--- /dev/null
+++ b/src/com/android/settings/applications/DefaultHomePreference.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.settings.applications;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
+import android.os.Build;
+import android.os.UserManager;
+import android.util.AttributeSet;
+import com.android.settings.AppListPreference;
+import com.android.settings.R;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DefaultHomePreference extends AppListPreference {
+
+ private final ArrayList<ComponentName> mAllHomeComponents = new ArrayList<>();
+ private final IntentFilter mHomeFilter;
+
+ public DefaultHomePreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
+ mHomeFilter.addCategory(Intent.CATEGORY_HOME);
+ mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
+ refreshHomeOptions();
+ }
+
+ @Override
+ public void performClick() {
+ refreshHomeOptions();
+ super.performClick();
+ }
+
+ @Override
+ protected boolean persistString(String value) {
+ if (value != null) {
+ ComponentName component = ComponentName.unflattenFromString(value);
+ getContext().getPackageManager().replacePreferredActivity(mHomeFilter,
+ IntentFilter.MATCH_CATEGORY_EMPTY,
+ mAllHomeComponents.toArray(new ComponentName[0]), component);
+ setSummary(getEntry());
+ }
+ return super.persistString(value);
+ }
+
+ public void refreshHomeOptions() {
+ final String myPkg = getContext().getPackageName();
+ ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
+ PackageManager pm = getContext().getPackageManager();
+ ComponentName currentDefaultHome = pm.getHomeActivities(homeActivities);
+ ArrayList<ComponentName> components = new ArrayList<>();
+ mAllHomeComponents.clear();
+ List<CharSequence> summaries = new ArrayList<>();
+
+ boolean mustSupportManagedProfile = hasManagedProfile();
+ for (int i = 0; i < homeActivities.size(); i++) {
+ final ResolveInfo candidate = homeActivities.get(i);
+ final ActivityInfo info = candidate.activityInfo;
+ ComponentName activityName = new ComponentName(info.packageName, info.name);
+ mAllHomeComponents.add(activityName);
+ if (info.packageName.equals(myPkg)) {
+ continue;
+ }
+ components.add(activityName);
+ if (mustSupportManagedProfile && !launcherHasManagedProfilesFeature(candidate, pm)) {
+ summaries.add(getContext().getString(R.string.home_work_profile_not_supported));
+ } else {
+ summaries.add(null);
+ }
+ }
+ setComponentNames(components.toArray(new ComponentName[0]), currentDefaultHome,
+ summaries.toArray(new CharSequence[0]));
+ }
+
+ private boolean launcherHasManagedProfilesFeature(ResolveInfo resolveInfo, PackageManager pm) {
+ try {
+ ApplicationInfo appInfo = pm.getApplicationInfo(
+ resolveInfo.activityInfo.packageName, 0 /* default flags */);
+ return versionNumberAtLeastL(appInfo.targetSdkVersion);
+ } catch (PackageManager.NameNotFoundException e) {
+ return false;
+ }
+ }
+
+ private boolean versionNumberAtLeastL(int versionNumber) {
+ return versionNumber >= Build.VERSION_CODES.LOLLIPOP;
+ }
+
+ private boolean hasManagedProfile() {
+ UserManager userManager = getContext().getSystemService(UserManager.class);
+ List<UserInfo> profiles = userManager.getProfiles(getContext().getUserId());
+ for (UserInfo userInfo : profiles) {
+ if (userInfo.isManagedProfile()) return true;
+ }
+ return false;
+ }
+
+}