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
17package com.android.settings;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.os.Bundle;
26import android.provider.SearchIndexableResource;
27import android.support.v7.preference.PreferenceGroup;
28
29import com.android.internal.logging.MetricsProto.MetricsEvent;
30import com.android.settings.search.BaseSearchIndexProvider;
31import com.android.settings.search.Indexable;
32
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
36
37public class LegalSettings extends SettingsPreferenceFragment implements Indexable {
38
39    private static final String KEY_TERMS = "terms";
40    private static final String KEY_LICENSE = "license";
41    private static final String KEY_COPYRIGHT = "copyright";
42    private static final String KEY_WEBVIEW_LICENSE = "webview_license";
43
44    public void onCreate(Bundle icicle) {
45        super.onCreate(icicle);
46        addPreferencesFromResource(R.xml.about_legal);
47
48        final Activity act = getActivity();
49        // These are contained in the "container" preference group
50        PreferenceGroup parentPreference = getPreferenceScreen();
51        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
52                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
53        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
54                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
55        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
56                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
57        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_WEBVIEW_LICENSE,
58                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
59    }
60
61    @Override
62    protected int getMetricsCategory() {
63        return MetricsEvent.ABOUT_LEGAL_SETTINGS;
64    }
65
66    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
67        new BaseSearchIndexProvider() {
68
69            @Override
70            public List<SearchIndexableResource> getXmlResourcesToIndex(
71                    Context context, boolean enabled) {
72                final SearchIndexableResource sir = new SearchIndexableResource(context);
73                sir.xmlResId = R.xml.about_legal;
74                return Arrays.asList(sir);
75            }
76
77            @Override
78            public List<String> getNonIndexableKeys(Context context) {
79                final List<String> keys = new ArrayList<String>();
80                if (!checkIntentAction(context, "android.settings.TERMS")) {
81                    keys.add(KEY_TERMS);
82                }
83                if (!checkIntentAction(context, "android.settings.LICENSE")) {
84                    keys.add(KEY_LICENSE);
85                }
86                if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
87                    keys.add(KEY_COPYRIGHT);
88                }
89                if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) {
90                    keys.add(KEY_WEBVIEW_LICENSE);
91                }
92                return keys;
93            }
94
95            private boolean checkIntentAction(Context context, String action) {
96                final Intent intent = new Intent(action);
97
98                // Find the activity that is in the system image
99                final PackageManager pm = context.getPackageManager();
100                final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
101                final int listSize = list.size();
102
103                for (int i = 0; i < listSize; i++) {
104                    ResolveInfo resolveInfo = list.get(i);
105                    if ((resolveInfo.activityInfo.applicationInfo.flags &
106                            ApplicationInfo.FLAG_SYSTEM) != 0) {
107                        return true;
108                    }
109                }
110
111                return false;
112            }
113    };
114
115}
116