OSDN Git Service

am 25bd33e1: am d7b2dccb: Display IMEI and ICCID for LTE device.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / Settings.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.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ActivityInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.os.Bundle;
26 import android.preference.PreferenceActivity;
27 import android.text.TextUtils;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.View.OnClickListener;
32 import android.view.ViewGroup;
33 import android.widget.ArrayAdapter;
34 import android.widget.Button;
35 import android.widget.ImageView;
36 import android.widget.ListAdapter;
37 import android.widget.Switch;
38 import android.widget.TextView;
39
40 import com.android.settings.bluetooth.BluetoothEnabler;
41 import com.android.settings.wifi.WifiEnabler;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46
47 /**
48  * Top-level settings activity to handle single pane and double pane UI layout.
49  */
50 public class Settings extends PreferenceActivity implements ButtonBarHandler {
51
52     private static final String LOG_TAG = "Settings";
53     private static final String META_DATA_KEY_HEADER_ID =
54         "com.android.settings.TOP_LEVEL_HEADER_ID";
55     private static final String META_DATA_KEY_FRAGMENT_CLASS =
56         "com.android.settings.FRAGMENT_CLASS";
57     private static final String META_DATA_KEY_PARENT_TITLE =
58         "com.android.settings.PARENT_FRAGMENT_TITLE";
59     private static final String META_DATA_KEY_PARENT_FRAGMENT_CLASS =
60         "com.android.settings.PARENT_FRAGMENT_CLASS";
61
62     private static final String EXTRA_THEME = "settings:theme";
63
64     private static final String SAVE_KEY_CURRENT_HEADER = "com.android.settings.CURRENT_HEADER";
65     private static final String SAVE_KEY_PARENT_HEADER = "com.android.settings.PARENT_HEADER";
66
67     private String mFragmentClass;
68     private int mTopLevelHeaderId;
69     private Header mFirstHeader;
70     private Header mCurrentHeader;
71     private Header mParentHeader;
72     private boolean mInLocalHeaderSwitch;
73
74     // TODO: Update Call Settings based on airplane mode state.
75
76     protected HashMap<Integer, Integer> mHeaderIndexMap = new HashMap<Integer, Integer>();
77     private List<Header> mHeaders;
78
79     @Override
80     protected void onCreate(Bundle savedInstanceState) {
81         final int theme = getIntent().getIntExtra(
82                 EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar_SplitActionBarWhenNarrow);
83         setTheme(theme);
84
85         getMetaData();
86         mInLocalHeaderSwitch = true;
87         super.onCreate(savedInstanceState);
88         mInLocalHeaderSwitch = false;
89
90         if (!onIsHidingHeaders() && onIsMultiPane()) {
91             highlightHeader();
92             // Force the title so that it doesn't get overridden by a direct launch of
93             // a specific settings screen.
94             setTitle(R.string.settings_label);
95         }
96
97         // Retrieve any saved state
98         if (savedInstanceState != null) {
99             mCurrentHeader = savedInstanceState.getParcelable(SAVE_KEY_CURRENT_HEADER);
100             mParentHeader = savedInstanceState.getParcelable(SAVE_KEY_PARENT_HEADER);
101         }
102
103         // If the current header was saved, switch to it
104         if (savedInstanceState != null && mCurrentHeader != null) {
105             //switchToHeaderLocal(mCurrentHeader);
106             showBreadCrumbs(mCurrentHeader.title, null);
107         }
108
109         if (mParentHeader != null) {
110             setParentTitle(mParentHeader.title, null, new OnClickListener() {
111                 public void onClick(View v) {
112                     switchToParent(mParentHeader.fragment);
113                 }
114             });
115         }
116
117         // TODO Add support for android.R.id.home in all Setting's onOptionsItemSelected
118         // getActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
119         // ActionBar.DISPLAY_HOME_AS_UP);
120     }
121
122     @Override
123     protected void onSaveInstanceState(Bundle outState) {
124         super.onSaveInstanceState(outState);
125
126         // Save the current fragment, if it is the same as originally launched
127         if (mCurrentHeader != null) {
128             outState.putParcelable(SAVE_KEY_CURRENT_HEADER, mCurrentHeader);
129         }
130         if (mParentHeader != null) {
131             outState.putParcelable(SAVE_KEY_PARENT_HEADER, mParentHeader);
132         }
133     }
134
135     @Override
136     public void onResume() {
137         super.onResume();
138
139         ListAdapter listAdapter = getListAdapter();
140         if (listAdapter instanceof HeaderAdapter) {
141             ((HeaderAdapter) listAdapter).resume();
142         }
143     }
144
145     @Override
146     public void onPause() {
147         super.onPause();
148
149         ListAdapter listAdapter = getListAdapter();
150         if (listAdapter instanceof HeaderAdapter) {
151             ((HeaderAdapter) listAdapter).pause();
152         }
153     }
154
155     private void switchToHeaderLocal(Header header) {
156         mInLocalHeaderSwitch = true;
157         switchToHeader(header);
158         mInLocalHeaderSwitch = false;
159     }
160
161     @Override
162     public void switchToHeader(Header header) {
163         if (!mInLocalHeaderSwitch) {
164             mCurrentHeader = null;
165             mParentHeader = null;
166         }
167         super.switchToHeader(header);
168     }
169
170     /**
171      * Switch to parent fragment and store the grand parent's info
172      * @param className name of the activity wrapper for the parent fragment.
173      */
174     private void switchToParent(String className) {
175         final ComponentName cn = new ComponentName(this, className);
176         try {
177             final PackageManager pm = getPackageManager();
178             final ActivityInfo parentInfo = pm.getActivityInfo(cn, PackageManager.GET_META_DATA);
179
180             if (parentInfo != null && parentInfo.metaData != null) {
181                 String fragmentClass = parentInfo.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
182                 CharSequence fragmentTitle = parentInfo.loadLabel(pm);
183                 Header parentHeader = new Header();
184                 parentHeader.fragment = fragmentClass;
185                 parentHeader.title = fragmentTitle;
186                 mCurrentHeader = parentHeader;
187
188                 switchToHeaderLocal(parentHeader);
189
190                 mParentHeader = new Header();
191                 mParentHeader.fragment
192                         = parentInfo.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
193                 mParentHeader.title = parentInfo.metaData.getString(META_DATA_KEY_PARENT_TITLE);
194             }
195         } catch (NameNotFoundException nnfe) {
196             Log.w(LOG_TAG, "Could not find parent activity : " + className);
197         }
198     }
199
200     @Override
201     public void onNewIntent(Intent intent) {
202         super.onNewIntent(intent);
203
204         // If it is not launched from history, then reset to top-level
205         if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
206                 && mFirstHeader != null && !onIsHidingHeaders() && onIsMultiPane()) {
207             switchToHeaderLocal(mFirstHeader);
208         }
209     }
210
211     private void highlightHeader() {
212         if (mTopLevelHeaderId != 0) {
213             Integer index = mHeaderIndexMap.get(mTopLevelHeaderId);
214             if (index != null) {
215                 getListView().setItemChecked(index, true);
216             }
217         }
218     }
219
220     @Override
221     public Intent getIntent() {
222         Intent superIntent = super.getIntent();
223         String startingFragment = getStartingFragmentClass(superIntent);
224         // This is called from super.onCreate, isMultiPane() is not yet reliable
225         // Do not use onIsHidingHeaders either, which relies itself on this method
226         if (startingFragment != null && !onIsMultiPane()) {
227             Intent modIntent = new Intent(superIntent);
228             modIntent.putExtra(EXTRA_SHOW_FRAGMENT, startingFragment);
229             Bundle args = superIntent.getExtras();
230             if (args != null) {
231                 args = new Bundle(args);
232             } else {
233                 args = new Bundle();
234             }
235             args.putParcelable("intent", superIntent);
236             modIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, superIntent.getExtras());
237             return modIntent;
238         }
239         return superIntent;
240     }
241
242     /**
243      * Checks if the component name in the intent is different from the Settings class and
244      * returns the class name to load as a fragment.
245      */
246     protected String getStartingFragmentClass(Intent intent) {
247         if (mFragmentClass != null) return mFragmentClass;
248
249         String intentClass = intent.getComponent().getClassName();
250         if (intentClass.equals(getClass().getName())) return null;
251
252         if ("com.android.settings.ManageApplications".equals(intentClass)
253                 || "com.android.settings.RunningServices".equals(intentClass)
254                 || "com.android.settings.applications.StorageUse".equals(intentClass)) {
255             // Old names of manage apps.
256             intentClass = com.android.settings.applications.ManageApplications.class.getName();
257         }
258
259         return intentClass;
260     }
261
262     /**
263      * Override initial header when an activity-alias is causing Settings to be launched
264      * for a specific fragment encoded in the android:name parameter.
265      */
266     @Override
267     public Header onGetInitialHeader() {
268         String fragmentClass = getStartingFragmentClass(super.getIntent());
269         if (fragmentClass != null) {
270             Header header = new Header();
271             header.fragment = fragmentClass;
272             header.title = getTitle();
273             header.fragmentArguments = getIntent().getExtras();
274             mCurrentHeader = header;
275             return header;
276         }
277
278         return mFirstHeader;
279     }
280
281     @Override
282     public Intent onBuildStartFragmentIntent(String fragmentName, Bundle args,
283             int titleRes, int shortTitleRes) {
284         Intent intent = super.onBuildStartFragmentIntent(fragmentName, args,
285                 titleRes, shortTitleRes);
286
287         // some fragments would like a custom activity theme
288         if (DataUsageSummary.class.getName().equals(fragmentName)) {
289             intent.putExtra(EXTRA_THEME, android.R.style.Theme_Holo_SolidActionBar);
290         }
291
292         intent.setClass(this, SubSettings.class);
293         return intent;
294     }
295     
296     /**
297      * Populate the activity with the top-level headers.
298      */
299     @Override
300     public void onBuildHeaders(List<Header> headers) {
301         loadHeadersFromResource(R.xml.settings_headers, headers);
302
303         updateHeaderList(headers);
304
305         mHeaders = headers;
306     }
307
308     private void updateHeaderList(List<Header> target) {
309         int i = 0;
310         while (i < target.size()) {
311             Header header = target.get(i);
312             // Ids are integers, so downcasting
313             int id = (int) header.id;
314             if (id == R.id.dock_settings) {
315                 if (!needsDockSettings())
316                     target.remove(header);
317             } else if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {
318                 Utils.updateHeaderToSpecificActivityFromMetaDataOrRemove(this, target, header);
319             } else if (id == R.id.wifi_settings) {
320                 // Remove WiFi Settings if WiFi service is not available.
321                 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
322                     target.remove(header);
323                 }
324             } else if (id == R.id.bluetooth_settings) {
325                 // Remove Bluetooth Settings if Bluetooth service is not available.
326                 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
327                     target.remove(header);
328                 }
329             }
330
331             // Increment if the current one wasn't removed by the Utils code.
332             if (target.get(i) == header) {
333                 // Hold on to the first header, when we need to reset to the top-level
334                 if (mFirstHeader == null &&
335                         HeaderAdapter.getHeaderType(header) != HeaderAdapter.HEADER_TYPE_CATEGORY) {
336                     mFirstHeader = header;
337                 }
338                 mHeaderIndexMap.put(id, i);
339                 i++;
340             }
341         }
342     }
343
344     private boolean needsDockSettings() {
345         return getResources().getBoolean(R.bool.has_dock_settings);
346     }
347
348     private void getMetaData() {
349         try {
350             ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(),
351                     PackageManager.GET_META_DATA);
352             if (ai == null || ai.metaData == null) return;
353             mTopLevelHeaderId = ai.metaData.getInt(META_DATA_KEY_HEADER_ID);
354             mFragmentClass = ai.metaData.getString(META_DATA_KEY_FRAGMENT_CLASS);
355             
356             // Check if it has a parent specified and create a Header object
357             final int parentHeaderTitleRes = ai.metaData.getInt(META_DATA_KEY_PARENT_TITLE);
358             String parentFragmentClass = ai.metaData.getString(META_DATA_KEY_PARENT_FRAGMENT_CLASS);
359             if (parentFragmentClass != null) {
360                 mParentHeader = new Header();
361                 mParentHeader.fragment = parentFragmentClass;
362                 if (parentHeaderTitleRes != 0) {
363                     mParentHeader.title = getResources().getString(parentHeaderTitleRes);
364                 }
365             }
366         } catch (NameNotFoundException nnfe) {
367             // No recovery
368         }
369     }
370
371     @Override
372     public boolean hasNextButton() {
373         return super.hasNextButton();
374     }
375
376     @Override
377     public Button getNextButton() {
378         return super.getNextButton();
379     }
380
381     private static class HeaderAdapter extends ArrayAdapter<Header> {
382         static final int HEADER_TYPE_CATEGORY = 0;
383         static final int HEADER_TYPE_NORMAL = 1;
384         static final int HEADER_TYPE_SWITCH = 2;
385         private static final int HEADER_TYPE_COUNT = HEADER_TYPE_SWITCH + 1;
386
387         private final WifiEnabler mWifiEnabler;
388         private final BluetoothEnabler mBluetoothEnabler;
389
390         private static class HeaderViewHolder {
391             ImageView icon;
392             TextView title;
393             TextView summary;
394             Switch switch_;
395         }
396
397         private LayoutInflater mInflater;
398
399         static int getHeaderType(Header header) {
400             if (header.fragment == null && header.intent == null) {
401                 return HEADER_TYPE_CATEGORY;
402             } else if (header.id == R.id.wifi_settings || header.id == R.id.bluetooth_settings) {
403                 return HEADER_TYPE_SWITCH;
404             } else {
405                 return HEADER_TYPE_NORMAL;
406             }
407         }
408
409         @Override
410         public int getItemViewType(int position) {
411             Header header = getItem(position);
412             return getHeaderType(header);
413         }
414
415         @Override
416         public boolean areAllItemsEnabled() {
417             return false; // because of categories
418         }
419
420         @Override
421         public boolean isEnabled(int position) {
422             return getItemViewType(position) != HEADER_TYPE_CATEGORY;
423         }
424
425         @Override
426         public int getViewTypeCount() {
427             return HEADER_TYPE_COUNT;
428         }
429
430         @Override
431         public boolean hasStableIds() {
432             return true;
433         }
434
435         public HeaderAdapter(Context context, List<Header> objects) {
436             super(context, 0, objects);
437             mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
438             
439             // Temp Switches provided as placeholder until the adapter replaces these with actual
440             // Switches inflated from their layouts. Must be done before adapter is set in super
441             mWifiEnabler = new WifiEnabler(context, new Switch(context));
442             mBluetoothEnabler = new BluetoothEnabler(context, new Switch(context));
443         }
444
445         @Override
446         public View getView(int position, View convertView, ViewGroup parent) {
447             HeaderViewHolder holder;
448             Header header = getItem(position);
449             int headerType = getHeaderType(header);
450             View view = null;
451
452             if (convertView == null) {
453                 holder = new HeaderViewHolder();
454                 switch (headerType) {
455                     case HEADER_TYPE_CATEGORY:
456                         view = new TextView(getContext(), null,
457                                 android.R.attr.listSeparatorTextViewStyle);
458                         holder.title = (TextView) view;
459                         break;
460
461                     case HEADER_TYPE_SWITCH:
462                         view = mInflater.inflate(R.layout.preference_header_switch_item, parent,
463                                 false);
464                         holder.icon = (ImageView) view.findViewById(R.id.icon);
465                         holder.title = (TextView)
466                                 view.findViewById(com.android.internal.R.id.title);
467                         holder.summary = (TextView)
468                                 view.findViewById(com.android.internal.R.id.summary);
469                         holder.switch_ = (Switch) view.findViewById(R.id.switchWidget);
470                         break;
471
472                     case HEADER_TYPE_NORMAL:
473                         view = mInflater.inflate(
474                                 com.android.internal.R.layout.preference_header_item, parent,
475                                 false);
476                         holder.icon = (ImageView) view.findViewById(com.android.internal.R.id.icon);
477                         holder.title = (TextView)
478                                 view.findViewById(com.android.internal.R.id.title);
479                         holder.summary = (TextView)
480                                 view.findViewById(com.android.internal.R.id.summary);
481                         break;
482                 }
483                 view.setTag(holder);
484             } else {
485                 view = convertView;
486                 holder = (HeaderViewHolder) view.getTag();
487             }
488
489             // All view fields must be updated every time, because the view may be recycled
490             switch (headerType) {
491                 case HEADER_TYPE_CATEGORY:
492                     holder.title.setText(header.getTitle(getContext().getResources()));
493                     break;
494
495                 case HEADER_TYPE_SWITCH:
496                     // Would need a different treatment if the main menu had more switches
497                     if (header.id == R.id.wifi_settings) {
498                         mWifiEnabler.setSwitch(holder.switch_);
499                     } else {
500                         mBluetoothEnabler.setSwitch(holder.switch_);
501                     }
502                     // No break, fall through on purpose to update common fields
503
504                     //$FALL-THROUGH$
505                 case HEADER_TYPE_NORMAL:
506                     holder.icon.setImageResource(header.iconRes);
507                     holder.title.setText(header.getTitle(getContext().getResources()));
508                     CharSequence summary = header.getSummary(getContext().getResources());
509                     if (!TextUtils.isEmpty(summary)) {
510                         holder.summary.setVisibility(View.VISIBLE);
511                         holder.summary.setText(summary);
512                     } else {
513                         holder.summary.setVisibility(View.GONE);
514                     }
515                     break;
516             }
517
518             return view;
519         }
520
521         public void resume() {
522             mWifiEnabler.resume();
523             mBluetoothEnabler.resume();
524         }
525         
526         public void pause() {
527             mWifiEnabler.pause();
528             mBluetoothEnabler.pause();
529         }
530     }
531
532     @Override
533     public void setListAdapter(ListAdapter adapter) {
534         if (mHeaders == null) {
535             mHeaders = new ArrayList<Header>();
536             // When the saved state provides the list of headers, onBuildHeaders is not called
537             // Copy the list of Headers from the adapter, preserving their order
538             for (int i = 0; i < adapter.getCount(); i++) {
539                 mHeaders.add((Header) adapter.getItem(i));
540             }
541         }
542
543         // Ignore the adapter provided by PreferenceActivity and substitute ours instead
544         super.setListAdapter(new HeaderAdapter(this, mHeaders));
545     }
546
547     /*
548      * Settings subclasses for launching independently.
549      */
550     public static class BluetoothSettingsActivity extends Settings { /* empty */ }
551     public static class WirelessSettingsActivity extends Settings { /* empty */ }
552     public static class TetherSettingsActivity extends Settings { /* empty */ }
553     public static class VpnSettingsActivity extends Settings { /* empty */ }
554     public static class DateTimeSettingsActivity extends Settings { /* empty */ }
555     public static class StorageSettingsActivity extends Settings { /* empty */ }
556     public static class WifiSettingsActivity extends Settings { /* empty */ }
557     public static class InputMethodAndLanguageSettingsActivity extends Settings { /* empty */ }
558     public static class InputMethodAndSubtypeEnablerActivity extends Settings { /* empty */ }
559     public static class LocalePickerActivity extends Settings { /* empty */ }
560     public static class UserDictionarySettingsActivity extends Settings { /* empty */ }
561     public static class SoundSettingsActivity extends Settings { /* empty */ }
562     public static class DisplaySettingsActivity extends Settings { /* empty */ }
563     public static class DeviceInfoSettingsActivity extends Settings { /* empty */ }
564     public static class ApplicationSettingsActivity extends Settings { /* empty */ }
565     public static class ManageApplicationsActivity extends Settings { /* empty */ }
566     public static class StorageUseActivity extends Settings { /* empty */ }
567     public static class DevelopmentSettingsActivity extends Settings { /* empty */ }
568     public static class AccessibilitySettingsActivity extends Settings { /* empty */ }
569     public static class SecuritySettingsActivity extends Settings { /* empty */ }
570     public static class LocationSettingsActivity extends Settings { /* empty */ }
571     public static class PrivacySettingsActivity extends Settings { /* empty */ }
572     public static class DockSettingsActivity extends Settings { /* empty */ }
573     public static class RunningServicesActivity extends Settings { /* empty */ }
574     public static class ManageAccountsSettingsActivity extends Settings { /* empty */ }
575     public static class PowerUsageSummaryActivity extends Settings { /* empty */ }
576     public static class AccountSyncSettingsActivity extends Settings { /* empty */ }
577     public static class AccountSyncSettingsInAddAccountActivity extends Settings { /* empty */ }
578     public static class CryptKeeperSettingsActivity extends Settings { /* empty */ }
579     public static class DeviceAdminSettingsActivity extends Settings { /* empty */ }
580     public static class DataUsageSummaryActivity extends Settings { /* empty */ }
581     public static class AdvancedWifiSettingsActivity extends Settings { /* empty */ }
582     public static class AdvancedBluetoothSettingsActivity extends Settings { /* empty */ }
583     public static class TextToSpeechSettingsActivity extends Settings { /* empty */ }
584 }