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 17 package com.android.settings.deviceinfo.firmwareversion; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.support.annotation.VisibleForTesting; 23 import android.text.TextUtils; 24 import android.util.Log; 25 import android.view.View; 26 27 import com.android.settings.R; 28 import com.android.settingslib.DeviceInfoUtils; 29 import com.android.settingslib.wrapper.PackageManagerWrapper; 30 31 public class SecurityPatchLevelDialogController implements View.OnClickListener { 32 33 private static final String TAG = "SecurityPatchCtrl"; 34 private static final Uri INTENT_URI_DATA = Uri.parse( 35 "https://source.android.com/security/bulletin/"); 36 37 @VisibleForTesting 38 static final int SECURITY_PATCH_VALUE_ID = R.id.security_patch_level_value; 39 @VisibleForTesting 40 static final int SECURITY_PATCH_LABEL_ID = R.id.security_patch_level_label; 41 42 private final FirmwareVersionDialogFragment mDialog; 43 private final Context mContext; 44 private final PackageManagerWrapper mPackageManager; 45 private final String mCurrentPatch; 46 47 public SecurityPatchLevelDialogController(FirmwareVersionDialogFragment dialog) { 48 mDialog = dialog; 49 mContext = dialog.getContext(); 50 mPackageManager = new PackageManagerWrapper(mContext.getPackageManager()); 51 mCurrentPatch = DeviceInfoUtils.getSecurityPatch(); 52 } 53 54 @Override 55 public void onClick(View v) { 56 final Intent intent = new Intent(); 57 intent.setAction(Intent.ACTION_VIEW); 58 intent.setData(INTENT_URI_DATA); 59 if (mPackageManager.queryIntentActivities(intent, 0).isEmpty()) { 60 // Don't send out the intent to stop crash 61 Log.w(TAG, "Stop click action on " + SECURITY_PATCH_VALUE_ID + ": " 62 + "queryIntentActivities() returns empty"); 63 return; 64 } 65 66 mContext.startActivity(intent); 67 } 68 69 /** 70 * Populates the security patch level field in the dialog and registers click listeners. 71 */ 72 public void initialize() { 73 if (TextUtils.isEmpty(mCurrentPatch)) { 74 mDialog.removeSettingFromScreen(SECURITY_PATCH_LABEL_ID); 75 mDialog.removeSettingFromScreen(SECURITY_PATCH_VALUE_ID); 76 return; 77 } 78 registerListeners(); 79 mDialog.setText(SECURITY_PATCH_VALUE_ID, mCurrentPatch); 80 } 81 82 private void registerListeners() { 83 mDialog.registerClickListener(SECURITY_PATCH_LABEL_ID, this /* listener */); 84 mDialog.registerClickListener(SECURITY_PATCH_VALUE_ID, this /* listener */); 85 } 86 } 87