[RK3399][Android7.1.1]TvSettings添加默认主界面(Home app)设置

测试平台

Platform: RK3399
OS: Android 8.1

现象

系统代码中Settings是有默认主界面(Home app)的设置项,但是TvSettings里面却没有,做了一下功能移植。

代码补丁

packages/apps/TvSettings:

From dd123ca39ef930cb1fc0faa99c19fd39f348872e Mon Sep 17 00:00:00 2001
From: "[email protected]" <sjf@t-chip.com.cn>
Date: Mon, 25 Mar 2019 01:01:27 +0800
Subject: [PATCH] Android->Screen:Add HomeSettings

---
 Settings/AndroidManifest.xml                       |  16 +-
 Settings/res/drawable/ic_menu_delete.xml           |  25 ++
 Settings/res/layout/preference_home_app.xml        |  88 +++++
 Settings/res/values-zh-rCN/strings.xml             |   5 +
 Settings/res/values-zh-rHK/strings.xml             |   4 +
 Settings/res/values-zh-rTW/strings.xml             |   4 +
 Settings/res/values/strings.xml                    |   9 +
 Settings/res/xml/home_screen.xml                   |   9 +
 Settings/res/xml/main_prefs.xml                    |   5 +-
 .../src/com/android/tv/settings/MainFragment.java  |   2 +-
 .../tv/settings/system/HomeScreenActivity.java     |  43 +++
 .../tv/settings/system/HomeScreenFragment.java     | 390 +++++++++++++++++++++
 12 files changed, 595 insertions(+), 5 deletions(-)
 create mode 100644 Settings/res/drawable/ic_menu_delete.xml
 create mode 100644 Settings/res/layout/preference_home_app.xml
 create mode 100644 Settings/res/xml/home_screen.xml
 create mode 100644 Settings/src/com/android/tv/settings/system/HomeScreenActivity.java
 create mode 100644 Settings/src/com/android/tv/settings/system/HomeScreenFragment.java

diff --git a/Settings/AndroidManifest.xml b/Settings/AndroidManifest.xml
index 2cc97aa..c12a331 100644
--- a/Settings/AndroidManifest.xml
+++ b/Settings/AndroidManifest.xml
@@ -55,7 +55,7 @@
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <action android:name="android.settings.SETTINGS" />
-                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 
             <!-- Intent filter used to display this activity as a settings
@@ -323,6 +323,20 @@
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
+
+        <activity
+            android:name=".system.HomeScreenActivity"
+            android:theme="@style/Theme.Settings.Transparent"
+            android:configChanges="keyboard|keyboardHidden|navigation"
+            android:excludeFromRecents="true"
+            android:exported="true" >
+            <intent-filter>
+                <action android:name="android.settings.HOME_SETTINGS" />
+
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity
             android:name=".system.LocationActivity"
             android:theme="@style/Theme.Settings.Transparent"
