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