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