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     private final PackageManager mPackageManager;
51 
52     private String mModuleVersion;
53 
MainlineModuleVersionPreferenceController(Context context, String key)54     public MainlineModuleVersionPreferenceController(Context context, String key) {
55         super(context, key);
56         mPackageManager = mContext.getPackageManager();
57         initModules();
58     }
59 
60     @Override
getAvailabilityStatus()61     public int getAvailabilityStatus() {
62         return !TextUtils.isEmpty(mModuleVersion) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
63     }
64 
initModules()65     private void initModules() {
66         final String moduleProvider = mContext.getString(
67                 com.android.internal.R.string.config_defaultModuleMetadataProvider);
68         if (!TextUtils.isEmpty(moduleProvider)) {
69             try {
70                 mModuleVersion =
71                         mPackageManager.getPackageInfo(moduleProvider, 0 /* flags */).versionName;
72                 return;
73             } catch (PackageManager.NameNotFoundException e) {
74                 Log.e(TAG, "Failed to get mainline version.", e);
75                 mModuleVersion = null;
76             }
77         }
78     }
79 
80     @Override
updateState(Preference preference)81     public void updateState(Preference preference) {
82         super.updateState(preference);
83 
84         // Confirm MODULE_UPDATE_INTENT is handleable, and set it to Preference.
85         final ResolveInfo resolved =
86                 mPackageManager.resolveActivity(MODULE_UPDATE_INTENT, 0 /* flags */);
87         if (resolved != null) {
88             preference.setIntent(MODULE_UPDATE_INTENT);
89         } else {
90             preference.setIntent(null);
91         }
92     }
93 
94     @Override
getSummary()95     public CharSequence getSummary() {
96         if (TextUtils.isEmpty(mModuleVersion)) {
97             return mModuleVersion;
98         }
99 
100         final Optional<Date> parsedDate = parseDateFromVersionName(mModuleVersion);
101         if (!parsedDate.isPresent()) {
102             Log.w("Could not parse mainline versionName (%s) as date.", mModuleVersion);
103             return mModuleVersion;
104         }
105 
106         return DateFormat.getLongDateFormat(mContext).format(parsedDate.get());
107     }
108 
parseDateFromVersionName(String text)109     private Optional<Date> parseDateFromVersionName(String text) {
110         for (String pattern : VERSION_NAME_DATE_PATTERNS) {
111             try {
112                 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,
113                         Locale.getDefault());
114                 simpleDateFormat.setTimeZone(TimeZone.getDefault());
115                 return Optional.of(simpleDateFormat.parse(text));
116             } catch (ParseException e) {
117                 // ignore and try next pattern
118             }
119         }
120         return Optional.empty();
121     }
122 }
123