diff options
author | Michael Wright <michaelwr@google.com> | 2013-02-05 16:29:39 -0800 |
---|---|---|
committer | Michael Wright <michaelwr@google.com> | 2013-02-20 14:39:03 -0800 |
commit | 0087a14d4b4bcfe57c6f6e36c70eec966088d7bb (patch) | |
tree | 7d084053ff0a15bb4d29af8f04bd2aae7e11445e /packages/SystemUI/src/com/android/systemui/settings | |
parent | 81aaf87d097aae2f0a5f8bd7286f82a4d0658b77 (diff) | |
download | frameworks_base-0087a14d4b4bcfe57c6f6e36c70eec966088d7bb.zip frameworks_base-0087a14d4b4bcfe57c6f6e36c70eec966088d7bb.tar.gz frameworks_base-0087a14d4b4bcfe57c6f6e36c70eec966088d7bb.tar.bz2 |
Add brightness dialog to SystemUI
Change-Id: If31406c9144bb2583876f08dd54b259d1dfa3601
Diffstat (limited to 'packages/SystemUI/src/com/android/systemui/settings')
5 files changed, 639 insertions, 0 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java new file mode 100644 index 0000000..fdeead1 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.settings; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.IPowerManager; +import android.os.PowerManager; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.os.UserHandle; +import android.provider.Settings; +import android.provider.Settings.SettingNotFoundException; +import android.util.Slog; +import android.view.IWindowManager; +import android.widget.CompoundButton; +import android.widget.ImageView; + +import java.util.ArrayList; + +public class BrightnessController implements ToggleSlider.Listener { + private static final String TAG = "StatusBar.BrightnessController"; + + private final int mMinimumBacklight; + private final int mMaximumBacklight; + + private final Context mContext; + private final ImageView mIcon; + private final ToggleSlider mControl; + private final boolean mAutomaticAvailable; + private final IPowerManager mPower; + private final CurrentUserTracker mUserTracker; + private final Handler mHandler; + private final BrightnessObserver mBrightnessObserver; + + private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = + new ArrayList<BrightnessStateChangeCallback>(); + + public interface BrightnessStateChangeCallback { + public void onBrightnessLevelChanged(); + } + + /** ContentObserver to watch brightness **/ + private class BrightnessObserver extends ContentObserver { + + private final Uri BRIGHTNESS_MODE_URI = + Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE); + private final Uri BRIGHTNESS_URI = + Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); + + public BrightnessObserver(Handler handler) { + super(handler); + } + + @Override + public void onChange(boolean selfChange) { + onChange(selfChange, null); + } + + @Override + public void onChange(boolean selfChange, Uri uri) { + if (selfChange) return; + if (BRIGHTNESS_MODE_URI.equals(uri)) { + updateMode(); + } else if (BRIGHTNESS_URI.equals(uri)) { + updateSlider(); + } else { + updateMode(); + updateSlider(); + } + for (BrightnessStateChangeCallback cb : mChangeCallbacks) { + cb.onBrightnessLevelChanged(); + } + } + + public void startObserving() { + final ContentResolver cr = mContext.getContentResolver(); + cr.unregisterContentObserver(this); + cr.registerContentObserver( + BRIGHTNESS_MODE_URI, + false, this, UserHandle.USER_ALL); + cr.registerContentObserver( + BRIGHTNESS_URI, + false, this, UserHandle.USER_ALL); + } + + public void stopObserving() { + final ContentResolver cr = mContext.getContentResolver(); + cr.unregisterContentObserver(this); + } + + } + + public BrightnessController(Context context, ImageView icon, ToggleSlider control) { + mContext = context; + mIcon = icon; + mControl = control; + mHandler = new Handler(); + mUserTracker = new CurrentUserTracker(mContext) { + @Override + public void onUserSwitched(int newUserId) { + updateMode(); + updateSlider(); + } + }; + mBrightnessObserver = new BrightnessObserver(mHandler); + mBrightnessObserver.startObserving(); + + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + mMinimumBacklight = pm.getMinimumScreenBrightnessSetting(); + mMaximumBacklight = pm.getMaximumScreenBrightnessSetting(); + + mAutomaticAvailable = context.getResources().getBoolean( + com.android.internal.R.bool.config_automatic_brightness_available); + mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); + + // Update the slider and mode before attaching the listener so we don't receive the + // onChanged notifications for the initial values. + updateMode(); + updateSlider(); + + control.setOnChangedListener(this); + } + + public void addStateChangedCallback(BrightnessStateChangeCallback cb) { + mChangeCallbacks.add(cb); + } + + public boolean removeStateChangedCallback(BrightnessStateChangeCallback cb) { + return mChangeCallbacks.remove(cb); + } + + @Override + public void onInit(ToggleSlider control) { + // Do nothing + } + + /** Unregister all call backs, both to and from the controller */ + public void unregisterCallbacks() { + mBrightnessObserver.stopObserving(); + mChangeCallbacks.clear(); + mUserTracker.stopTracking(); + } + + public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { + setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC + : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); + updateIcon(automatic); + if (!automatic) { + final int val = value + mMinimumBacklight; + setBrightness(val); + if (!tracking) { + AsyncTask.execute(new Runnable() { + public void run() { + Settings.System.putIntForUser(mContext.getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS, val, + UserHandle.USER_CURRENT); + } + }); + } + } + + for (BrightnessStateChangeCallback cb : mChangeCallbacks) { + cb.onBrightnessLevelChanged(); + } + } + + private void setMode(int mode) { + Settings.System.putIntForUser(mContext.getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS_MODE, mode, + mUserTracker.getCurrentUserId()); + } + + private void setBrightness(int brightness) { + try { + mPower.setTemporaryScreenBrightnessSettingOverride(brightness); + } catch (RemoteException ex) { + } + } + + private void updateIcon(boolean automatic) { + if (mIcon != null) { + mIcon.setImageResource(automatic ? + com.android.systemui.R.drawable.ic_qs_brightness_auto_on : + com.android.systemui.R.drawable.ic_qs_brightness_auto_off); + } + } + + /** Fetch the brightness mode from the system settings and update the icon */ + private void updateMode() { + if (mAutomaticAvailable) { + int automatic; + try { + automatic = Settings.System.getIntForUser(mContext.getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS_MODE, + UserHandle.USER_CURRENT); + } catch (SettingNotFoundException snfe) { + automatic = 0; + } + mControl.setChecked(automatic != 0); + updateIcon(automatic != 0); + } else { + mControl.setChecked(false); + updateIcon(false /*automatic*/); + } + } + + /** Fetch the brightness from the system settings and update the slider */ + private void updateSlider() { + int value; + try { + value = Settings.System.getIntForUser(mContext.getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS, + UserHandle.USER_CURRENT); + } catch (SettingNotFoundException ex) { + value = mMaximumBacklight; + } + mControl.setMax(mMaximumBacklight - mMinimumBacklight); + mControl.setValue(value - mMinimumBacklight); + } + +} diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java new file mode 100644 index 0000000..1b05084 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.settings; + +import com.android.systemui.R; + +import android.app.Dialog; +import android.content.Context; +import android.content.res.Resources; +import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.view.Window; +import android.view.WindowManager; +import android.widget.ImageView; + +import java.lang.Runnable; + +/** A dialog that provides controls for adjusting the screen brightness. */ +public class BrightnessDialog extends Dialog implements + BrightnessController.BrightnessStateChangeCallback { + + private static final String TAG = "BrightnessDialog"; + private static final boolean DEBUG = false; + + protected Handler mHandler = new Handler(); + + private BrightnessController mBrightnessController; + private final int mBrightnessDialogLongTimeout; + private final int mBrightnessDialogShortTimeout; + + private final Runnable mDismissDialogRunnable = new Runnable() { + public void run() { + if (BrightnessDialog.this.isShowing()) { + BrightnessDialog.this.dismiss(); + } + }; + }; + + + public BrightnessDialog(Context ctx) { + super(ctx); + Resources r = ctx.getResources(); + mBrightnessDialogLongTimeout = + r.getInteger(R.integer.quick_settings_brightness_dialog_long_timeout); + mBrightnessDialogShortTimeout = + r.getInteger(R.integer.quick_settings_brightness_dialog_short_timeout); + } + + + /** + * Create the brightness dialog and any resources that are used for the + * entire lifetime of the dialog. + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Window window = getWindow(); + window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); + window.getAttributes().privateFlags |= + WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; + window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); + window.requestFeature(Window.FEATURE_NO_TITLE); + + setContentView(R.layout.quick_settings_brightness_dialog); + setCanceledOnTouchOutside(true); + } + + + @Override + protected void onStart() { + super.onStart(); + mBrightnessController = new BrightnessController(getContext(), + (ImageView) findViewById(R.id.brightness_icon), + (ToggleSlider) findViewById(R.id.brightness_slider)); + dismissBrightnessDialog(mBrightnessDialogLongTimeout); + mBrightnessController.addStateChangedCallback(this); + } + + @Override + protected void onStop() { + super.onStop(); + mBrightnessController.unregisterCallbacks(); + removeAllBrightnessDialogCallbacks(); + } + + public void onBrightnessLevelChanged() { + dismissBrightnessDialog(mBrightnessDialogShortTimeout); + } + + private void dismissBrightnessDialog(int timeout) { + removeAllBrightnessDialogCallbacks(); + mHandler.postDelayed(mDismissDialogRunnable, timeout); + } + + private void removeAllBrightnessDialogCallbacks() { + mHandler.removeCallbacks(mDismissDialogRunnable); + } + +} diff --git a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java new file mode 100644 index 0000000..122f81e --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.settings; + +import android.app.ActivityManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +public abstract class CurrentUserTracker extends BroadcastReceiver { + + private Context mContext; + private int mCurrentUserId; + + public CurrentUserTracker(Context context) { + IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED); + context.registerReceiver(this, filter); + mCurrentUserId = ActivityManager.getCurrentUser(); + mContext = context; + } + + public int getCurrentUserId() { + return mCurrentUserId; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { + int oldUserId = mCurrentUserId; + mCurrentUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); + if (oldUserId != mCurrentUserId) { + onUserSwitched(mCurrentUserId); + } + } + } + + public void stopTracking() { + mContext.unregisterReceiver(this); + } + + public abstract void onUserSwitched(int newUserId); +} diff --git a/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java b/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java new file mode 100644 index 0000000..f65123a --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/settings/SettingsUI.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.settings; + +import java.io.FileDescriptor; +import java.io.PrintWriter; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Handler; +import android.util.Slog; + +import com.android.systemui.SystemUI; + +public class SettingsUI extends SystemUI { + private static final String TAG = "SettingsUI"; + private static final boolean DEBUG = false; + + private final Handler mHandler = new Handler(); + private BrightnessDialog mBrightnessDialog; + + private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) { + if (DEBUG) Slog.d(TAG, "showing brightness dialog"); + + if (mBrightnessDialog == null) { + mBrightnessDialog = new BrightnessDialog(mContext); + mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + mBrightnessDialog = null; + } + }); + } + + if (!mBrightnessDialog.isShowing()) { + mBrightnessDialog.show(); + } + + } else { + Slog.w(TAG, "unknown intent: " + intent); + } + } + }; + + public void start() { + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG); + mContext.registerReceiver(mIntentReceiver, filter, null, mHandler); + } + + public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + pw.print("mBrightnessDialog="); + pw.println(mBrightnessDialog == null ? "null" : mBrightnessDialog.toString()); + } +} diff --git a/packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java b/packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java new file mode 100644 index 0000000..c7c361c --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/settings/ToggleSlider.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.settings; + +import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.util.Slog; +import android.view.View; +import android.widget.CompoundButton; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.SeekBar; +import android.widget.TextView; + +import com.android.systemui.R; + +public class ToggleSlider extends RelativeLayout + implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener { + private static final String TAG = "StatusBar.ToggleSlider"; + + public interface Listener { + public void onInit(ToggleSlider v); + public void onChanged(ToggleSlider v, boolean tracking, boolean checked, int value); + } + + private Listener mListener; + private boolean mTracking; + + private CompoundButton mToggle; + private SeekBar mSlider; + private TextView mLabel; + + public ToggleSlider(Context context) { + this(context, null); + } + + public ToggleSlider(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public ToggleSlider(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + View.inflate(context, R.layout.status_bar_toggle_slider, this); + + final Resources res = context.getResources(); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleSlider, + defStyle, 0); + + mToggle = (CompoundButton)findViewById(R.id.toggle); + mToggle.setOnCheckedChangeListener(this); + mToggle.setBackgroundDrawable(res.getDrawable(R.drawable.status_bar_toggle_button)); + + mSlider = (SeekBar)findViewById(R.id.slider); + mSlider.setOnSeekBarChangeListener(this); + + mLabel = (TextView)findViewById(R.id.label); + mLabel.setText(a.getString(R.styleable.ToggleSlider_text)); + + a.recycle(); + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (mListener != null) { + mListener.onInit(this); + } + } + + public void onCheckedChanged(CompoundButton toggle, boolean checked) { + Drawable thumb; + Drawable slider; + final Resources res = getContext().getResources(); + if (checked) { + thumb = res.getDrawable( + com.android.internal.R.drawable.scrubber_control_disabled_holo); + slider = res.getDrawable( + R.drawable.status_bar_settings_slider_disabled); + } else { + thumb = res.getDrawable( + com.android.internal.R.drawable.scrubber_control_selector_holo); + slider = res.getDrawable( + com.android.internal.R.drawable.scrubber_progress_horizontal_holo_dark); + } + mSlider.setThumb(thumb); + mSlider.setProgressDrawable(slider); + + if (mListener != null) { + mListener.onChanged(this, mTracking, checked, mSlider.getProgress()); + } + } + + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (mListener != null) { + mListener.onChanged(this, mTracking, mToggle.isChecked(), progress); + } + } + + public void onStartTrackingTouch(SeekBar seekBar) { + mTracking = true; + if (mListener != null) { + mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress()); + } + mToggle.setChecked(false); + } + + public void onStopTrackingTouch(SeekBar seekBar) { + mTracking = false; + if (mListener != null) { + mListener.onChanged(this, mTracking, mToggle.isChecked(), mSlider.getProgress()); + } + } + + public void setOnChangedListener(Listener l) { + mListener = l; + } + + public void setChecked(boolean checked) { + mToggle.setChecked(checked); + } + + public boolean isChecked() { + return mToggle.isChecked(); + } + + public void setMax(int max) { + mSlider.setMax(max); + } + + public void setValue(int value) { + mSlider.setProgress(value); + } +} + |