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