OSDN Git Service

am 4fedc0fd: (-s ours) Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DeviceInfoSettings.java
1 /*
2  * Copyright (C) 2008 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.settings;
18
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Binder;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.os.Parcel;
29 import android.os.RemoteException;
30 import android.os.SELinux;
31 import android.os.SystemClock;
32 import android.os.SystemProperties;
33 import android.os.UserHandle;
34 import android.os.UserManager;
35 import android.preference.Preference;
36 import android.preference.PreferenceGroup;
37 import android.preference.PreferenceScreen;
38 import android.provider.SearchIndexableResource;
39 import android.provider.Settings;
40 import android.text.TextUtils;
41 import android.util.Log;
42 import android.widget.Toast;
43 import com.android.settings.search.BaseSearchIndexProvider;
44 import com.android.settings.search.Index;
45 import com.android.settings.search.Indexable;
46
47 import java.io.BufferedReader;
48 import java.io.FileReader;
49 import java.io.IOException;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.List;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55
56 public class DeviceInfoSettings extends SettingsPreferenceFragment implements Indexable {
57
58     private static final String LOG_TAG = "DeviceInfoSettings";
59     private static final String FILENAME_PROC_VERSION = "/proc/version";
60     private static final String FILENAME_MSV = "/sys/board_properties/soc/msv";
61
62     private static final String KEY_CONTAINER = "container";
63     private static final String KEY_REGULATORY_INFO = "regulatory_info";
64     private static final String KEY_TERMS = "terms";
65     private static final String KEY_LICENSE = "license";
66     private static final String KEY_COPYRIGHT = "copyright";
67     private static final String KEY_WEBVIEW_LICENSE = "webview_license";
68     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
69     private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
70     private static final String PROPERTY_SELINUX_STATUS = "ro.build.selinux";
71     private static final String KEY_KERNEL_VERSION = "kernel_version";
72     private static final String KEY_BUILD_NUMBER = "build_number";
73     private static final String KEY_DEVICE_MODEL = "device_model";
74     private static final String KEY_SELINUX_STATUS = "selinux_status";
75     private static final String KEY_BASEBAND_VERSION = "baseband_version";
76     private static final String KEY_FIRMWARE_VERSION = "firmware_version";
77     private static final String KEY_SECURITY_PATCH = "security_patch";
78     private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
79     private static final String KEY_EQUIPMENT_ID = "fcc_equipment_id";
80     private static final String PROPERTY_EQUIPMENT_ID = "ro.ril.fccid";
81     private static final String KEY_DEVICE_FEEDBACK = "device_feedback";
82     private static final String KEY_SAFETY_LEGAL = "safetylegal";
83
84     static final int TAPS_TO_BE_A_DEVELOPER = 7;
85
86     long[] mHits = new long[3];
87     int mDevHitCountdown;
88     Toast mDevHitToast;
89
90     @Override
91     public void onCreate(Bundle icicle) {
92         super.onCreate(icicle);
93
94         addPreferencesFromResource(R.xml.device_info_settings);
95
96         setStringSummary(KEY_FIRMWARE_VERSION, Build.VERSION.RELEASE);
97         findPreference(KEY_FIRMWARE_VERSION).setEnabled(true);
98         String patch = Build.VERSION.SECURITY_PATCH;
99         if (!"".equals(patch)) {
100             setStringSummary(KEY_SECURITY_PATCH, patch);
101         } else {
102             getPreferenceScreen().removePreference(findPreference(KEY_SECURITY_PATCH));
103
104         }
105         setValueSummary(KEY_BASEBAND_VERSION, "gsm.version.baseband");
106         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL + getMsvSuffix());
107         setValueSummary(KEY_EQUIPMENT_ID, PROPERTY_EQUIPMENT_ID);
108         setStringSummary(KEY_DEVICE_MODEL, Build.MODEL);
109         setStringSummary(KEY_BUILD_NUMBER, Build.DISPLAY);
110         findPreference(KEY_BUILD_NUMBER).setEnabled(true);
111         findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());
112
113         if (!SELinux.isSELinuxEnabled()) {
114             String status = getResources().getString(R.string.selinux_status_disabled);
115             setStringSummary(KEY_SELINUX_STATUS, status);
116         } else if (!SELinux.isSELinuxEnforced()) {
117             String status = getResources().getString(R.string.selinux_status_permissive);
118             setStringSummary(KEY_SELINUX_STATUS, status);
119         }
120
121         // Remove selinux information if property is not present
122         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SELINUX_STATUS,
123                 PROPERTY_SELINUX_STATUS);
124
125         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
126         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_SAFETY_LEGAL,
127                 PROPERTY_URL_SAFETYLEGAL);
128
129         // Remove Equipment id preference if FCC ID is not set by RIL
130         removePreferenceIfPropertyMissing(getPreferenceScreen(), KEY_EQUIPMENT_ID,
131                 PROPERTY_EQUIPMENT_ID);
132
133         // Remove Baseband version if wifi-only device
134         if (Utils.isWifiOnly(getActivity())) {
135             getPreferenceScreen().removePreference(findPreference(KEY_BASEBAND_VERSION));
136         }
137
138         // Dont show feedback option if there is no reporter.
139         if (TextUtils.isEmpty(getFeedbackReporterPackage(getActivity()))) {
140             getPreferenceScreen().removePreference(findPreference(KEY_DEVICE_FEEDBACK));
141         }
142
143         /*
144          * Settings is a generic app and should not contain any device-specific
145          * info.
146          */
147         final Activity act = getActivity();
148         // These are contained in the "container" preference group
149         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
150         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
151                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
152         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
153                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
154         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
155                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
156         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_WEBVIEW_LICENSE,
157                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
158
159         // These are contained by the root preference screen
160         parentPreference = getPreferenceScreen();
161         if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
162             Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
163                     KEY_SYSTEM_UPDATE_SETTINGS,
164                     Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
165         } else {
166             // Remove for secondary users
167             removePreference(KEY_SYSTEM_UPDATE_SETTINGS);
168         }
169
170         // Read platform settings for additional system update setting
171         removePreferenceIfBoolFalse(KEY_UPDATE_SETTING,
172                 R.bool.config_additional_system_update_setting_enable);
173
174         // Remove regulatory information if none present.
175         final Intent intent = new Intent(Settings.ACTION_SHOW_REGULATORY_INFO);
176         if (getPackageManager().queryIntentActivities(intent, 0).isEmpty()) {
177             Preference pref = findPreference(KEY_REGULATORY_INFO);
178             if (pref != null) {
179                 getPreferenceScreen().removePreference(pref);
180             }
181         }
182     }
183
184     @Override
185     public void onResume() {
186         super.onResume();
187         mDevHitCountdown = getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
188                 Context.MODE_PRIVATE).getBoolean(DevelopmentSettings.PREF_SHOW,
189                         android.os.Build.TYPE.equals("eng")) ? -1 : TAPS_TO_BE_A_DEVELOPER;
190         mDevHitToast = null;
191     }
192
193     @Override
194     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
195         if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
196             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
197             mHits[mHits.length-1] = SystemClock.uptimeMillis();
198             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
199                 Intent intent = new Intent(Intent.ACTION_MAIN);
200                 intent.setClassName("android",
201                         com.android.internal.app.PlatLogoActivity.class.getName());
202                 try {
203                     startActivity(intent);
204                 } catch (Exception e) {
205                     Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
206                 }
207             }
208         } else if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
209             // Don't enable developer options for secondary users.
210             if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;
211
212             final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
213             if (um.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)) return true;
214
215             if (mDevHitCountdown > 0) {
216                 mDevHitCountdown--;
217                 if (mDevHitCountdown == 0) {
218                     getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
219                             Context.MODE_PRIVATE).edit().putBoolean(
220                                     DevelopmentSettings.PREF_SHOW, true).apply();
221                     if (mDevHitToast != null) {
222                         mDevHitToast.cancel();
223                     }
224                     mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
225                             Toast.LENGTH_LONG);
226                     mDevHitToast.show();
227                     // This is good time to index the Developer Options
228                     Index.getInstance(
229                             getActivity().getApplicationContext()).updateFromClassNameResource(
230                                     DevelopmentSettings.class.getName(), true, true);
231
232                 } else if (mDevHitCountdown > 0
233                         && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
234                     if (mDevHitToast != null) {
235                         mDevHitToast.cancel();
236                     }
237                     mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
238                             R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
239                             Toast.LENGTH_SHORT);
240                     mDevHitToast.show();
241                 }
242             } else if (mDevHitCountdown < 0) {
243                 if (mDevHitToast != null) {
244                     mDevHitToast.cancel();
245                 }
246                 mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
247                         Toast.LENGTH_LONG);
248                 mDevHitToast.show();
249             }
250         } else if (preference.getKey().equals(KEY_DEVICE_FEEDBACK)) {
251             sendFeedback();
252         }
253         return super.onPreferenceTreeClick(preferenceScreen, preference);
254     }
255
256     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
257             String preference, String property ) {
258         if (SystemProperties.get(property).equals("")) {
259             // Property is missing so remove preference from group
260             try {
261                 preferenceGroup.removePreference(findPreference(preference));
262             } catch (RuntimeException e) {
263                 Log.d(LOG_TAG, "Property '" + property + "' missing and no '"
264                         + preference + "' preference");
265             }
266         }
267     }
268
269     private void removePreferenceIfBoolFalse(String preference, int resId) {
270         if (!getResources().getBoolean(resId)) {
271             Preference pref = findPreference(preference);
272             if (pref != null) {
273                 getPreferenceScreen().removePreference(pref);
274             }
275         }
276     }
277
278     private void setStringSummary(String preference, String value) {
279         try {
280             findPreference(preference).setSummary(value);
281         } catch (RuntimeException e) {
282             findPreference(preference).setSummary(
283                 getResources().getString(R.string.device_info_default));
284         }
285     }
286
287     private void setValueSummary(String preference, String property) {
288         try {
289             findPreference(preference).setSummary(
290                     SystemProperties.get(property,
291                             getResources().getString(R.string.device_info_default)));
292         } catch (RuntimeException e) {
293             // No recovery
294         }
295     }
296
297     private void sendFeedback() {
298         String reporterPackage = getFeedbackReporterPackage(getActivity());
299         if (TextUtils.isEmpty(reporterPackage)) {
300             return;
301         }
302         Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
303         intent.setPackage(reporterPackage);
304         startActivityForResult(intent, 0);
305     }
306
307     /**
308      * Reads a line from the specified file.
309      * @param filename the file to read from
310      * @return the first line, if any.
311      * @throws IOException if the file couldn't be read
312      */
313     private static String readLine(String filename) throws IOException {
314         BufferedReader reader = new BufferedReader(new FileReader(filename), 256);
315         try {
316             return reader.readLine();
317         } finally {
318             reader.close();
319         }
320     }
321
322     public static String getFormattedKernelVersion() {
323         try {
324             return formatKernelVersion(readLine(FILENAME_PROC_VERSION));
325
326         } catch (IOException e) {
327             Log.e(LOG_TAG,
328                 "IO Exception when getting kernel version for Device Info screen",
329                 e);
330
331             return "Unavailable";
332         }
333     }
334
335     public static String formatKernelVersion(String rawKernelVersion) {
336         // Example (see tests for more):
337         // Linux version 3.0.31-g6fb96c9 (android-build@xxx.xxx.xxx.xxx.com) \
338         //     (gcc version 4.6.x-xxx 20120106 (prerelease) (GCC) ) #1 SMP PREEMPT \
339         //     Thu Jun 28 11:02:39 PDT 2012
340
341         final String PROC_VERSION_REGEX =
342             "Linux version (\\S+) " + /* group 1: "3.0.31-g6fb96c9" */
343             "\\((\\S+?)\\) " +        /* group 2: "x@y.com" (kernel builder) */
344             "(?:\\(gcc.+? \\)) " +    /* ignore: GCC version information */
345             "(#\\d+) " +              /* group 3: "#1" */
346             "(?:.*?)?" +              /* ignore: optional SMP, PREEMPT, and any CONFIG_FLAGS */
347             "((Sun|Mon|Tue|Wed|Thu|Fri|Sat).+)"; /* group 4: "Thu Jun 28 11:02:39 PDT 2012" */
348
349         Matcher m = Pattern.compile(PROC_VERSION_REGEX).matcher(rawKernelVersion);
350         if (!m.matches()) {
351             Log.e(LOG_TAG, "Regex did not match on /proc/version: " + rawKernelVersion);
352             return "Unavailable";
353         } else if (m.groupCount() < 4) {
354             Log.e(LOG_TAG, "Regex match on /proc/version only returned " + m.groupCount()
355                     + " groups");
356             return "Unavailable";
357         }
358         return m.group(1) + "\n" +                 // 3.0.31-g6fb96c9
359             m.group(2) + " " + m.group(3) + "\n" + // x@y.com #1
360             m.group(4);                            // Thu Jun 28 11:02:39 PDT 2012
361     }
362
363     /**
364      * Returns " (ENGINEERING)" if the msv file has a zero value, else returns "".
365      * @return a string to append to the model number description.
366      */
367     private String getMsvSuffix() {
368         // Production devices should have a non-zero value. If we can't read it, assume it's a
369         // production device so that we don't accidentally show that it's an ENGINEERING device.
370         try {
371             String msv = readLine(FILENAME_MSV);
372             // Parse as a hex number. If it evaluates to a zero, then it's an engineering build.
373             if (Long.parseLong(msv, 16) == 0) {
374                 return " (ENGINEERING)";
375             }
376         } catch (IOException ioe) {
377             // Fail quietly, as the file may not exist on some devices.
378         } catch (NumberFormatException nfe) {
379             // Fail quietly, returning empty string should be sufficient
380         }
381         return "";
382     }
383
384     private static String getFeedbackReporterPackage(Context context) {
385         final String feedbackReporter =
386                 context.getResources().getString(R.string.oem_preferred_feedback_reporter);
387         if (TextUtils.isEmpty(feedbackReporter)) {
388             // Reporter not configured. Return.
389             return feedbackReporter;
390         }
391         // Additional checks to ensure the reporter is on system image, and reporter is
392         // configured to listen to the intent. Otherwise, dont show the "send feedback" option.
393         final Intent intent = new Intent(Intent.ACTION_BUG_REPORT);
394
395         PackageManager pm = context.getPackageManager();
396         List<ResolveInfo> resolvedPackages =
397                 pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
398         for (ResolveInfo info : resolvedPackages) {
399             if (info.activityInfo != null) {
400                 if (!TextUtils.isEmpty(info.activityInfo.packageName)) {
401                     try {
402                         ApplicationInfo ai = pm.getApplicationInfo(info.activityInfo.packageName, 0);
403                         if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
404                             // Package is on the system image
405                             if (TextUtils.equals(
406                                         info.activityInfo.packageName, feedbackReporter)) {
407                                 return feedbackReporter;
408                             }
409                         }
410                     } catch (PackageManager.NameNotFoundException e) {
411                          // No need to do anything here.
412                     }
413                 }
414             }
415         }
416         return null;
417     }
418
419     /**
420      * For Search.
421      */
422     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
423         new BaseSearchIndexProvider() {
424
425             @Override
426             public List<SearchIndexableResource> getXmlResourcesToIndex(
427                     Context context, boolean enabled) {
428                 final SearchIndexableResource sir = new SearchIndexableResource(context);
429                 sir.xmlResId = R.xml.device_info_settings;
430                 return Arrays.asList(sir);
431             }
432
433             @Override
434             public List<String> getNonIndexableKeys(Context context) {
435                 final List<String> keys = new ArrayList<String>();
436                 if (isPropertyMissing(PROPERTY_SELINUX_STATUS)) {
437                     keys.add(KEY_SELINUX_STATUS);
438                 }
439                 if (isPropertyMissing(PROPERTY_URL_SAFETYLEGAL)) {
440                     keys.add(KEY_SAFETY_LEGAL);
441                 }
442                 if (isPropertyMissing(PROPERTY_EQUIPMENT_ID)) {
443                     keys.add(KEY_EQUIPMENT_ID);
444                 }
445                 // Remove Baseband version if wifi-only device
446                 if (Utils.isWifiOnly(context)) {
447                     keys.add((KEY_BASEBAND_VERSION));
448                 }
449                 // Dont show feedback option if there is no reporter.
450                 if (TextUtils.isEmpty(getFeedbackReporterPackage(context))) {
451                     keys.add(KEY_DEVICE_FEEDBACK);
452                 }
453                 if (!checkIntentAction(context, "android.settings.TERMS")) {
454                     keys.add(KEY_TERMS);
455                 }
456                 if (!checkIntentAction(context, "android.settings.LICENSE")) {
457                     keys.add(KEY_LICENSE);
458                 }
459                 if (!checkIntentAction(context, "android.settings.COPYRIGHT")) {
460                     keys.add(KEY_COPYRIGHT);
461                 }
462                 if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) {
463                     keys.add(KEY_WEBVIEW_LICENSE);
464                 }
465                 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
466                     keys.add(KEY_SYSTEM_UPDATE_SETTINGS);
467                 }
468                 if (!context.getResources().getBoolean(
469                         R.bool.config_additional_system_update_setting_enable)) {
470                     keys.add(KEY_UPDATE_SETTING);
471                 }
472                 return keys;
473             }
474
475             private boolean isPropertyMissing(String property) {
476                 return SystemProperties.get(property).equals("");
477             }
478
479             private boolean checkIntentAction(Context context, String action) {
480                 final Intent intent = new Intent(action);
481
482                 // Find the activity that is in the system image
483                 final PackageManager pm = context.getPackageManager();
484                 final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
485                 final int listSize = list.size();
486
487                 for (int i = 0; i < listSize; i++) {
488                     ResolveInfo resolveInfo = list.get(i);
489                     if ((resolveInfo.activityInfo.applicationInfo.flags &
490                             ApplicationInfo.FLAG_SYSTEM) != 0) {
491                         return true;
492                     }
493                 }
494
495                 return false;
496             }
497         };
498
499 }
500