import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
+import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
// Alarm action for midnight (so we can update the date display).
private static final String ACTION_MIDNIGHT = "com.android.deskclock.MIDNIGHT";
- // Interval between polls of the weather widget. Its refresh period is
- // likely to be much longer (~3h), but we want to pick up any changes
- // within 5 minutes.
- private final long QUERY_WEATHER_DELAY = 5 * 60 * 1000; // 5 min
+ // Interval between forced polls of the weather widget.
+ private final long QUERY_WEATHER_DELAY = 60 * 60 * 1000; // 1 hr
// Delay before engaging the burn-in protection mode (green-on-black).
- private final long SCREEN_SAVER_TIMEOUT = 5* 60 * 1000; // 10 min
+ private final long SCREEN_SAVER_TIMEOUT = 5 * 60 * 1000; // 5 min
// Repositioning delay in screen saver.
private final long SCREEN_SAVER_MOVE_DELAY = 60 * 1000; // 1 min
private boolean mLaunchedFromDock = false;
- private int mIdleTimeoutEpoch = 0;
-
private Random mRNG;
private PendingIntent mMidnightIntent;
} else if (m.what == UPDATE_WEATHER_DISPLAY_MSG) {
updateWeatherDisplay();
} else if (m.what == SCREEN_SAVER_TIMEOUT_MSG) {
- if (m.arg1 == mIdleTimeoutEpoch) {
- saveScreen();
- }
+ saveScreen();
} else if (m.what == SCREEN_SAVER_MOVE_MSG) {
moveScreenSaver();
}
}
};
+ private final ContentObserver mContentObserver = new ContentObserver(mHandy) {
+ @Override
+ public void onChange(boolean selfChange) {
+ if (DEBUG) Log.d(LOG_TAG, "content observer notified that weather changed");
+ refreshWeather();
+ }
+ };
+
private void moveScreenSaver() {
moveScreenSaverTo(-1,-1);
win.setAttributes(winParams);
}
+ private void scheduleScreenSaver() {
+ // reschedule screen saver
+ mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG);
+ mHandy.sendMessageDelayed(
+ Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG),
+ SCREEN_SAVER_TIMEOUT);
+ }
+
private void restoreScreen() {
if (!mScreenSaverMode) return;
if (DEBUG) Log.d(LOG_TAG, "restoreScreen");
doDim(false); // restores previous dim mode
// policy: update weather info when returning from screen saver
if (mPluggedIn) requestWeatherDataFetch();
+
+ scheduleScreenSaver();
+
refreshAll();
}
private void requestWeatherDataFetch() {
if (DEBUG) Log.d(LOG_TAG, "forcing the Genie widget to update weather now...");
sendBroadcast(new Intent(ACTION_GENIE_REFRESH).putExtra("requestWeather", true));
- // update the display with any new data
- scheduleWeatherQueryDelayed(5000);
+ // we expect the result to show up in our content observer
}
private boolean supportsWeather() {
mWeatherIconDrawable = mGenieResources.getDrawable(cur.getInt(
cur.getColumnIndexOrThrow("iconResId")));
-
+ mWeatherCurrentTemperatureString = String.format("%d\u00b0",
+ (cur.getInt(cur.getColumnIndexOrThrow("temperature"))));
+ mWeatherHighTemperatureString = String.format("%d\u00b0",
+ (cur.getInt(cur.getColumnIndexOrThrow("highTemperature"))));
+ mWeatherLowTemperatureString = String.format("%d\u00b0",
+ (cur.getInt(cur.getColumnIndexOrThrow("lowTemperature"))));
mWeatherLocationString = cur.getString(
cur.getColumnIndexOrThrow("location"));
-
- // any of these may be NULL
- final int colTemp = cur.getColumnIndexOrThrow("temperature");
- final int colHigh = cur.getColumnIndexOrThrow("highTemperature");
- final int colLow = cur.getColumnIndexOrThrow("lowTemperature");
-
- mWeatherCurrentTemperatureString =
- cur.isNull(colTemp)
- ? "\u2014"
- : String.format("%d\u00b0", cur.getInt(colTemp));
- mWeatherHighTemperatureString =
- cur.isNull(colHigh)
- ? "\u2014"
- : String.format("%d\u00b0", cur.getInt(colHigh));
- mWeatherLowTemperatureString =
- cur.isNull(colLow)
- ? "\u2014"
- : String.format("%d\u00b0", cur.getInt(colLow));
} else {
Log.w(LOG_TAG, "No weather information available (cur="
+ cur +")");
mWeatherIconDrawable = null;
+ mWeatherHighTemperatureString = "";
+ mWeatherLowTemperatureString = "";
mWeatherLocationString = getString(R.string.weather_fetch_failure);
- mWeatherCurrentTemperatureString =
- mWeatherHighTemperatureString =
- mWeatherLowTemperatureString = "";
}
mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG);
filter.addAction(ACTION_MIDNIGHT);
registerReceiver(mIntentReceiver, filter);
+ // Listen for updates to weather data
+ Uri weatherNotificationUri = new Uri.Builder()
+ .scheme(android.content.ContentResolver.SCHEME_CONTENT)
+ .authority(WEATHER_CONTENT_AUTHORITY)
+ .path(WEATHER_CONTENT_PATH)
+ .build();
+ getContentResolver().registerContentObserver(
+ weatherNotificationUri, true, mContentObserver);
+
+ // Elaborate mechanism to find out when the day rolls over
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
setWakeLock(mPluggedIn);
- mIdleTimeoutEpoch++;
- mHandy.sendMessageDelayed(
- Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG, mIdleTimeoutEpoch, 0),
- SCREEN_SAVER_TIMEOUT);
+ scheduleScreenSaver();
final boolean launchedFromDock
= getIntent().hasCategory(Intent.CATEGORY_DESK_DOCK);
public void onPause() {
if (DEBUG) Log.d(LOG_TAG, "onPause");
- // Turn off the screen saver. (But don't un-dim.)
+ // Turn off the screen saver and cancel any pending timeouts.
+ // (But don't un-dim.)
+ mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG);
restoreScreen();
// Other things we don't want to be doing in the background.
unregisterReceiver(mIntentReceiver);
+ getContentResolver().unregisterContentObserver(mContentObserver);
+
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.cancel(mMidnightIntent);
unscheduleWeatherQuery();