1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright (C) 2012 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.settings.notificationlight;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.settings.R;
public class ApplicationLightPreference extends DialogPreference {
private static String TAG = "AppLightPreference";
public static final int DEFAULT_TIME = 1000;
public static final int DEFAULT_COLOR = 0xFFFFFF; //White
private ImageView mLightColorView;
private TextView mOnValueView;
private TextView mOffValueView;
private int mColorValue;
private int mOnValue;
private int mOffValue;
private boolean mOnOffChangeable;
private Resources mResources;
private ScreenReceiver mReceiver = null;
private AlertDialog mTestDialog;
/**
* @param context
* @param attrs
*/
public ApplicationLightPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mColorValue = DEFAULT_COLOR;
mOnValue = DEFAULT_TIME;
mOffValue = DEFAULT_TIME;
mOnOffChangeable = true;
init();
}
/**
* @param context
* @param color
* @param onValue
* @param offValue
*/
public ApplicationLightPreference(Context context, int color, int onValue, int offValue) {
super(context, null);
mColorValue = color;
mOnValue = onValue;
mOffValue = offValue;
mOnOffChangeable = true;
init();
}
/**
* @param context
* @param onLongClickListener
* @param color
* @param onValue
* @param offValue
*/
public ApplicationLightPreference(Context context, int color, int onValue, int offValue, boolean onOffChangeable) {
super(context, null);
mColorValue = color;
mOnValue = onValue;
mOffValue = offValue;
mOnOffChangeable = onOffChangeable;
init();
}
private void init() {
setLayoutResource(R.layout.preference_application_light);
mResources = getContext().getResources();
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
mLightColorView = (ImageView) view.findViewById(R.id.light_color);
mOnValueView = (TextView) view.findViewById(R.id.textViewTimeOnValue);
mOffValueView = (TextView) view.findViewById(R.id.textViewTimeOffValue);
// Hide the summary text - it takes up too much space on a low res device
// We use it for storing the package name for the longClickListener
TextView tView = (TextView) view.findViewById(android.R.id.summary);
tView.setVisibility(View.GONE);
updatePreferenceViews();
}
private void updatePreferenceViews() {
final int width = (int) mResources.getDimension(R.dimen.device_memory_usage_button_width);
final int height = (int) mResources.getDimension(R.dimen.device_memory_usage_button_height);
if (mLightColorView != null) {
mLightColorView.setEnabled(true);
mLightColorView.setImageDrawable(createRectShape(width, height, 0xFF000000 + mColorValue));
}
if (mOnValueView != null) {
mOnValueView.setText(mapLengthValue(mOnValue));
}
if (mOffValueView != null) {
if (mOnValue == 1) {
mOffValueView.setVisibility(View.GONE);
} else {
mOffValueView.setVisibility(View.VISIBLE);
}
mOffValueView.setText(mapSpeedValue(mOffValue));
}
}
@Override
protected void showDialog(Bundle state) {
super.showDialog(state);
final LightSettingsDialog d = (LightSettingsDialog) getDialog();
// Intercept the click on the middle button to show the test dialog and prevent the onDismiss
d.findViewById(android.R.id.button3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int onTime = d.getPulseSpeedOn();
int offTime = d.getPulseSpeedOff();
showTestDialog(d.getColor() - 0xFF000000, onTime, offTime);
}
});
}
@Override
protected Dialog createDialog() {
final LightSettingsDialog d = new LightSettingsDialog(getContext(),
0xFF000000 + mColorValue, mOnValue, mOffValue, mOnOffChangeable);
d.setAlphaSliderVisible(false);
d.setButton(AlertDialog.BUTTON_POSITIVE, mResources.getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mColorValue = d.getColor() - 0xFF000000; // strip alpha, led does not support it
mOnValue = d.getPulseSpeedOn();
mOffValue = d.getPulseSpeedOff();
updatePreferenceViews();
callChangeListener(this);
}
});
d.setButton(AlertDialog.BUTTON_NEUTRAL, mResources.getString(R.string.dialog_test),
(DialogInterface.OnClickListener) null);
d.setButton(AlertDialog.BUTTON_NEGATIVE, mResources.getString(R.string.cancel),
(DialogInterface.OnClickListener) null);
return d;
}
private void showTestDialog(int color, int speedOn, int speedOff) {
final Context context = getContext();
if (mReceiver != null) {
context.getApplicationContext().unregisterReceiver(mReceiver);
}
if (mTestDialog != null) {
mTestDialog.dismiss();
}
mReceiver = new ScreenReceiver(color, speedOn, speedOff);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
context.getApplicationContext().registerReceiver(mReceiver, filter);
mTestDialog = new AlertDialog.Builder(context)
.setTitle(R.string.dialog_test)
.setMessage(R.string.dialog_test_message)
.setPositiveButton(R.string.dialog_test_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mReceiver != null) {
context.getApplicationContext().unregisterReceiver(mReceiver);
mReceiver = null;
}
}
})
.create();
mTestDialog.show();
}
/**
* Getters and Setters
*/
public int getColor() {
return mColorValue;
}
public void setColor(int color) {
mColorValue = color;
updatePreferenceViews();
}
public void setOnValue(int value) {
mOnValue = value;
updatePreferenceViews();
}
public int getOnValue() {
return mOnValue;
}
public void setOffValue(int value) {
mOffValue = value;
updatePreferenceViews();
}
public int getOffValue() {
return mOffValue;
}
public void setAllValues(int color, int onValue, int offValue) {
mColorValue = color;
mOnValue = onValue;
mOffValue = offValue;
mOnOffChangeable = true;
updatePreferenceViews();
}
public void setAllValues(int color, int onValue, int offValue, boolean onOffChangeable) {
mColorValue = color;
mOnValue = onValue;
mOffValue = offValue;
mOnOffChangeable = onOffChangeable;
updatePreferenceViews();
}
public void setOnOffValue(int onValue, int offValue) {
mOnValue = onValue;
mOffValue = offValue;
updatePreferenceViews();
}
public void setOnOffChangeable(boolean value) {
mOnOffChangeable = value;
}
/**
* Utility methods
*/
public class ScreenReceiver extends BroadcastReceiver {
protected int timeon;
protected int timeoff;
protected int color;
public ScreenReceiver(int color, int timeon, int timeoff) {
this.timeon = timeon;
this.timeoff = timeoff;
this.color = color;
}
@Override
public void onReceive(Context context, Intent intent) {
final NotificationManager nm =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Notification.Builder builder = new Notification.Builder(context);
builder.setAutoCancel(true);
builder.setLights(color, timeon, timeoff);
Notification n = builder.getNotification();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
nm.notify(1, n);
} else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
nm.cancel(1);
context.getApplicationContext().unregisterReceiver(mReceiver);
mReceiver = null;
mTestDialog.dismiss();
mTestDialog = null;
}
}
}
private static ShapeDrawable createRectShape(int width, int height, int color) {
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.setIntrinsicHeight(height);
shape.setIntrinsicWidth(width);
shape.getPaint().setColor(color);
return shape;
}
private String mapLengthValue(Integer time) {
if (time == DEFAULT_TIME) {
return getContext().getString(R.string.default_time);
}
String[] timeNames = mResources.getStringArray(R.array.notification_pulse_length_entries);
String[] timeValues = mResources.getStringArray(R.array.notification_pulse_length_values);
for (int i = 0; i < timeValues.length; i++) {
if (Integer.decode(timeValues[i]).equals(time)) {
return timeNames[i];
}
}
return getContext().getString(R.string.custom_time);
}
private String mapSpeedValue(Integer time) {
if (time == DEFAULT_TIME) {
return getContext().getString(R.string.default_time);
}
String[] timeNames = mResources.getStringArray(R.array.notification_pulse_speed_entries);
String[] timeValues = mResources.getStringArray(R.array.notification_pulse_speed_values);
for (int i = 0; i < timeValues.length; i++) {
if (Integer.decode(timeValues[i]).equals(time)) {
return timeNames[i];
}
}
return getContext().getString(R.string.custom_time);
}
}
|