1 /* 2 * Copyright (C) 2019 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.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 29 import com.android.settings.core.BasePreferenceController; 30 31 public class MainlineModuleVersionPreferenceController extends BasePreferenceController { 32 33 private static final String TAG = "MainlineModuleControl"; 34 35 @VisibleForTesting 36 static final Intent MODULE_UPDATE_INTENT = 37 new Intent("android.settings.MODULE_UPDATE_SETTINGS"); 38 private final PackageManager mPackageManager; 39 40 private String mModuleVersion; 41 MainlineModuleVersionPreferenceController(Context context, String key)42 public MainlineModuleVersionPreferenceController(Context context, String key) { 43 super(context, key); 44 mPackageManager = mContext.getPackageManager(); 45 initModules(); 46 } 47 48 @Override getAvailabilityStatus()49 public int getAvailabilityStatus() { 50 return !TextUtils.isEmpty(mModuleVersion) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 51 } 52 initModules()53 private void initModules() { 54 final String moduleProvider = mContext.getString( 55 com.android.internal.R.string.config_defaultModuleMetadataProvider); 56 if (!TextUtils.isEmpty(moduleProvider)) { 57 try { 58 mModuleVersion = 59 mPackageManager.getPackageInfo(moduleProvider, 0 /* flags */).versionName; 60 return; 61 } catch (PackageManager.NameNotFoundException e) { 62 Log.e(TAG, "Failed to get mainline version.", e); 63 mModuleVersion = null; 64 } 65 } 66 } 67 68 @Override updateState(Preference preference)69 public void updateState(Preference preference) { 70 super.updateState(preference); 71 72 // Confirm MODULE_UPDATE_INTENT is handleable, and set it to Preference. 73 final ResolveInfo resolved = 74 mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0 /* flags */); 75 if (resolved != null) { 76 preference.setIntent(MODULE_UPDATE_INTENT); 77 } else { 78 preference.setIntent(null); 79 } 80 } 81 82 @Override getSummary()83 public CharSequence getSummary() { 84 return mModuleVersion; 85 } 86 } 87