diff options
author | Danny Baumann <dannybaumann@web.de> | 2012-06-30 16:23:37 +0400 |
---|---|---|
committer | Gerrit Code Review <gerrit@review.cyanogenmod.com> | 2012-06-30 16:23:37 +0400 |
commit | 9ba2de56e07106926118d4b719a8357733cbdbec (patch) | |
tree | deff78c9074eed6c4d0df1094a4a65c2c3ba8eb2 | |
parent | 964474de6d1d2172d347104d39184644497ce80e (diff) | |
parent | 8367b64863e0213d397b18850766caaf4ac3085f (diff) | |
download | frameworks_base-9ba2de56e07106926118d4b719a8357733cbdbec.zip frameworks_base-9ba2de56e07106926118d4b719a8357733cbdbec.tar.gz frameworks_base-9ba2de56e07106926118d4b719a8357733cbdbec.tar.bz2 |
Merge "Port power widget improvements from CM7 to CM9." into ics
26 files changed, 648 insertions, 656 deletions
diff --git a/packages/SystemUI/res/values-de/strings.xml b/packages/SystemUI/res/values-de/strings.xml index 9fc2256..cf033c9 100644 --- a/packages/SystemUI/res/values-de/strings.xml +++ b/packages/SystemUI/res/values-de/strings.xml @@ -140,4 +140,5 @@ <string name="gps_notification_searching_text" msgid="8574247005642736060">"GPS wird gesucht"</string> <string name="gps_notification_found_text" msgid="4619274244146446464">"Standort durch GPS festgelegt"</string> <string name="accessibility_clear_all" msgid="5235938559247164925">"Alle Benachrichtigungen löschen"</string> + <string name="powerwidget_screen_timeout_toast">Bildschirm-Timeout ist jetzt %1$d %2$s</string> </resources> diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 664bfb1..19c9c5f 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -403,4 +403,6 @@ <string name="navbar_menu_conditional_button">Menu (autoHide) button</string> <string name="navbar_menu_always_button">Menu (alwaysShow) button</string> <string name="navbar_menu_big_button">Menu button</string> + + <string name="powerwidget_screen_timeout_toast">Screen timeout set to: %1$d %2$s</string> </resources> diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java index e2ae054..d0fba75 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java @@ -401,7 +401,6 @@ public class PhoneStatusBar extends StatusBar { mPowerWidget = (PowerWidget)expanded.findViewById(R.id.exp_power_stat); - mPowerWidget.setupSettingsObserver(mHandler); mPowerWidget.setGlobalButtonOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(Settings.System.getInt(mContext.getContentResolver(), @@ -612,7 +611,7 @@ public class PhoneStatusBar extends StatusBar { StatusBarIconView view = new StatusBarIconView(mContext, slot, null); view.set(icon); mStatusIcons.addView(view, viewIndex, new LinearLayout.LayoutParams(mIconSize, mIconSize)); - mPowerWidget.updateWidget(); + mPowerWidget.updateAllButtons(); } public void updateIcon(String slot, int index, int viewIndex, diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AirplaneButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AirplaneButton.java index 7e2f20c..e1820ef 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AirplaneButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AirplaneButton.java @@ -21,8 +21,8 @@ public class AirplaneButton extends PowerButton { public AirplaneButton() { mType = BUTTON_AIRPLANE; } @Override - protected void updateState() { - if (getState(mView.getContext())) { + protected void updateState(Context context) { + if (getState(context)) { mIcon = R.drawable.stat_airplane_on; mState = STATE_ENABLED; } else { @@ -32,8 +32,7 @@ public class AirplaneButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); + protected void toggleState(Context context) { boolean state = getState(context); Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1); @@ -45,11 +44,11 @@ public class AirplaneButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.AIRPLANE_MODE_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @@ -58,7 +57,7 @@ public class AirplaneButton extends PowerButton { return OBSERVED_URIS; } - private static boolean getState(Context context) { + private boolean getState(Context context) { return Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,0) == 1; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AutoRotateButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AutoRotateButton.java index 2cc4715..7e93d4f 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AutoRotateButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/AutoRotateButton.java @@ -28,8 +28,8 @@ public class AutoRotateButton extends PowerButton { public AutoRotateButton() { mType = BUTTON_AUTOROTATE; } @Override - protected void updateState() { - if (getAutoRotation()) { + protected void updateState(Context context) { + if (getAutoRotation(context)) { mIcon = R.drawable.stat_orientation_on; mState = STATE_ENABLED; } else { @@ -39,16 +39,16 @@ public class AutoRotateButton extends PowerButton { } @Override - protected void toggleState() { - setAutoRotation(!getAutoRotation()); + protected void toggleState(Context context) { + setAutoRotation(!getAutoRotation(context)); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.DISPLAY_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @@ -57,9 +57,9 @@ public class AutoRotateButton extends PowerButton { return OBSERVED_URIS; } - private boolean getAutoRotation() { - ContentResolver cr = mView.getContext().getContentResolver(); - return 0 != Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION, 0); + private boolean getAutoRotation(Context context) { + ContentResolver cr = context.getContentResolver(); + return Settings.System.getInt(cr, Settings.System.ACCELEROMETER_ROTATION, 0) != 0; } private void setAutoRotation(final boolean autorotate) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BluetoothButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BluetoothButton.java index fad57d3..6b1a2a2 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BluetoothButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BluetoothButton.java @@ -80,8 +80,8 @@ public class BluetoothButton extends PowerButton { public BluetoothButton() { mType = BUTTON_BLUETOOTH; } @Override - protected void updateState() { - mState = sBluetoothState.getTriState(mView.getContext()); + protected void updateState(Context context) { + mState = sBluetoothState.getTriState(context); switch (mState) { case STATE_DISABLED: mIcon = R.drawable.stat_bluetooth_off; @@ -105,16 +105,16 @@ public class BluetoothButton extends PowerButton { } @Override - protected void toggleState() { - sBluetoothState.toggleState(mView.getContext()); + protected void toggleState(Context context) { + sBluetoothState.toggleState(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.BLUETOOTH_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BrightnessButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BrightnessButton.java index 1804cf38..edd04d2 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BrightnessButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/BrightnessButton.java @@ -7,8 +7,8 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.net.Uri; -import android.os.IPowerManager; import android.os.Power; +import android.os.PowerManager; import android.os.RemoteException; import android.os.ServiceManager; import android.preference.ListPreferenceMultiSelect; @@ -79,12 +79,12 @@ public class BrightnessButton extends PowerButton { Context context = mView.getContext(); mAutoBrightnessSupported = context.getResources().getBoolean( com.android.internal.R.bool.config_automatic_brightness_available); - updateSettings(); + updateSettings(context.getContentResolver()); } } @Override - protected void updateState() { + protected void updateState(Context context) { if (mAutoBrightness) { mIcon = R.drawable.stat_brightness_auto; mState = STATE_ENABLED; @@ -101,42 +101,37 @@ public class BrightnessButton extends PowerButton { } @Override - protected void toggleState() { - try { - IPowerManager power = IPowerManager.Stub - .asInterface(ServiceManager.getService("power")); - if (power != null) { - ContentResolver resolver = mView.getContext().getContentResolver(); - mCurrentBacklightIndex++; - if (mCurrentBacklightIndex > mBacklightValues.length - 1) { - mCurrentBacklightIndex = 0; - } - int backlightIndex = mBacklightValues[mCurrentBacklightIndex]; - int brightness = BACKLIGHTS[backlightIndex]; - if (brightness == AUTO_BACKLIGHT) { - Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, - Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); - } else { - if (mAutoBrightnessSupported) { - Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, - Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); - } - power.setBacklightBrightness(brightness); - Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness); - } - } - } catch (RemoteException e) { - Log.e(TAG, "toggleState()", e); + protected void toggleState(Context context) { + PowerManager power = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + ContentResolver resolver = context.getContentResolver(); + + mCurrentBacklightIndex++; + if (mCurrentBacklightIndex > mBacklightValues.length - 1) { + mCurrentBacklightIndex = 0; } + int backlightIndex = mBacklightValues[mCurrentBacklightIndex]; + int brightness = BACKLIGHTS[backlightIndex]; + + if (brightness == AUTO_BACKLIGHT) { + Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, + Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); + } else { + if (mAutoBrightnessSupported) { + Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, + Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); + } + power.setBacklightBrightness(brightness); + Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness); + } } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.DISPLAY_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @@ -146,8 +141,7 @@ public class BrightnessButton extends PowerButton { } @Override - protected void onChangeUri(Uri uri) { - ContentResolver resolver = mView.getContext().getContentResolver(); + protected void onChangeUri(ContentResolver resolver, Uri uri) { if (BRIGHTNESS_URI.equals(uri)) { mCurrentBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS, 0); @@ -155,13 +149,11 @@ public class BrightnessButton extends PowerButton { mAutoBrightness = (Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE, 0) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } else { - updateSettings(); + updateSettings(resolver); } } - private void updateSettings() { - ContentResolver resolver = mView.getContext().getContentResolver(); - + private void updateSettings(ContentResolver resolver) { boolean lightSensorCustom = (Settings.System.getInt(resolver, Settings.System.LIGHT_SENSOR_CUSTOM, 0) != 0); if (lightSensorCustom) { @@ -198,7 +190,5 @@ public class BrightnessButton extends PowerButton { } } } - updateState(); } - } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/FlashlightButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/FlashlightButton.java index a9f17bc..cf8ed2d 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/FlashlightButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/FlashlightButton.java @@ -20,8 +20,8 @@ public class FlashlightButton extends PowerButton { public FlashlightButton() { mType = BUTTON_FLASHLIGHT; } @Override - protected void updateState() { - boolean enabled = Settings.System.getInt(mView.getContext().getContentResolver(), Settings.System.TORCH_STATE, 0) == 1; + protected void updateState(Context context) { + boolean enabled = Settings.System.getInt(context.getContentResolver(), Settings.System.TORCH_STATE, 0) == 1; if(enabled) { mIcon = R.drawable.stat_flashlight_on; mState = STATE_ENABLED; @@ -32,8 +32,7 @@ public class FlashlightButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); + protected void toggleState(Context context) { boolean bright = Settings.System.getInt(context.getContentResolver(), Settings.System.EXPANDED_FLASH_MODE, 0) == 1; Intent i = new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"); @@ -42,13 +41,13 @@ public class FlashlightButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { // it may be better to make an Intent action for the Torch // we may want to look at that option later Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("net.cactii.flash2", "net.cactii.flash2.MainActivity"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/GPSButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/GPSButton.java index 32a1ea3..579d1d9 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/GPSButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/GPSButton.java @@ -22,8 +22,8 @@ public class GPSButton extends PowerButton { public GPSButton() { mType = BUTTON_GPS; } @Override - protected void updateState() { - if(getGpsState(mView.getContext())) { + protected void updateState(Context context) { + if (getGpsState(context)) { mIcon = R.drawable.stat_gps_on; mState = STATE_ENABLED; } else { @@ -33,8 +33,7 @@ public class GPSButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); + protected void toggleState(Context context) { ContentResolver resolver = context.getContentResolver(); boolean enabled = getGpsState(context); Settings.Secure.setLocationProviderEnabled(resolver, @@ -42,11 +41,11 @@ public class GPSButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @@ -55,7 +54,7 @@ public class GPSButton extends PowerButton { return OBSERVED_URIS; } - private static boolean getGpsState(Context context) { + private boolean getGpsState(Context context) { ContentResolver resolver = context.getContentResolver(); return Settings.Secure.isLocationProviderEnabled(resolver, LocationManager.GPS_PROVIDER); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LTEButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LTEButton.java index bb12f84..52028ea 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LTEButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LTEButton.java @@ -24,9 +24,9 @@ public class LTEButton extends PowerButton{ public LTEButton() { mType = BUTTON_LTE; } @Override - protected void updateState() { - ContentResolver resolver = mView.getContext().getContentResolver(); - int network = getCurrentPreferredNetworkMode(mView.getContext()); + protected void updateState(Context context) { + ContentResolver resolver = context.getContentResolver(); + int network = getCurrentPreferredNetworkMode(context); switch(network) { case Phone.NT_MODE_GLOBAL: case Phone.NT_MODE_LTE_GSM_WCDMA: @@ -44,11 +44,11 @@ public class LTEButton extends PowerButton{ } @Override - protected void toggleState() { - TelephonyManager tm = (TelephonyManager) mView.getContext() - .getSystemService(Context.TELEPHONY_SERVICE); - int network = getCurrentPreferredNetworkMode(mView.getContext()); - ContentResolver resolver = mView.getContext().getContentResolver(); + protected void toggleState(Context context) { + TelephonyManager tm = (TelephonyManager) + context.getSystemService(Context.TELEPHONY_SERVICE); + int network = getCurrentPreferredNetworkMode(context); + ContentResolver resolver = context.getContentResolver(); if (Phone.NT_MODE_GLOBAL == network || Phone.NT_MODE_LTE_GSM_WCDMA == network) { tm.toggleLTE(false); @@ -63,11 +63,11 @@ public class LTEButton extends PowerButton{ } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("com.android.phone", "com.android.phone.Settings"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LockScreenButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LockScreenButton.java index a36c495..0a1959f 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LockScreenButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LockScreenButton.java @@ -2,30 +2,24 @@ package com.android.systemui.statusbar.powerwidget; import com.android.systemui.R; -import android.app.Activity; import android.app.KeyguardManager; import android.app.KeyguardManager.KeyguardLock; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.view.Gravity; -import android.widget.Toast; +import android.view.View; public class LockScreenButton extends PowerButton { - - private static Boolean LOCK_SCREEN_STATE = null; + private static final String KEY_DISABLED = "lockscreen_disabled"; private KeyguardLock mLock = null; + private boolean mDisabledLockscreen = false; public LockScreenButton() { mType = BUTTON_LOCKSCREEN; } @Override - protected void updateState() { - getState(); - if (LOCK_SCREEN_STATE == null) { - mIcon = R.drawable.stat_lock_screen_off; - mState = STATE_INTERMEDIATE; - } else if (LOCK_SCREEN_STATE) { + protected void updateState(Context context) { + if (!mDisabledLockscreen) { mIcon = R.drawable.stat_lock_screen_on; mState = STATE_ENABLED; } else { @@ -35,54 +29,50 @@ public class LockScreenButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); - getState(); - if(LOCK_SCREEN_STATE == null) { - Toast msg = Toast.makeText(context, "Not yet initialized", Toast.LENGTH_LONG); - msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2); - msg.show(); - } else { - getLock(context); - if (mLock != null) { - if (LOCK_SCREEN_STATE) { - mLock.disableKeyguard(); - LOCK_SCREEN_STATE = false; - } else { - mLock.reenableKeyguard(); - LOCK_SCREEN_STATE = true; - } - } + protected void setupButton(View view) { + super.setupButton(view); + + if (view == null && mDisabledLockscreen) { + mLock.reenableKeyguard(); + mLock = null; + } else if (view != null) { + Context context = view.getContext(); + mDisabledLockscreen = getPreferences(context).getBoolean(KEY_DISABLED, false); + applyState(context); } + } - // we're handling this, so just update our buttons now - // this is UGLY, do it better later >.> - update(); + @Override + protected void toggleState(Context context) { + mDisabledLockscreen = !mDisabledLockscreen; + + SharedPreferences.Editor editor = getPreferences(context).edit(); + editor.putBoolean(KEY_DISABLED, mDisabledLockscreen); + editor.apply(); + + applyState(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.SECURITY_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } - private KeyguardLock getLock(Context context) { + private void applyState(Context context) { if (mLock == null) { - KeyguardManager keyguardManager = (KeyguardManager)context. - getSystemService(Activity.KEYGUARD_SERVICE); - mLock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE); + KeyguardManager keyguardManager = (KeyguardManager) + context.getSystemService(Context.KEYGUARD_SERVICE); + mLock = keyguardManager.newKeyguardLock("PowerWidget"); } - return mLock; - } - - private static boolean getState() { - if (LOCK_SCREEN_STATE == null) { - LOCK_SCREEN_STATE = true; + if (mDisabledLockscreen) { + mLock.disableKeyguard(); + } else { + mLock.reenableKeyguard(); } - return LOCK_SCREEN_STATE; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaKeyEventButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaKeyEventButton.java index f722036..62ddbbd 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaKeyEventButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaKeyEventButton.java @@ -14,10 +14,9 @@ import android.view.View; public abstract class MediaKeyEventButton extends PowerButton { private static final String TAG = "MediaKeyEventButton"; - private static AudioManager AUDIO_MANAGER = null; + private AudioManager mAM = null; - protected void sendMediaKeyEvent(int code) { - Context context = mView.getContext(); + protected void sendMediaKeyEvent(Context context, int code) { long eventtime = SystemClock.uptimeMillis(); Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); @@ -31,11 +30,11 @@ public abstract class MediaKeyEventButton extends PowerButton { context.sendOrderedBroadcast(upIntent, null); } - protected static AudioManager getAudioManager(Context context) { - if(AUDIO_MANAGER == null) { - AUDIO_MANAGER = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + protected AudioManager getAudioManager(Context context) { + if (mAM == null) { + mAM = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } - return AUDIO_MANAGER; + return mAM; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaNextButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaNextButton.java index d416c9a..c5c0791 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaNextButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaNextButton.java @@ -9,18 +9,18 @@ public class MediaNextButton extends MediaKeyEventButton { public MediaNextButton() { mType = BUTTON_MEDIA_NEXT; } @Override - protected void updateState() { + protected void updateState(Context context) { mIcon = R.drawable.stat_media_next; mState = STATE_DISABLED; } @Override - protected void toggleState() { - sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT); + protected void toggleState(Context context) { + sendMediaKeyEvent(context, KeyEvent.KEYCODE_MEDIA_NEXT); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { return false; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPlayPauseButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPlayPauseButton.java index ea814e3..9a6ec71 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPlayPauseButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPlayPauseButton.java @@ -16,9 +16,9 @@ public class MediaPlayPauseButton extends MediaKeyEventButton { private int mCurrentState = MEDIA_STATE_UNKNOWN; @Override - protected void updateState() { + protected void updateState(Context context) { mState = STATE_DISABLED; - if(isMusicActive()) { + if (isMusicActive(context)) { mIcon = R.drawable.stat_media_pause; } else { mIcon = R.drawable.stat_media_play; @@ -26,24 +26,24 @@ public class MediaPlayPauseButton extends MediaKeyEventButton { } @Override - protected void toggleState() { - sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE); + protected void toggleState(Context context) { + sendMediaKeyEvent(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE); - mCurrentState = (isMusicActive() ? MEDIA_STATE_INACTIVE : MEDIA_STATE_ACTIVE); + mCurrentState = (isMusicActive(context) ? MEDIA_STATE_INACTIVE : MEDIA_STATE_ACTIVE); - update(); + update(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { return false; } - private boolean isMusicActive() { - if(mCurrentState == MEDIA_STATE_UNKNOWN) { + private boolean isMusicActive(Context context) { + if (mCurrentState == MEDIA_STATE_UNKNOWN) { mCurrentState = MEDIA_STATE_INACTIVE; - AudioManager am = getAudioManager(mView.getContext()); - if(am != null) { + AudioManager am = getAudioManager(context); + if (am != null) { mCurrentState = (am.isMusicActive() ? MEDIA_STATE_ACTIVE : MEDIA_STATE_INACTIVE); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPreviousButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPreviousButton.java index b7657f6..362b1f7 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPreviousButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MediaPreviousButton.java @@ -9,18 +9,18 @@ public class MediaPreviousButton extends MediaKeyEventButton { public MediaPreviousButton() { mType = BUTTON_MEDIA_PREVIOUS; } @Override - protected void updateState() { + protected void updateState(Context context) { mIcon = R.drawable.stat_media_previous; mState = STATE_DISABLED; } @Override - protected void toggleState() { - sendMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS); + protected void toggleState(Context context) { + sendMediaKeyEvent(context, KeyEvent.KEYCODE_MEDIA_PREVIOUS); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { return false; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MobileDataButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MobileDataButton.java index d5d3282..e53d1fa 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MobileDataButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/MobileDataButton.java @@ -13,18 +13,14 @@ import com.android.internal.telephony.TelephonyIntents; public class MobileDataButton extends PowerButton { - public static final String MOBILE_DATA_CHANGED = "com.android.internal.telephony.MOBILE_DATA_CHANGED"; - - public static boolean STATE_CHANGE_REQUEST = false; + public static final String ACTION_MODIFY_NETWORK_MODE = "com.android.internal.telephony.MODIFY_NETWORK_MODE"; + public static final String EXTRA_NETWORK_MODE = "networkMode"; public MobileDataButton() { mType = BUTTON_MOBILEDATA; } @Override - protected void updateState() { - if (STATE_CHANGE_REQUEST) { - mIcon = R.drawable.stat_data_on; - mState = STATE_INTERMEDIATE; - } else if (getDataState(mView.getContext())) { + protected void updateState(Context context) { + if (getDataState(context)) { mIcon = R.drawable.stat_data_on; mState = STATE_ENABLED; } else { @@ -34,8 +30,7 @@ public class MobileDataButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); + protected void toggleState(Context context) { boolean enabled = getDataState(context); ConnectivityManager cm = (ConnectivityManager) context @@ -48,13 +43,13 @@ public class MobileDataButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { // it may be better to make an Intent action for this or find the appropriate one // we may want to look at that option later Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("com.android.phone", "com.android.phone.Settings"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @@ -65,24 +60,9 @@ public class MobileDataButton extends PowerButton { return filter; } - private static boolean getDataRomingEnabled(Context context) { - return Settings.Secure.getInt(context.getContentResolver(), - Settings.Secure.DATA_ROAMING,0) > 0; - } - - private static boolean getDataState(Context context) { + private boolean getDataState(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getMobileDataEnabled(); } - - public void networkModeChanged(Context context, int networkMode) { - if (STATE_CHANGE_REQUEST) { - ConnectivityManager cm = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - cm.setMobileDataEnabled(true); - STATE_CHANGE_REQUEST=false; - } - } - } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/NetworkModeButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/NetworkModeButton.java index 8aa5e5c..028d49c 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/NetworkModeButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/NetworkModeButton.java @@ -36,16 +36,15 @@ public class NetworkModeButton extends PowerButton{ private static final int CM_MODE_3GONLY = 1; private static final int CM_MODE_BOTH = 2; - private static int NETWORK_MODE = NO_NETWORK_MODE_YET; - private static int INTENDED_NETWORK_MODE = NO_NETWORK_MODE_YET; - private static int CURRENT_INTERNAL_STATE = STATE_INTERMEDIATE; + private int mMode = NO_NETWORK_MODE_YET; + private int mIntendedMode = NO_NETWORK_MODE_YET; + private int mInternalState = STATE_INTERMEDIATE; public NetworkModeButton() { mType = BUTTON_NETWORKMODE; } @Override - protected void updateState() { - Context context = mView.getContext(); - NETWORK_MODE = get2G3G(context); + protected void updateState(Context context) { + mMode = get2G3G(context); mState = networkModeToState(context); switch (mState) { @@ -53,7 +52,7 @@ public class NetworkModeButton extends PowerButton{ mIcon = R.drawable.stat_2g3g_off; break; case STATE_ENABLED: - if (NETWORK_MODE == Phone.NT_MODE_WCDMA_ONLY) { + if (mMode == Phone.NT_MODE_WCDMA_ONLY) { mIcon = R.drawable.stat_3g_on; } else { mIcon = R.drawable.stat_2g3g_on; @@ -65,8 +64,8 @@ public class NetworkModeButton extends PowerButton{ // the top dark-gray-or-bright-white logo shows the // user's intent. This is much easier to see in // sunlight. - if (CURRENT_INTERNAL_STATE == STATE_TURNING_ON) { - if (INTENDED_NETWORK_MODE == Phone.NT_MODE_WCDMA_ONLY) { + if (mInternalState == STATE_TURNING_ON) { + if (mIntendedMode == Phone.NT_MODE_WCDMA_ONLY) { mIcon = R.drawable.stat_3g_on; } else { mIcon = R.drawable.stat_2g3g_on; @@ -79,76 +78,67 @@ public class NetworkModeButton extends PowerButton{ } @Override - protected void toggleState() { - Context context = mView.getContext(); + protected void toggleState(Context context) { int currentMode = getCurrentCMMode(context); Intent intent = new Intent(ACTION_MODIFY_NETWORK_MODE); - switch (NETWORK_MODE) { + switch (mMode) { case Phone.NT_MODE_WCDMA_PREF: case Phone.NT_MODE_GSM_UMTS: intent.putExtra(EXTRA_NETWORK_MODE, Phone.NT_MODE_GSM_ONLY); - CURRENT_INTERNAL_STATE = STATE_TURNING_OFF; - INTENDED_NETWORK_MODE=Phone.NT_MODE_GSM_ONLY; + mInternalState = STATE_TURNING_OFF; + mIntendedMode = Phone.NT_MODE_GSM_ONLY; break; case Phone.NT_MODE_WCDMA_ONLY: - if(currentMode == CM_MODE_3GONLY) { + if (currentMode == CM_MODE_3GONLY) { intent.putExtra(EXTRA_NETWORK_MODE, Phone.NT_MODE_GSM_ONLY); - CURRENT_INTERNAL_STATE = STATE_TURNING_OFF; - INTENDED_NETWORK_MODE = Phone.NT_MODE_GSM_ONLY; + mInternalState = STATE_TURNING_OFF; + mIntendedMode = Phone.NT_MODE_GSM_ONLY; } else { intent.putExtra(EXTRA_NETWORK_MODE, Phone.NT_MODE_WCDMA_PREF); - CURRENT_INTERNAL_STATE = STATE_TURNING_ON; - INTENDED_NETWORK_MODE = Phone.NT_MODE_WCDMA_PREF; + mInternalState = STATE_TURNING_ON; + mIntendedMode = Phone.NT_MODE_WCDMA_PREF; } break; case Phone.NT_MODE_GSM_ONLY: - if(currentMode == CM_MODE_3GONLY || currentMode == CM_MODE_BOTH) { + if (currentMode == CM_MODE_3GONLY || currentMode == CM_MODE_BOTH) { intent.putExtra(EXTRA_NETWORK_MODE, Phone.NT_MODE_WCDMA_ONLY); - CURRENT_INTERNAL_STATE = STATE_TURNING_ON; - INTENDED_NETWORK_MODE = Phone.NT_MODE_WCDMA_ONLY; + mInternalState = STATE_TURNING_ON; + mIntendedMode = Phone.NT_MODE_WCDMA_ONLY; } else { intent.putExtra(EXTRA_NETWORK_MODE, Phone.NT_MODE_WCDMA_PREF); - CURRENT_INTERNAL_STATE = STATE_TURNING_ON; - INTENDED_NETWORK_MODE = Phone.NT_MODE_WCDMA_PREF; + mInternalState = STATE_TURNING_ON; + mIntendedMode = Phone.NT_MODE_WCDMA_PREF; } break; } - NETWORK_MODE = NETWORK_MODE_UNKNOWN; + mMode = NETWORK_MODE_UNKNOWN; context.sendBroadcast(intent); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { // it may be better to make an Intent action for this or find the appropriate one // we may want to look at that option later Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("com.android.phone", "com.android.phone.Settings"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @Override public void onReceive(Context context, Intent intent) { if (intent.getExtras() != null) { - NETWORK_MODE = intent.getExtras().getInt(EXTRA_NETWORK_MODE); + mMode = intent.getExtras().getInt(EXTRA_NETWORK_MODE); //Update to actual state - INTENDED_NETWORK_MODE = NETWORK_MODE; + mIntendedMode = mMode; } //need to clear intermediate states - CURRENT_INTERNAL_STATE = STATE_ENABLED; - - int widgetState = networkModeToState(context); - CURRENT_INTERNAL_STATE = widgetState; - if (widgetState == STATE_ENABLED) { - MobileDataButton mdb = (MobileDataButton)getLoadedButton(BUTTON_MOBILEDATA); - if(mdb != null) { - mdb.networkModeChanged(context, NETWORK_MODE); - } - } + mInternalState = STATE_ENABLED; + mInternalState = networkModeToState(context); } @Override @@ -173,12 +163,12 @@ public class NetworkModeButton extends PowerButton{ return state; } - private static int networkModeToState(Context context) { - if (CURRENT_INTERNAL_STATE == STATE_TURNING_ON || - CURRENT_INTERNAL_STATE == STATE_TURNING_OFF) + private int networkModeToState(Context context) { + if (mInternalState == STATE_TURNING_ON || mInternalState == STATE_TURNING_OFF) { return STATE_INTERMEDIATE; + } - switch(NETWORK_MODE) { + switch (mMode) { case Phone.NT_MODE_WCDMA_PREF: case Phone.NT_MODE_WCDMA_ONLY: case Phone.NT_MODE_GSM_UMTS: @@ -190,7 +180,7 @@ public class NetworkModeButton extends PowerButton{ case Phone.NT_MODE_EVDO_NO_CDMA: case Phone.NT_MODE_GLOBAL: // need to check wtf is going on - Log.d(TAG, "Unexpected network mode (" + NETWORK_MODE + ")"); + Log.d(TAG, "Unexpected network mode (" + mMode + ")"); return STATE_DISABLED; } return STATE_INTERMEDIATE; diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerButton.java index a4549bd..4b9750d 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerButton.java @@ -3,9 +3,11 @@ package com.android.systemui.statusbar.powerwidget; import com.android.systemui.R; import android.app.ActivityManagerNative; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.graphics.PorterDuff.Mode; @@ -13,14 +15,13 @@ import android.net.Uri; import android.os.Handler; import android.os.Message; import android.os.RemoteException; +import android.os.Vibrator; import android.util.Log; import android.widget.ImageView; import android.provider.Settings; import android.view.View; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; public abstract class PowerButton { public static final String TAG = "PowerButton"; @@ -56,92 +57,64 @@ public abstract class PowerButton { private static final Mode MASK_MODE = Mode.SCREEN; - // this is a list of all of our buttons and their corresponding classes - private static final HashMap<String, Class<? extends PowerButton>> BUTTONS = new HashMap<String, Class<? extends PowerButton>>(); - static { - BUTTONS.put(BUTTON_WIFI, WifiButton.class); - BUTTONS.put(BUTTON_GPS, GPSButton.class); - BUTTONS.put(BUTTON_BLUETOOTH, BluetoothButton.class); - BUTTONS.put(BUTTON_BRIGHTNESS, BrightnessButton.class); - BUTTONS.put(BUTTON_SOUND, SoundButton.class); - BUTTONS.put(BUTTON_SYNC, SyncButton.class); - BUTTONS.put(BUTTON_WIFIAP, WifiApButton.class); - BUTTONS.put(BUTTON_SCREENTIMEOUT, ScreenTimeoutButton.class); - BUTTONS.put(BUTTON_MOBILEDATA, MobileDataButton.class); - BUTTONS.put(BUTTON_LOCKSCREEN, LockScreenButton.class); - BUTTONS.put(BUTTON_NETWORKMODE, NetworkModeButton.class); - BUTTONS.put(BUTTON_AUTOROTATE, AutoRotateButton.class); - BUTTONS.put(BUTTON_AIRPLANE, AirplaneButton.class); - BUTTONS.put(BUTTON_FLASHLIGHT, FlashlightButton.class); - BUTTONS.put(BUTTON_SLEEP, SleepButton.class); - BUTTONS.put(BUTTON_MEDIA_PLAY_PAUSE, MediaPlayPauseButton.class); - BUTTONS.put(BUTTON_MEDIA_PREVIOUS, MediaPreviousButton.class); - BUTTONS.put(BUTTON_MEDIA_NEXT, MediaNextButton.class); - BUTTONS.put(BUTTON_LTE, LTEButton.class); - BUTTONS.put(BUTTON_WIMAX, WimaxButton.class); - } - // this is a list of our currently loaded buttons - private static final HashMap<String, PowerButton> BUTTONS_LOADED = new HashMap<String, PowerButton>(); - protected int mIcon; protected int mState; protected View mView; protected String mType = BUTTON_UNKNOWN; - // a static onclicklistener that can be set to register a callback when ANY button is clicked - private static View.OnClickListener GLOBAL_ON_CLICK_LISTENER = null; + private ImageView mIconView; + private ImageView mIndicatorView; + + private View.OnClickListener mExternalClickListener; + private View.OnLongClickListener mExternalLongClickListener; - // a static onlongclicklistener that can be set to register a callback when ANY button is long clicked - private static View.OnLongClickListener GLOBAL_ON_LONG_CLICK_LISTENER = null; + protected boolean mHapticFeedback; + protected Vibrator mVibrator; + private long[] mClickPattern; + private long[] mLongClickPattern; // we use this to ensure we update our views on the UI thread private Handler mViewUpdateHandler = new Handler() { - public void handleMessage(Message msg) { - // this is only used to update the view, so do it - if(mView != null) { - Context context = mView.getContext(); - Resources res = context.getResources(); - int buttonLayer = R.id.power_widget_button; - int buttonIcon = R.id.power_widget_button_image; - int buttonState = R.id.power_widget_button_indic; - ImageView indic = (ImageView)mView.findViewById(R.id.power_widget_button_indic); - if ((Settings.System.getInt(context.getContentResolver(),Settings.System.EXPANDED_HIDE_INDICATOR, 0)) == 1){ - indic.setVisibility(8); - }else{ - indic.setVisibility(0); - } - updateImageView(buttonIcon, mIcon); - - int sColorMaskBase = res.getColor(android.R.color.holo_blue_light); - int sColorMaskOn = sColorMaskBase; - int sColorMaskOff = (sColorMaskBase & 0x00FFFFFF) | 0x33000000; - int sColorMaskInter = (sColorMaskBase & 0x00FFFFFF) | 0x60000000; - - /* Button State */ - switch(mState) { - case STATE_ENABLED: - updateImageView(buttonState, - res.getDrawable(R.drawable.stat_bgon_custom, sColorMaskOn, MASK_MODE)); - break; - case STATE_DISABLED: - updateImageView(buttonState, - res.getDrawable(R.drawable.stat_bgon_custom, sColorMaskOff, MASK_MODE)); - break; - default: - updateImageView(buttonState, - res.getDrawable(R.drawable.stat_bgon_custom, sColorMaskInter, MASK_MODE)); - break; - } + @Override + public void handleMessage(Message msg) { + if (mIconView != null) { + mIconView.setImageResource(mIcon); + } + if (mIndicatorView != null) { + Context context = mIndicatorView.getContext(); + ContentResolver cr = context.getContentResolver(); + Resources res = context.getResources(); + + boolean visible = Settings.System.getInt(cr, + Settings.System.EXPANDED_HIDE_INDICATOR, 0) != 1; + int colorMask = res.getColor(android.R.color.holo_blue_light); + + mIndicatorView.setVisibility(visible ? View.VISIBLE : View.GONE); + + switch (mState) { + case STATE_ENABLED: + colorMask = (colorMask & 0x00FFFFFF) | 0xA0000000; + break; + case STATE_DISABLED: + colorMask = (colorMask & 0x00FFFFFF) | 0x33000000; + break; + default: + colorMask = (colorMask & 0x00FFFFFF) | 0x60000000; + break; } + + mIndicatorView.setImageDrawable(context.getResources().getDrawable( + R.drawable.stat_bgon_custom, colorMask, MASK_MODE)); } - }; + } + }; - protected abstract void updateState(); - protected abstract void toggleState(); - protected abstract boolean handleLongClick(); + protected abstract void updateState(Context context); + protected abstract void toggleState(Context context); + protected abstract boolean handleLongClick(Context context); - protected void update() { - updateState(); + protected void update(Context context) { + updateState(context); updateView(); } @@ -150,11 +123,18 @@ public abstract class PowerButton { // to broadcast events from the StatusBarService broadcast receiver } - protected void onChangeUri(Uri uri) { + protected void onChangeUri(ContentResolver resolver, Uri uri) { // do nothing as a standard, override this if the button needs to respond // to a changed setting } + /* package */ void setHapticFeedback(boolean enabled, + long[] clickPattern, long[] longClickPattern) { + mHapticFeedback = enabled; + mClickPattern = clickPattern; + mLongClickPattern = longClickPattern; + } + protected IntentFilter getBroadcastIntentFilter() { return new IntentFilter(); } @@ -165,10 +145,17 @@ public abstract class PowerButton { protected void setupButton(View view) { mView = view; - if(mView != null) { + if (mView != null) { mView.setTag(mType); mView.setOnClickListener(mClickListener); mView.setOnLongClickListener(mLongClickListener); + + mIconView = (ImageView) mView.findViewById(R.id.power_widget_button_image); + mIndicatorView = (ImageView) mView.findViewById(R.id.power_widget_button_indic); + mVibrator = (Vibrator) mView.getContext().getSystemService(Context.VIBRATOR_SERVICE); + } else { + mIconView = null; + mIndicatorView = null; } } @@ -176,197 +163,49 @@ public abstract class PowerButton { mViewUpdateHandler.sendEmptyMessage(0); } - private void updateImageView(int id, int resId) { - ImageView imageIcon = (ImageView)mView.findViewById(id); - imageIcon.setImageResource(resId); - } - - private void updateImageView(int id, Drawable resDraw) { - ImageView imageIcon = (ImageView)mView.findViewById(id); - imageIcon.setImageResource(R.drawable.stat_bgon_custom); - imageIcon.setImageDrawable(resDraw); - } - private View.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) { - String type = (String)v.getTag(); - - for(Map.Entry<String, PowerButton> entry : BUTTONS_LOADED.entrySet()) { - if(entry.getKey().equals(type)) { - entry.getValue().toggleState(); - break; - } + if (mHapticFeedback && mClickPattern != null) { + mVibrator.vibrate(mClickPattern, -1); } + toggleState(v.getContext()); + update(v.getContext()); - // call our static listener if it's set - if(GLOBAL_ON_CLICK_LISTENER != null) { - GLOBAL_ON_CLICK_LISTENER.onClick(v); + if (mExternalClickListener != null) { + mExternalClickListener.onClick(v); } } }; private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { public boolean onLongClick(View v) { - boolean result = false; - String type = (String)v.getTag(); - for (Map.Entry<String, PowerButton> entry : BUTTONS_LOADED.entrySet()) { - if(entry.getKey().endsWith(type)) { - result = entry.getValue().handleLongClick(); - try { - ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity(); - } catch (RemoteException e) { - } - break; - } - } - - if(result && GLOBAL_ON_LONG_CLICK_LISTENER != null) { - GLOBAL_ON_LONG_CLICK_LISTENER.onLongClick(v); - } - return result; - } - }; + boolean result = handleLongClick(v.getContext()); - public static boolean loadButton(String key, View view) { - // first make sure we have a valid button - if(BUTTONS.containsKey(key) && view != null) { - synchronized (BUTTONS_LOADED) { - if(BUTTONS_LOADED.containsKey(key)) { - // setup the button again - BUTTONS_LOADED.get(key).setupButton(view); - } else { - try { - // we need to instantiate a new button and add it - PowerButton pb = BUTTONS.get(key).newInstance(); - // set it up - pb.setupButton(view); - // save it - BUTTONS_LOADED.put(key, pb); - } catch(Exception e) { - Log.e(TAG, "Error loading button: " + key, e); - } - } + if (result && mHapticFeedback && mLongClickPattern != null) { + mVibrator.vibrate(mLongClickPattern, -1); } - return true; - } else { - return false; - } - } - public static void unloadButton(String key) { - synchronized (BUTTONS_LOADED) { - // first make sure we have a valid button - if(BUTTONS_LOADED.containsKey(key)) { - // wipe out the button view - BUTTONS_LOADED.get(key).setupButton(null); - // remove the button from our list of loaded ones - BUTTONS_LOADED.remove(key); + try { + ActivityManagerNative.getDefault().dismissKeyguardOnNextActivity(); + } catch (RemoteException e) { } - } - } - public static void unloadAllButtons() { - synchronized (BUTTONS_LOADED) { - // cycle through setting the buttons to null - for(PowerButton pb : BUTTONS_LOADED.values()) { - pb.setupButton(null); - } - - // clear our list - BUTTONS_LOADED.clear(); - } - } - - public static void updateAllButtons() { - synchronized (BUTTONS_LOADED) { - // cycle through our buttons and update them - for(PowerButton pb : BUTTONS_LOADED.values()) { - pb.update(); - } - } - } - - // glue for broadcast receivers - public static IntentFilter getAllBroadcastIntentFilters() { - IntentFilter filter = new IntentFilter(); - - synchronized(BUTTONS_LOADED) { - for(PowerButton button : BUTTONS_LOADED.values()) { - IntentFilter tmp = button.getBroadcastIntentFilter(); - - // cycle through these actions, and see if we need them - int num = tmp.countActions(); - for(int i = 0; i < num; i++) { - String action = tmp.getAction(i); - if(!filter.hasAction(action)) { - filter.addAction(action); - } - } - } - } - - // return our merged filter - return filter; - } - - // glue for content observation - public static List<Uri> getAllObservedUris() { - List<Uri> uris = new ArrayList<Uri>(); - - synchronized(BUTTONS_LOADED) { - for(PowerButton button : BUTTONS_LOADED.values()) { - List<Uri> tmp = button.getObservedUris(); - - for(Uri uri : tmp) { - if(!uris.contains(uri)) { - uris.add(uri); - } - } - } - } - - return uris; - } - - public static void handleOnReceive(Context context, Intent intent) { - String action = intent.getAction(); - - // cycle through power buttons - synchronized(BUTTONS_LOADED) { - for(PowerButton button : BUTTONS_LOADED.values()) { - // call "onReceive" on those that matter - if(button.getBroadcastIntentFilter().hasAction(action)) { - button.onReceive(context, intent); - } - } - } - } - - public static void handleOnChangeUri(Uri uri) { - synchronized(BUTTONS_LOADED) { - for(PowerButton button : BUTTONS_LOADED.values()) { - if(button.getObservedUris().contains(uri)) { - button.onChangeUri(uri); - } + if (result && mExternalLongClickListener != null) { + mExternalLongClickListener.onLongClick(v); } + return result; } - } + }; - public static void setGlobalOnClickListener(View.OnClickListener listener) { - GLOBAL_ON_CLICK_LISTENER = listener; + void setExternalClickListener(View.OnClickListener listener) { + mExternalClickListener = listener; } - public static void setGlobalOnLongClickListener(View.OnLongClickListener listener) { - GLOBAL_ON_LONG_CLICK_LISTENER = listener; + void setExternalLongClickListener(View.OnLongClickListener listener) { + mExternalLongClickListener = listener; } - protected static PowerButton getLoadedButton(String key) { - synchronized(BUTTONS_LOADED) { - if(BUTTONS_LOADED.containsKey(key)) { - return BUTTONS_LOADED.get(key); - } else { - return null; - } - } + protected SharedPreferences getPreferences(Context context) { + return context.getSharedPreferences("PowerButton-" + mType, Context.MODE_PRIVATE); } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerWidget.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerWidget.java index 251d62a..5f8f91f 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerWidget.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/PowerWidget.java @@ -41,6 +41,8 @@ import com.android.systemui.R; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; public class PowerWidget extends FrameLayout { private static final String TAG = "PowerWidget"; @@ -65,41 +67,98 @@ public class PowerWidget extends FrameLayout { private static final int LAYOUT_SCROLL_BUTTON_THRESHOLD = 6; + // this is a list of all possible buttons and their corresponding classes + private static final HashMap<String, Class<? extends PowerButton>> sPossibleButtons = + new HashMap<String, Class<? extends PowerButton>>(); + + static { + sPossibleButtons.put(PowerButton.BUTTON_WIFI, WifiButton.class); + sPossibleButtons.put(PowerButton.BUTTON_GPS, GPSButton.class); + sPossibleButtons.put(PowerButton.BUTTON_BLUETOOTH, BluetoothButton.class); + sPossibleButtons.put(PowerButton.BUTTON_BRIGHTNESS, BrightnessButton.class); + sPossibleButtons.put(PowerButton.BUTTON_SOUND, SoundButton.class); + sPossibleButtons.put(PowerButton.BUTTON_SYNC, SyncButton.class); + sPossibleButtons.put(PowerButton.BUTTON_WIFIAP, WifiApButton.class); + sPossibleButtons.put(PowerButton.BUTTON_SCREENTIMEOUT, ScreenTimeoutButton.class); + sPossibleButtons.put(PowerButton.BUTTON_MOBILEDATA, MobileDataButton.class); + sPossibleButtons.put(PowerButton.BUTTON_LOCKSCREEN, LockScreenButton.class); + sPossibleButtons.put(PowerButton.BUTTON_NETWORKMODE, NetworkModeButton.class); + sPossibleButtons.put(PowerButton.BUTTON_AUTOROTATE, AutoRotateButton.class); + sPossibleButtons.put(PowerButton.BUTTON_AIRPLANE, AirplaneButton.class); + sPossibleButtons.put(PowerButton.BUTTON_FLASHLIGHT, FlashlightButton.class); + sPossibleButtons.put(PowerButton.BUTTON_SLEEP, SleepButton.class); + sPossibleButtons.put(PowerButton.BUTTON_MEDIA_PLAY_PAUSE, MediaPlayPauseButton.class); + sPossibleButtons.put(PowerButton.BUTTON_MEDIA_PREVIOUS, MediaPreviousButton.class); + sPossibleButtons.put(PowerButton.BUTTON_MEDIA_NEXT, MediaNextButton.class); + sPossibleButtons.put(PowerButton.BUTTON_WIMAX, WimaxButton.class); + sPossibleButtons.put(PowerButton.BUTTON_LTE, LTEButton.class); + } + + // this is a list of our currently loaded buttons + private final HashMap<String, PowerButton> mButtons = new HashMap<String, PowerButton>(); + private final ArrayList<String> mButtonNames = new ArrayList<String>(); + + private View.OnClickListener mAllButtonClickListener; + private View.OnLongClickListener mAllButtonLongClickListener; + private Context mContext; + private Handler mHandler; private LayoutInflater mInflater; private WidgetBroadcastReceiver mBroadcastReceiver = null; private WidgetSettingsObserver mObserver = null; + private long[] mShortPressVibePattern; + private long[] mLongPressVibePattern; + + private LinearLayout mButtonLayout; private HorizontalScrollView mScrollView; public PowerWidget(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; + mHandler = new Handler(); mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mShortPressVibePattern = getLongIntArray(mContext.getResources(), + com.android.internal.R.array.config_virtualKeyVibePattern); + mLongPressVibePattern = getLongIntArray(mContext.getResources(), + com.android.internal.R.array.config_longPressVibePattern); + // get an initial width updateButtonLayoutWidth(); setupWidget(); updateVisibility(); } + static long[] getLongIntArray(Resources r, int resid) { + int[] ar = r.getIntArray(resid); + if (ar == null) { + return null; + } + long[] out = new long[ar.length]; + for (int i=0; i < ar.length; i++) { + out[i] = ar[i]; + } + return out; + } + public void destroyWidget() { Log.i(TAG, "Clearing any old widget stuffs"); // remove all views from the layout removeAllViews(); // unregister our content receiver - if(mBroadcastReceiver != null) { + if (mBroadcastReceiver != null) { mContext.unregisterReceiver(mBroadcastReceiver); } // unobserve our content - if(mObserver != null) { + if (mObserver != null) { mObserver.unobserve(); } // clear the button instances - PowerButton.unloadAllButtons(); + unloadAllButtons(); } public void setupWidget() { @@ -108,7 +167,7 @@ public class PowerWidget extends FrameLayout { Log.i(TAG, "Setting up widget"); String buttons = Settings.System.getString(mContext.getContentResolver(), Settings.System.WIDGET_BUTTONS); - if(buttons == null) { + if (buttons == null) { Log.i(TAG, "Default buttons being loaded"); buttons = BUTTONS_DEFAULT; // Add the WiMAX button if it's supported @@ -118,28 +177,95 @@ public class PowerWidget extends FrameLayout { } Log.i(TAG, "Button list: " + buttons); - // create a linearlayout to hold our buttons - LinearLayout ll = new LinearLayout(mContext); - ll.setOrientation(LinearLayout.HORIZONTAL); - ll.setGravity(Gravity.CENTER_HORIZONTAL); - - int buttonCount = 0; - for(String button : buttons.split("\\|")) { - Log.i(TAG, "Setting up button: " + button); - // inflate our button, we don't add it to a parent and don't do any layout shit yet - View buttonView = mInflater.inflate(R.layout.power_widget_button, null, false); - - if(PowerButton.loadButton(button, buttonView)) { - // add the button here - ll.addView(buttonView, BUTTON_LAYOUT_PARAMS); - buttonCount++; + for (String button : buttons.split("\\|")) { + if (loadButton(button)) { + mButtonNames.add(button); } else { Log.e(TAG, "Error setting up button: " + button); } } + recreateButtonLayout(); + updateHapticFeedbackSetting(); + + // set up a broadcast receiver for our intents, based off of what our power buttons have been loaded + setupBroadcastReceiver(); + IntentFilter filter = getMergedBroadcastIntentFilter(); + // we add this so we can update views and such if the settings for our widget change + filter.addAction(Settings.SETTINGS_CHANGED); + // we need to detect orientation changes and update the static button width value appropriately + filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); + // register the receiver + mContext.registerReceiver(mBroadcastReceiver, filter); + // register our observer + mObserver = new WidgetSettingsObserver(mHandler); + mObserver.observe(); + } + + private boolean loadButton(String key) { + // first make sure we have a valid button + if (!sPossibleButtons.containsKey(key)) { + return false; + } + + if (mButtons.containsKey(key)) { + return true; + } + + try { + // we need to instantiate a new button and add it + PowerButton pb = sPossibleButtons.get(key).newInstance(); + pb.setExternalClickListener(mAllButtonClickListener); + pb.setExternalLongClickListener(mAllButtonLongClickListener); + // save it + mButtons.put(key, pb); + } catch (Exception e) { + Log.e(TAG, "Error loading button: " + key, e); + return false; + } + + return true; + } + + private void unloadButton(String key) { + // first make sure we have a valid button + if (mButtons.containsKey(key)) { + // wipe out the button view + mButtons.get(key).setupButton(null); + // remove the button from our list of loaded ones + mButtons.remove(key); + } + } + + private void unloadAllButtons() { + // cycle through setting the buttons to null + for (PowerButton pb : mButtons.values()) { + pb.setupButton(null); + } + + // clear our list + mButtons.clear(); + mButtonNames.clear(); + } + + private void recreateButtonLayout() { + removeAllViews(); + + // create a linearlayout to hold our buttons + mButtonLayout = new LinearLayout(mContext); + mButtonLayout.setOrientation(LinearLayout.HORIZONTAL); + mButtonLayout.setGravity(Gravity.CENTER_HORIZONTAL); + + for (String button : mButtonNames) { + PowerButton pb = mButtons.get(button); + if (pb != null) { + View buttonView = mInflater.inflate(R.layout.power_widget_button, null, false); + pb.setupButton(buttonView); + mButtonLayout.addView(buttonView, BUTTON_LAYOUT_PARAMS); + } + } // we determine if we're using a horizontal scroll view based on a threshold of button counts - if(buttonCount > LAYOUT_SCROLL_BUTTON_THRESHOLD) { + if (mButtonLayout.getChildCount() > LAYOUT_SCROLL_BUTTON_THRESHOLD) { // we need our horizontal scroll view to wrap the linear layout mScrollView = new HorizontalScrollView(mContext); // make the fading edge the size of a button (makes it more noticible that we can scroll @@ -147,50 +273,78 @@ public class PowerWidget extends FrameLayout { mScrollView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); mScrollView.setOverScrollMode(View.OVER_SCROLL_NEVER); // set the padding on the linear layout to the size of our scrollbar, so we don't have them overlap - ll.setPadding(ll.getPaddingLeft(), ll.getPaddingTop(), ll.getPaddingRight(), mScrollView.getVerticalScrollbarWidth()); - mScrollView.addView(ll, WIDGET_LAYOUT_PARAMS); + mButtonLayout.setPadding(mButtonLayout.getPaddingLeft(), + mButtonLayout.getPaddingTop(), + mButtonLayout.getPaddingRight(), + mScrollView.getVerticalScrollbarWidth()); + mScrollView.addView(mButtonLayout, WIDGET_LAYOUT_PARAMS); updateScrollbar(); addView(mScrollView, WIDGET_LAYOUT_PARAMS); } else { // not needed, just add the linear layout - addView(ll, WIDGET_LAYOUT_PARAMS); + addView(mButtonLayout, WIDGET_LAYOUT_PARAMS); } + } - // set up a broadcast receiver for our intents, based off of what our power buttons have been loaded - setupBroadcastReceiver(); - IntentFilter filter = PowerButton.getAllBroadcastIntentFilters(); - // we add this so we can update views and such if the settings for our widget change - filter.addAction(Settings.SETTINGS_CHANGED); - // we need to detect orientation changes and update the static button width value appropriately - filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); - // register the receiver - mContext.registerReceiver(mBroadcastReceiver, filter); - // register our observer - if(mObserver != null) { - mObserver.observe(); + public void updateAllButtons() { + // cycle through our buttons and update them + for (PowerButton pb : mButtons.values()) { + pb.update(mContext); } } - public void updateWidget() { - PowerButton.updateAllButtons(); + private IntentFilter getMergedBroadcastIntentFilter() { + IntentFilter filter = new IntentFilter(); + + for (PowerButton button : mButtons.values()) { + IntentFilter tmp = button.getBroadcastIntentFilter(); + + // cycle through these actions, and see if we need them + int num = tmp.countActions(); + for (int i = 0; i < num; i++) { + String action = tmp.getAction(i); + if(!filter.hasAction(action)) { + filter.addAction(action); + } + } + } + + // return our merged filter + return filter; } - public void setupSettingsObserver(Handler handler) { - if(mObserver == null) { - mObserver = new WidgetSettingsObserver(handler); + private List<Uri> getAllObservedUris() { + List<Uri> uris = new ArrayList<Uri>(); + + for (PowerButton button : mButtons.values()) { + List<Uri> tmp = button.getObservedUris(); + + for (Uri uri : tmp) { + if (!uris.contains(uri)) { + uris.add(uri); + } + } } + + return uris; } public void setGlobalButtonOnClickListener(View.OnClickListener listener) { - PowerButton.setGlobalOnClickListener(listener); + mAllButtonClickListener = listener; + for (PowerButton pb : mButtons.values()) { + pb.setExternalClickListener(listener); + } } public void setGlobalButtonOnLongClickListener(View.OnLongClickListener listener) { - PowerButton.setGlobalOnLongClickListener(listener); + mAllButtonLongClickListener = listener; + for (PowerButton pb : mButtons.values()) { + pb.setExternalLongClickListener(listener); + } } private void setupBroadcastReceiver() { - if(mBroadcastReceiver == null) { + if (mBroadcastReceiver == null) { mBroadcastReceiver = new WidgetBroadcastReceiver(); } } @@ -218,19 +372,50 @@ public class PowerWidget extends FrameLayout { mScrollView.setHorizontalScrollBarEnabled(!hideScrollBar); } + private void updateHapticFeedbackSetting() { + ContentResolver cr = mContext.getContentResolver(); + int expandedHapticFeedback = Settings.System.getInt(cr, + Settings.System.EXPANDED_HAPTIC_FEEDBACK, 2); + long[] clickPattern = null, longClickPattern = null; + boolean hapticFeedback; + + if (expandedHapticFeedback == 2) { + hapticFeedback = Settings.System.getInt(cr, + Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1; + } else { + hapticFeedback = (expandedHapticFeedback == 1); + } + + if (hapticFeedback) { + clickPattern = mShortPressVibePattern; + longClickPattern = mLongPressVibePattern; + } + + for (PowerButton button : mButtons.values()) { + button.setHapticFeedback(hapticFeedback, clickPattern, longClickPattern); + } + } + // our own broadcast receiver :D private class WidgetBroadcastReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { - if(intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) { + String action = intent.getAction(); + + if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { updateButtonLayoutWidth(); - setupWidget(); + recreateButtonLayout(); } else { // handle the intent through our power buttons - PowerButton.handleOnReceive(context, intent); + for (PowerButton button : mButtons.values()) { + // call "onReceive" on those that matter + if (button.getBroadcastIntentFilter().hasAction(action)) { + button.onReceive(context, intent); + } + } } // update our widget - updateWidget(); + updateAllButtons(); } }; @@ -257,6 +442,9 @@ public class PowerWidget extends FrameLayout { resolver.registerContentObserver( Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK), false, this); + resolver.registerContentObserver( + Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED), + false, this); // watch for changes in buttons resolver.registerContentObserver( @@ -274,7 +462,7 @@ public class PowerWidget extends FrameLayout { false, this); // watch for power-button specifc stuff that has been loaded - for(Uri uri : PowerButton.getAllObservedUris()) { + for(Uri uri : getAllObservedUris()) { resolver.registerContentObserver(uri, false, this); } } @@ -301,11 +489,20 @@ public class PowerWidget extends FrameLayout { updateScrollbar(); } + if (uri.equals(Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED)) + || uri.equals(Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK))) { + updateHapticFeedbackSetting(); + } + // do whatever the individual buttons must - PowerButton.handleOnChangeUri(uri); + for (PowerButton button : mButtons.values()) { + if (button.getObservedUris().contains(uri)) { + button.onChangeUri(resolver, uri); + } + } // something happened so update the widget - updateWidget(); + updateAllButtons(); } } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/ScreenTimeoutButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/ScreenTimeoutButton.java index acac53e..70a0d87 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/ScreenTimeoutButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/ScreenTimeoutButton.java @@ -5,9 +5,11 @@ import com.android.systemui.R; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.res.Resources; import android.net.Uri; import android.provider.Settings; import android.view.Gravity; +import android.view.View; import android.widget.Toast; import java.util.ArrayList; @@ -26,7 +28,7 @@ public class ScreenTimeoutButton extends PowerButton { private static final int CM_MODE_15_60_300 = 0; private static final int CM_MODE_30_120_300 = 1; - private static Toast TOAST = null; + private Toast mToast = null; private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>(); static { @@ -36,8 +38,17 @@ public class ScreenTimeoutButton extends PowerButton { public ScreenTimeoutButton() { mType = BUTTON_SCREENTIMEOUT; } @Override - protected void updateState() { - int timeout=getScreenTtimeout(mView.getContext()); + protected void setupButton(View view) { + super.setupButton(view); + if (view == null && mToast != null) { + mToast.cancel(); + mToast = null; + } + } + + @Override + protected void updateState(Context context) { + int timeout = getScreenTimeout(context); if (timeout <= SCREEN_TIMEOUT_LOW) { mIcon = R.drawable.stat_screen_timeout_off; @@ -52,59 +63,56 @@ public class ScreenTimeoutButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); - int screentimeout = getScreenTtimeout(context); + protected void toggleState(Context context) { + int screenTimeout = getScreenTimeout(context); int currentMode = getCurrentCMMode(context); - if (screentimeout < SCREEN_TIMEOUT_MIN) { + if (screenTimeout < SCREEN_TIMEOUT_MIN) { if (currentMode == CM_MODE_15_60_300) { - screentimeout = SCREEN_TIMEOUT_MIN; + screenTimeout = SCREEN_TIMEOUT_MIN; } else { - screentimeout = SCREEN_TIMEOUT_LOW; + screenTimeout = SCREEN_TIMEOUT_LOW; } - } else if (screentimeout < SCREEN_TIMEOUT_LOW) { + } else if (screenTimeout < SCREEN_TIMEOUT_LOW) { if (currentMode == CM_MODE_15_60_300) { - screentimeout = SCREEN_TIMEOUT_NORMAL; + screenTimeout = SCREEN_TIMEOUT_NORMAL; } else { - screentimeout = SCREEN_TIMEOUT_LOW; + screenTimeout = SCREEN_TIMEOUT_LOW; } - } else if (screentimeout < SCREEN_TIMEOUT_NORMAL) { + } else if (screenTimeout < SCREEN_TIMEOUT_NORMAL) { if (currentMode == CM_MODE_15_60_300) { - screentimeout = SCREEN_TIMEOUT_NORMAL; + screenTimeout = SCREEN_TIMEOUT_NORMAL; } else { - screentimeout = SCREEN_TIMEOUT_HIGH; + screenTimeout = SCREEN_TIMEOUT_HIGH; } - } else if (screentimeout < SCREEN_TIMEOUT_HIGH) { + } else if (screenTimeout < SCREEN_TIMEOUT_HIGH) { if (currentMode == CM_MODE_15_60_300) { - screentimeout = SCREEN_TIMEOUT_MAX; + screenTimeout = SCREEN_TIMEOUT_MAX; } else { - screentimeout = SCREEN_TIMEOUT_HIGH; + screenTimeout = SCREEN_TIMEOUT_HIGH; } - } else if (screentimeout < SCREEN_TIMEOUT_MAX) { - screentimeout = SCREEN_TIMEOUT_MAX; + } else if (screenTimeout < SCREEN_TIMEOUT_MAX) { + screenTimeout = SCREEN_TIMEOUT_MAX; } else if (currentMode == CM_MODE_30_120_300) { - screentimeout = SCREEN_TIMEOUT_LOW; + screenTimeout = SCREEN_TIMEOUT_LOW; } else { - screentimeout = SCREEN_TIMEOUT_MIN; + screenTimeout = SCREEN_TIMEOUT_MIN; } Settings.System.putInt( context.getContentResolver(), - Settings.System.SCREEN_OFF_TIMEOUT, screentimeout); - - // create our toast - if(TOAST == null) { - TOAST = Toast.makeText(context, "", Toast.LENGTH_LONG); - } + Settings.System.SCREEN_OFF_TIMEOUT, screenTimeout); // cancel any previous toast - TOAST.cancel(); + if (mToast != null) { + mToast.cancel(); + } // inform users of how long the timeout is now - TOAST.setText("Screen timeout set to: " + timeoutToString(screentimeout)); - TOAST.setGravity(Gravity.CENTER, TOAST.getXOffset() / 2, TOAST.getYOffset() / 2); - TOAST.show(); + final String toast = makeTimeoutToastString(context, screenTimeout); + mToast = Toast.makeText(context, toast, Toast.LENGTH_LONG); + mToast.setGravity(Gravity.CENTER, mToast.getXOffset() / 2, mToast.getYOffset() / 2); + mToast.show(); } @Override @@ -113,39 +121,51 @@ public class ScreenTimeoutButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.DISPLAY_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } - private static int getScreenTtimeout(Context context) { + private String makeTimeoutToastString(Context context, int timeout) { + Resources res = context.getResources(); + int resId; + + /* ms -> seconds */ + timeout /= 1000; + + if (timeout >= 60 && timeout % 60 == 0) { + /* seconds -> minutes */ + timeout /= 60; + if (timeout >= 60 && timeout % 60 == 0) { + /* minutes -> hours */ + timeout /= 60; + resId = timeout == 1 + ? com.android.internal.R.string.hour + : com.android.internal.R.string.hours; + } else { + resId = timeout == 1 + ? com.android.internal.R.string.minute + : com.android.internal.R.string.minutes; + } + } else { + resId = timeout == 1 + ? com.android.internal.R.string.second + : com.android.internal.R.string.seconds; + } + + return res.getString(R.string.powerwidget_screen_timeout_toast, + timeout, res.getString(resId)); + } + + private static int getScreenTimeout(Context context) { return Settings.System.getInt( context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 0); } - private static String timeoutToString(int timeout) { - String[] tags = new String[] { - "second(s)", - "minute(s)", - "hour(s)" - }; - - // default to however many seconds we have - int tmp = (timeout / 1000); - String sTimeout = tmp + " " + tags[0]; - - for(int i = 1; i < tags.length && tmp >= 60; i++) { - tmp /= (60 * i); - sTimeout = tmp + " " + tags[i]; - } - - return sTimeout; - } - private static int getCurrentCMMode(Context context) { return Settings.System.getInt(context.getContentResolver(), Settings.System.EXPANDED_SCREENTIMEOUT_MODE, diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SleepButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SleepButton.java index 61fecc9..65f3963 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SleepButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SleepButton.java @@ -13,24 +13,24 @@ public class SleepButton extends PowerButton { public SleepButton() { mType = BUTTON_SLEEP; } @Override - protected void updateState() { + protected void updateState(Context context) { mIcon = R.drawable.stat_sleep; mState = STATE_DISABLED; } @Override - protected void toggleState() { - PowerManager pm = (PowerManager)mView.getContext() - .getSystemService(Context.POWER_SERVICE); + protected void toggleState(Context context) { + PowerManager pm = (PowerManager) + context.getSystemService(Context.POWER_SERVICE); pm.goToSleep(SystemClock.uptimeMillis() + 1); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.DISPLAY_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SoundButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SoundButton.java index 20b96a6..f248377 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SoundButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SoundButton.java @@ -9,7 +9,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.media.AudioManager; import android.net.Uri; -import android.os.Vibrator; import android.preference.ListPreferenceMultiSelect; import android.provider.Settings; import android.view.View; @@ -21,7 +20,7 @@ public class SoundButton extends PowerButton { private static final String TAG = "SoundButton"; - private static final int VIBRATE_DURATION = 500; // 0.5s + private static final int VIBRATE_DURATION = 250; // 0.25s private static final IntentFilter INTENT_FILTER = new IntentFilter(); static { @@ -32,8 +31,6 @@ public class SoundButton extends PowerButton { private static final List<Uri> OBSERVED_URIS = new ArrayList<Uri>(); static { OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_RING_MODE)); - OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_HAPTIC_FEEDBACK)); - OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED)); } private final Ringer mSilentRinger = new Ringer(false, AudioManager.VIBRATE_SETTING_OFF, @@ -54,10 +51,7 @@ public class SoundButton extends PowerButton { }; private int mRingerValuesIndex = 2; - private boolean mHapticFeedbackEnabled = false; - private AudioManager mAudioManager; - private Vibrator mVibrator; public SoundButton() { mType = BUTTON_SOUND; @@ -68,15 +62,13 @@ public class SoundButton extends PowerButton { super.setupButton(view); if (mView != null) { Context context = mView.getContext(); - mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - updateSettings(); + updateSettings(context.getContentResolver()); } } @Override - protected void updateState() { - findCurrentState(); + protected void updateState(Context context) { + findCurrentState(context); switch (mRingersIndex) { case 0: mIcon = R.drawable.stat_silent; @@ -104,7 +96,7 @@ public class SoundButton extends PowerButton { } @Override - protected void toggleState() { + protected void toggleState(Context context) { mRingerValuesIndex++; if (mRingerValuesIndex > mRingerValues.length - 1) { mRingerValuesIndex = 0; @@ -114,21 +106,21 @@ public class SoundButton extends PowerButton { mRingersIndex = 0; } Ringer ringer = mRingers[mRingersIndex]; - ringer.execute(); + ringer.execute(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.SOUND_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } @Override - protected void onChangeUri(Uri uri) { - updateSettings(); + protected void onChangeUri(ContentResolver cr, Uri uri) { + updateSettings(cr); } @Override @@ -141,18 +133,7 @@ public class SoundButton extends PowerButton { return INTENT_FILTER; } - private void updateSettings() { - ContentResolver resolver = mView.getContext().getContentResolver(); - - int expandedHapticFeedback = Settings.System.getInt(resolver, - Settings.System.EXPANDED_HAPTIC_FEEDBACK, 2); - if (expandedHapticFeedback == 2) { - mHapticFeedbackEnabled = (Settings.System.getInt(resolver, - Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) == 1); - } else { - mHapticFeedbackEnabled = (expandedHapticFeedback == 1); - } - + private void updateSettings(ContentResolver resolver) { String[] modes = ListPreferenceMultiSelect.parseStoredValue(Settings.System.getString( resolver, Settings.System.EXPANDED_RING_MODE)); if (modes == null || modes.length == 0) { @@ -165,13 +146,12 @@ public class SoundButton extends PowerButton { mRingerValues[i] = Integer.valueOf(modes[i]); } } - - updateState(); } - private void findCurrentState() { - ContentResolver resolver = mView.getContext().getContentResolver(); - boolean vibrateInSilent = Settings.System.getInt(resolver, + private void findCurrentState(Context context) { + ensureAudioManager(context); + + boolean vibrateInSilent = Settings.System.getInt(context.getContentResolver(), Settings.System.VIBRATE_IN_SILENT, 0) == 1; int vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); int ringerMode = mAudioManager.getRingerMode(); @@ -198,8 +178,13 @@ public class SoundButton extends PowerButton { } } - private class Ringer { + private void ensureAudioManager(Context context) { + if (mAudioManager == null) { + mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + } + } + private class Ringer { final boolean mVibrateInSilent; final int mVibrateSetting; final int mRingerMode; @@ -212,13 +197,15 @@ public class SoundButton extends PowerButton { mDoHapticFeedback = doHapticFeedback; } - void execute() { - ContentResolver resolver = mView.getContext().getContentResolver(); + void execute(Context context) { + ContentResolver resolver = context.getContentResolver(); Settings.System.putInt(resolver, Settings.System.VIBRATE_IN_SILENT, (mVibrateInSilent ? 1 : 0)); + + ensureAudioManager(context); mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, mVibrateSetting); mAudioManager.setRingerMode(mRingerMode); - if (mDoHapticFeedback && mHapticFeedbackEnabled) { + if (mDoHapticFeedback && mHapticFeedback) { mVibrator.vibrate(VIBRATE_DURATION); } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SyncButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SyncButton.java index 81167c2..3c5f547 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SyncButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SyncButton.java @@ -18,7 +18,9 @@ public class SyncButton extends PowerButton { private SyncStatusObserver mSyncObserver = new SyncStatusObserver() { public void onStatusChanged(int which) { // update state/view if something happened - update(); + if (mView != null) { + update(mView.getContext()); + } } }; private Object mSyncObserverHandle = null; @@ -38,8 +40,8 @@ public class SyncButton extends PowerButton { } @Override - protected void updateState() { - if (getSyncState(mView.getContext())) { + protected void updateState(Context context) { + if (getSyncState(context)) { mIcon = R.drawable.stat_sync_on; mState = STATE_ENABLED; } else { @@ -49,10 +51,9 @@ public class SyncButton extends PowerButton { } @Override - protected void toggleState() { - Context context = mView.getContext(); - ConnectivityManager connManager = (ConnectivityManager)context - .getSystemService(Context.CONNECTIVITY_SERVICE); + protected void toggleState(Context context) { + ConnectivityManager connManager = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); boolean backgroundData = getBackgroundDataState(context); boolean sync = ContentResolver.getMasterSyncAutomatically(); @@ -82,21 +83,21 @@ public class SyncButton extends PowerButton { } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.SYNC_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } - private static boolean getBackgroundDataState(Context context) { - ConnectivityManager connManager = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); + private boolean getBackgroundDataState(Context context) { + ConnectivityManager connManager = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); return connManager.getBackgroundDataSetting(); } - private static boolean getSyncState(Context context) { + private boolean getSyncState(Context context) { boolean backgroundData = getBackgroundDataState(context); boolean sync = ContentResolver.getMasterSyncAutomatically(); return backgroundData && sync; diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiApButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiApButton.java index 3df4578..f90fb87 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiApButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiApButton.java @@ -99,8 +99,8 @@ public class WifiApButton extends PowerButton { public WifiApButton() { mType = BUTTON_WIFIAP; } @Override - protected void updateState() { - mState = sWifiApState.getTriState(mView.getContext()); + protected void updateState(Context context) { + mState = sWifiApState.getTriState(context); switch (mState) { case STATE_DISABLED: mIcon = R.drawable.stat_wifi_ap_off; @@ -124,18 +124,18 @@ public class WifiApButton extends PowerButton { } @Override - protected void toggleState() { - sWifiApState.toggleState(mView.getContext()); + protected void toggleState(Context context) { + sWifiApState.toggleState(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { // it may be better to make an Intent action for the WifiAp settings // we may want to look at that option later Intent intent = new Intent(Intent.ACTION_MAIN); intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiButton.java index bc0aca9..b55ba36 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WifiButton.java @@ -95,8 +95,8 @@ public class WifiButton extends PowerButton{ public WifiButton() { mType = BUTTON_WIFI; } @Override - protected void updateState() { - mState = sWifiState.getTriState(mView.getContext()); + protected void updateState(Context context) { + mState = sWifiState.getTriState(context); switch (mState) { case STATE_DISABLED: mIcon = R.drawable.stat_wifi_off; @@ -120,16 +120,16 @@ public class WifiButton extends PowerButton{ } @Override - protected void toggleState() { - sWifiState.toggleState(mView.getContext()); + protected void toggleState(Context context) { + sWifiState.toggleState(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.settings.WIFI_SETTINGS"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WimaxButton.java b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WimaxButton.java index f4f9f9d..6f065b5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WimaxButton.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/WimaxButton.java @@ -105,8 +105,8 @@ public class WimaxButton extends PowerButton { public WimaxButton() { mType = BUTTON_WIMAX; } @Override - protected void updateState() { - mState = sWimaxState.getTriState(mView.getContext()); + protected void updateState(Context context) { + mState = sWimaxState.getTriState(context); switch (mState) { case STATE_DISABLED: mIcon = R.drawable.stat_wimax_off; @@ -130,17 +130,17 @@ public class WimaxButton extends PowerButton { } @Override - protected void toggleState() { - sWimaxState.toggleState(mView.getContext()); + protected void toggleState(Context context) { + sWimaxState.toggleState(context); } @Override - protected boolean handleLongClick() { + protected boolean handleLongClick(Context context) { Intent intent = new Intent("android.intent.action.MAIN"); intent.setClassName("com.android.settings.wimax", "com.android.settings.wimax.WimaxSettings"); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mView.getContext().startActivity(intent); + context.startActivity(intent); return true; } |