diff --git a/Settings/res/drawable/ic_menu_delete.xml b/Settings/res/drawable/ic_menu_delete.xml
new file mode 100644
index 0000000..74f63c3
--- /dev/null
+++ b/Settings/res/drawable/ic_menu_delete.xml
@@ -0,0 +1,25 @@
+<!--
+    Copyright (C) 2015 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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0"
+        android:tint="?android:attr/colorAccent">
+    <path
+        android:fillColor="@android:color/white"
+        android:pathData="M12.0,38.0c0.0,2.21 1.79,4.0 4.0,4.0l16.0,0.0c2.21,0.0 4.0,-1.79 4.0,-4.0L36.0,14.0L12.0,14.0l0.0,24.0zM38.0,8.0l-7.0,0.0l-2.0,-2.0L19.0,6.0l-2.0,2.0l-7.0,0.0l0.0,4.0l28.0,0.0L38.0,8.0z"/>
+</vector>
diff --git a/Settings/res/layout/preference_home_app.xml b/Settings/res/layout/preference_home_app.xml
new file mode 100644
index 0000000..c74b7f1
--- /dev/null
+++ b/Settings/res/layout/preference_home_app.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:gravity="center_vertical"
+    android:paddingStart="@*android:dimen/preference_item_padding_side">
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:id="@+id/home_app_pref"
+        android:focusable="true"
+        android:clickable="true"
+        android:gravity="center_vertical"
+        android:background="?android:attr/selectableItemBackground" >
+
+            <RadioButton
+                android:id="@+id/home_radio"
+                android:layout_width="wrap_content"
+                android:layout_height="match_parent"
+                android:layout_marginStart="10dip"
+                android:layout_marginEnd="4dip"
+                android:layout_gravity="center_vertical"
+                android:orientation="vertical"
+                android:clickable="false"
+                android:focusable="false" />
+
+            <ImageView
+                android:id="@+android:id/icon"
+                android:layout_width="48dp"
+                android:layout_height="48dp"
+                android:layout_gravity="center"
+                android:minWidth="48dp"
+                android:scaleType="centerInside"
+                android:layout_marginEnd="@*android:dimen/preference_item_padding_inner" />
+
+            <LinearLayout
+                android:layout_width="wrap_content"
+                android:layout_height="match_parent"
+                android:layout_weight="1"
+                android:focusable="true"
+                android:clickable="true"
+                android:gravity="center_vertical"
+                android:orientation="vertical">
+
+                    <TextView
+                        android:id="@+android:id/title"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:singleLine="true"
+                        android:textAppearance="?android:attr/textAppearanceMedium"
+                        android:ellipsize="end" />
+
+                    <TextView
+                        android:id="@+android:id/summary"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:textAppearance="?android:attr/textAppearanceSmall" />
+
+            </LinearLayout>
+
+    </LinearLayout>
+
+    <View
+        android:id="@+id/home_divider"
+        android:layout_width="2dip"
+        android:layout_height="match_parent"
+        android:layout_marginTop="5dip"
+        android:layout_marginBottom="5dip"
+        android:background="@android:drawable/divider_horizontal_dark" />
+
+    <ImageView
+        android:id="@+id/home_app_uninstall"
+        android:layout_width="wrap_content"
+        android:layout_height="fill_parent"
+        android:paddingStart="12dip"
+        android:paddingEnd="12dp"
+        android:src="@drawable/ic_menu_delete"
+        android:contentDescription="@string/home_app_uninstall_button"
+        android:layout_gravity="center"
+        android:clickable="true"
+        android:focusable="true"
+        android:background="?android:attr/selectableItemBackground" />
+
+</LinearLayout>
diff --git a/Settings/res/values-zh-rCN/strings.xml b/Settings/res/values-zh-rCN/strings.xml
index 54dd3c0..940c2e0 100644
--- a/Settings/res/values-zh-rCN/strings.xml
+++ b/Settings/res/values-zh-rCN/strings.xml
@@ -674,4 +674,9 @@
     <string name="app_permission_summary_not_allowed" msgid="3667663331476577913">"不允许"</string>
     <string name="usage_access" msgid="1914242907684452328">"使用情况访问权限"</string>
     <string name="usage_access_description" msgid="6290001769893747651">"使用情况访问权限允许应用跟踪您正在使用的其他应用和使用频率,以及您的运营商、语言设置及其他详细信息。"</string>
+    
+    <string name="home_settings">"主屏幕"</string>
+    <string name="home_app_uninstall_button">"卸载此应用"</string>
+    <string name="only_one_home_message">"在您安装其他主屏幕应用之前,主屏幕设置将一直处于隐藏状态。"</string>
+    
 </resources>
diff --git a/Settings/res/values-zh-rHK/strings.xml b/Settings/res/values-zh-rHK/strings.xml
index 43c6729..aa9191b 100644
--- a/Settings/res/values-zh-rHK/strings.xml
+++ b/Settings/res/values-zh-rHK/strings.xml
@@ -674,4 +674,8 @@
     <string name="app_permission_summary_not_allowed" msgid="3667663331476577913">"不允許"</string>
     <string name="usage_access" msgid="1914242907684452328">"使用記錄存取權"</string>
     <string name="usage_access_description" msgid="6290001769893747651">"使用記錄存取權允許應用程式追蹤您正在使用的其他應用程式、使用頻率、流動服務供應商、語言設定和其他詳情。"</string>
