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.nano.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    private static final String KEY_WALLPAPER_ATTRIBUTIONS = "wallpaper_attributions";
44
45    public void onCreate(Bundle icicle) {
46        super.onCreate(icicle);
47        addPreferencesFromResource(R.xml.about_legal);
48
49        final Activity act = getActivity();
50        // These are contained in the "container" preference group
51        PreferenceGroup parentPreference = getPreferenceScreen();
52        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
53                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
54        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
55                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
56        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
57                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
58        Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_WEBVIEW_LICENSE,
59                Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
60    }
61
62    @Override
63    public int getMetricsCategory() {
64        return MetricsEvent.ABOUT_LEGAL_SETTINGS;
65    }
66
67    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
68        new BaseSearchIndexProvider() {
69
70            @Override
71            public List<SearchIndexableResource> getXmlResourcesToIndex(
72                    Context context, boolean enabled) {
73                final SearchIndexableResource sir = new SearchIndexableResource(context);
74                sir.xmlResId = R.xml.about_legal;
75                return Arrays.asList(sir);
76            }
77
78            @Override
79            public List<String> getNonIndexableKeys(Context context) {
80                final List<String> keys = super.getNonIndexableKeys(context);
81                if (!checkIntentAction(context, "android.settings.TERMS")) {
82                    keys.add(KEY_TERMS);
83                }
84                if (!checkIntentAction(context, "android.settings.LICENSE")) {
85                    keys.add(KEY_LICENSE);
86                }
87                if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
88                    keys.add(KEY_COPYRIGHT);
89                }
90                if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) {
91                    keys.add(KEY_WEBVIEW_LICENSE);
92                }
93                keys.add(KEY_WALLPAPER_ATTRIBUTIONS);
94                return keys;
95            }
96
97            private boolean checkIntentAction(Context context, String action) {
98                final Intent intent = new Intent(action);
99
100                // Find the activity that is in the system image
101                final PackageManager pm = context.getPackageManager();
102                final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
103                final int listSize = list.size();
104
105                for (int i = 0; i < listSize; i++) {
106                    ResolveInfo resolveInfo = list.get(i);
107                    if ((resolveInfo.activityInfo.applicationInfo.flags &
108                            ApplicationInfo.FLAG_SYSTEM) != 0) {
109                        return true;
110                    }
111                }
112
113                return false;
114            }
115    };
116
117}
118