Skip to content

Commit 8531c09

Browse files
DvTonderGodFox
authored andcommitted
Framework: Add Screen timeout Quick Settings Tile (2 of 2)
1 parent b613a6e commit 8531c09

9 files changed

Lines changed: 177 additions & 1 deletion

File tree

3.94 KB
Loading
3.98 KB
Loading
3.42 KB
Loading
3.41 KB
Loading
4.13 KB
Loading
4.04 KB
Loading

packages/SystemUI/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@
541541
<string name="quick_settings_sync">Sync</string>
542542
<string name="quick_settings_torch">Torch</string>
543543
<string name="quick_settings_nfc">NFC</string>
544+
<string name="quick_settings_screen_timeout">Timeout</string>
545+
<string name="quick_settings_screen_timeout_summary">%1$d %2$s</string>
544546

545547
<!-- Text to display next to the minimal graphical battery meter. [CHAR LIMIT=3] -->
546548
<string name="status_bar_settings_battery_meter_min_format" translatable="false">
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.android.systemui.quicksettings.BrightnessTile;
4343
import com.android.systemui.quicksettings.BugReportTile;
4444
import com.android.systemui.quicksettings.NfcTile;
45+
import com.android.systemui.quicksettings.ScreenTimeoutTile;
4546
import com.android.systemui.quicksettings.TorchTile;
4647
import com.android.systemui.quicksettings.GPSTile;
4748
import com.android.systemui.quicksettings.InputMethodTile;
@@ -144,6 +145,7 @@ public class QuickSettingsController {
144145
public static final int PROFILE_TILE = 19;
145146
public static final int SYNC_TILE = 20;
146147
public static final int NFC_TILE = 21;
148+
public static final int SCREENTIMEOUT_TILE = 22;
147149
public static final int USER_TILE = 99;
148150
private InputMethodTile IMETile;
149151

@@ -196,7 +198,7 @@ void loadTiles() {
196198
mQuickSettings.add(WIFIAP_TILE);
197199
}
198200
} else if (tile.equals(TILE_SCREENTIMEOUT)) {
199-
// Not available yet
201+
mQuickSettings.add(SCREENTIMEOUT_TILE);
200202
} else if (tile.equals(TILE_MOBILEDATA)) {
201203
if(deviceSupportsTelephony()) {
202204
mQuickSettings.add(MOBILE_NETWORK_TILE);
@@ -446,6 +448,10 @@ void addQuickSettings(LayoutInflater inflater){
446448
qs = new NfcTile(mContext, inflater,
447449
(QuickSettingsContainerView) mContainerView, this);
448450
break;
451+
case SCREENTIMEOUT_TILE:
452+
qs = new ScreenTimeoutTile(mContext, inflater,
453+
(QuickSettingsContainerView) mContainerView, this);
454+
break;
449455
}
450456
if (qs != null) {
451457
qs.setupQuickSettingsTile();

0 commit comments

Comments
 (0)