OSDN Git Service

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