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 "(?:\\(gcc.+? \\)) " + /* ignore: GCC version information */ 87 "(#\\d+) " + /* group 3: "#1" */ 88 "(?:.*?)?" + /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */ 89 "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */ 90 91 Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion); 92 if (!m.matches()) { 93 Log.e(TAG, "Regex did not match on /proc/version: " + rawKernelVersion); 94 return "Unavailable"; 95 } else if (m.groupCount() < 4) { 96 Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount() 97 + " groups"); 98 return "Unavailable"; 99 } 100 return m.group(1) + "\n" + // 3.0.31-g6fb96c9 101 m.group(2) + " " + m.group(3) + "\n" + // x@y.com #1 102 m.group(4); // Thu Jun 28 11:02:39 PDT 2012 103 } 104 105 /** 106 * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "". 107 * @return a string to append to the model number description. 108 */ getMsvSuffix()109 public static String getMsvSuffix() { 110 // Production devices should have a non-zero value. If we can't read it, assume it's a 111 // production device so that we don't accidentally show that it's an ENGINEERING device. 112 try { 113 String msv = readLine(FILENAME_MSV); 114 // Parse as a hex number. If it evaluates to a zero, then it's an engineering build. 115 if (Long.parseLong(msv, 16) == 0) { 116 return " (ENGINEERING)"; 117 } 118 } catch (IOException|NumberFormatException e) { 119 // Fail quietly, as the file may not exist on some devices, or may be unreadable 120 } 121 return ""; 122 } 123 getFeedbackReporterPackage(Context context)124 public static String getFeedbackReporterPackage(Context context) { 125 final String feedbackReporter = 126 context.getResources().getString(R.string.oem_preferred_feedback_reporter); 127 if (TextUtils.isEmpty(feedbackReporter)) { 128 // Reporter not configured. Return. 129 return feedbackReporter; 130 } 131 // Additional checks to ensure the reporter is on system image, and reporter is 132 // configured to listen to the intent. Otherwise, dont show the "send feedback" option. 133 final Intent intent = new Intent(Intent.ACTION_BUG_REPORT); 134 135 PackageManager pm = context.getPackageManager(); 136 List<ResolveInfo> resolvedPackages = 137 pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER); 138 for (ResolveInfo info : resolvedPackages) { 139 if (info.activityInfo != null) { 140 if (!TextUtils.isEmpty(info.activityInfo.packageName)) { 141 try { 142 ApplicationInfo ai = 143 pm.getApplicationInfo(info.activityInfo.packageName, 0); 144 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 145 // Package is on the system image 146 if (TextUtils.equals( 147 info.activityInfo.packageName, feedbackReporter)) { 148 return feedbackReporter; 149 } 150 } 151 } catch (PackageManager.NameNotFoundException e) { 152 // No need to do anything here. 153 } 154 } 155 } 156 } 157 return null; 158 } 159 getSecurityPatch()160 public static String getSecurityPatch() { 161 String patch = Build.VERSION.SECURITY_PATCH; 162 if (!"".equals(patch)) { 163 try { 164 SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd"); 165 Date patchDate = template.parse(patch); 166 String format = DateFormat.getBestDateTimePattern(Locale.getDefault(), "dMMMMyyyy"); 167 patch = DateFormat.format(format, patchDate).toString(); 168 } catch (ParseException e) { 169 // broken parse; fall through and use the raw string 170 } 171 return patch; 172 } else { 173 return null; 174 } 175 } 176 getFormattedPhoneNumber(Context context, SubscriptionInfo subscriptionInfo)177 public static String getFormattedPhoneNumber(Context context, SubscriptionInfo subscriptionInfo) { 178 String formattedNumber = null; 179 if (subscriptionInfo != null) { 180 final TelephonyManager telephonyManager = 181 (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE); 182 final String rawNumber = 183 telephonyManager.getLine1Number(subscriptionInfo.getSubscriptionId()); 184 if (!TextUtils.isEmpty(rawNumber)) { 185 formattedNumber = PhoneNumberUtils.formatNumber(rawNumber); 186 } 187 188 } 189 return formattedNumber; 190 } 191 getFormattedPhoneNumbers(Context context, List<SubscriptionInfo> subscriptionInfo)192 public static String getFormattedPhoneNumbers(Context context, 193 List<SubscriptionInfo> subscriptionInfo) { 194 StringBuilder sb = new StringBuilder(); 195 if (subscriptionInfo != null) { 196 final TelephonyManager telephonyManager = 197 (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE); 198 final int count = subscriptionInfo.size(); 199 for (int i = 0; i < count; i++) { 200 final String rawNumber = telephonyManager.getLine1Number( 201 subscriptionInfo.get(i).getSubscriptionId()); 202 if (!TextUtils.isEmpty(rawNumber)) { 203 sb.append(PhoneNumberUtils.formatNumber(rawNumber)); 204 if (i < count - 1) { 205 sb.append("\n"); 206 } 207 } 208 } 209 } 210 return sb.toString(); 211 } 212 213 } 214