Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2012 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.ActionBar;
     20 import android.app.Activity;
     21 import android.app.AlertDialog;
     22 import android.app.Dialog;
     23 import android.content.BroadcastReceiver;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.os.Bundle;
     29 import android.preference.PreferenceActivity;
     30 import android.util.Log;
     31 import android.view.Gravity;
     32 import android.view.LayoutInflater;
     33 import android.view.Menu;
     34 import android.view.MenuInflater;
     35 import android.view.MenuItem;
     36 import android.view.MenuItem.OnMenuItemClickListener;
     37 import android.view.MotionEvent;
     38 import android.view.View;
     39 import android.view.View.OnClickListener;
     40 import android.view.View.OnTouchListener;
     41 import android.view.ViewGroup;
     42 import android.widget.ArrayAdapter;
     43 import android.widget.CompoundButton;
     44 import android.widget.CompoundButton.OnCheckedChangeListener;
     45 import android.widget.ImageView;
     46 import android.widget.ListView;
     47 import android.widget.RadioButton;
     48 import android.widget.Switch;
     49 import android.widget.TextView;
     50 
     51 import com.android.settings.DreamBackend.DreamInfo;
     52 
     53 import java.util.List;
     54 
     55 public class DreamSettings extends SettingsPreferenceFragment {
     56     private static final String TAG = DreamSettings.class.getSimpleName();
     57     static final boolean DEBUG = false;
     58     private static final int DIALOG_WHEN_TO_DREAM = 1;
     59     private static final String PACKAGE_SCHEME = "package";
     60 
     61     private final PackageReceiver mPackageReceiver = new PackageReceiver();
     62 
     63     private Context mContext;
     64     private DreamBackend mBackend;
     65     private DreamInfoAdapter mAdapter;
     66     private Switch mSwitch;
     67     private MenuItem[] mMenuItemsWhenEnabled;
     68     private boolean mRefreshing;
     69 
     70     @Override
     71     public int getHelpResource() {
     72         return R.string.help_url_dreams;
     73     }
     74 
     75     @Override
     76     public void onAttach(Activity activity) {
     77         logd("onAttach(%s)", activity.getClass().getSimpleName());
     78         super.onAttach(activity);
     79         mContext = activity;
     80     }
     81 
     82     @Override
     83     public void onCreate(Bundle icicle) {
     84         logd("onCreate(%s)", icicle);
     85         super.onCreate(icicle);
     86         Activity activity = getActivity();
     87 
     88         mBackend = new DreamBackend(activity);
     89         mSwitch = new Switch(activity);
     90         mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     91             @Override
     92             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     93                 if (!mRefreshing) {
     94                     mBackend.setEnabled(isChecked);
     95                     refreshFromBackend();
     96                 }
     97             }
     98         });
     99 
    100         final int padding = activity.getResources().getDimensionPixelSize(
    101                 R.dimen.action_bar_switch_padding);
    102         mSwitch.setPadding(0, 0, padding, 0);
    103         activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
    104                 ActionBar.DISPLAY_SHOW_CUSTOM);
    105         activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
    106                 ActionBar.LayoutParams.WRAP_CONTENT,
    107                 ActionBar.LayoutParams.WRAP_CONTENT,
    108                 Gravity.CENTER_VERTICAL | Gravity.END));
    109 
    110         setHasOptionsMenu(true);
    111     }
    112 
    113     @Override
    114     public void onDestroyView() {
    115         getActivity().getActionBar().setCustomView(null);
    116         super.onDestroyView();
    117     }
    118 
    119     @Override
    120     public void onActivityCreated(Bundle savedInstanceState) {
    121         logd("onActivityCreated(%s)", savedInstanceState);
    122         super.onActivityCreated(savedInstanceState);
    123 
    124         ListView listView = getListView();
    125 
    126         TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
    127         emptyView.setText(R.string.screensaver_settings_disabled_prompt);
    128         listView.setEmptyView(emptyView);
    129 
    130         mAdapter = new DreamInfoAdapter(mContext);
    131         listView.setAdapter(mAdapter);
    132     }
    133 
    134     @Override
    135     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    136         logd("onCreateOptionsMenu()");
    137 
    138         boolean isEnabled = mBackend.isEnabled();
    139 
    140         // create "start" action
    141         MenuItem start = createMenuItem(menu, R.string.screensaver_settings_dream_start,
    142                 MenuItem.SHOW_AS_ACTION_ALWAYS,
    143                 isEnabled, new Runnable(){
    144                     @Override
    145                     public void run() {
    146                         mBackend.startDreaming();
    147                     }});
    148 
    149         // create "when to dream" overflow menu item
    150         MenuItem whenToDream = createMenuItem(menu,
    151                 R.string.screensaver_settings_when_to_dream,
    152                 MenuItem.SHOW_AS_ACTION_IF_ROOM,
    153                 isEnabled,
    154                 new Runnable() {
    155                     @Override
    156                     public void run() {
    157                         showDialog(DIALOG_WHEN_TO_DREAM);
    158                     }});
    159 
    160         // create "help" overflow menu item (make sure it appears last)
    161         super.onCreateOptionsMenu(menu, inflater);
    162 
    163         mMenuItemsWhenEnabled = new MenuItem[] { start, whenToDream };
    164     }
    165 
    166     private MenuItem createMenuItem(Menu menu,
    167             int titleRes, int actionEnum, boolean isEnabled, final Runnable onClick) {
    168         MenuItem item = menu.add(titleRes);
    169         item.setShowAsAction(actionEnum);
    170         item.setEnabled(isEnabled);
    171         item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
    172             @Override
    173             public boolean onMenuItemClick(MenuItem item) {
    174                 onClick.run();
    175                 return true;
    176             }
    177         });
    178         return item;
    179     }
    180 
    181     @Override
    182     public Dialog onCreateDialog(int dialogId) {
    183         logd("onCreateDialog(%s)", dialogId);
    184         if (dialogId == DIALOG_WHEN_TO_DREAM)
    185             return createWhenToDreamDialog();
    186         return super.onCreateDialog(dialogId);
    187     }
    188 
    189     private Dialog createWhenToDreamDialog() {
    190         final CharSequence[] items = {
    191                 mContext.getString(R.string.screensaver_settings_summary_dock),
    192                 mContext.getString(R.string.screensaver_settings_summary_sleep),
    193                 mContext.getString(R.string.screensaver_settings_summary_either_short)
    194         };
    195 
    196         int initialSelection = mBackend.isActivatedOnDock() && mBackend.isActivatedOnSleep() ? 2
    197                 : mBackend.isActivatedOnDock() ? 0
    198                 : mBackend.isActivatedOnSleep() ? 1
    199                 : -1;
    200 
    201         return new AlertDialog.Builder(mContext)
    202                 .setTitle(R.string.screensaver_settings_when_to_dream)
    203                 .setSingleChoiceItems(items, initialSelection, new DialogInterface.OnClickListener() {
    204                     public void onClick(DialogInterface dialog, int item) {
    205                         mBackend.setActivatedOnDock(item == 0 || item == 2);
    206                         mBackend.setActivatedOnSleep(item == 1 || item == 2);
    207                     }
    208                 })
    209                 .create();
    210     }
    211 
    212     @Override
    213     public void onPause() {
    214         logd("onPause()");
    215         super.onPause();
    216         mContext.unregisterReceiver(mPackageReceiver);
    217     }
    218 
    219     @Override
    220     public void onResume() {
    221         logd("onResume()");
    222         super.onResume();
    223         refreshFromBackend();
    224 
    225         // listen for package changes
    226         IntentFilter filter = new IntentFilter();
    227         filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    228         filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    229         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    230         filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    231         filter.addDataScheme(PACKAGE_SCHEME);
    232         mContext.registerReceiver(mPackageReceiver , filter);
    233     }
    234 
    235     public static int getSummaryResource(Context context) {
    236         DreamBackend backend = new DreamBackend(context);
    237         boolean isEnabled = backend.isEnabled();
    238         boolean activatedOnSleep = backend.isActivatedOnSleep();
    239         boolean activatedOnDock = backend.isActivatedOnDock();
    240         boolean activatedOnEither = activatedOnSleep && activatedOnDock;
    241         return !isEnabled ? R.string.screensaver_settings_summary_off
    242                 : activatedOnEither ? R.string.screensaver_settings_summary_either_long
    243                 : activatedOnSleep ? R.string.screensaver_settings_summary_sleep
    244                 : activatedOnDock ? R.string.screensaver_settings_summary_dock
    245                 : 0;
    246     }
    247 
    248     public static CharSequence getSummaryTextWithDreamName(Context context) {
    249         DreamBackend backend = new DreamBackend(context);
    250         boolean isEnabled = backend.isEnabled();
    251         if (!isEnabled) {
    252             return context.getString(R.string.screensaver_settings_summary_off);
    253         } else {
    254             return backend.getActiveDreamName();
    255         }
    256     }
    257 
    258     private void refreshFromBackend() {
    259         logd("refreshFromBackend()");
    260         mRefreshing = true;
    261         boolean dreamsEnabled = mBackend.isEnabled();
    262         if (mSwitch.isChecked() != dreamsEnabled)
    263             mSwitch.setChecked(dreamsEnabled);
    264 
    265         mAdapter.clear();
    266         if (dreamsEnabled) {
    267             List<DreamInfo> dreamInfos = mBackend.getDreamInfos();
    268             mAdapter.addAll(dreamInfos);
    269         }
    270         if (mMenuItemsWhenEnabled != null)
    271             for (MenuItem menuItem : mMenuItemsWhenEnabled)
    272                 menuItem.setEnabled(dreamsEnabled);
    273         mRefreshing = false;
    274     }
    275 
    276     private static void logd(String msg, Object... args) {
    277         if (DEBUG)
    278             Log.d(TAG, args == null || args.length == 0 ? msg : String.format(msg, args));
    279     }
    280 
    281     private class DreamInfoAdapter extends ArrayAdapter<DreamInfo> {
    282         private final LayoutInflater mInflater;
    283 
    284         public DreamInfoAdapter(Context context) {
    285             super(context, 0);
    286             mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    287         }
    288 
    289         @Override
    290         public View getView(int position, View convertView, ViewGroup parent) {
    291             DreamInfo dreamInfo = getItem(position);
    292             logd("getView(%s)", dreamInfo.caption);
    293             final View row = convertView != null ? convertView : createDreamInfoRow(parent);
    294             row.setTag(dreamInfo);
    295 
    296             // bind icon
    297             ((ImageView) row.findViewById(android.R.id.icon)).setImageDrawable(dreamInfo.icon);
    298 
    299             // bind caption
    300             ((TextView) row.findViewById(android.R.id.title)).setText(dreamInfo.caption);
    301 
    302             // bind radio button
    303             RadioButton radioButton = (RadioButton) row.findViewById(android.R.id.button1);
    304             radioButton.setChecked(dreamInfo.isActive);
    305             radioButton.setOnTouchListener(new OnTouchListener() {
    306                 @Override
    307                 public boolean onTouch(View v, MotionEvent event) {
    308                     row.onTouchEvent(event);
    309                     return false;
    310                 }});
    311 
    312             // bind settings button + divider
    313             boolean showSettings = dreamInfo.settingsComponentName != null;
    314             View settingsDivider = row.findViewById(R.id.divider);
    315             settingsDivider.setVisibility(showSettings ? View.VISIBLE : View.INVISIBLE);
    316 
    317             ImageView settingsButton = (ImageView) row.findViewById(android.R.id.button2);
    318             settingsButton.setVisibility(showSettings ? View.VISIBLE : View.INVISIBLE);
    319             settingsButton.setAlpha(dreamInfo.isActive ? 1f : 0.33f);
    320             settingsButton.setEnabled(dreamInfo.isActive);
    321             settingsButton.setOnClickListener(new OnClickListener(){
    322                 @Override
    323                 public void onClick(View v) {
    324                     mBackend.launchSettings((DreamInfo) row.getTag());
    325                 }});
    326 
    327             return row;
    328         }
    329 
    330         private View createDreamInfoRow(ViewGroup parent) {
    331             final View row =  mInflater.inflate(R.layout.dream_info_row, parent, false);
    332             row.setOnClickListener(new OnClickListener(){
    333                 @Override
    334                 public void onClick(View v) {
    335                     v.setPressed(true);
    336                     activate((DreamInfo) row.getTag());
    337                 }});
    338             return row;
    339         }
    340 
    341         private DreamInfo getCurrentSelection() {
    342             for (int i = 0; i < getCount(); i++) {
    343                 DreamInfo dreamInfo = getItem(i);
    344                 if (dreamInfo.isActive)
    345                     return dreamInfo;
    346             }
    347             return null;
    348         }
    349         private void activate(DreamInfo dreamInfo) {
    350             if (dreamInfo.equals(getCurrentSelection()))
    351                 return;
    352             for (int i = 0; i < getCount(); i++) {
    353                 getItem(i).isActive = false;
    354             }
    355             dreamInfo.isActive = true;
    356             mBackend.setActiveDream(dreamInfo.componentName);
    357             notifyDataSetChanged();
    358         }
    359     }
    360 
    361     private class PackageReceiver extends BroadcastReceiver {
    362         @Override
    363         public void onReceive(Context context, Intent intent) {
    364             logd("PackageReceiver.onReceive");
    365             refreshFromBackend();
    366         }
    367     }
    368 }
    369