1 /*
2  * Copyright (C) 2017 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 package com.android.settings.deviceinfo;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 import android.text.TextUtils;
23 import android.util.Log;
24 
25 import com.android.settings.core.PreferenceController;
26 import com.android.settingslib.DeviceInfoUtils;
27 
28 public class SecurityPatchPreferenceController extends PreferenceController {
29 
30     private static final String KEY_SECURITY_PATCH = "security_patch";
31     private static final String TAG = "SecurityPatchPref";
32 
33     private final String mPatch;
34     private final PackageManager mPackageManager;
35 
SecurityPatchPreferenceController(Context context)36     public SecurityPatchPreferenceController(Context context) {
37         super(context);
38         mPackageManager = mContext.getPackageManager();
39         mPatch = DeviceInfoUtils.getSecurityPatch();
40     }
41 
42     @Override
isAvailable()43     public boolean isAvailable() {
44         return !TextUtils.isEmpty(mPatch);
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY_SECURITY_PATCH;
50     }
51 
52     @Override
displayPreference(PreferenceScreen screen)53     public void displayPreference(PreferenceScreen screen) {
54         super.displayPreference(screen);
55         final Preference pref = screen.findPreference(KEY_SECURITY_PATCH);
56         if (pref != null) {
57             pref.setSummary(mPatch);
58         }
59     }
60 
61     @Override
handlePreferenceTreeClick(Preference preference)62     public boolean handlePreferenceTreeClick(Preference preference) {
63         if (!TextUtils.equals(preference.getKey(), KEY_SECURITY_PATCH)) {
64             return false;
65         }
66         if (mPackageManager.queryIntentActivities(preference.getIntent(), 0).isEmpty()) {
67             // Don't send out the intent to stop crash
68             Log.w(TAG, "Stop click action on " + KEY_SECURITY_PATCH + ": "
69                     + "queryIntentActivities() returns empty");
70             return true;
71         }
72         return false;
73     }
74 }
75