OSDN Git Service

Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / deviceinfo / Status.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.deviceinfo;
18
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.BroadcastReceiver;
21 import android.content.ClipboardManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.res.Resources;
26 import android.net.ConnectivityManager;
27 import android.net.NetworkInfo;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.os.Handler;
33 import android.os.Message;
34 import android.os.SystemClock;
35 import android.os.SystemProperties;
36 import android.os.UserHandle;
37 import android.preference.Preference;
38 import android.preference.PreferenceActivity;
39 import android.text.TextUtils;
40 import android.view.View;
41 import android.widget.AdapterView;
42 import android.widget.ListAdapter;
43 import android.widget.Toast;
44
45 import com.android.internal.util.ArrayUtils;
46 import com.android.settings.R;
47 import com.android.settings.Utils;
48
49 import java.lang.ref.WeakReference;
50
51 /**
52  * Display the following information
53  * # Battery Strength  : TODO
54  * # Uptime
55  * # Awake Time
56  * # XMPP/buzz/tickle status : TODO
57  *
58  */
59 public class Status extends PreferenceActivity {
60
61     private static final String KEY_BATTERY_STATUS = "battery_status";
62     private static final String KEY_BATTERY_LEVEL = "battery_level";
63     private static final String KEY_IP_ADDRESS = "wifi_ip_address";
64     private static final String KEY_WIFI_MAC_ADDRESS = "wifi_mac_address";
65     private static final String KEY_BT_ADDRESS = "bt_address";
66     private static final String KEY_SERIAL_NUMBER = "serial_number";
67     private static final String KEY_WIMAX_MAC_ADDRESS = "wimax_mac_address";
68     private static final String KEY_SIM_STATUS = "sim_status";
69
70     // Broadcasts to listen to for connectivity changes.
71     private static final String[] CONNECTIVITY_INTENTS = {
72             BluetoothAdapter.ACTION_STATE_CHANGED,
73             ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE,
74             WifiManager.LINK_CONFIGURATION_CHANGED_ACTION,
75             WifiManager.NETWORK_STATE_CHANGED_ACTION,
76     };
77
78     private static final int EVENT_UPDATE_STATS = 500;
79
80     private static final int EVENT_UPDATE_CONNECTIVITY = 600;
81
82     private ConnectivityManager mCM;
83     private WifiManager mWifiManager;
84
85     private Resources mRes;
86
87     private String mUnknown;
88     private String mUnavailable;
89
90     private Preference mUptime;
91     private Preference mBatteryStatus;
92     private Preference mBatteryLevel;
93     private Preference mBtAddress;
94     private Preference mIpAddress;
95     private Preference mWifiMacAddress;
96     private Preference mWimaxMacAddress;
97
98     private Handler mHandler;
99
100     private static class MyHandler extends Handler {
101         private WeakReference<Status> mStatus;
102
103         public MyHandler(Status activity) {
104             mStatus = new WeakReference<Status>(activity);
105         }
106
107         @Override
108         public void handleMessage(Message msg) {
109             Status status = mStatus.get();
110             if (status == null) {
111                 return;
112             }
113
114             switch (msg.what) {
115                 case EVENT_UPDATE_STATS:
116                     status.updateTimes();
117                     sendEmptyMessageDelayed(EVENT_UPDATE_STATS, 1000);
118                     break;
119
120                 case EVENT_UPDATE_CONNECTIVITY:
121                     status.updateConnectivity();
122                     break;
123             }
124         }
125     }
126
127     private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
128
129         @Override
130         public void onReceive(Context context, Intent intent) {
131             String action = intent.getAction();
132             if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
133                 mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
134                 mBatteryStatus.setSummary(Utils.getBatteryStatus(getResources(), intent));
135             }
136         }
137     };
138
139     private IntentFilter mConnectivityIntentFilter;
140     private final BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() {
141         @Override
142         public void onReceive(Context context, Intent intent) {
143             String action = intent.getAction();
144             if (ArrayUtils.contains(CONNECTIVITY_INTENTS, action)) {
145                 mHandler.sendEmptyMessage(EVENT_UPDATE_CONNECTIVITY);
146             }
147         }
148     };
149
150     private boolean hasBluetooth() {
151         return BluetoothAdapter.getDefaultAdapter() != null;
152     }
153
154     private boolean hasWimax() {
155         return  mCM.getNetworkInfo(ConnectivityManager.TYPE_WIMAX) != null;
156     }
157
158     @Override
159     protected void onCreate(Bundle icicle) {
160         super.onCreate(icicle);
161
162         mHandler = new MyHandler(this);
163
164         mCM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
165         mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
166
167         addPreferencesFromResource(R.xml.device_info_status);
168         mBatteryLevel = findPreference(KEY_BATTERY_LEVEL);
169         mBatteryStatus = findPreference(KEY_BATTERY_STATUS);
170         mBtAddress = findPreference(KEY_BT_ADDRESS);
171         mWifiMacAddress = findPreference(KEY_WIFI_MAC_ADDRESS);
172         mWimaxMacAddress = findPreference(KEY_WIMAX_MAC_ADDRESS);
173         mIpAddress = findPreference(KEY_IP_ADDRESS);
174
175         mRes = getResources();
176         mUnknown = mRes.getString(R.string.device_info_default);
177         mUnavailable = mRes.getString(R.string.status_unavailable);
178
179         // Note - missing in zaku build, be careful later...
180         mUptime = findPreference("up_time");
181
182         if (!hasBluetooth()) {
183             getPreferenceScreen().removePreference(mBtAddress);
184             mBtAddress = null;
185         }
186
187         if (!hasWimax()) {
188             getPreferenceScreen().removePreference(mWimaxMacAddress);
189             mWimaxMacAddress = null;
190         }
191
192         mConnectivityIntentFilter = new IntentFilter();
193         for (String intent: CONNECTIVITY_INTENTS) {
194              mConnectivityIntentFilter.addAction(intent);
195         }
196
197         updateConnectivity();
198
199         String serial = Build.SERIAL;
200         if (serial != null && !serial.equals("")) {
201             setSummaryText(KEY_SERIAL_NUMBER, serial);
202         } else {
203             removePreferenceFromScreen(KEY_SERIAL_NUMBER);
204         }
205
206         // Make every pref on this screen copy its data to the clipboard on longpress.
207         // Super convenient for capturing the IMEI, MAC addr, serial, etc.
208         getListView().setOnItemLongClickListener(
209             new AdapterView.OnItemLongClickListener() {
210                 @Override
211                 public boolean onItemLongClick(AdapterView<?> parent, View view,
212                         int position, long id) {
213                     ListAdapter listAdapter = (ListAdapter) parent.getAdapter();
214                     Preference pref = (Preference) listAdapter.getItem(position);
215
216                     ClipboardManager cm = (ClipboardManager)
217                             getSystemService(Context.CLIPBOARD_SERVICE);
218                     cm.setText(pref.getSummary());
219                     Toast.makeText(
220                         Status.this,
221                         com.android.internal.R.string.text_copied,
222                         Toast.LENGTH_SHORT).show();
223                     return true;
224                 }
225             });
226     }
227
228     @Override
229     protected void onResume() {
230         super.onResume();
231         registerReceiver(mConnectivityReceiver, mConnectivityIntentFilter,
232                          android.Manifest.permission.CHANGE_NETWORK_STATE, null);
233         registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
234         mHandler.sendEmptyMessage(EVENT_UPDATE_STATS);
235     }
236
237     @Override
238     public void onPause() {
239         super.onPause();
240
241         unregisterReceiver(mBatteryInfoReceiver);
242         unregisterReceiver(mConnectivityReceiver);
243         mHandler.removeMessages(EVENT_UPDATE_STATS);
244     }
245
246     /**
247      * Removes the specified preference, if it exists.
248      * @param key the key for the Preference item
249      */
250     private void removePreferenceFromScreen(String key) {
251         Preference pref = findPreference(key);
252         if (pref != null) {
253             getPreferenceScreen().removePreference(pref);
254         }
255     }
256
257     /**
258      * @param preference The key for the Preference item
259      * @param property The system property to fetch
260      * @param alt The default value, if the property doesn't exist
261      */
262     private void setSummary(String preference, String property, String alt) {
263         try {
264             findPreference(preference).setSummary(
265                     SystemProperties.get(property, alt));
266         } catch (RuntimeException e) {
267
268         }
269     }
270
271     private void setSummaryText(String preference, String text) {
272             if (TextUtils.isEmpty(text)) {
273                text = mUnknown;
274              }
275              // some preferences may be missing
276              if (findPreference(preference) != null) {
277                  findPreference(preference).setSummary(text);
278              }
279     }
280
281     private void setWimaxStatus() {
282         if (mWimaxMacAddress != null) {
283             String macAddress = SystemProperties.get("net.wimax.mac.address", mUnavailable);
284             mWimaxMacAddress.setSummary(macAddress);
285         }
286     }
287
288     private void setWifiStatus() {
289         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
290         String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
291         mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
292     }
293
294     private void setIpAddressStatus() {
295         String ipAddress = Utils.getDefaultIpAddresses(this.mCM);
296         if (ipAddress != null) {
297             mIpAddress.setSummary(ipAddress);
298         } else {
299             mIpAddress.setSummary(mUnavailable);
300         }
301     }
302
303     private void setBtStatus() {
304         BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
305         if (bluetooth != null && mBtAddress != null) {
306             String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
307             if (!TextUtils.isEmpty(address)) {
308                // Convert the address to lowercase for consistency with the wifi MAC address.
309                 mBtAddress.setSummary(address.toLowerCase());
310             } else {
311                 mBtAddress.setSummary(mUnavailable);
312             }
313         }
314     }
315
316     void updateConnectivity() {
317         setWimaxStatus();
318         setWifiStatus();
319         setBtStatus();
320         setIpAddressStatus();
321     }
322
323     void updateTimes() {
324         long at = SystemClock.uptimeMillis() / 1000;
325         long ut = SystemClock.elapsedRealtime() / 1000;
326
327         if (ut == 0) {
328             ut = 1;
329         }
330
331         mUptime.setSummary(convert(ut));
332     }
333
334     private String pad(int n) {
335         if (n >= 10) {
336             return String.valueOf(n);
337         } else {
338             return "0" + String.valueOf(n);
339         }
340     }
341
342     private String convert(long t) {
343         int s = (int)(t % 60);
344         int m = (int)((t / 60) % 60);
345         int h = (int)((t / 3600));
346
347         return h + ":" + pad(m) + ":" + pad(s);
348     }
349 }