DeskClock.java revision 4e8ad3a40141d05ced3279c6a0296b71ffc5117d
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 17package com.android.deskclock; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.content.SharedPreferences; 27import android.content.pm.PackageManager; 28import android.content.res.Configuration; 29import android.content.res.Resources; 30import android.database.Cursor; 31import android.graphics.drawable.BitmapDrawable; 32import android.graphics.drawable.ColorDrawable; 33import android.graphics.drawable.Drawable; 34import android.net.Uri; 35import android.os.Bundle; 36import android.os.Handler; 37import android.os.Message; 38import android.os.SystemClock; 39import android.os.PowerManager; 40import android.provider.Settings; 41import android.text.TextUtils; 42import android.util.DisplayMetrics; 43import android.util.Log; 44import android.view.ContextMenu.ContextMenuInfo; 45import android.view.ContextMenu; 46import android.view.LayoutInflater; 47import android.view.Menu; 48import android.view.MenuInflater; 49import android.view.MenuItem; 50import android.view.View.OnClickListener; 51import android.view.View.OnCreateContextMenuListener; 52import android.view.View; 53import android.view.ViewGroup; 54import android.view.Window; 55import android.view.WindowManager; 56import android.view.animation.Animation; 57import android.view.animation.AnimationUtils; 58import android.view.animation.TranslateAnimation; 59import android.widget.AbsoluteLayout; 60import android.widget.AdapterView.AdapterContextMenuInfo; 61import android.widget.AdapterView.OnItemClickListener; 62import android.widget.AdapterView; 63import android.widget.Button; 64import android.widget.CheckBox; 65import android.widget.ImageButton; 66import android.widget.ImageView; 67import android.widget.TextView; 68 69import static android.os.BatteryManager.BATTERY_STATUS_CHARGING; 70import static android.os.BatteryManager.BATTERY_STATUS_FULL; 71import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 72 73import java.io.IOException; 74import java.io.InputStream; 75import java.text.DateFormat; 76import java.text.SimpleDateFormat; 77import java.util.Date; 78import java.util.Locale; 79import java.util.Random; 80 81/** 82 * DeskClock clock view for desk docks. 83 */ 84public class DeskClock extends Activity { 85 private static final boolean DEBUG = false; 86 87 private static final String LOG_TAG = "DeskClock"; 88 89 // Intent used to start the music player. 90 private static final String MUSIC_NOW_PLAYING = "com.android.music.PLAYBACK_VIEWER"; 91 92 // Interval between polls of the weather widget. Its refresh period is 93 // likely to be much longer (~3h), but we want to pick up any changes 94 // within 5 minutes. 95 private final long FETCH_WEATHER_DELAY = 5 * 60 * 1000; // 5 min 96 97 // Delay before engaging the burn-in protection mode (green-on-black). 98 private final long SCREEN_SAVER_TIMEOUT = 10 * 60 * 1000; // 10 min 99 100 // Repositioning delay in screen saver. 101 private final long SCREEN_SAVER_MOVE_DELAY = 60 * 1000; // 1 min 102 103 // Color to use for text & graphics in screen saver mode. 104 private final int SCREEN_SAVER_COLOR = 0xFF308030; 105 private final int SCREEN_SAVER_COLOR_DIM = 0xFF183018; 106 107 // Internal message IDs. 108 private final int FETCH_WEATHER_DATA_MSG = 0x1000; 109 private final int UPDATE_WEATHER_DISPLAY_MSG = 0x1001; 110 private final int SCREEN_SAVER_TIMEOUT_MSG = 0x2000; 111 private final int SCREEN_SAVER_MOVE_MSG = 0x2001; 112 113 // Weather widget query information. 114 private static final String GENIE_PACKAGE_ID = "com.google.android.apps.genie.geniewidget"; 115 private static final String WEATHER_CONTENT_AUTHORITY = GENIE_PACKAGE_ID + ".weather"; 116 private static final String WEATHER_CONTENT_PATH = "/weather/current"; 117 private static final String[] WEATHER_CONTENT_COLUMNS = new String[] { 118 "location", 119 "timestamp", 120 "temperature", 121 "highTemperature", 122 "lowTemperature", 123 "iconUrl", 124 "iconResId", 125 "description", 126 }; 127 128 // State variables follow. 129 private DigitalClock mTime; 130 private TextView mDate; 131 132 private TextView mNextAlarm = null; 133 private TextView mBatteryDisplay; 134 135 private TextView mWeatherCurrentTemperature; 136 private TextView mWeatherHighTemperature; 137 private TextView mWeatherLowTemperature; 138 private TextView mWeatherLocation; 139 private ImageView mWeatherIcon; 140 141 private String mWeatherCurrentTemperatureString; 142 private String mWeatherHighTemperatureString; 143 private String mWeatherLowTemperatureString; 144 private String mWeatherLocationString; 145 private Drawable mWeatherIconDrawable; 146 147 private Resources mGenieResources = null; 148 149 private boolean mDimmed = false; 150 private boolean mScreenSaverMode = false; 151 152 private DateFormat mDateFormat; 153 154 private int mBatteryLevel = -1; 155 private boolean mPluggedIn = false; 156 157 private boolean mInDock = false; 158 159 private int mIdleTimeoutEpoch = 0; 160 161 private boolean mWeatherFetchScheduled = false; 162 163 private Random mRNG; 164 165 private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 166 @Override 167 public void onReceive(Context context, Intent intent) { 168 final String action = intent.getAction(); 169 if (Intent.ACTION_DATE_CHANGED.equals(action)) { 170 refreshDate(); 171 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 172 handleBatteryUpdate( 173 intent.getIntExtra("status", BATTERY_STATUS_UNKNOWN), 174 intent.getIntExtra("level", 0)); 175 } 176 } 177 }; 178 179 private final Handler mHandy = new Handler() { 180 @Override 181 public void handleMessage(Message m) { 182 if (m.what == FETCH_WEATHER_DATA_MSG) { 183 if (!mWeatherFetchScheduled) return; 184 mWeatherFetchScheduled = false; 185 new Thread() { public void run() { fetchWeatherData(); } }.start(); 186 scheduleWeatherFetchDelayed(FETCH_WEATHER_DELAY); 187 } else if (m.what == UPDATE_WEATHER_DISPLAY_MSG) { 188 updateWeatherDisplay(); 189 } else if (m.what == SCREEN_SAVER_TIMEOUT_MSG) { 190 if (m.arg1 == mIdleTimeoutEpoch) { 191 saveScreen(); 192 } 193 } else if (m.what == SCREEN_SAVER_MOVE_MSG) { 194 moveScreenSaver(); 195 } 196 } 197 }; 198 199 200 private void moveScreenSaver() { 201 moveScreenSaverTo(-1,-1); 202 } 203 private void moveScreenSaverTo(int x, int y) { 204 if (!mScreenSaverMode) return; 205 206 final View time_date = findViewById(R.id.time_date); 207 208 DisplayMetrics metrics = new DisplayMetrics(); 209 getWindowManager().getDefaultDisplay().getMetrics(metrics); 210 211 if (x < 0 || y < 0) { 212 int myWidth = time_date.getMeasuredWidth(); 213 int myHeight = time_date.getMeasuredHeight(); 214 x = (int)(mRNG.nextFloat()*(metrics.widthPixels - myWidth)); 215 y = (int)(mRNG.nextFloat()*(metrics.heightPixels - myHeight)); 216 } 217 218 if (DEBUG) Log.d(LOG_TAG, String.format("screen saver: %d: jumping to (%d,%d)", 219 System.currentTimeMillis(), x, y)); 220 221 time_date.setLayoutParams(new AbsoluteLayout.LayoutParams( 222 ViewGroup.LayoutParams.WRAP_CONTENT, 223 ViewGroup.LayoutParams.WRAP_CONTENT, 224 x, 225 y)); 226 227 // Synchronize our jumping so that it happens exactly on the second. 228 mHandy.sendEmptyMessageDelayed(SCREEN_SAVER_MOVE_MSG, 229 SCREEN_SAVER_MOVE_DELAY + 230 (1000 - (System.currentTimeMillis() % 1000))); 231 } 232 233 private void setWakeLock(boolean hold) { 234 if (DEBUG) Log.d(LOG_TAG, (hold ? "hold" : " releas") + "ing wake lock"); 235 Window win = getWindow(); 236 WindowManager.LayoutParams winParams = win.getAttributes(); 237 winParams.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD; 238 winParams.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; 239 winParams.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON; 240 if (hold) 241 winParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 242 else 243 winParams.flags &= (~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 244 win.setAttributes(winParams); 245 } 246 247 private void restoreScreen() { 248 if (!mScreenSaverMode) return; 249 if (DEBUG) Log.d(LOG_TAG, "restoreScreen"); 250 mScreenSaverMode = false; 251 initViews(); 252 doDim(false); // restores previous dim mode 253 refreshAll(); 254 } 255 256 // Special screen-saver mode for OLED displays that burn in quickly 257 private void saveScreen() { 258 if (mScreenSaverMode) return; 259 if (DEBUG) Log.d(LOG_TAG, "saveScreen"); 260 261 // quickly stash away the x/y of the current date 262 final View oldTimeDate = findViewById(R.id.time_date); 263 int oldLoc[] = new int[2]; 264 oldTimeDate.getLocationOnScreen(oldLoc); 265 266 mScreenSaverMode = true; 267 Window win = getWindow(); 268 WindowManager.LayoutParams winParams = win.getAttributes(); 269 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 270 win.setAttributes(winParams); 271 272 // give up any internal focus before we switch layouts 273 final View focused = getCurrentFocus(); 274 if (focused != null) focused.clearFocus(); 275 276 setContentView(R.layout.desk_clock_saver); 277 278 mTime = (DigitalClock) findViewById(R.id.time); 279 mDate = (TextView) findViewById(R.id.date); 280 281 final int color = mDimmed ? SCREEN_SAVER_COLOR_DIM : SCREEN_SAVER_COLOR; 282 283 ((TextView)findViewById(R.id.timeDisplay)).setTextColor(color); 284 ((TextView)findViewById(R.id.am_pm)).setTextColor(color); 285 mDate.setTextColor(color); 286 287 mBatteryDisplay = 288 mNextAlarm = 289 mWeatherCurrentTemperature = 290 mWeatherHighTemperature = 291 mWeatherLowTemperature = 292 mWeatherLocation = null; 293 mWeatherIcon = null; 294 295 refreshDate(); 296 297 moveScreenSaverTo(oldLoc[0], oldLoc[1]); 298 } 299 300 @Override 301 public void onUserInteraction() { 302 if (mScreenSaverMode) 303 restoreScreen(); 304 } 305 306 private boolean supportsWeather() { 307 return (mGenieResources != null); 308 } 309 310 private void scheduleWeatherFetchDelayed(long delay) { 311 if (mWeatherFetchScheduled) return; 312 313 if (DEBUG) Log.d(LOG_TAG, "scheduling weather fetch message for " + delay + "ms from now"); 314 315 mWeatherFetchScheduled = true; 316 317 mHandy.sendEmptyMessageDelayed(FETCH_WEATHER_DATA_MSG, delay); 318 } 319 320 private void unscheduleWeatherFetch() { 321 mWeatherFetchScheduled = false; 322 } 323 324 private static final boolean sCelsius; 325 static { 326 String cc = Locale.getDefault().getCountry().toLowerCase(); 327 sCelsius = !("us".equals(cc) || "bz".equals(cc) || "jm".equals(cc)); 328 } 329 330 private static int celsiusToLocal(int tempC) { 331 return sCelsius ? tempC : (int) Math.round(tempC * 1.8f + 32); 332 } 333 334 private void fetchWeatherData() { 335 // if we couldn't load the weather widget's resources, we simply 336 // assume it's not present on the device. 337 if (mGenieResources == null) return; 338 339 Uri queryUri = new Uri.Builder() 340 .scheme(android.content.ContentResolver.SCHEME_CONTENT) 341 .authority(WEATHER_CONTENT_AUTHORITY) 342 .path(WEATHER_CONTENT_PATH) 343 .appendPath(new Long(System.currentTimeMillis()).toString()) 344 .build(); 345 346 if (DEBUG) Log.d(LOG_TAG, "querying genie: " + queryUri); 347 348 Cursor cur; 349 try { 350 cur = managedQuery( 351 queryUri, 352 WEATHER_CONTENT_COLUMNS, 353 null, 354 null, 355 null); 356 } catch (RuntimeException e) { 357 Log.e(LOG_TAG, "Weather query failed", e); 358 cur = null; 359 } 360 361 if (cur != null && cur.moveToFirst()) { 362 if (DEBUG) { 363 java.lang.StringBuilder sb = 364 new java.lang.StringBuilder("Weather query result: {"); 365 for(int i=0; i<cur.getColumnCount(); i++) { 366 if (i>0) sb.append(", "); 367 sb.append(cur.getColumnName(i)) 368 .append("=") 369 .append(cur.getString(i)); 370 } 371 sb.append("}"); 372 Log.d(LOG_TAG, sb.toString()); 373 } 374 375 mWeatherIconDrawable = mGenieResources.getDrawable(cur.getInt( 376 cur.getColumnIndexOrThrow("iconResId"))); 377 mWeatherCurrentTemperatureString = String.format("%d\u00b0", 378 celsiusToLocal(cur.getInt(cur.getColumnIndexOrThrow("temperature")))); 379 mWeatherHighTemperatureString = String.format("%d\u00b0", 380 celsiusToLocal(cur.getInt(cur.getColumnIndexOrThrow("highTemperature")))); 381 mWeatherLowTemperatureString = String.format("%d\u00b0", 382 celsiusToLocal(cur.getInt(cur.getColumnIndexOrThrow("lowTemperature")))); 383 mWeatherLocationString = cur.getString( 384 cur.getColumnIndexOrThrow("location")); 385 } else { 386 Log.w(LOG_TAG, "No weather information available (cur=" 387 + cur +")"); 388 mWeatherIconDrawable = null; 389 mWeatherHighTemperatureString = ""; 390 mWeatherLowTemperatureString = ""; 391 mWeatherLocationString = "Weather data unavailable."; // TODO: internationalize 392 } 393 394 mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG); 395 } 396 397 private void refreshWeather() { 398 if (supportsWeather()) 399 scheduleWeatherFetchDelayed(0); 400 updateWeatherDisplay(); // in case we have it cached 401 } 402 403 private void updateWeatherDisplay() { 404 if (mWeatherCurrentTemperature == null) return; 405 406 mWeatherCurrentTemperature.setText(mWeatherCurrentTemperatureString); 407 mWeatherHighTemperature.setText(mWeatherHighTemperatureString); 408 mWeatherLowTemperature.setText(mWeatherLowTemperatureString); 409 mWeatherLocation.setText(mWeatherLocationString); 410 mWeatherIcon.setImageDrawable(mWeatherIconDrawable); 411 } 412 413 // Adapted from KeyguardUpdateMonitor.java 414 private void handleBatteryUpdate(int plugStatus, int batteryLevel) { 415 final boolean pluggedIn = (plugStatus == BATTERY_STATUS_CHARGING || plugStatus == BATTERY_STATUS_FULL); 416 if (pluggedIn != mPluggedIn) { 417 setWakeLock(pluggedIn); 418 } 419 if (pluggedIn != mPluggedIn || batteryLevel != mBatteryLevel) { 420 mBatteryLevel = batteryLevel; 421 mPluggedIn = pluggedIn; 422 refreshBattery(); 423 } 424 } 425 426 private void refreshBattery() { 427 if (mBatteryDisplay == null) return; 428 429 if (mPluggedIn /* || mBatteryLevel < LOW_BATTERY_THRESHOLD */) { 430 mBatteryDisplay.setCompoundDrawablesWithIntrinsicBounds( 431 0, 0, android.R.drawable.ic_lock_idle_charging, 0); 432 mBatteryDisplay.setText( 433 getString(R.string.battery_charging_level, mBatteryLevel)); 434 mBatteryDisplay.setVisibility(View.VISIBLE); 435 } else { 436 mBatteryDisplay.setVisibility(View.INVISIBLE); 437 } 438 } 439 440 private void refreshDate() { 441 mDate.setText(mDateFormat.format(new Date())); 442 } 443 444 private void refreshAlarm() { 445 if (mNextAlarm == null) return; 446 447 String nextAlarm = Settings.System.getString(getContentResolver(), 448 Settings.System.NEXT_ALARM_FORMATTED); 449 if (!TextUtils.isEmpty(nextAlarm)) { 450 mNextAlarm.setText(nextAlarm); 451 //mNextAlarm.setCompoundDrawablesWithIntrinsicBounds( 452 // android.R.drawable.ic_lock_idle_alarm, 0, 0, 0); 453 mNextAlarm.setVisibility(View.VISIBLE); 454 } else { 455 mNextAlarm.setVisibility(View.INVISIBLE); 456 } 457 } 458 459 private void refreshAll() { 460 refreshDate(); 461 refreshAlarm(); 462 refreshBattery(); 463 refreshWeather(); 464 } 465 466 private void doDim(boolean fade) { 467 View tintView = findViewById(R.id.window_tint); 468 if (tintView == null) return; 469 470 Window win = getWindow(); 471 WindowManager.LayoutParams winParams = win.getAttributes(); 472 473 // secret! 474 winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); 475 winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 476 477 // dim the wallpaper somewhat (how much is determined below) 478 winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND); 479 480 if (mDimmed) { 481 winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 482 winParams.dimAmount = 0.67f; // pump up contrast in dim mode 483 484 // show the window tint 485 tintView.startAnimation(AnimationUtils.loadAnimation(this, 486 fade ? R.anim.dim 487 : R.anim.dim_instant)); 488 } else { 489 winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); 490 winParams.dimAmount = 0.5f; // lower contrast in normal mode 491 492 // hide the window tint 493 tintView.startAnimation(AnimationUtils.loadAnimation(this, 494 fade ? R.anim.undim 495 : R.anim.undim_instant)); 496 } 497 498 win.setAttributes(winParams); 499 } 500 501 @Override 502 public void onResume() { 503 super.onResume(); 504 if (DEBUG) Log.d(LOG_TAG, "onResume"); 505 506 // reload the date format in case the user has changed settings 507 // recently 508 final SimpleDateFormat dateFormat = (SimpleDateFormat) 509 java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL); 510 // This is a little clumsy; we want to honor the locale's date format 511 // (rather than simply hardcoding "Weekday, Month Date") but 512 // DateFormat.FULL includes the year (at least, in enUS). So we lop 513 // that bit off if it's there; should have no effect on 514 // locale-specific date strings that look different. 515 mDateFormat = new SimpleDateFormat(dateFormat.toPattern() 516 .replace(", yyyy", "")); // no year 517 518 IntentFilter filter = new IntentFilter(); 519 filter.addAction(Intent.ACTION_DATE_CHANGED); 520 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 521 registerReceiver(mIntentReceiver, filter); 522 523 doDim(false); 524 restoreScreen(); 525 refreshAll(); // will schedule periodic weather fetch 526 527 setWakeLock(mPluggedIn); 528 529 mIdleTimeoutEpoch++; 530 mHandy.sendMessageDelayed( 531 Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG, mIdleTimeoutEpoch, 0), 532 SCREEN_SAVER_TIMEOUT); 533 534 final boolean launchedFromDock 535 = getIntent().hasCategory(Intent.CATEGORY_DESK_DOCK); 536 537 if (supportsWeather() && launchedFromDock && !mInDock) { 538 if (DEBUG) Log.d(LOG_TAG, "Device now docked; forcing weather to refresh right now"); 539 sendBroadcast( 540 new Intent("com.google.android.apps.genie.REFRESH") 541 .putExtra("requestWeather", true)); 542 } 543 544 mInDock = launchedFromDock; 545 } 546 547 @Override 548 public void onPause() { 549 if (DEBUG) Log.d(LOG_TAG, "onPause"); 550 551 // Turn off the screen saver. 552 restoreScreen(); 553 554 // Avoid situations where the user launches Alarm Clock and is 555 // surprised to find it in dim mode (because it was last used in dim 556 // mode, but that last use is long in the past). 557 mDimmed = false; 558 559 // Other things we don't want to be doing in the background. 560 unregisterReceiver(mIntentReceiver); 561 unscheduleWeatherFetch(); 562 563 super.onPause(); 564 } 565 566 567 private void initViews() { 568 // give up any internal focus before we switch layouts 569 final View focused = getCurrentFocus(); 570 if (focused != null) focused.clearFocus(); 571 572 setContentView(R.layout.desk_clock); 573 574 mTime = (DigitalClock) findViewById(R.id.time); 575 mDate = (TextView) findViewById(R.id.date); 576 mBatteryDisplay = (TextView) findViewById(R.id.battery); 577 578 mTime.getRootView().requestFocus(); 579 580 mWeatherCurrentTemperature = (TextView) findViewById(R.id.weather_temperature); 581 mWeatherHighTemperature = (TextView) findViewById(R.id.weather_high_temperature); 582 mWeatherLowTemperature = (TextView) findViewById(R.id.weather_low_temperature); 583 mWeatherLocation = (TextView) findViewById(R.id.weather_location); 584 mWeatherIcon = (ImageView) findViewById(R.id.weather_icon); 585 586 mNextAlarm = (TextView) findViewById(R.id.nextAlarm); 587 588 final ImageButton alarmButton = (ImageButton) findViewById(R.id.alarm_button); 589 alarmButton.setOnClickListener(new View.OnClickListener() { 590 public void onClick(View v) { 591 startActivity(new Intent(DeskClock.this, AlarmClock.class)); 592 } 593 }); 594 595 final ImageButton galleryButton = (ImageButton) findViewById(R.id.gallery_button); 596 galleryButton.setOnClickListener(new View.OnClickListener() { 597 public void onClick(View v) { 598 try { 599 startActivity(new Intent( 600 Intent.ACTION_VIEW, 601 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) 602 .putExtra("slideshow", true)); 603 } catch (android.content.ActivityNotFoundException e) { 604 Log.e(LOG_TAG, "Couldn't launch image browser", e); 605 } 606 } 607 }); 608 609 final ImageButton musicButton = (ImageButton) findViewById(R.id.music_button); 610 musicButton.setOnClickListener(new View.OnClickListener() { 611 public void onClick(View v) { 612 try { 613 startActivity(new Intent(MUSIC_NOW_PLAYING)); 614 } catch (android.content.ActivityNotFoundException e) { 615 Log.e(LOG_TAG, "Couldn't launch music browser", e); 616 } 617 } 618 }); 619 620 final ImageButton homeButton = (ImageButton) findViewById(R.id.home_button); 621 homeButton.setOnClickListener(new View.OnClickListener() { 622 public void onClick(View v) { 623 startActivity( 624 new Intent(Intent.ACTION_MAIN) 625 .addCategory(Intent.CATEGORY_HOME)); 626 } 627 }); 628 629 final ImageButton nightmodeButton = (ImageButton) findViewById(R.id.nightmode_button); 630 nightmodeButton.setOnClickListener(new View.OnClickListener() { 631 public void onClick(View v) { 632 mDimmed = ! mDimmed; 633 doDim(true); 634 } 635 }); 636 637 nightmodeButton.setOnLongClickListener(new View.OnLongClickListener() { 638 public boolean onLongClick(View v) { 639 saveScreen(); 640 return true; 641 } 642 }); 643 644 final View weatherView = findViewById(R.id.weather); 645 weatherView.setOnClickListener(new View.OnClickListener() { 646 public void onClick(View v) { 647 if (!supportsWeather()) return; 648 649 Intent genieAppQuery = getPackageManager().getLaunchIntentForPackage(GENIE_PACKAGE_ID); 650 if (genieAppQuery != null) { 651 startActivity(genieAppQuery); 652 } 653 } 654 }); 655 } 656 657 @Override 658 public void onConfigurationChanged(Configuration newConfig) { 659 super.onConfigurationChanged(newConfig); 660 if (!mScreenSaverMode) { 661 initViews(); 662 doDim(false); 663 refreshAll(); 664 } 665 } 666 667 @Override 668 public boolean onOptionsItemSelected(MenuItem item) { 669 if (item.getItemId() == R.id.menu_item_alarms) { 670 startActivity(new Intent(DeskClock.this, AlarmClock.class)); 671 return true; 672 } 673 return false; 674 } 675 676 @Override 677 public boolean onCreateOptionsMenu(Menu menu) { 678 MenuInflater inflater = getMenuInflater(); 679 inflater.inflate(R.menu.desk_clock_menu, menu); 680 return true; 681 } 682 683 @Override 684 protected void onCreate(Bundle icicle) { 685 super.onCreate(icicle); 686 687 mRNG = new Random(); 688 689 try { 690 mGenieResources = getPackageManager().getResourcesForApplication(GENIE_PACKAGE_ID); 691 } catch (PackageManager.NameNotFoundException e) { 692 // no weather info available 693 Log.w(LOG_TAG, "Can't find "+GENIE_PACKAGE_ID+". Weather forecast will not be available."); 694 } 695 696 initViews(); 697 } 698 699} 700