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