|
| 1 | +package com.android.systemui.quicksettings; |
| 2 | + |
| 3 | +import android.content.ContentResolver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.content.res.Resources; |
| 8 | +import android.net.Uri; |
| 9 | +import android.provider.Settings; |
| 10 | +import android.view.Gravity; |
| 11 | +import android.view.LayoutInflater; |
| 12 | +import android.view.View; |
| 13 | +import android.view.View.OnClickListener; |
| 14 | +import android.view.View.OnLongClickListener; |
| 15 | +import android.widget.Toast; |
| 16 | + |
| 17 | +import com.android.systemui.R; |
| 18 | +import com.android.systemui.statusbar.phone.QuickSettingsController; |
| 19 | +import com.android.systemui.statusbar.phone.QuickSettingsContainerView; |
| 20 | + |
| 21 | +public class ScreenTimeoutTile extends QuickSettingsTile { |
| 22 | + |
| 23 | + // timeout values |
| 24 | + private static final int SCREEN_TIMEOUT_MIN = 15000; |
| 25 | + private static final int SCREEN_TIMEOUT_LOW = 30000; |
| 26 | + private static final int SCREEN_TIMEOUT_NORMAL = 60000; |
| 27 | + private static final int SCREEN_TIMEOUT_HIGH = 120000; |
| 28 | + private static final int SCREEN_TIMEOUT_MAX = 300000; |
| 29 | + |
| 30 | + // cm modes |
| 31 | + private static final int CM_MODE_15_60_300 = 0; |
| 32 | + private static final int CM_MODE_30_120_300 = 1; |
| 33 | + |
| 34 | + public ScreenTimeoutTile(Context context, |
| 35 | + LayoutInflater inflater, QuickSettingsContainerView container, QuickSettingsController qsc) { |
| 36 | + super(context, inflater, container, qsc); |
| 37 | + |
| 38 | + updateTileState(); |
| 39 | + |
| 40 | + mOnClick = new OnClickListener() { |
| 41 | + @Override |
| 42 | + public void onClick(View v) { |
| 43 | + toggleState(); |
| 44 | + applyTimeoutChanges(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + mOnLongClick = new OnLongClickListener() { |
| 49 | + @Override |
| 50 | + public boolean onLongClick(View v) { |
| 51 | + Intent intent = new Intent("android.settings.DISPLAY_SETTINGS"); |
| 52 | + startSettingsActivity(intent); |
| 53 | + return true; |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + qsc.registerObservedContent(Settings.System.getUriFor(Settings.System.SCREEN_OFF_TIMEOUT) |
| 58 | + , this); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onChangeUri(ContentResolver resolver, Uri uri) { |
| 63 | + applyTimeoutChanges(); |
| 64 | + } |
| 65 | + |
| 66 | + void applyTimeoutChanges() { |
| 67 | + updateTileState(); |
| 68 | + updateQuickSettings(); |
| 69 | + } |
| 70 | + |
| 71 | + protected void updateTileState() { |
| 72 | + int timeout = getScreenTimeout(); |
| 73 | + mLabel = makeTimeoutSummaryString(mContext, timeout); |
| 74 | + mDrawable = R.drawable.ic_qs_screen_timeout_off; |
| 75 | + |
| 76 | + /* TODO: Determine if we need an on and off state |
| 77 | + if (timeout <= SCREEN_TIMEOUT_LOW) { |
| 78 | + mDrawable = R.drawable.ic_qs_screen_timeout_off; |
| 79 | + } else if (timeout <= SCREEN_TIMEOUT_HIGH) { |
| 80 | + mDrawable = R.drawable.ic_qs_screen_timeout_off; |
| 81 | + } else { |
| 82 | + mDrawable = R.drawable.ic_qs_screen_timeout_on; |
| 83 | + } |
| 84 | + */ |
| 85 | + } |
| 86 | + |
| 87 | + protected void toggleState() { |
| 88 | + int screenTimeout = getScreenTimeout(); |
| 89 | + int currentMode = getCurrentCMMode(); |
| 90 | + |
| 91 | + if (screenTimeout < SCREEN_TIMEOUT_MIN) { |
| 92 | + if (currentMode == CM_MODE_15_60_300) { |
| 93 | + screenTimeout = SCREEN_TIMEOUT_MIN; |
| 94 | + } else { |
| 95 | + screenTimeout = SCREEN_TIMEOUT_LOW; |
| 96 | + } |
| 97 | + } else if (screenTimeout < SCREEN_TIMEOUT_LOW) { |
| 98 | + if (currentMode == CM_MODE_15_60_300) { |
| 99 | + screenTimeout = SCREEN_TIMEOUT_NORMAL; |
| 100 | + } else { |
| 101 | + screenTimeout = SCREEN_TIMEOUT_LOW; |
| 102 | + } |
| 103 | + } else if (screenTimeout < SCREEN_TIMEOUT_NORMAL) { |
| 104 | + if (currentMode == CM_MODE_15_60_300) { |
| 105 | + screenTimeout = SCREEN_TIMEOUT_NORMAL; |
| 106 | + } else { |
| 107 | + screenTimeout = SCREEN_TIMEOUT_HIGH; |
| 108 | + } |
| 109 | + } else if (screenTimeout < SCREEN_TIMEOUT_HIGH) { |
| 110 | + if (currentMode == CM_MODE_15_60_300) { |
| 111 | + screenTimeout = SCREEN_TIMEOUT_MAX; |
| 112 | + } else { |
| 113 | + screenTimeout = SCREEN_TIMEOUT_HIGH; |
| 114 | + } |
| 115 | + } else if (screenTimeout < SCREEN_TIMEOUT_MAX) { |
| 116 | + screenTimeout = SCREEN_TIMEOUT_MAX; |
| 117 | + } else if (currentMode == CM_MODE_30_120_300) { |
| 118 | + screenTimeout = SCREEN_TIMEOUT_LOW; |
| 119 | + } else { |
| 120 | + screenTimeout = SCREEN_TIMEOUT_MIN; |
| 121 | + } |
| 122 | + |
| 123 | + Settings.System.putInt( |
| 124 | + mContext.getContentResolver(), |
| 125 | + Settings.System.SCREEN_OFF_TIMEOUT, screenTimeout); |
| 126 | + } |
| 127 | + |
| 128 | + private String makeTimeoutSummaryString(Context context, int timeout) { |
| 129 | + Resources res = context.getResources(); |
| 130 | + int resId; |
| 131 | + |
| 132 | + /* ms -> seconds */ |
| 133 | + timeout /= 1000; |
| 134 | + |
| 135 | + if (timeout >= 60 && timeout % 60 == 0) { |
| 136 | + /* seconds -> minutes */ |
| 137 | + timeout /= 60; |
| 138 | + if (timeout >= 60 && timeout % 60 == 0) { |
| 139 | + /* minutes -> hours */ |
| 140 | + timeout /= 60; |
| 141 | + resId = timeout == 1 |
| 142 | + ? com.android.internal.R.string.hour |
| 143 | + : com.android.internal.R.string.hours; |
| 144 | + } else { |
| 145 | + resId = timeout == 1 |
| 146 | + ? com.android.internal.R.string.minute |
| 147 | + : com.android.internal.R.string.minutes; |
| 148 | + } |
| 149 | + } else { |
| 150 | + resId = timeout == 1 |
| 151 | + ? com.android.internal.R.string.second |
| 152 | + : com.android.internal.R.string.seconds; |
| 153 | + } |
| 154 | + |
| 155 | + return res.getString(R.string.quick_settings_screen_timeout_summary, |
| 156 | + timeout, res.getString(resId)); |
| 157 | + } |
| 158 | + |
| 159 | + private int getScreenTimeout() { |
| 160 | + return Settings.System.getInt(mContext.getContentResolver(), |
| 161 | + Settings.System.SCREEN_OFF_TIMEOUT, 0); |
| 162 | + } |
| 163 | + |
| 164 | + private int getCurrentCMMode() { |
| 165 | + return Settings.System.getInt(mContext.getContentResolver(), |
| 166 | + Settings.System.EXPANDED_SCREENTIMEOUT_MODE, CM_MODE_15_60_300); |
| 167 | + } |
| 168 | +} |
0 commit comments