OSDN Git Service

2bd8610295f577e012c291e210cdbc13712201be
[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.ComponentName;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.os.SystemClock;
27 import android.os.SystemProperties;
28 import android.preference.Preference;
29 import android.preference.PreferenceGroup;
30 import android.preference.PreferenceScreen;
31 import android.provider.Settings;
32 import android.util.Log;
33
34 import java.io.BufferedReader;
35 import java.io.FileReader;
36 import java.io.IOException;
37 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40
41 public class DeviceInfoSettings extends SettingsPreferenceFragment {
42     private static final String TAG = "DeviceInfoSettings";
43
44     private static final String KEY_CONTAINER = "container";
45     private static final String KEY_TEAM = "team";
46     private static final String KEY_CONTRIBUTORS = "contributors";
47     private static final String KEY_TERMS = "terms";
48     private static final String KEY_LICENSE = "license";
49     private static final String KEY_COPYRIGHT = "copyright";
50     private static final String KEY_SYSTEM_UPDATE_SETTINGS = "system_update_settings";
51     private static final String PROPERTY_URL_SAFETYLEGAL = "ro.url.safetylegal";
52
53     long[] mHits = new long[3];
54
55     @Override
56     public void onCreate(Bundle icicle) {
57         super.onCreate(icicle);
58
59         addPreferencesFromResource(R.xml.device_info_settings);
60
61         // If we don't have an IME tutorial, remove that option
62         String currentIme = Settings.Secure.getString(getContentResolver(),
63                 Settings.Secure.DEFAULT_INPUT_METHOD);
64         ComponentName component = ComponentName.unflattenFromString(currentIme);
65         Intent imeIntent = new Intent(component.getPackageName() + ".tutorial");
66         PackageManager pm = getPackageManager();
67         List<ResolveInfo> tutorials = pm.queryIntentActivities(imeIntent, 0);
68         if(tutorials == null || tutorials.isEmpty()) {
69             getPreferenceScreen().removePreference(findPreference("system_tutorial"));
70         }
71
72         setStringSummary("firmware_version", Build.VERSION.RELEASE);
73         findPreference("firmware_version").setEnabled(true);
74         setValueSummary("baseband_version", "gsm.version.baseband");
75         setStringSummary("device_model", Build.MODEL);
76         setStringSummary("build_number", Build.DISPLAY);
77         findPreference("kernel_version").setSummary(getFormattedKernelVersion());
78
79         // Remove Safety information preference if PROPERTY_URL_SAFETYLEGAL is not set
80         removePreferenceIfPropertyMissing(getPreferenceScreen(), "safetylegal",
81                 PROPERTY_URL_SAFETYLEGAL);
82
83         /*
84          * Settings is a generic app and should not contain any device-specific
85          * info.
86          */
87         final Activity act = getActivity();
88         // These are contained in the "container" preference group
89         PreferenceGroup parentPreference = (PreferenceGroup) findPreference(KEY_CONTAINER);
90         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS,
91                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
92         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE,
93                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
94         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT,
95                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
96         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TEAM,
97                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
98
99         // These are contained by the root preference screen
100         parentPreference = getPreferenceScreen();
101         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference,
102                 KEY_SYSTEM_UPDATE_SETTINGS,
103                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
104         Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_CONTRIBUTORS,
105                 Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
106     }
107
108     @Override
109     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
110         if (preference.getKey().equals("firmware_version")) {
111             System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
112             mHits[mHits.length-1] = SystemClock.uptimeMillis();
113             if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
114                 Intent intent = new Intent(Intent.ACTION_MAIN);
115                 intent.setClassName("android",
116                         com.android.internal.app.PlatLogoActivity.class.getName());
117                 try {
118                     startActivity(intent);
119                 } catch (Exception e) {
120                 }
121             }
122         }
123         return super.onPreferenceTreeClick(preferenceScreen, preference);
124     }
125
126     private void removePreferenceIfPropertyMissing(PreferenceGroup preferenceGroup,
127             String preference, String property ) {
128         if (SystemProperties.get(property).equals(""))
129         {
130             // Property is missing so remove preference from group
131             try {
132                 preferenceGroup.removePreference(findPreference(preference));
133             } catch (RuntimeException e) {
134                 Log.d(TAG, "Property '" + property + "' missing and no '"
135                         + preference + "' preference");
136             }
137         }
138     }
139
140     private void setStringSummary(String preference, String value) {
141         try {
142             findPreference(preference).setSummary(value);
143         } catch (RuntimeException e) {
144             findPreference(preference).setSummary(
145                 getResources().getString(R.string.device_info_default));
146         }
147     }
148
149     private void setValueSummary(String preference, String property) {
150         try {
151             findPreference(preference).setSummary(
152                     SystemProperties.get(property,
153                             getResources().getString(R.string.device_info_default)));
154         } catch (RuntimeException e) {
155
156         }
157     }
158
159     private String getFormattedKernelVersion() {
160         String procVersionStr;
161
162         try {
163             BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
164             try {
165                 procVersionStr = reader.readLine();
166             } finally {
167                 reader.close();
168             }
169
170             final String PROC_VERSION_REGEX =
171                 "\\w+\\s+" + /* ignore: Linux */
172                 "\\w+\\s+" + /* ignore: version */
173                 "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
174                 "\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
175                 "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+" + /* ignore: (gcc ..) */
176                 "([^\\s]+)\\s+" + /* group 3: #26 */
177                 "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
178                 "(.+)"; /* group 4: date */
179
180             Pattern p = Pattern.compile(PROC_VERSION_REGEX);
181             Matcher m = p.matcher(procVersionStr);
182
183             if (!m.matches()) {
184                 Log.e(TAG, "Regex did not match on /proc/version: " + procVersionStr);
185                 return "Unavailable";
186             } else if (m.groupCount() < 4) {
187                 Log.e(TAG, "Regex match on /proc/version only returned " + m.groupCount()
188                         + " groups");
189                 return "Unavailable";
190             } else {
191                 return (new StringBuilder(m.group(1)).append("\n").append(
192                         m.group(2)).append(" ").append(m.group(3)).append("\n")
193                         .append(m.group(4))).toString();
194             }
195         } catch (IOException e) {
196             Log.e(TAG,
197                 "IO Exception when getting kernel version for Device Info screen",
198                 e);
199
200             return "Unavailable";
201         }
202     }
203
204 }