Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.statusbartest; |
| 18 | |
Joe Onorato | 80a4440 | 2011-01-15 16:22:24 -0800 | [diff] [blame] | 19 | import java.util.GregorianCalendar; |
| 20 | |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.NotificationManager; |
| 24 | import android.app.PendingIntent; |
| 25 | import android.content.Context; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Intent; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.drawable.BitmapDrawable; |
| 30 | import android.net.Uri; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 31 | import android.os.Bundle; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 32 | import android.os.Environment; |
| 33 | import android.os.Vibrator; |
| 34 | import android.os.Handler; |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 35 | import android.text.SpannableStringBuilder; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 36 | import android.text.TextUtils; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | import android.net.Uri; |
| 39 | import android.os.SystemClock; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 40 | import android.view.View; |
| 41 | import android.widget.CompoundButton; |
| 42 | import android.widget.RadioButton; |
| 43 | import android.widget.RadioGroup; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 44 | import android.widget.RemoteViews; |
| 45 | import android.os.PowerManager; |
| 46 | |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 47 | public class NotificationBuilderTest extends Activity |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 48 | { |
| 49 | private final static String TAG = "NotificationTestList"; |
| 50 | |
| 51 | NotificationManager mNM; |
| 52 | |
| 53 | @Override |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 54 | public void onCreate(Bundle icicle) { |
| 55 | super.onCreate(icicle); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 56 | mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 57 | setContentView(R.layout.notification_builder_test); |
| 58 | if (icicle == null) { |
| 59 | setDefaults(); |
| 60 | } |
| 61 | for (int id: new int[] { |
| 62 | R.id.clear_1, |
| 63 | R.id.clear_2, |
| 64 | R.id.clear_3, |
| 65 | R.id.clear_4, |
| 66 | R.id.clear_5, |
| 67 | R.id.clear_6, |
| 68 | R.id.clear_7, |
| 69 | R.id.clear_8, |
| 70 | R.id.clear_9, |
| 71 | R.id.clear_10, |
| 72 | R.id.notify_1, |
| 73 | R.id.notify_2, |
| 74 | R.id.notify_3, |
| 75 | R.id.notify_4, |
| 76 | R.id.notify_5, |
| 77 | R.id.notify_6, |
| 78 | R.id.notify_7, |
| 79 | R.id.notify_8, |
| 80 | R.id.notify_9, |
| 81 | R.id.notify_10, |
| 82 | R.id.ten, |
| 83 | R.id.clear_all, |
| 84 | }) { |
| 85 | findViewById(id).setOnClickListener(mClickListener); |
| 86 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 89 | private void setDefaults() { |
| 90 | setChecked(R.id.when_now); |
| 91 | setChecked(R.id.icon_surprise); |
| 92 | setChecked(R.id.title_medium); |
| 93 | setChecked(R.id.text_medium); |
| 94 | setChecked(R.id.info_none); |
| 95 | setChecked(R.id.number_0); |
| 96 | setChecked(R.id.intent_alert); |
| 97 | setChecked(R.id.delete_none); |
| 98 | setChecked(R.id.full_screen_none); |
| 99 | setChecked(R.id.ticker_none); |
| 100 | setChecked(R.id.large_icon_none); |
| 101 | setChecked(R.id.sound_none); |
| 102 | setChecked(R.id.vibrate_none); |
| 103 | setChecked(R.id.lights_red); |
| 104 | setChecked(R.id.lights_off); |
| 105 | } |
| 106 | |
| 107 | private View.OnClickListener mClickListener = new View.OnClickListener() { |
| 108 | public void onClick(View v) { |
| 109 | switch (v.getId()) { |
| 110 | case R.id.clear_1: |
| 111 | mNM.cancel(1); |
| 112 | break; |
| 113 | case R.id.clear_2: |
| 114 | mNM.cancel(2); |
| 115 | break; |
| 116 | case R.id.clear_3: |
| 117 | mNM.cancel(3); |
| 118 | break; |
| 119 | case R.id.clear_4: |
| 120 | mNM.cancel(4); |
| 121 | break; |
| 122 | case R.id.clear_5: |
| 123 | mNM.cancel(5); |
| 124 | break; |
| 125 | case R.id.clear_6: |
| 126 | mNM.cancel(6); |
| 127 | break; |
| 128 | case R.id.clear_7: |
| 129 | mNM.cancel(7); |
| 130 | break; |
| 131 | case R.id.clear_8: |
| 132 | mNM.cancel(8); |
| 133 | break; |
| 134 | case R.id.clear_9: |
| 135 | mNM.cancel(9); |
| 136 | break; |
| 137 | case R.id.clear_10: |
| 138 | mNM.cancel(10); |
| 139 | break; |
| 140 | case R.id.notify_1: |
| 141 | sendNotification(1); |
| 142 | break; |
| 143 | case R.id.notify_2: |
| 144 | sendNotification(2); |
| 145 | break; |
| 146 | case R.id.notify_3: |
| 147 | sendNotification(3); |
| 148 | break; |
| 149 | case R.id.notify_4: |
| 150 | sendNotification(4); |
| 151 | break; |
| 152 | case R.id.notify_5: |
| 153 | sendNotification(5); |
| 154 | break; |
| 155 | case R.id.notify_6: |
| 156 | sendNotification(6); |
| 157 | break; |
| 158 | case R.id.notify_7: |
| 159 | sendNotification(7); |
| 160 | break; |
| 161 | case R.id.notify_8: |
| 162 | sendNotification(8); |
| 163 | break; |
| 164 | case R.id.notify_9: |
| 165 | sendNotification(9); |
| 166 | break; |
| 167 | case R.id.notify_10: |
| 168 | sendNotification(10); |
| 169 | break; |
| 170 | case R.id.ten: { |
| 171 | for (int id=1; id<=10; id++) { |
| 172 | sendNotification(id); |
| 173 | } |
| 174 | break; |
| 175 | } |
| 176 | case R.id.clear_all: { |
| 177 | for (int id=1; id<=10; id++) { |
| 178 | mNM.cancel(id); |
| 179 | } |
| 180 | break; |
| 181 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 182 | } |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 183 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 186 | private void sendNotification(int id) { |
| 187 | final Notification n = buildNotification(id); |
| 188 | mNM.notify(id, n); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 191 | private static CharSequence subst(CharSequence in, char ch, CharSequence sub) { |
| 192 | int i=0; |
| 193 | SpannableStringBuilder edit = new SpannableStringBuilder(in); |
| 194 | while (i<edit.length()) { |
| 195 | if (edit.charAt(i) == ch) { |
| 196 | edit.replace(i, i+1, sub); |
| 197 | i += sub.length(); |
| 198 | } else { |
| 199 | i ++; |
| 200 | } |
| 201 | } |
| 202 | return edit; |
| 203 | } |
| 204 | |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 205 | private Notification buildNotification(int id) { |
| 206 | Notification.Builder b = new Notification.Builder(this); |
| 207 | |
| 208 | // when |
| 209 | switch (getRadioChecked(R.id.group_when)) { |
Joe Onorato | 80a4440 | 2011-01-15 16:22:24 -0800 | [diff] [blame] | 210 | case R.id.when_midnight: { |
| 211 | GregorianCalendar c = new GregorianCalendar(); |
| 212 | c.set(GregorianCalendar.HOUR_OF_DAY, 0); |
| 213 | c.set(GregorianCalendar.MINUTE, 0); |
| 214 | c.set(GregorianCalendar.SECOND, 0); |
| 215 | b.setWhen(c.getTimeInMillis()); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 216 | break; |
Joe Onorato | 80a4440 | 2011-01-15 16:22:24 -0800 | [diff] [blame] | 217 | } |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 218 | case R.id.when_now: |
| 219 | b.setWhen(System.currentTimeMillis()); |
| 220 | break; |
| 221 | case R.id.when_now_plus_1h: |
| 222 | break; |
| 223 | case R.id.when_tomorrow: |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | // icon |
| 228 | switch (getRadioChecked(R.id.group_icon)) { |
| 229 | case R.id.icon_im: |
| 230 | b.setSmallIcon(R.drawable.icon1); |
| 231 | break; |
| 232 | case R.id.icon_alert: |
| 233 | b.setSmallIcon(R.drawable.icon2); |
| 234 | break; |
| 235 | case R.id.icon_surprise: |
| 236 | b.setSmallIcon(R.drawable.emo_im_kissing); |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | // title |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 241 | final CharSequence title = getRadioTag(R.id.group_title); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 242 | if (!TextUtils.isEmpty(title)) { |
| 243 | b.setContentTitle(title); |
| 244 | } |
| 245 | |
| 246 | // text |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 247 | final CharSequence text = getRadioTag(R.id.group_text); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 248 | if (!TextUtils.isEmpty(text)) { |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 249 | if (getRadioChecked(R.id.group_text) == R.id.text_emoji) { |
| 250 | // UTF-16 for +1F335 |
| 251 | b.setContentText(subst(text, |
| 252 | '_', "\ud83c\udf35")); |
| 253 | } else { |
| 254 | b.setContentText(text); |
| 255 | } |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // info |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 259 | final CharSequence info = getRadioTag(R.id.group_info); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 260 | if (!TextUtils.isEmpty(info)) { |
| 261 | b.setContentInfo(info); |
| 262 | } |
| 263 | |
| 264 | // number |
| 265 | b.setNumber(getRadioInt(R.id.group_number, 0)); |
| 266 | |
| 267 | // contentIntent |
| 268 | switch (getRadioChecked(R.id.group_intent)) { |
| 269 | case R.id.intent_none: |
| 270 | break; |
| 271 | case R.id.intent_alert: |
| 272 | b.setContentIntent(makeContentIntent(id)); |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | // deleteIntent |
| 277 | switch (getRadioChecked(R.id.group_delete)) { |
| 278 | case R.id.delete_none: |
| 279 | break; |
| 280 | case R.id.delete_alert: |
| 281 | b.setDeleteIntent(makeDeleteIntent(id)); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | // fullScreenIntent TODO |
| 286 | |
| 287 | // ticker |
| 288 | switch (getRadioChecked(R.id.group_ticker)) { |
| 289 | case R.id.ticker_none: |
| 290 | break; |
| 291 | case R.id.ticker_short: |
| 292 | case R.id.ticker_wrap: |
| 293 | case R.id.ticker_haiku: |
| 294 | b.setTicker(getRadioTag(R.id.group_ticker)); |
| 295 | break; |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 296 | case R.id.ticker_emoji: |
| 297 | // UTF-16 for +1F335 |
| 298 | b.setTicker(subst(getRadioTag(R.id.group_ticker), |
| 299 | '_', "\ud83c\udf35")); |
| 300 | break; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 301 | case R.id.ticker_custom: |
| 302 | // TODO |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | // largeIcon |
| 307 | switch (getRadioChecked(R.id.group_large_icon)) { |
| 308 | case R.id.large_icon_none: |
| 309 | break; |
| 310 | case R.id.large_icon_pineapple: |
| 311 | b.setLargeIcon(loadBitmap(R.drawable.pineapple)); |
| 312 | break; |
Joe Onorato | 80a4440 | 2011-01-15 16:22:24 -0800 | [diff] [blame] | 313 | case R.id.large_icon_pineapple2: |
| 314 | b.setLargeIcon(loadBitmap(R.drawable.pineapple2)); |
| 315 | break; |
Daniel Sandler | a3850b6 | 2011-12-06 13:36:03 -0500 | [diff] [blame] | 316 | case R.id.large_icon_small: |
| 317 | b.setLargeIcon(loadBitmap(R.drawable.icon2)); |
| 318 | break; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // sound TODO |
| 322 | |
| 323 | // vibrate |
| 324 | switch (getRadioChecked(R.id.group_vibrate)) { |
| 325 | case R.id.vibrate_none: |
| 326 | break; |
| 327 | case R.id.vibrate_short: |
| 328 | b.setVibrate(new long[] { 0, 200 }); |
| 329 | break; |
| 330 | case R.id.vibrate_medium: |
| 331 | b.setVibrate(new long[] { 0, 500 }); |
| 332 | break; |
| 333 | case R.id.vibrate_long: |
| 334 | b.setVibrate(new long[] { 0, 1000 }); |
| 335 | break; |
| 336 | case R.id.vibrate_pattern: |
| 337 | b.setVibrate(new long[] { 0, 250, 250, 250, 250, 250, 250, 250 }); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | // lights |
| 342 | final int color = getRadioInt(R.id.group_lights_color, 0xff0000); |
| 343 | int onMs; |
| 344 | int offMs; |
| 345 | switch (getRadioChecked(R.id.group_lights_blink)) { |
| 346 | case R.id.lights_slow: |
| 347 | onMs = 1300; |
| 348 | offMs = 1300; |
| 349 | break; |
| 350 | case R.id.lights_fast: |
| 351 | onMs = 300; |
| 352 | offMs = 300; |
| 353 | break; |
| 354 | case R.id.lights_on: |
| 355 | onMs = 1; |
| 356 | offMs = 0; |
| 357 | break; |
| 358 | case R.id.lights_off: |
| 359 | default: |
| 360 | onMs = 0; |
| 361 | offMs = 0; |
| 362 | break; |
| 363 | } |
| 364 | if (onMs != 0 && offMs != 0) { |
| 365 | b.setLights(color, onMs, offMs); |
| 366 | } |
| 367 | |
| 368 | // flags |
| 369 | b.setOngoing(getChecked(R.id.flag_ongoing)); |
| 370 | b.setOnlyAlertOnce(getChecked(R.id.flag_once)); |
| 371 | b.setAutoCancel(getChecked(R.id.flag_auto_cancel)); |
| 372 | |
| 373 | // defaults |
| 374 | int defaults = 0; |
| 375 | if (getChecked(R.id.default_sound)) { |
| 376 | defaults |= Notification.DEFAULT_SOUND; |
| 377 | } |
| 378 | if (getChecked(R.id.default_vibrate)) { |
| 379 | defaults |= Notification.DEFAULT_VIBRATE; |
| 380 | } |
| 381 | if (getChecked(R.id.default_lights)) { |
| 382 | defaults |= Notification.DEFAULT_LIGHTS; |
| 383 | } |
| 384 | b.setDefaults(defaults); |
| 385 | |
| 386 | return b.getNotification(); |
| 387 | } |
| 388 | |
| 389 | private void setChecked(int id) { |
| 390 | final CompoundButton b = (CompoundButton)findViewById(id); |
| 391 | b.setChecked(true); |
| 392 | } |
| 393 | |
| 394 | private int getRadioChecked(int id) { |
| 395 | final RadioGroup g = (RadioGroup)findViewById(id); |
| 396 | return g.getCheckedRadioButtonId(); |
| 397 | } |
| 398 | |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 399 | private CharSequence getRadioTag(int id) { |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 400 | final RadioGroup g = (RadioGroup)findViewById(id); |
| 401 | final View v = findViewById(g.getCheckedRadioButtonId()); |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 402 | return (CharSequence) v.getTag(); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | private int getRadioInt(int id, int def) { |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 406 | CharSequence str = getRadioTag(id); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 407 | if (TextUtils.isEmpty(str)) { |
| 408 | return def; |
| 409 | } else { |
| 410 | try { |
Daniel Sandler | b9d3664 | 2012-10-19 13:36:58 -0400 | [diff] [blame] | 411 | return Integer.parseInt(str.toString()); |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 412 | } catch (NumberFormatException ex) { |
| 413 | return def; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private boolean getChecked(int id) { |
| 419 | final CompoundButton b = (CompoundButton)findViewById(id); |
| 420 | return b.isChecked(); |
| 421 | } |
| 422 | |
| 423 | private Bitmap loadBitmap(int id) { |
| 424 | final BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(id); |
| 425 | return Bitmap.createBitmap(bd.getBitmap()); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | private PendingIntent makeDeleteIntent(int id) { |
| 429 | Intent intent = new Intent(this, ConfirmationActivity.class); |
| 430 | intent.setData(Uri.fromParts("content", "//status_bar_test/delete/" + id, null)); |
| 431 | intent.putExtra(ConfirmationActivity.EXTRA_TITLE, "Delete intent"); |
| 432 | intent.putExtra(ConfirmationActivity.EXTRA_TEXT, "id: " + id); |
| 433 | return PendingIntent.getActivity(this, 0, intent, 0); |
| 434 | } |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 435 | |
| 436 | private PendingIntent makeContentIntent(int id) { |
| 437 | Intent intent = new Intent(this, ConfirmationActivity.class); |
| 438 | intent.setData(Uri.fromParts("content", "//status_bar_test/content/" + id, null)); |
| 439 | intent.putExtra(ConfirmationActivity.EXTRA_TITLE, "Content intent"); |
| 440 | intent.putExtra(ConfirmationActivity.EXTRA_TEXT, "id: " + id); |
| 441 | return PendingIntent.getActivity(this, 0, intent, 0); |
| 442 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 443 | } |
| 444 | |