165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
20a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.content.Intent;
21a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.content.pm.ApplicationInfo;
22a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.content.pm.PackageManager;
23a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.content.pm.ResolveInfo;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.provider.Settings;
25a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.support.v7.preference.Preference;
26a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport android.support.v7.preference.PreferenceGroup;
27a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler
28a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantlerimport java.util.List;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Utilities for saving and loading shared prefs.
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic final class PreferenceUtils {
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
35a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler    public static final int FLAG_SET_TITLE = 1;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
37a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler    public static void resolveSystemActivityOrRemove(Context context, PreferenceGroup parent,
38a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            Preference preference, int flags) {
39a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        if (preference == null) {
40a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            return;
41a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        }
42a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler
43a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        Intent intent = preference.getIntent();
44a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        if (intent != null) {
45a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            // Find the activity that is in the system image
46a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            PackageManager pm = context.getPackageManager();
47a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
48a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            for (final ResolveInfo resolveInfo : list) {
49a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
50a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                        != 0) {
51a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler
52a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                    // Replace the intent with this specific activity
53a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                    preference.setIntent(new Intent().setClassName(
54a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                            resolveInfo.activityInfo.packageName,
55a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                            resolveInfo.activityInfo.name));
56a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler
57a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                    if ((flags & FLAG_SET_TITLE) != 0) {
58a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                        // Set the preference title to the activity's label
59a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                        preference.setTitle(resolveInfo.loadLabel(pm));
60a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                    }
61a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler
62a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                    return;
63a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                }
64a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler            }
65a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        }
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
67a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        // Did not find a matching activity, so remove the preference
68a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        parent.removePreference(preference);
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
71a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler    public static boolean isDeveloperEnabled(Context context) {
72a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler        return Settings.Global.getInt(context.getContentResolver(),
73a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
74a02bb000ef2150e85cdcbc06b9c7465b1d8076c6Tony Mantler                android.os.Build.TYPE.equals("eng") ? 1 : 0) == 1;
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
77