2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
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.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceViewHolder;
30 import android.util.Log;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.MenuItem.OnMenuItemClickListener;
35 import android.view.MotionEvent;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.view.View.OnTouchListener;
39 import android.widget.ImageView;
40 import android.widget.RadioButton;
41 import android.widget.Switch;
42 import android.widget.TextView;
44 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
45 import com.android.settings.widget.SwitchBar;
46 import com.android.settingslib.dream.DreamBackend;
47 import com.android.settingslib.dream.DreamBackend.DreamInfo;
49 import java.util.List;
51 public class DreamSettings extends SettingsPreferenceFragment implements
52 SwitchBar.OnSwitchChangeListener {
53 private static final String TAG = DreamSettings.class.getSimpleName();
54 static final boolean DEBUG = false;
55 private static final int DIALOG_WHEN_TO_DREAM = 1;
56 private static final String PACKAGE_SCHEME = "package";
58 private final PackageReceiver mPackageReceiver = new PackageReceiver();
60 private Context mContext;
61 private DreamBackend mBackend;
62 private SwitchBar mSwitchBar;
63 private MenuItem[] mMenuItemsWhenEnabled;
64 private boolean mRefreshing;
67 public int getHelpResource() {
68 return R.string.help_url_dreams;
72 public void onAttach(Activity activity) {
73 logd("onAttach(%s)", activity.getClass().getSimpleName());
74 super.onAttach(activity);
79 public int getMetricsCategory() {
80 return MetricsEvent.DREAM;
84 public void onCreate(Bundle icicle) {
85 logd("onCreate(%s)", icicle);
86 super.onCreate(icicle);
88 mBackend = new DreamBackend(getActivity());
90 setHasOptionsMenu(true);
94 public void onSwitchChanged(Switch switchView, boolean isChecked) {
96 mBackend.setEnabled(isChecked);
102 public void onStart() {
108 public void onDestroyView() {
109 logd("onDestroyView()");
110 super.onDestroyView();
112 mSwitchBar.removeOnSwitchChangeListener(this);
117 public void onActivityCreated(Bundle savedInstanceState) {
118 logd("onActivityCreated(%s)", savedInstanceState);
119 super.onActivityCreated(savedInstanceState);
121 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
122 emptyView.setText(R.string.screensaver_settings_disabled_prompt);
123 setEmptyView(emptyView);
125 final SettingsActivity sa = (SettingsActivity) getActivity();
126 mSwitchBar = sa.getSwitchBar();
127 mSwitchBar.addOnSwitchChangeListener(this);
132 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
133 logd("onCreateOptionsMenu()");
135 boolean isEnabled = mBackend.isEnabled();
137 // create "start" action
138 MenuItem start = createMenuItem(menu, R.string.screensaver_settings_dream_start,
139 MenuItem.SHOW_AS_ACTION_NEVER,
140 isEnabled, new Runnable(){
143 mBackend.startDreaming();
146 // create "when to dream" overflow menu item
147 MenuItem whenToDream = createMenuItem(menu,
148 R.string.screensaver_settings_when_to_dream,
149 MenuItem.SHOW_AS_ACTION_NEVER,
154 showDialog(DIALOG_WHEN_TO_DREAM);
157 // create "help" overflow menu item (make sure it appears last)
158 super.onCreateOptionsMenu(menu, inflater);
160 mMenuItemsWhenEnabled = new MenuItem[] { start, whenToDream };
163 private MenuItem createMenuItem(Menu menu,
164 int titleRes, int actionEnum, boolean isEnabled, final Runnable onClick) {
165 MenuItem item = menu.add(titleRes);
166 item.setShowAsAction(actionEnum);
167 item.setEnabled(isEnabled);
168 item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
170 public boolean onMenuItemClick(MenuItem item) {
179 public Dialog onCreateDialog(int dialogId) {
180 logd("onCreateDialog(%s)", dialogId);
181 if (dialogId == DIALOG_WHEN_TO_DREAM)
182 return createWhenToDreamDialog();
183 return super.onCreateDialog(dialogId);
187 public int getDialogMetricsCategory(int dialogId) {
188 if (dialogId == DIALOG_WHEN_TO_DREAM) {
189 return MetricsEvent.DIALOG_DREAM_START_DELAY;
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)
201 int initialSelection = mBackend.isActivatedOnDock() && mBackend.isActivatedOnSleep() ? 2
202 : mBackend.isActivatedOnDock() ? 0
203 : mBackend.isActivatedOnSleep() ? 1
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);
219 public void onPause() {
223 mContext.unregisterReceiver(mPackageReceiver);
227 public void onResume() {
230 refreshFromBackend();
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);
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
255 public static CharSequence getSummaryTextWithDreamName(Context context) {
256 DreamBackend backend = new DreamBackend(context);
257 boolean isEnabled = backend.isEnabled();
259 return context.getString(R.string.screensaver_settings_summary_off);
261 return backend.getActiveDreamName();
265 private void refreshFromBackend() {
266 logd("refreshFromBackend()");
268 boolean dreamsEnabled = mBackend.isEnabled();
269 if (mSwitchBar.isChecked() != dreamsEnabled) {
270 mSwitchBar.setChecked(dreamsEnabled);
273 if (getPreferenceScreen() == null) {
274 setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getContext()));
276 getPreferenceScreen().removeAll();
278 List<DreamBackend.DreamInfo> dreamInfos = mBackend.getDreamInfos();
279 final int N = dreamInfos.size();
280 for (int i = 0; i < N; i++) {
281 getPreferenceScreen().addPreference(
282 new DreamInfoPreference(getPrefContext(), dreamInfos.get(i)));
285 if (mMenuItemsWhenEnabled != null) {
286 for (MenuItem menuItem : mMenuItemsWhenEnabled) {
287 menuItem.setEnabled(dreamsEnabled);
293 private static void logd(String msg, Object... args) {
294 if (DEBUG) Log.d(TAG, args == null || args.length == 0 ? msg : String.format(msg, args));
297 private class DreamInfoPreference extends Preference {
299 private final DreamInfo mInfo;
301 public DreamInfoPreference(Context context, DreamInfo info) {
304 setLayoutResource(R.layout.dream_info_row);
305 setTitle(mInfo.caption);
309 public void onBindViewHolder(final PreferenceViewHolder holder) {
310 super.onBindViewHolder(holder);
313 RadioButton radioButton = (RadioButton) holder.findViewById(android.R.id.button1);
314 radioButton.setChecked(mInfo.isActive);
315 radioButton.setOnTouchListener(new OnTouchListener() {
317 public boolean onTouch(View v, MotionEvent event) {
318 holder.itemView.onTouchEvent(event);
323 // bind settings button + divider
324 boolean showSettings = mInfo.settingsComponentName != null;
325 View settingsDivider = holder.findViewById(R.id.divider);
326 settingsDivider.setVisibility(showSettings ? View.VISIBLE : View.INVISIBLE);
328 ImageView settingsButton = (ImageView) holder.findViewById(android.R.id.button2);
329 settingsButton.setVisibility(showSettings ? View.VISIBLE : View.INVISIBLE);
330 settingsButton.setAlpha(mInfo.isActive ? 1f : Utils.DISABLED_ALPHA);
331 settingsButton.setEnabled(mInfo.isActive);
332 settingsButton.setFocusable(mInfo.isActive);
333 settingsButton.setOnClickListener(new OnClickListener(){
335 public void onClick(View v) {
336 mBackend.launchSettings(mInfo);
342 public void performClick() {
345 for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
346 DreamInfoPreference preference =
347 (DreamInfoPreference) getPreferenceScreen().getPreference(i);
348 preference.mInfo.isActive = false;
349 preference.notifyChanged();
351 mInfo.isActive = true;
352 mBackend.setActiveDream(mInfo.componentName);
357 private class PackageReceiver extends BroadcastReceiver {
359 public void onReceive(Context context, Intent intent) {
360 logd("PackageReceiver.onReceive");
361 refreshFromBackend();