OSDN Git Service

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