Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
| 25 | import android.os.Bundle; |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 26 | import android.provider.SearchIndexableResource; |
Ben Lin | e4ca92a | 2017-12-12 16:44:53 -0800 | [diff] [blame] | 27 | import android.support.annotation.VisibleForTesting; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 28 | import android.support.v7.preference.PreferenceGroup; |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 29 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 30 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Ben Lin | e4ca92a | 2017-12-12 16:44:53 -0800 | [diff] [blame] | 31 | import com.android.settings.R; |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 32 | import com.android.settings.search.BaseSearchIndexProvider; |
| 33 | import com.android.settings.search.Indexable; |
| 34 | |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 35 | import java.util.Arrays; |
| 36 | import java.util.List; |
| 37 | |
| 38 | public class LegalSettings extends SettingsPreferenceFragment implements Indexable { |
| 39 | |
| 40 | private static final String KEY_TERMS = "terms"; |
| 41 | private static final String KEY_LICENSE = "license"; |
| 42 | private static final String KEY_COPYRIGHT = "copyright"; |
| 43 | private static final String KEY_WEBVIEW_LICENSE = "webview_license"; |
Ben Lin | e4ca92a | 2017-12-12 16:44:53 -0800 | [diff] [blame] | 44 | @VisibleForTesting static final String KEY_WALLPAPER_ATTRIBUTIONS = "wallpaper_attributions"; |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 45 | |
| 46 | public void onCreate(Bundle icicle) { |
| 47 | super.onCreate(icicle); |
| 48 | addPreferencesFromResource(R.xml.about_legal); |
| 49 | |
| 50 | final Activity act = getActivity(); |
| 51 | // These are contained in the "container" preference group |
| 52 | PreferenceGroup parentPreference = getPreferenceScreen(); |
| 53 | Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS, |
| 54 | Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); |
| 55 | Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE, |
| 56 | Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); |
| 57 | Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT, |
| 58 | Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); |
| 59 | Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_WEBVIEW_LICENSE, |
| 60 | Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); |
Ben Lin | e4ca92a | 2017-12-12 16:44:53 -0800 | [diff] [blame] | 61 | |
| 62 | checkWallpaperAttributionAvailability(act); |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 66 | public int getMetricsCategory() { |
Chris Wren | 9d1bfd1 | 2016-01-26 18:04:01 -0500 | [diff] [blame] | 67 | return MetricsEvent.ABOUT_LEGAL_SETTINGS; |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Ben Lin | e4ca92a | 2017-12-12 16:44:53 -0800 | [diff] [blame] | 70 | @VisibleForTesting |
| 71 | void checkWallpaperAttributionAvailability(Context context) { |
| 72 | if (!context.getResources().getBoolean( |
| 73 | R.bool.config_show_wallpaper_attribution)) { |
| 74 | removePreference(KEY_WALLPAPER_ATTRIBUTIONS); |
| 75 | } |
| 76 | } |
| 77 | |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 78 | public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
| 79 | new BaseSearchIndexProvider() { |
| 80 | |
| 81 | @Override |
| 82 | public List<SearchIndexableResource> getXmlResourcesToIndex( |
| 83 | Context context, boolean enabled) { |
| 84 | final SearchIndexableResource sir = new SearchIndexableResource(context); |
| 85 | sir.xmlResId = R.xml.about_legal; |
| 86 | return Arrays.asList(sir); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public List<String> getNonIndexableKeys(Context context) { |
Matthew Fritze | 031af9f | 2017-05-11 13:32:37 -0700 | [diff] [blame] | 91 | final List<String> keys = super.getNonIndexableKeys(context); |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 92 | if (!checkIntentAction(context, "android.settings.TERMS")) { |
| 93 | keys.add(KEY_TERMS); |
| 94 | } |
| 95 | if (!checkIntentAction(context, "android.settings.LICENSE")) { |
| 96 | keys.add(KEY_LICENSE); |
| 97 | } |
| 98 | if (!checkIntentAction(context, "android.settings.COPYRIGHT")) { |
| 99 | keys.add(KEY_COPYRIGHT); |
| 100 | } |
| 101 | if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) { |
| 102 | keys.add(KEY_WEBVIEW_LICENSE); |
| 103 | } |
Matthew Fritze | 031af9f | 2017-05-11 13:32:37 -0700 | [diff] [blame] | 104 | keys.add(KEY_WALLPAPER_ATTRIBUTIONS); |
Jason Monk | f940232 | 2015-06-10 10:02:59 -0400 | [diff] [blame] | 105 | return keys; |
| 106 | } |
| 107 | |
| 108 | private boolean checkIntentAction(Context context, String action) { |
| 109 | final Intent intent = new Intent(action); |
| 110 | |
| 111 | // Find the activity that is in the system image |
| 112 | final PackageManager pm = context.getPackageManager(); |
| 113 | final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 114 | final int listSize = list.size(); |
| 115 | |
| 116 | for (int i = 0; i < listSize; i++) { |
| 117 | ResolveInfo resolveInfo = list.get(i); |
| 118 | if ((resolveInfo.activityInfo.applicationInfo.flags & |
| 119 | ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | } |