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