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