diff options
author | Keith Mok <kmok@cyngn.com> | 2016-02-04 11:10:22 -0800 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2016-02-11 17:06:13 -0800 |
commit | 4522c9ea9b579a33c1a3ebb9fd3b113a637ffbd7 (patch) | |
tree | c2604414a542a0c8b9c051974fa796a1f8cb857c /packages/SystemUI/src | |
parent | ef5b0f47e3034eeafc7eb89af6e1edb9e4c20668 (diff) | |
download | frameworks_base-4522c9ea9b579a33c1a3ebb9fd3b113a637ffbd7.zip frameworks_base-4522c9ea9b579a33c1a3ebb9fd3b113a637ffbd7.tar.gz frameworks_base-4522c9ea9b579a33c1a3ebb9fd3b113a637ffbd7.tar.bz2 |
SystemUI: add caffeine qs tile (1/2)
Change-Id: I5d4aa97ae63fc0bcc40f0fa7d9305efbba091029
Diffstat (limited to 'packages/SystemUI/src')
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/qs/tiles/CaffeineTile.java | 202 | ||||
-rw-r--r-- | packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java | 4 |
2 files changed, 206 insertions, 0 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/CaffeineTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/CaffeineTile.java new file mode 100644 index 0000000..fb6ec42 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/CaffeineTile.java @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2016 The CyanogenMod 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.qs.tiles; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.database.ContentObserver; +import android.net.Uri; +import android.os.CountDownTimer; +import android.os.PowerManager; +import android.os.SystemClock; +import android.provider.Settings; + +import com.android.systemui.qs.QSTile; +import com.android.systemui.R; + +import org.cyanogenmod.internal.logging.CMMetricsLogger; + +/** Quick settings tile: Caffeine **/ +public class CaffeineTile extends QSTile<QSTile.BooleanState> { + + private final PowerManager.WakeLock mWakeLock; + private int mSecondsRemaining; + private int mDuration; + private static int[] DURATIONS = new int[] { + 5 * 60, // 5 min + 10 * 60, // 10 min + 30 * 60, // 30 min + -1, // infinity + }; + private CountDownTimer mCountdownTimer = null; + public long mLastClickTime = -1; + private final Receiver mReceiver = new Receiver(); + private boolean mListening; + + public CaffeineTile(Host host) { + super(host); + mWakeLock = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE)).newWakeLock( + PowerManager.FULL_WAKE_LOCK, "CaffeineTile"); + mReceiver.init(); + } + + @Override + protected BooleanState newTileState() { + return new BooleanState(); + } + + @Override + protected void handleDestroy() { + super.handleDestroy(); + stopCountDown(); + mReceiver.destroy(); + if (mWakeLock.isHeld()) { + mWakeLock.release(); + } + } + + @Override + public void setListening(boolean listening) { + } + + @Override + public void handleClick() { + // If last user clicks < 5 seconds + // we cycle different duration + // otherwise toggle on/off + if (mWakeLock.isHeld() && (mLastClickTime != -1) && + (SystemClock.elapsedRealtime() - mLastClickTime < 5000)) { + // cycle duration + mDuration++; + if (mDuration >= DURATIONS.length) { + // all durations cycled, turn if off + mDuration = -1; + stopCountDown(); + if (mWakeLock.isHeld()) { + mWakeLock.release(); + } + } else { + // change duration + startCountDown(DURATIONS[mDuration]); + if (!mWakeLock.isHeld()) { + mWakeLock.acquire(); + } + } + } else { + // toggle + if (mWakeLock.isHeld()) { + mWakeLock.release(); + stopCountDown(); + } else { + mWakeLock.acquire(); + mDuration = 0; + startCountDown(DURATIONS[mDuration]); + } + } + mLastClickTime = SystemClock.elapsedRealtime(); + refreshState(); + } + + private void startCountDown(long duration) { + stopCountDown(); + mSecondsRemaining = (int)duration; + if (duration == -1) { + // infinity timing, no need to start timer + return; + } + mCountdownTimer = new CountDownTimer(duration * 1000, 1000) { + + @Override + public void onTick(long millisUntilFinished) { + mSecondsRemaining = (int) (millisUntilFinished / 1000); + refreshState(); + } + + @Override + public void onFinish() { + if (mWakeLock.isHeld()) + mWakeLock.release(); + refreshState(); + } + + }.start(); + } + + private void stopCountDown() { + if (mCountdownTimer != null) { + mCountdownTimer.cancel(); + mCountdownTimer = null; + } + } + + private String formatValueWithRemainingTime() { + if (mSecondsRemaining == -1) { + return "\u221E"; // infinity + } + return String.format("%02d:%02d", + mSecondsRemaining / 60 % 60, mSecondsRemaining % 60); + } + + @Override + protected void handleUpdateState(BooleanState state, Object arg) { + state.value = mWakeLock.isHeld(); + state.visible = true; + if (state.value) { + state.label = formatValueWithRemainingTime(); + state.icon = ResourceIcon.get(R.drawable.ic_qs_caffeine_on); + state.contentDescription = mContext.getString( + R.string.accessibility_quick_settings_caffeine_on); + } else { + state.label = mContext.getString(R.string.quick_settings_caffeine_label); + state.icon = ResourceIcon.get(R.drawable.ic_qs_caffeine_off); + state.contentDescription = mContext.getString( + R.string.accessibility_quick_settings_caffeine_off); + } + } + + @Override + public int getMetricsCategory() { + return CMMetricsLogger.TILE_CAFFEINE; + } + + private final class Receiver extends BroadcastReceiver { + public void init() { + // Register for Intent broadcasts for... + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_SCREEN_OFF); + mContext.registerReceiver(this, filter, null, mHandler); + } + + public void destroy() { + mContext.unregisterReceiver(this); + } + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (Intent.ACTION_SCREEN_OFF.equals(action)) { + // disable caffeine if user force off (power button) + stopCountDown(); + if (mWakeLock.isHeld()) + mWakeLock.release(); + refreshState(); + } + } + } +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java index c4ce350..b776a9f 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java @@ -42,6 +42,7 @@ import com.android.systemui.qs.tiles.AirplaneModeTile; import com.android.systemui.qs.tiles.AmbientDisplayTile; import com.android.systemui.qs.tiles.BatterySaverTile; import com.android.systemui.qs.tiles.BluetoothTile; +import com.android.systemui.qs.tiles.CaffeineTile; import com.android.systemui.qs.tiles.CastTile; import com.android.systemui.qs.tiles.CellularTile; import com.android.systemui.qs.tiles.ColorInversionTile; @@ -363,6 +364,7 @@ public class QSTileHost implements QSTile.Host, Tunable { else if (tileSpec.equals("live_display")) return new LiveDisplayTile(this); else if (tileSpec.equals("heads_up")) return new HeadsUpTile(this); else if (tileSpec.equals("battery_saver")) return new BatterySaverTile(this); + else if (tileSpec.equals("caffeine")) return new CaffeineTile(this); else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec); else throw new IllegalArgumentException("Bad tile spec: " + tileSpec); } @@ -456,6 +458,7 @@ public class QSTileHost implements QSTile.Host, Tunable { else if (spec.equals("live_display")) return R.string.live_display_title; else if (spec.equals("heads_up")) return R.string.quick_settings_heads_up_label; else if (spec.equals("battery_saver")) return R.string.quick_settings_battery_saver_label; + else if (spec.equals("caffeine")) return R.string.quick_settings_caffeine_label; return 0; } @@ -486,6 +489,7 @@ public class QSTileHost implements QSTile.Host, Tunable { else if (spec.equals("live_display")) return R.drawable.ic_livedisplay_auto; else if (spec.equals("heads_up")) return R.drawable.ic_qs_heads_up_on; else if (spec.equals("battery_saver")) return R.drawable.ic_qs_battery_saver_on; + else if (spec.equals("caffeine")) return R.drawable.ic_qs_caffeine_on; return 0; } |