+    
+    <string name="home_settings" msgid="212375129455718176">"主畫面"</string>
+    <string name="home_app_uninstall_button">"解除安裝這個應用程式"</string>
+    <string name="only_one_home_message">"主畫面設定將會隱藏,直至您安裝另一個主畫面應用程式為止。"</string>
 </resources>
diff --git a/Settings/res/values-zh-rTW/strings.xml b/Settings/res/values-zh-rTW/strings.xml
index 516105c..53d1913 100644
--- a/Settings/res/values-zh-rTW/strings.xml
+++ b/Settings/res/values-zh-rTW/strings.xml
@@ -674,4 +674,8 @@
     <string name="app_permission_summary_not_allowed" msgid="3667663331476577913">"不允許"</string>
     <string name="usage_access" msgid="1914242907684452328">"使用紀錄存取權"</string>
     <string name="usage_access_description" msgid="6290001769893747651">"具有使用紀錄存取權的應用程式可以追蹤你使用的其他應用程式與頻率,以及你的電信業者、語言設定和其他詳細資料。"</string>
+    
+    <string name="home_settings" msgid="212375129455718176">"主畫面"</string>
+    <string name="home_app_uninstall_button">"解除安裝這個應用程式"</string>
+    <string name="only_one_home_message">"必須安裝其他主螢幕應用程式,主螢幕設定才會顯示。"</string>
 </resources>
diff --git a/Settings/res/values/strings.xml b/Settings/res/values/strings.xml
index 88cfc1a..fe981c7 100644
--- a/Settings/res/values/strings.xml
+++ b/Settings/res/values/strings.xml
@@ -1469,4 +1469,13 @@
     <!-- Description of the usage access setting [CHAR LIMIT=NONE] -->
     <string name="usage_access_description">Usage access allows an app to track what other apps you\u2019re using and how often, as well as your carrier, language settings, and other details.</string>
 
+    
+    <string name="home_settings">Home</string>
+    <!-- Home application selection - uninstall button [CHAR LIMIT=80] -->
+    <string name="home_app_uninstall_button">Uninstall this application</string>
+    <!-- Message to user that Home Settings will be hidden because there is now only one available home application -->
+    <string name="only_one_home_message">Home settings will be hidden until you install another home application.</string>
+    
+    <!-- Home settings screen, text indicating that a launcer does not support work profiles [CHAR LIMIT=100] -->
+    <string name="home_work_profile_not_supported">Doesn\u2019t support work profiles</string>
 </resources>
diff --git a/Settings/res/xml/home_screen.xml b/Settings/res/xml/home_screen.xml
new file mode 100644
index 0000000..7e09d95
--- /dev/null
+++ b/Settings/res/xml/home_screen.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+                  xmlns:app="http://schemas.android.com/apk/res-auto"
+                  android:title="@string/system_home"
+                  android:key="home_settings">
+              
+    
+</PreferenceScreen>
\ No newline at end of file
diff --git a/Settings/res/xml/main_prefs.xml b/Settings/res/xml/main_prefs.xml
index 5d4b494..d1a62a8 100644
--- a/Settings/res/xml/main_prefs.xml
+++ b/Settings/res/xml/main_prefs.xml
@@ -84,9 +84,8 @@
         <Preference
             android:icon="@drawable/ic_home"
             android:key="home"
-            android:title="@string/system_home" >
-            <intent android:action="com.google.android.tvlauncher.SETTINGS" />
-        </Preference>
+            android:title="@string/system_home" 
+            android:fragment="com.android.tv.settings.system.HomeScreenFragment" />
         <Preference
             android:icon="@drawable/ic_search"
             android:key="search"
