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