1 /* 2 * Copyright (C) 2009 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.deskclock; 18 19 import android.app.Activity; 20 import android.app.AlarmManager; 21 import android.app.AlertDialog; 22 import android.app.PendingIntent; 23 import android.app.UiModeManager; 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.ContentResolver; 27 import android.content.DialogInterface; 28 import android.content.Intent; 29 import android.content.IntentFilter; 30 import android.content.SharedPreferences; 31 import android.content.pm.PackageManager; 32 import android.content.res.Configuration; 33 import android.content.res.Resources; 34 import android.database.ContentObserver; 35 import android.database.Cursor; 36 import android.graphics.Rect; 37 import android.graphics.drawable.BitmapDrawable; 38 import android.graphics.drawable.ColorDrawable; 39 import android.graphics.drawable.Drawable; 40 import android.net.Uri; 41 import android.os.BatteryManager; 42 import android.os.Bundle; 43 import android.os.Handler; 44 import android.os.Message; 45 import android.os.SystemClock; 46 import android.os.PowerManager; 47 import android.provider.Settings; 48 import android.provider.MediaStore; 49 import android.text.TextUtils; 50 import android.text.format.DateFormat; 51 import android.util.DisplayMetrics; 52 import android.util.Log; 53 import android.view.ContextMenu.ContextMenuInfo; 54 import android.view.ContextMenu; 55 import android.view.LayoutInflater; 56 import android.view.Menu; 57 import android.view.MenuInflater; 58 import android.view.MenuItem; 59 import android.view.MotionEvent; 60 import android.view.View.OnClickListener; 61 import android.view.View.OnCreateContextMenuListener; 62 import android.view.View; 63 import android.view.ViewGroup; 64 import android.view.ViewTreeObserver; 65 import android.view.ViewTreeObserver.OnGlobalFocusChangeListener; 66 import android.view.Window; 67 import android.view.WindowManager; 68 import android.view.animation.Animation; 69 import android.view.animation.AnimationUtils; 70 import android.view.animation.TranslateAnimation; 71 import android.widget.AbsoluteLayout; 72 import android.widget.AdapterView.AdapterContextMenuInfo; 73 import android.widget.AdapterView.OnItemClickListener; 74 import android.widget.AdapterView; 75 import android.widget.Button; 76 import android.widget.CheckBox; 77 import android.widget.ImageButton; 78 import android.widget.ImageView; 79 import android.widget.TextView; 80 81 import static android.os.BatteryManager.BATTERY_STATUS_CHARGING; 82 import static android.os.BatteryManager.BATTERY_STATUS_FULL; 83 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 84 85 import java.io.IOException; 86 import java.io.InputStream; 87 import java.util.Calendar; 88 import java.util.Date; 89 import java.util.Locale; 90 import java.util.Random; 91 92 /** 93 * DeskClock clock view for desk docks. 94 */ 95 public class DeskClock extends Activity { 96 private static final boolean DEBUG = false; 97 98 private static final String LOG_TAG = "DeskClock"; 99 100 // Alarm action for midnight (so we can update the date display). 101 private static final String ACTION_MIDNIGHT = "com.android.deskclock.MIDNIGHT"; 102 103 // Interval between forced polls of the weather widget. 104 private final long QUERY_WEATHER_DELAY = 60 * 60 * 1000; // 1 hr 105 106 // Intent to broadcast for dock settings. 107 private static final String DOCK_SETTINGS_ACTION = "com.android.settings.DOCK_SETTINGS"; 108 109 // Delay before engaging the burn-in protection mode (green-on-black). 110 private final long SCREEN_SAVER_TIMEOUT = 5 * 60 * 1000; // 5 min 111 112 // Repositioning delay in screen saver. 113 private final long SCREEN_SAVER_MOVE_DELAY = 60 * 1000; // 1 min 114 115 // Color to use for text & graphics in screen saver mode. 116 private final int SCREEN_SAVER_COLOR = 0xFF308030; 117 private final int SCREEN_SAVER_COLOR_DIM = 0xFF183018; 118 119 // Opacity of black layer between clock display and wallpaper. 120 private final float DIM_BEHIND_AMOUNT_NORMAL = 0.4f; 121 private final float DIM_BEHIND_AMOUNT_DIMMED = 0.8f; // higher contrast when display dimmed 122 123 // Internal message IDs. 124 private final int QUERY_WEATHER_DATA_MSG = 0x1000; 125 private final int UPDATE_WEATHER_DISPLAY_MSG = 0x1001; 126 private final int SCREEN_SAVER_TIMEOUT_MSG = 0x2000; 127 private final int SCREEN_SAVER_MOVE_MSG = 0x2001; 128 129 // Weather widget query information. 130 private static final String GENIE_PACKAGE_ID = "com.google.android.apps.genie.geniewidget"; 131 private static final String WEATHER_CONTENT_AUTHORITY = GENIE_PACKAGE_ID + ".weather"; 132 private static final String WEATHER_CONTENT_PATH = "/weather/current"; 133 private static final String[] WEATHER_CONTENT_COLUMNS = new String[] { 134 "location", 135 "timestamp", 136 "temperature", 137 "highTemperature", 138 "lowTemperature", 139 "iconUrl", 140 "iconResId", 141 "description", 142 }; 143 144 private static final String ACTION_GENIE_REFRESH = "com.google.android.apps.genie.REFRESH"; 145 146 // State variables follow. 147 private DigitalClock mTime; 148 private TextView mDate; 149 150 private TextView mNextAlarm = null; 151 private TextView mBatteryDisplay; 152 153 private TextView mWeatherCurrentTemperature; 154 private TextView mWeatherHighTemperature; 155 private TextView mWeatherLowTemperature; 156 private TextView mWeatherLocation; 157 private ImageView mWeatherIcon; 158 159 private String mWeatherCurrentTemperatureString; 160 private String mWeatherHighTemperatureString; 161 private String mWeatherLowTemperatureString; 162 private String mWeatherLocationString; 163 private Drawable mWeatherIconDrawable; 164 165 private Resources mGenieResources = null; 166 167 private boolean mDimmed = false; 168 private boolean mScreenSaverMode = false; 169 170 private String mDateFormat; 171 172 private int mBatteryLevel = -1; 173 private boolean mPluggedIn = false; 174 175 private boolean mLaunchedFromDock = false; 176 177 private Random mRNG; 178 179 private PendingIntent mMidnightIntent; 180 181 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 182 @Override 183 public void onReceive(Context context, Intent intent) { 184 final String action = intent.getAction(); 185 if (DEBUG) Log.d(LOG_TAG, "mIntentReceiver.onReceive: action=" + action + ", intent=" + intent); 186 if (Intent.ACTION_DATE_CHANGED.equals(action) || ACTION_MIDNIGHT.equals(action)) { 187 refreshDate(); 188 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 189 handleBatteryUpdate( 190 intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0), 191 intent.getIntExtra(BatteryManager.EXTRA_STATUS, BATTERY_STATUS_UNKNOWN), 192 intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)); 193 } else if (UiModeManager.ACTION_EXIT_DESK_MODE.equals(action)) { 194 if (mLaunchedFromDock) { 195 // moveTaskToBack(false); 196 finish(); 197 } 198 mLaunchedFromDock = false; 199 } 200 } 201 }; 202 203 private final Handler mHandy = new Handler() { 204 @Override 205 public void handleMessage(Message m) { 206 if (m.what == QUERY_WEATHER_DATA_MSG) { 207 new Thread() { public void run() { queryWeatherData(); } }.start(); 208 scheduleWeatherQueryDelayed(QUERY_WEATHER_DELAY); 209 } else if (m.what == UPDATE_WEATHER_DISPLAY_MSG) { 210 updateWeatherDisplay(); 211 } else if (m.what == SCREEN_SAVER_TIMEOUT_MSG) { 212 saveScreen(); 213 } else if (m.what == SCREEN_SAVER_MOVE_MSG) { 214 moveScreenSaver(); 215 } 216 } 217 }; 218 219 private final ContentObserver mContentObserver = new ContentObserver(mHandy) { 220 @Override 221 public void onChange(boolean selfChange) { 222 if (DEBUG) Log.d(LOG_TAG, "content observer notified that weather changed"); 223 refreshWeather(); 224 } 225 }; 226 227 228 private void moveScreenSaver() { 229 moveScreenSaverTo(-1,-1); 230 } 231 private void moveScreenSaverTo(int x, int y) { 232 if (!mScreenSaverMode) return; 233 234 final View saver_view = findViewById(R.id.saver_view); 235 236 DisplayMetrics metrics = new DisplayMetrics(); 237 getWindowManager().getDefaultDisplay().getMetrics(metrics); 238 239 if (x < 0 || y < 0) { 240 int myWidth = saver_view.getMeasuredWidth(); 241 int myHeight = saver_view.getMeasuredHeight(); 242 x = (int)(mRNG.nextFloat()*(metrics.widthPixels - myWidth)); 243 y = (int)(mRNG.nextFloat()*(metrics.heightPixels - myHeight)); 244 } 245 246 if (DEBUG) Log.d(LOG_TAG, String.format("screen saver: %d: jumping to (%d,%d)", 247 System.currentTimeMillis(), x, y)); 248 249 saver_view.setLayoutParams(new AbsoluteLayout.LayoutParams( 250 ViewGroup.LayoutParams.WRAP_CONTENT, 251 ViewGroup.LayoutParams.WRAP_CONTENT, 252 x, 253 y)); 254 255 // Synchronize our jumping so that it happens exactly on the second. 256 mHandy.sendEmptyMessageDelayed(SCREEN_SAVER_MOVE_MSG, 257 SCREEN_SAVER_MOVE_DELAY + 258 (1000 - (System.currentTimeMillis() % 1000))); 259 } 260 261 private void setWakeLock(boolean hold) { 262 if (DEBUG) Log.d(LOG_TAG, (hold ? "hold" : " releas") + "ing wake lock"); 263 Window win = getWindow(); 264 WindowManager.LayoutParams winParams = win.getAttributes(); 265 winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 266 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 267 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 268 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 269 if (hold) 270 winParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 271 else 272 winParams.flags &= (~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 273 win.setAttributes(winParams); 274 } 275 276 private void scheduleScreenSaver() { 277 // reschedule screen saver 278 mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG); 279 mHandy.sendMessageDelayed( 280 Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG), 281 SCREEN_SAVER_TIMEOUT); 282 } 283 284 private void restoreScreen() { 285 if (!mScreenSaverMode) return; 286 if (DEBUG) Log.d(LOG_TAG, "restoreScreen"); 287 mScreenSaverMode = false; 288 initViews(); 289 doDim(false); // restores previous dim mode 290 // policy: update weather info when returning from screen saver 291 if (mPluggedIn) requestWeatherDataFetch(); 292 293 scheduleScreenSaver(); 294 295 refreshAll(); 296 } 297 298 // Special screen-saver mode for OLED displays that burn in quickly 299 private void saveScreen() { 300 if (mScreenSaverMode) return; 301 if (DEBUG) Log.d(LOG_TAG, "saveScreen"); 302 303 // quickly stash away the x/y of the current date 304 final View oldTimeDate = findViewById(R.id.time_date); 305 int oldLoc[] = new int[2]; 306 oldTimeDate.getLocationOnScreen(oldLoc); 307 308 mScreenSaverMode = true; 309 Window win = getWindow(); 310 WindowManager.LayoutParams winParams = win.getAttributes(); 311 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 312 win.setAttributes(winParams); 313 314 // give up any internal focus before we switch layouts 315 final View focused = getCurrentFocus(); 316 if (focused != null) focused.clearFocus(); 317 318 setContentView(R.layout.desk_clock_saver); 319 320 mTime = (DigitalClock) findViewById(R.id.time); 321 mDate = (TextView) findViewById(R.id.date); 322 mNextAlarm = (TextView) findViewById(R.id.nextAlarm); 323 324 final int color = mDimmed ? SCREEN_SAVER_COLOR_DIM : SCREEN_SAVER_COLOR; 325 326 ((TextView)findViewById(R.id.timeDisplay)).setTextColor(color); 327 ((TextView)findViewById(R.id.am_pm)).setTextColor(color); 328 mDate.setTextColor(color); 329 mNextAlarm.setTextColor(color); 330 mNextAlarm.setCompoundDrawablesWithIntrinsicBounds( 331 getResources().getDrawable(mDimmed 332 ? R.drawable.ic_lock_idle_alarm_saver_dim 333 : R.drawable.ic_lock_idle_alarm_saver), 334 null, null, null); 335 336 mBatteryDisplay = 337 mWeatherCurrentTemperature = 338 mWeatherHighTemperature = 339 mWeatherLowTemperature = 340 mWeatherLocation = null; 341 mWeatherIcon = null; 342 343 refreshDate(); 344 refreshAlarm(); 345 346 moveScreenSaverTo(oldLoc[0], oldLoc[1]); 347 } 348 349 @Override 350 public void onUserInteraction() { 351 if (mScreenSaverMode) 352 restoreScreen(); 353 } 354 355 // Tell the Genie widget to load new data from the network. 356 private void requestWeatherDataFetch() { 357 if (DEBUG) Log.d(LOG_TAG, "forcing the Genie widget to update weather now..."); 358 sendBroadcast(new Intent(ACTION_GENIE_REFRESH).putExtra("requestWeather", true)); 359 // we expect the result to show up in our content observer 360 } 361 362 private boolean supportsWeather() { 363 return (mGenieResources != null); 364 } 365 366 private void scheduleWeatherQueryDelayed(long delay) { 367 // cancel any existing scheduled queries 368 unscheduleWeatherQuery(); 369 370 if (DEBUG) Log.d(LOG_TAG, "scheduling weather fetch message for " + delay + "ms from now"); 371 372 mHandy.sendEmptyMessageDelayed(QUERY_WEATHER_DATA_MSG, delay); 373 } 374 375 private void unscheduleWeatherQuery() { 376 mHandy.removeMessages(QUERY_WEATHER_DATA_MSG); 377 } 378 379 private void queryWeatherData() { 380 // if we couldn't load the weather widget's resources, we simply 381 // assume it's not present on the device. 382 if (mGenieResources == null) return; 383 384 Uri queryUri = new Uri.Builder() 385 .scheme(android.content.ContentResolver.SCHEME_CONTENT) 386 .authority(WEATHER_CONTENT_AUTHORITY) 387 .path(WEATHER_CONTENT_PATH) 388 .appendPath(new Long(System.currentTimeMillis()).toString()) 389 .build(); 390 391 if (DEBUG) Log.d(LOG_TAG, "querying genie: " + queryUri); 392 393 Cursor cur; 394 try { 395 cur = getContentResolver().query( 396 queryUri, 397 WEATHER_CONTENT_COLUMNS, 398 null, 399 null, 400 null); 401 } catch (RuntimeException e) { 402 Log.e(LOG_TAG, "Weather query failed", e); 403 cur = null; 404 } 405 406 if (cur != null && cur.moveToFirst()) { 407 if (DEBUG) { 408 java.lang.StringBuilder sb = 409 new java.lang.StringBuilder("Weather query result: {"); 410 for(int i=0; i<cur.getColumnCount(); i++) { 411 if (i>0) sb.append(", "); 412 sb.append(cur.getColumnName(i)) 413 .append("=") 414 .append(cur.getString(i)); 415 } 416 sb.append("}"); 417 Log.d(LOG_TAG, sb.toString()); 418 } 419 420 mWeatherIconDrawable = mGenieResources.getDrawable(cur.getInt( 421 cur.getColumnIndexOrThrow("iconResId"))); 422 423 mWeatherLocationString = cur.getString( 424 cur.getColumnIndexOrThrow("location")); 425 426 // any of these may be NULL 427 final int colTemp = cur.getColumnIndexOrThrow("temperature"); 428 final int colHigh = cur.getColumnIndexOrThrow("highTemperature"); 429 final int colLow = cur.getColumnIndexOrThrow("lowTemperature"); 430 431 mWeatherCurrentTemperatureString = 432 cur.isNull(colTemp) 433 ? "\u2014" 434 : String.format("%d\u00b0", cur.getInt(colTemp)); 435 mWeatherHighTemperatureString = 436 cur.isNull(colHigh) 437 ? "\u2014" 438 : String.format("%d\u00b0", cur.getInt(colHigh)); 439 mWeatherLowTemperatureString = 440 cur.isNull(colLow) 441 ? "\u2014" 442 : String.format("%d\u00b0", cur.getInt(colLow)); 443 } else { 444 Log.w(LOG_TAG, "No weather information available (cur=" 445 + cur +")"); 446 mWeatherIconDrawable = null; 447 mWeatherLocationString = getString(R.string.weather_fetch_failure); 448 mWeatherCurrentTemperatureString = 449 mWeatherHighTemperatureString = 450 mWeatherLowTemperatureString = ""; 451 } 452 453 if (cur != null) { 454 // clean up cursor 455 cur.close(); 456 } 457 458 mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG); 459 } 460 461 private void refreshWeather() { 462 if (supportsWeather()) 463 scheduleWeatherQueryDelayed(0); 464 updateWeatherDisplay(); // in case we have it cached 465 } 466 467 private void updateWeatherDisplay() { 468 if (mWeatherCurrentTemperature == null) return; 469 470 mWeatherCurrentTemperature.setText(mWeatherCurrentTemperatureString); 471 mWeatherHighTemperature.setText(mWeatherHighTemperatureString); 472 mWeatherLowTemperature.setText(mWeatherLowTemperatureString); 473 mWeatherLocation.setText(mWeatherLocationString); 474 mWeatherIcon.setImageDrawable(mWeatherIconDrawable); 475 } 476 477 // Adapted from KeyguardUpdateMonitor.java 478 private void handleBatteryUpdate(int plugged, int status, int level) { 479 final boolean pluggedIn = (plugged != 0); 480 if (pluggedIn != mPluggedIn) { 481 setWakeLock(pluggedIn); 482 483 if (pluggedIn) { 484 // policy: update weather info when attaching to power 485 requestWeatherDataFetch(); 486 } 487 } 488 if (pluggedIn != mPluggedIn || level != mBatteryLevel) { 489 mBatteryLevel = level; 490 mPluggedIn = pluggedIn; 491 refreshBattery(); 492 } 493 } 494 495 private void refreshBattery() { 496 if (mBatteryDisplay == null) return; 497 498 if (mPluggedIn /* || mBatteryLevel < LOW_BATTERY_THRESHOLD */) { 499 mBatteryDisplay.setCompoundDrawablesWithIntrinsicBounds( 500 0, 0, android.R.drawable.ic_lock_idle_charging, 0); 501 mBatteryDisplay.setText( 502 getString(R.string.battery_charging_level, mBatteryLevel)); 503 mBatteryDisplay.setVisibility(View.VISIBLE); 504 } else { 505 mBatteryDisplay.setVisibility(View.INVISIBLE); 506 } 507 } 508 509 private void refreshDate() { 510 final Date now = new Date(); 511 if (DEBUG) Log.d(LOG_TAG, "refreshing date..." + now); 512 mDate.setText(DateFormat.format(mDateFormat, now)); 513 } 514 515 private void refreshAlarm() { 516 if (mNextAlarm == null) return; 517 518 String nextAlarm = Settings.System.getString(getContentResolver(), 519 Settings.System.NEXT_ALARM_FORMATTED); 520 if (!TextUtils.isEmpty(nextAlarm)) { 521 mNextAlarm.setText(nextAlarm); 522 //mNextAlarm.setCompoundDrawablesWithIntrinsicBounds( 523 // android.R.drawable.ic_lock_idle_alarm, 0, 0, 0); 524 mNextAlarm.setVisibility(View.VISIBLE); 525 } else { 526 mNextAlarm.setVisibility(View.INVISIBLE); 527 } 528 } 529 530 private void refreshAll() { 531 refreshDate(); 532 refreshAlarm(); 533 refreshBattery(); 534 refreshWeather(); 535 } 536 537 private void doDim(boolean fade) { 538 View tintView = findViewById(R.id.window_tint); 539 if (tintView == null) return; 540 541 Window win = getWindow(); 542 WindowManager.LayoutParams winParams = win.getAttributes(); 543 544 winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 545 winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 546 547 // dim the wallpaper somewhat (how much is determined below) 548 winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND); 549 550 if (mDimmed) { 551 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 552 winParams.dimAmount = DIM_BEHIND_AMOUNT_DIMMED; 553 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF; 554 555 // show the window tint 556 tintView.startAnimation(AnimationUtils.loadAnimation(this, 557 fade ? R.anim.dim 558 : R.anim.dim_instant)); 559 } else { 560 winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); 561 winParams.dimAmount = DIM_BEHIND_AMOUNT_NORMAL; 562 winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE; 563 564 // hide the window tint 565 tintView.startAnimation(AnimationUtils.loadAnimation(this, 566 fade ? R.anim.undim 567 : R.anim.undim_instant)); 568 } 569 570 win.setAttributes(winParams); 571 } 572 573 @Override 574 public void onNewIntent(Intent newIntent) { 575 super.onNewIntent(newIntent); 576 if (DEBUG) Log.d(LOG_TAG, "onNewIntent with intent: " + newIntent); 577 578 // update our intent so that we can consult it to determine whether or 579 // not the most recent launch was via a dock event 580 setIntent(newIntent); 581 } 582 583 @Override 584 public void onStart() { 585 super.onStart(); 586 587 IntentFilter filter = new IntentFilter(); 588 filter.addAction(Intent.ACTION_DATE_CHANGED); 589 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 590 filter.addAction(UiModeManager.ACTION_EXIT_DESK_MODE); 591 filter.addAction(ACTION_MIDNIGHT); 592 registerReceiver(mIntentReceiver, filter); 593 } 594 595 @Override 596 public void onStop() { 597 super.onStop(); 598 599 unregisterReceiver(mIntentReceiver); 600 } 601 602 @Override 603 public void onResume() { 604 super.onResume(); 605 if (DEBUG) Log.d(LOG_TAG, "onResume with intent: " + getIntent()); 606 607 // reload the date format in case the user has changed settings 608 // recently 609 mDateFormat = getString(R.string.full_wday_month_day_no_year); 610 611 // Listen for updates to weather data 612 Uri weatherNotificationUri = new Uri.Builder() 613 .scheme(android.content.ContentResolver.SCHEME_CONTENT) 614 .authority(WEATHER_CONTENT_AUTHORITY) 615 .path(WEATHER_CONTENT_PATH) 616 .build(); 617 getContentResolver().registerContentObserver( 618 weatherNotificationUri, true, mContentObserver); 619 620 // Elaborate mechanism to find out when the day rolls over 621 Calendar today = Calendar.getInstance(); 622 today.set(Calendar.HOUR_OF_DAY, 0); 623 today.set(Calendar.MINUTE, 0); 624 today.set(Calendar.SECOND, 0); 625 today.add(Calendar.DATE, 1); 626 long alarmTimeUTC = today.getTimeInMillis(); 627 628 mMidnightIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_MIDNIGHT), 0); 629 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 630 am.setRepeating(AlarmManager.RTC, alarmTimeUTC, AlarmManager.INTERVAL_DAY, mMidnightIntent); 631 if (DEBUG) Log.d(LOG_TAG, "set repeating midnight event at UTC: " 632 + alarmTimeUTC + " (" 633 + (alarmTimeUTC - System.currentTimeMillis()) 634 + " ms from now) repeating every " 635 + AlarmManager.INTERVAL_DAY + " with intent: " + mMidnightIntent); 636 637 // If we weren't previously visible but now we are, it's because we're 638 // being started from another activity. So it's OK to un-dim. 639 if (mTime != null && mTime.getWindowVisibility() != View.VISIBLE) { 640 mDimmed = false; 641 } 642 643 // Adjust the display to reflect the currently chosen dim mode. 644 doDim(false); 645 646 restoreScreen(); // disable screen saver 647 refreshAll(); // will schedule periodic weather fetch 648 649 setWakeLock(mPluggedIn); 650 651 scheduleScreenSaver(); 652 653 final boolean launchedFromDock 654 = getIntent().hasCategory(Intent.CATEGORY_DESK_DOCK); 655 656 if (supportsWeather() && launchedFromDock && !mLaunchedFromDock) { 657 // policy: fetch weather if launched via dock connection 658 if (DEBUG) Log.d(LOG_TAG, "Device now docked; forcing weather to refresh right now"); 659 requestWeatherDataFetch(); 660 } 661 662 mLaunchedFromDock = launchedFromDock; 663 } 664 665 @Override 666 public void onPause() { 667 if (DEBUG) Log.d(LOG_TAG, "onPause"); 668 669 // Turn off the screen saver and cancel any pending timeouts. 670 // (But don't un-dim.) 671 mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG); 672 restoreScreen(); 673 674 // Other things we don't want to be doing in the background. 675 // NB: we need to keep our broadcast receiver alive in case the dock 676 // is disconnected while the screen is off 677 getContentResolver().unregisterContentObserver(mContentObserver); 678 679 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 680 am.cancel(mMidnightIntent); 681 unscheduleWeatherQuery(); 682 683 super.onPause(); 684 } 685 686 private void initViews() { 687 // give up any internal focus before we switch layouts 688 final View focused = getCurrentFocus(); 689 if (focused != null) focused.clearFocus(); 690 691 setContentView(R.layout.desk_clock); 692 693 mTime = (DigitalClock) findViewById(R.id.time); 694 mDate = (TextView) findViewById(R.id.date); 695 mBatteryDisplay = (TextView) findViewById(R.id.battery); 696 697 mTime.getRootView().requestFocus(); 698 699 mWeatherCurrentTemperature = (TextView) findViewById(R.id.weather_temperature); 700 mWeatherHighTemperature = (TextView) findViewById(R.id.weather_high_temperature); 701 mWeatherLowTemperature = (TextView) findViewById(R.id.weather_low_temperature); 702 mWeatherLocation = (TextView) findViewById(R.id.weather_location); 703 mWeatherIcon = (ImageView) findViewById(R.id.weather_icon); 704 705 final View.OnClickListener alarmClickListener = new View.OnClickListener() { 706 public void onClick(View v) { 707 startActivity(new Intent(DeskClock.this, AlarmClock.class)); 708 } 709 }; 710 711 mNextAlarm = (TextView) findViewById(R.id.nextAlarm); 712 mNextAlarm.setOnClickListener(alarmClickListener); 713 714 final ImageButton alarmButton = (ImageButton) findViewById(R.id.alarm_button); 715 alarmButton.setOnClickListener(alarmClickListener); 716 717 final ImageButton galleryButton = (ImageButton) findViewById(R.id.gallery_button); 718 galleryButton.setOnClickListener(new View.OnClickListener() { 719 public void onClick(View v) { 720 try { 721 startActivity(new Intent( 722 Intent.ACTION_VIEW, 723 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) 724 .putExtra("slideshow", true) 725 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)); 726 } catch (android.content.ActivityNotFoundException e) { 727 Log.e(LOG_TAG, "Couldn't launch image browser", e); 728 } 729 } 730 }); 731 732 final ImageButton musicButton = (ImageButton) findViewById(R.id.music_button); 733 musicButton.setOnClickListener(new View.OnClickListener() { 734 public void onClick(View v) { 735 try { 736 startActivity(new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER) 737 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)); 738 739 } catch (android.content.ActivityNotFoundException e) { 740 Log.e(LOG_TAG, "Couldn't launch music browser", e); 741 } 742 } 743 }); 744 745 final ImageButton homeButton = (ImageButton) findViewById(R.id.home_button); 746 homeButton.setOnClickListener(new View.OnClickListener() { 747 public void onClick(View v) { 748 startActivity( 749 new Intent(Intent.ACTION_MAIN) 750 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP) 751 .addCategory(Intent.CATEGORY_HOME)); 752 } 753 }); 754 755 final ImageButton nightmodeButton = (ImageButton) findViewById(R.id.nightmode_button); 756 nightmodeButton.setOnClickListener(new View.OnClickListener() { 757 public void onClick(View v) { 758 mDimmed = ! mDimmed; 759 doDim(true); 760 } 761 }); 762 763 nightmodeButton.setOnLongClickListener(new View.OnLongClickListener() { 764 public boolean onLongClick(View v) { 765 saveScreen(); 766 return true; 767 } 768 }); 769 770 final View weatherView = findViewById(R.id.weather); 771 weatherView.setOnClickListener(new View.OnClickListener() { 772 public void onClick(View v) { 773 if (!supportsWeather()) return; 774 775 Intent genieAppQuery = getPackageManager() 776 .getLaunchIntentForPackage(GENIE_PACKAGE_ID) 777 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP); 778 if (genieAppQuery != null) { 779 startActivity(genieAppQuery); 780 } 781 } 782 }); 783 784 final View tintView = findViewById(R.id.window_tint); 785 tintView.setOnTouchListener(new View.OnTouchListener() { 786 public boolean onTouch(View v, MotionEvent event) { 787 if (mDimmed && event.getAction() == MotionEvent.ACTION_DOWN) { 788 // We want to un-dim the whole screen on tap. 789 // ...Unless the user is specifically tapping on the dim 790 // widget, in which case let it do the work. 791 Rect r = new Rect(); 792 nightmodeButton.getHitRect(r); 793 int[] gloc = new int[2]; 794 nightmodeButton.getLocationInWindow(gloc); 795 r.offsetTo(gloc[0], gloc[1]); // convert to window coords 796 797 if (!r.contains((int) event.getX(), (int) event.getY())) { 798 mDimmed = false; 799 doDim(true); 800 } 801 } 802 return false; // always pass the click through 803 } 804 }); 805 806 // Tidy up awkward focus behavior: the first view to be focused in 807 // trackball mode should be the alarms button 808 final ViewTreeObserver vto = alarmButton.getViewTreeObserver(); 809 vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() { 810 public void onGlobalFocusChanged(View oldFocus, View newFocus) { 811 if (oldFocus == null && newFocus == nightmodeButton) { 812 alarmButton.requestFocus(); 813 } 814 } 815 }); 816 } 817 818 @Override 819 public void onConfigurationChanged(Configuration newConfig) { 820 super.onConfigurationChanged(newConfig); 821 if (mScreenSaverMode) { 822 moveScreenSaver(); 823 } else { 824 initViews(); 825 doDim(false); 826 refreshAll(); 827 } 828 } 829 830 @Override 831 public boolean onOptionsItemSelected(MenuItem item) { 832 switch (item.getItemId()) { 833 case R.id.menu_item_alarms: 834 startActivity(new Intent(DeskClock.this, AlarmClock.class)); 835 return true; 836 case R.id.menu_item_add_alarm: 837 startActivity(new Intent(this, SetAlarm.class)); 838 return true; 839 case R.id.menu_item_dock_settings: 840 startActivity(new Intent(DOCK_SETTINGS_ACTION)); 841 return true; 842 default: 843 return false; 844 } 845 } 846 847 @Override 848 public boolean onCreateOptionsMenu(Menu menu) { 849 MenuInflater inflater = getMenuInflater(); 850 inflater.inflate(R.menu.desk_clock_menu, menu); 851 return true; 852 } 853 854 @Override 855 protected void onCreate(Bundle icicle) { 856 super.onCreate(icicle); 857 858 mRNG = new Random(); 859 860 try { 861 mGenieResources = getPackageManager().getResourcesForApplication(GENIE_PACKAGE_ID); 862 } catch (PackageManager.NameNotFoundException e) { 863 // no weather info available 864 Log.w(LOG_TAG, "Can't find "+GENIE_PACKAGE_ID+". Weather forecast will not be available."); 865 } 866 867 initViews(); 868 } 869 870 } 871