diff --git a/Settings/src/com/android/tv/settings/MainFragment.java b/Settings/src/com/android/tv/settings/MainFragment.java
index a48a734..4970f7e 100644
--- a/Settings/src/com/android/tv/settings/MainFragment.java
+++ b/Settings/src/com/android/tv/settings/MainFragment.java
@@ -162,7 +162,7 @@ public class MainFragment extends LeanbackPreferenceFragment {
         updateSounds();
         updateGoogleSettings();
 
-        hideIfIntentUnhandled(findPreference(KEY_HOME_SETTINGS));
+        //hideIfIntentUnhandled(findPreference(KEY_HOME_SETTINGS));
         hideIfIntentUnhandled(findPreference(KEY_CAST_SETTINGS));
         hideIfIntentUnhandled(findPreference(KEY_USAGE));
         hideIfIntentUnhandled(findPreference(KEY_SPEECH_SETTINGS));
diff --git a/Settings/src/com/android/tv/settings/system/HomeScreenActivity.java b/Settings/src/com/android/tv/settings/system/HomeScreenActivity.java
new file mode 100644
index 0000000..ee0176f
--- /dev/null
+++ b/Settings/src/com/android/tv/settings/system/HomeScreenActivity.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2014 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.tv.settings.system;
+
+import android.app.Fragment;
+
+import com.android.tv.settings.BaseSettingsFragment;
+import com.android.tv.settings.TvSettingsActivity;
+
+public class HomeScreenActivity extends TvSettingsActivity {
+
+    @Override
+    protected Fragment createSettingsFragment() {
+        return SettingsFragment.newInstance();
+    }
+
+    public static class SettingsFragment extends BaseSettingsFragment {
+
+        public static SettingsFragment newInstance() {
+            return new SettingsFragment();
+        }
+
+        @Override
+        public void onPreferenceStartInitialScreen() {
+            startPreferenceFragment(HomeScreenFragment.newInstance());
+        }
+    }
+
+}
diff --git a/Settings/src/com/android/tv/settings/system/HomeScreenFragment.java b/Settings/src/com/android/tv/settings/system/HomeScreenFragment.java
new file mode 100644
index 0000000..56abdb2
--- /dev/null
+++ b/Settings/src/com/android/tv/settings/system/HomeScreenFragment.java
@@ -0,0 +1,390 @@
+/*
+ * Copyright (C) 2015 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.tv.settings.system;
+
+import android.app.ActivityManager;
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.UserManager;
+import android.provider.Settings;
+import android.support.v14.preference.SwitchPreference;
+import android.support.v17.preference.LeanbackPreferenceFragment;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceViewHolder;
+import android.text.TextUtils;
+import android.text.format.DateFormat;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.ImageView;
+import android.widget.RadioButton;
+
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
+import android.graphics.ColorFilter;
+import android.graphics.ColorMatrix;
+import android.graphics.ColorMatrixColorFilter;
+import android.graphics.drawable.Drawable;
+
+import com.android.tv.settings.R;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+public class HomeScreenFragment extends LeanbackPreferenceFragment implements
+        Preference.OnPreferenceChangeListener {
+	static final String TAG = "HomeScreen";
+	
+    public static final String HOME_PREFS = "home_prefs";
+    public static final String HOME_PREFS_DO_SHOW = "do_show";
+
+    public static final String HOME_SHOW_NOTICE = "show";
+    
+    private static final String KEY_HOME_SETTINGS = "home_settings";
+
+    private static final String EXTRA_SUPPORT_MANAGED_PROFILES = "support_managed_profiles";
+
+    static final int REQUESTING_UNINSTALL = 10;
+
+    public static HomeScreenFragment newInstance() {
+        return new HomeScreenFragment();
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+    	super.onCreate(savedInstanceState);
+    }
+
+    @Override
+    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
+        setPreferencesFromResource(R.xml.home_screen, null);
+        
+        mPm = getActivity().getPackageManager();
+        mPrefGroup = (PreferenceGroup) findPreference(KEY_HOME_SETTINGS);
+        
+        mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
+        mHomeFilter.addCategory(Intent.CATEGORY_HOME);
+        mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        
+        final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
+        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
+        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
+        filter.addDataScheme("package");
+        getActivity().registerReceiver(mHomePackageReceiver, filter);
+
+        buildHomeActivitiesList();
+
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        getActivity().unregisterReceiver(mHomePackageReceiver);
+    }
+
+
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        super.onActivityResult(requestCode, resultCode, data);
+
+        // Rebuild the list now that we might have nuked something
+        buildHomeActivitiesList();
+
+        // if the previous home app is now gone, fall back to the system one
+        if (requestCode > REQUESTING_UNINSTALL) {
+            // if mCurrentHome has gone null, it means we didn't find the previously-
+            // default home app when rebuilding the list, i.e. it was the one we
+            // just uninstalled.  When that happens we make the system-bundled
+            // home app the active default.
+            if (mCurrentHome == null) {
+                for (int i = 0; i < mPrefs.size(); i++) {
+                    HomeAppPreference pref = mPrefs.get(i);
+                    if (pref.isSystem) {
+                        makeCurrentHome(pref);
+                        break;
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        return true;
+    }
+    
+    private class HomePackageReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            buildHomeActivitiesList();
+        }
+    }
+
+    private PreferenceGroup mPrefGroup;
+    private PackageManager mPm;
+    private ComponentName[] mHomeComponentSet;
+    private ArrayList<HomeAppPreference> mPrefs;
+    private HomeAppPreference mCurrentHome = null;
+    private  IntentFilter mHomeFilter;
+    private boolean mShowNotice;
+    private HomePackageReceiver mHomePackageReceiver = new HomePackageReceiver();
+    
+    private void buildHomeActivitiesList() {
+        ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
+        ComponentName currentDefaultHome  = mPm.getHomeActivities(homeActivities);
+
+        Context context = getActivity();
+        mCurrentHome = null;
+        mPrefGroup.removeAll();
+        mPrefs = new ArrayList<HomeAppPreference>();
+        mHomeComponentSet = new ComponentName[homeActivities.size()];
+        int prefIndex = 0;
+        boolean supportManagedProfilesExtra =
+                getActivity().getIntent().getBooleanExtra(EXTRA_SUPPORT_MANAGED_PROFILES, false);
+        boolean mustSupportManagedProfile = hasManagedProfile()
+                || supportManagedProfilesExtra;
+        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);
+            mHomeComponentSet[i] = activityName;
+            try {
+                Drawable icon = info.loadIcon(mPm);
+                CharSequence name = info.loadLabel(mPm);
+                HomeAppPreference pref;
+
+                if (mustSupportManagedProfile && !launcherHasManagedProfilesFeature(candidate)) {
+                    pref = new HomeAppPreference(context, activityName, prefIndex,
+                            icon, name, this, info, false /* not enabled */,
+                            context.getResources().getString(R.string.home_work_profile_not_supported));
+                } else  {
+                    pref = new HomeAppPreference(context, activityName, prefIndex,
+                            icon, name, this, info, true /* enabled */, null);
+                }
+
+                if ((info.flags & ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS) == 0){
+                	mPrefs.add(pref);
+                	mPrefGroup.addPreference(pref);
+                }
+                if (activityName.equals(currentDefaultHome)) {
+                    mCurrentHome = pref;
+                }
+                prefIndex++;
+            } catch (Exception e) {
+                Log.v(TAG, "Problem dealing with activity " + activityName, e);
+            }
+        }
+        
+        if (mCurrentHome != null) {
+            if (mCurrentHome.isEnabled()) {
+                getActivity().setResult(Activity.RESULT_OK);
+            }
+
+            new Handler().post(new Runnable() {
+               public void run() {
+                   mCurrentHome.setChecked(true);
+               }
+            });
+        }
+    }
+    
+
+    private boolean hasManagedProfile() {
+        Context context = getActivity();
+        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+        List<UserInfo> profiles = userManager.getProfiles(context.getUserId());
+        for (UserInfo userInfo : profiles) {
+            if (userInfo.isManagedProfile()) return true;
+        }
+        return false;
+    }
+
+    private boolean launcherHasManagedProfilesFeature(ResolveInfo resolveInfo) {
+        try {
+            ApplicationInfo appInfo = getActivity().getPackageManager().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;
+    }
+    
+    OnClickListener mHomeClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            int index = (Integer)v.getTag();
+            HomeAppPreference pref = mPrefs.get(index);
+            if (!pref.isChecked) {
+                makeCurrentHome(pref);
+            }
+        }
+    };
+
+    OnClickListener mDeleteClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View v) {
+            int index = (Integer)v.getTag();
+            uninstallApp(mPrefs.get(index));
+        }
+    };
+
+    void makeCurrentHome(HomeAppPreference newHome) {
+        if (mCurrentHome != null) {
+            mCurrentHome.setChecked(false);
+        }
+        newHome.setChecked(true);
+        mCurrentHome = newHome;
+
+        mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
+                mHomeComponentSet, newHome.activityName);
+
+        getActivity().setResult(Activity.RESULT_OK);
+    }
+
+    void uninstallApp(HomeAppPreference pref) {
+        // Uninstallation is done by asking the OS to do it
+       Uri packageURI = Uri.parse("package:" + pref.uninstallTarget);
+       Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
+       uninstallIntent.putExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, false);
+       int requestCode = REQUESTING_UNINSTALL + (pref.isChecked ? 1 : 0);
+       startActivityForResult(uninstallIntent, requestCode);
+   }
+
+    
+    private class HomeAppPreference extends Preference {
+        ComponentName activityName;
+        int index;
+        HomeScreenFragment fragment;
+        final ColorFilter grayscaleFilter;
+        boolean isChecked;
+
+        boolean isSystem;
+        String uninstallTarget;
+
+        public HomeAppPreference(Context context, ComponentName activity,
+                int i, Drawable icon, CharSequence title, HomeScreenFragment parent, ActivityInfo info,
+                boolean enabled, CharSequence summary) {
+            super(context);
+            setLayoutResource(R.layout.preference_home_app);
+            setIcon(icon);
+            setTitle(title);
+            setEnabled(enabled);
+            setSummary(summary);
+            activityName = activity;
+            fragment = parent;
+            index = i;
+
+            ColorMatrix colorMatrix = new ColorMatrix();
+            colorMatrix.setSaturation(0f);
+            float[] matrix = colorMatrix.getArray();
+            matrix[18] = 0.5f;
+            grayscaleFilter = new ColorMatrixColorFilter(colorMatrix);
+
+            determineTargets(info);
+        }
+
+        // Check whether this activity is bundled on the system, with awareness
+        // of the META_HOME_ALTERNATE mechanism.
+        private void determineTargets(ActivityInfo info) {
+            final Bundle meta = info.metaData;
+            if (meta != null) {
+                final String altHomePackage = meta.getString(ActivityManager.META_HOME_ALTERNATE);
+                if (altHomePackage != null) {
+                    try {
+                        final int match = mPm.checkSignatures(info.packageName, altHomePackage);
+                        if (match >= PackageManager.SIGNATURE_MATCH) {
+                            PackageInfo altInfo = mPm.getPackageInfo(altHomePackage, 0);
+                            final int altFlags = altInfo.applicationInfo.flags;
+                            isSystem = (altFlags & ApplicationInfo.FLAG_SYSTEM) != 0;
+                            uninstallTarget = altInfo.packageName;
+                            return;
+                        }
+                    } catch (Exception e) {
+                        // e.g. named alternate package not found during lookup
+                        Log.w(TAG, "Unable to compare/resolve alternate", e);
+                    }
+                }
+            }
+            // No suitable metadata redirect, so use the package's own info
+            isSystem = (info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
+            uninstallTarget = info.packageName;
+        }
+
+        @Override
+        public void onBindViewHolder(PreferenceViewHolder view) {
+            super.onBindViewHolder(view);
+
+            RadioButton radio = (RadioButton) view.findViewById(R.id.home_radio);
+            radio.setChecked(isChecked);
+
+            Integer indexObj = new Integer(index);
+
+            ImageView icon = (ImageView) view.findViewById(R.id.home_app_uninstall);
+            if (isSystem) {
+                icon.setEnabled(false);
+                icon.setColorFilter(grayscaleFilter);
+            } else {
+                icon.setEnabled(true);
+                icon.setOnClickListener(mDeleteClickListener);
+                icon.setTag(indexObj);
+            }
+
+            View v = view.findViewById(R.id.home_app_pref);
+            v.setTag(indexObj);
+
+            v.setOnClickListener(mHomeClickListener);
+        }
+
+        void setChecked(boolean state) {
+            if (state != isChecked) {
+                isChecked = state;
+                notifyChanged();
+            }
+        }
+    }
+
+ 
+
+}
-- 
2.7.4

你可能感兴趣的:([RK3399][Android7.1.1]TvSettings添加默认主界面(Home app)设置)