• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.settingslib;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.Build;
25 import android.system.Os;
26 import android.system.StructUtsname;
27 import android.telephony.PhoneNumberUtils;
28 import android.telephony.SubscriptionInfo;
29 import android.telephony.TelephonyManager;
30 import android.text.BidiFormatter;
31 import android.text.TextDirectionHeuristics;
32 import android.text.TextUtils;
33 import android.text.format.DateFormat;
34 import android.util.Log;
35 
36 import androidx.annotation.VisibleForTesting;
37 
38 import java.io.BufferedReader;
39 import java.io.FileReader;
40 import java.io.IOException;
41 import java.text.ParseException;
42 import java.text.SimpleDateFormat;
43 import java.util.Date;
44 import java.util.List;
45 import java.util.Locale;
46 import java.util.regex.Matcher;
47 import java.util.regex.Pattern;
48 
49 public class DeviceInfoUtils {
50     private static final String TAG = "DeviceInfoUtils";
51 
52     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
53 
54     /**
55      * Reads a line from the specified file.
56      * @param filename the file to read from
57      * @return the first line, if any.
58      * @throws IOException if the file couldn't be read
59      */
readLine(String filename)60     private static String readLine(String filename) throws IOException {
61         BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
62         try {
63             return reader.readLine();
64         } finally {
65             reader.close();
66         }
67     }
68 
getFormattedKernelVersion(Context context)69     public static String getFormattedKernelVersion(Context context) {
70             return formatKernelVersion(context, Os.uname());
71     }
72 
73     @VisibleForTesting
formatKernelVersion(Context context, StructUtsname uname)74     static String formatKernelVersion(Context context, StructUtsname uname) {
75         if (uname == null) {
76             return context.getString(R.string.status_unavailable);
77         }
78         // Example:
79         // 4.9.29-g958411d
80         // #1 SMP PREEMPT Wed Jun 7 00:06:03 CST 2017
81         final String VERSION_REGEX =
82                 "(#\\d+) " +              /* group 1: "#1" */
83                 "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
84                 "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 2: "Thu Jun 28 11:02:39 PDT 2012" */
85         Matcher m = Pattern.compile(VERSION_REGEX).matcher(uname.version);
86         if (!m.matches()) {
87             Log.e(TAG, "Regex did not match on uname version " + uname.version);
88             return context.getString(R.string.status_unavailable);
89         }
90 
91         // Example output:
92         // 4.9.29-g958411d
93         // #1 Wed Jun 7 00:06:03 CST 2017
94         return new StringBuilder().append(uname.release)
95                 .append("\n")
96                 .append(m.group(1))
97                 .append(" ")
98                 .append(m.group(2)).toString();
99     }
100 
101     /**
102      * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
103      * @return a string to append to the model number description.
104      */
getMsvSuffix()105     public static String getMsvSuffix() {
106         // Production devices should have a non-zero value. If we can't read it, assume it's a
107         // production device so that we don't accidentally show that it's an ENGINEERING device.
108         try {
109             String msv = readLine(FILENAME_MSV);
110             // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
111             if (Long.parseLong(msv, 16) == 0) {
112                 return " (ENGINEERING)";
113             }
114         } catch (IOException|NumberFormatException e) {
115             // Fail quietly, as the file may not exist on some devices, or may be unreadable
116         }
117         return "";
118     }
119 
getFeedbackReporterPackage(Context context)120     public static String getFeedbackReporterPackage(Context context) {
121         final String feedbackReporter =
122                 context.getResources().getString(R.string.oem_preferred_feedback_reporter);
123         if (TextUtils.isEmpty(feedbackReporter)) {
124             // Reporter not configured. Return.
125             return feedbackReporter;
126         }
127         // Additional checks to ensure the reporter is on system image, and reporter is
128         // configured to listen to the intent. Otherwise, dont show the "send feedback" option.
129         final Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
130 
131         PackageManager pm = context.getPackageManager();
132         List<ResolveInfo> resolvedPackages =
133                 pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
134         for (ResolveInfo info : resolvedPackages) {
135             if (info.activityInfo != null) {
136                 if (!TextUtils.isEmpty(info.activityInfo.packageName)) {
137                     try {
138                         ApplicationInfo ai =
139                                 pm.getApplicationInfo(info.activityInfo.packageName, 0);
140                         if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
141                             // Package is on the system image
142                             if (TextUtils.equals(
143                                     info.activityInfo.packageName, feedbackReporter)) {
144                                 return feedbackReporter;
145                             }
146                         }
147                     } catch (PackageManager.NameNotFoundException e) {
148                         // No need to do anything here.
149                     }
150                 }
151             }
152         }
153         return null;
154     }
155 
getSecurityPatch()156     public static String getSecurityPatch() {
157         String patch = Build.VERSION.SECURITY_PATCH;
158         if (!"".equals(patch)) {
159             try {
160                 SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd");
161                 Date patchDate = template.parse(patch);
162                 String format = DateFormat.getBestDateTimePattern(Locale.getDefault(), "dMMMMyyyy");
163                 patch = DateFormat.format(format, patchDate).toString();
164             } catch (ParseException e) {
165                 // broken parse; fall through and use the raw string
166             }
167             return patch;
168         } else {
169             return null;
170         }
171     }
172 
173     /**
174      * Format a phone number.
175      * @param subscriptionInfo {@link SubscriptionInfo} subscription information.
176      * @return Returns formatted phone number.
177      */
getFormattedPhoneNumber(Context context, SubscriptionInfo subscriptionInfo)178     public static String getFormattedPhoneNumber(Context context,
179             SubscriptionInfo subscriptionInfo) {
180         String formattedNumber = null;
181         if (subscriptionInfo != null) {
182             final TelephonyManager telephonyManager = context.getSystemService(
183                     TelephonyManager.class);
184             final String rawNumber = telephonyManager.createForSubscriptionId(
185                     subscriptionInfo.getSubscriptionId()).getLine1Number();
186             if (!TextUtils.isEmpty(rawNumber)) {
187                 formattedNumber = PhoneNumberUtils.formatNumber(rawNumber);
188             }
189         }
190         return formattedNumber;
191     }
192 
getFormattedPhoneNumbers(Context context, List<SubscriptionInfo> subscriptionInfoList)193     public static String getFormattedPhoneNumbers(Context context,
194             List<SubscriptionInfo> subscriptionInfoList) {
195         StringBuilder sb = new StringBuilder();
196         if (subscriptionInfoList != null) {
197             final TelephonyManager telephonyManager = context.getSystemService(
198                     TelephonyManager.class);
199             final int count = subscriptionInfoList.size();
200             for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
201                 final String rawNumber = telephonyManager.createForSubscriptionId(
202                         subscriptionInfo.getSubscriptionId()).getLine1Number();
203                 if (!TextUtils.isEmpty(rawNumber)) {
204                     sb.append(PhoneNumberUtils.formatNumber(rawNumber)).append("\n");
205                 }
206             }
207         }
208         return sb.toString();
209     }
210 
211     /**
212      * To get the formatting text for display in a potentially opposite-directionality context
213      * without garbling.
214      * @param subscriptionInfo {@link SubscriptionInfo} subscription information.
215      * @return Returns phone number with Bidi format.
216      */
getBidiFormattedPhoneNumber(Context context, SubscriptionInfo subscriptionInfo)217     public static String getBidiFormattedPhoneNumber(Context context,
218             SubscriptionInfo subscriptionInfo) {
219         final String phoneNumber = getFormattedPhoneNumber(context, subscriptionInfo);
220         return BidiFormatter.getInstance().unicodeWrap(phoneNumber, TextDirectionHeuristics.LTR);
221     }
222 }
223