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.text.format.DateFormat; 25 import android.util.Log; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 30 import com.android.settings.core.BasePreferenceController; 31 32 import java.text.ParseException; 33 import java.text.SimpleDateFormat; 34 import java.util.Arrays; 35 import java.util.Date; 36 import java.util.List; 37 import java.util.Locale; 38 import java.util.Optional; 39 import java.util.TimeZone; 40 41 public class MainlineModuleVersionPreferenceController extends BasePreferenceController { 42 43 private static final String TAG = "MainlineModuleControl"; 44 private static final List<String> VERSION_NAME_DATE_PATTERNS = Arrays.asList("yyyy-MM-dd", 45 "yyyy-MM"); 46 47 @VisibleForTesting 48 static final Intent MODULE_UPDATE_INTENT = 49 new Intent("android.settings.MODULE_UPDATE_SETTINGS"); 50 @VisibleForTesting 51 static final Intent MODULE_UPDATE_V2_INTENT = 52 new Intent("android.settings.MODULE_UPDATE_VERSIONS"); 53 54 private final PackageManager mPackageManager; 55 56 private String mModuleVersion; 57 MainlineModuleVersionPreferenceController(Context context, String key)58 public MainlineModuleVersionPreferenceController(Context context, String key) { 59 super(context, key); 60 mPackageManager = mContext.getPackageManager(); 61 initModules(); 62 } 63 64 @Override getAvailabilityStatus()65 public int getAvailabilityStatus() { 66 return !TextUtils.isEmpty(mModuleVersion) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 67 } 68 initModules()69 private void initModules() { 70 final String moduleProvider = mContext.getString( 71 com.android.internal.R.string.config_defaultModuleMetadataProvider); 72 if (!TextUtils.isEmpty(moduleProvider)) { 73 try { 74 mModuleVersion = 75 mPackageManager.getPackageInfo(moduleProvider, 0 /* flags */).versionName; 76 return; 77 } catch (PackageManager.NameNotFoundException e) { 78 Log.e(TAG, "Failed to get mainline version.", e); 79 mModuleVersion = null; 80 } 81 } 82 } 83 84 @Override updateState(Preference preference)85 public void updateState(Preference preference) { 86 super.updateState(preference); 87 88 final ResolveInfo resolvedV2 = 89 mPackageManager.resolveActivity(MODULE_UPDATE_V2_INTENT, 0 /* flags */); 90 if (resolvedV2 != null) { 91 preference.setIntent(MODULE_UPDATE_V2_INTENT); 92 preference.setSelectable(true); 93 return; 94 } 95 96 final ResolveInfo resolved = 97 mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0 /* flags */); 98 if (resolved != null) { 99 preference.setIntent(MODULE_UPDATE_INTENT); 100 preference.setSelectable(true); 101 } else { 102 Log.d(TAG, "The ResolveInfo of the update intent is null."); 103 preference.setIntent(null); 104 preference.setSelectable(false); 105 } 106 } 107 108 @Override getSummary()109 public CharSequence getSummary() { 110 if (TextUtils.isEmpty(mModuleVersion)) { 111 return mModuleVersion; 112 } 113 114 final Optional<Date> parsedDate = parseDateFromVersionName(mModuleVersion); 115 if (!parsedDate.isPresent()) { 116 Log.w("Could not parse mainline versionName (%s) as date.", mModuleVersion); 117 return mModuleVersion; 118 } 119 120 return DateFormat.getLongDateFormat(mContext).format(parsedDate.get()); 121 } 122 parseDateFromVersionName(String text)123 private Optional<Date> parseDateFromVersionName(String text) { 124 for (String pattern : VERSION_NAME_DATE_PATTERNS) { 125 try { 126 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, 127 Locale.getDefault()); 128 simpleDateFormat.setTimeZone(TimeZone.getDefault()); 129 return Optional.of(simpleDateFormat.parse(text)); 130 } catch (ParseException e) { 131 // ignore and try next pattern 132 } 133 } 134 return Optional.empty(); 135 } 136 } 137