OSDN Git Service

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