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