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