// 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 FETCH_WEATHER_DELAY = 5 * 60 * 1000; // 5 min
+ private final long QUERY_WEATHER_DELAY = 5 * 60 * 1000; // 5 min
// Delay before engaging the burn-in protection mode (green-on-black).
- private final long SCREEN_SAVER_TIMEOUT = 10 * 60 * 1000; // 10 min
+ private final long SCREEN_SAVER_TIMEOUT = 5* 60 * 1000; // 10 min
// Repositioning delay in screen saver.
private final long SCREEN_SAVER_MOVE_DELAY = 60 * 1000; // 1 min
private final int SCREEN_SAVER_COLOR = 0xFF308030;
private final int SCREEN_SAVER_COLOR_DIM = 0xFF183018;
+ // Opacity of black layer between clock display and wallpaper.
+ private final float DIM_BEHIND_AMOUNT_NORMAL = 0.4f;
+ private final float DIM_BEHIND_AMOUNT_DIMMED = 0.7f; // higher contrast when display dimmed
+
// Internal message IDs.
- private final int FETCH_WEATHER_DATA_MSG = 0x1000;
+ private final int QUERY_WEATHER_DATA_MSG = 0x1000;
private final int UPDATE_WEATHER_DISPLAY_MSG = 0x1001;
private final int SCREEN_SAVER_TIMEOUT_MSG = 0x2000;
private final int SCREEN_SAVER_MOVE_MSG = 0x2001;
"description",
};
+ private static final String ACTION_GENIE_REFRESH = "com.google.android.apps.genie.REFRESH";
+
// State variables follow.
private DigitalClock mTime;
private TextView mDate;
private int mIdleTimeoutEpoch = 0;
- private boolean mWeatherFetchScheduled = false;
-
private Random mRNG;
private PendingIntent mMidnightIntent;
private final Handler mHandy = new Handler() {
@Override
public void handleMessage(Message m) {
- if (m.what == FETCH_WEATHER_DATA_MSG) {
- if (!mWeatherFetchScheduled) return;
- mWeatherFetchScheduled = false;
- new Thread() { public void run() { fetchWeatherData(); } }.start();
- scheduleWeatherFetchDelayed(FETCH_WEATHER_DELAY);
+ if (m.what == QUERY_WEATHER_DATA_MSG) {
+ new Thread() { public void run() { queryWeatherData(); } }.start();
+ scheduleWeatherQueryDelayed(QUERY_WEATHER_DELAY);
} else if (m.what == UPDATE_WEATHER_DISPLAY_MSG) {
updateWeatherDisplay();
} else if (m.what == SCREEN_SAVER_TIMEOUT_MSG) {
mScreenSaverMode = false;
initViews();
doDim(false); // restores previous dim mode
+ // policy: update weather info when returning from screen saver
+ if (mPluggedIn) requestWeatherDataFetch();
refreshAll();
}
mDate.setTextColor(color);
mNextAlarm.setTextColor(color);
mNextAlarm.setCompoundDrawablesWithIntrinsicBounds(
- getResources().getDrawable(mDimmed
- ? R.drawable.ic_lock_idle_alarm_saver_dim
+ getResources().getDrawable(mDimmed
+ ? R.drawable.ic_lock_idle_alarm_saver_dim
: R.drawable.ic_lock_idle_alarm_saver),
null, null, null);
restoreScreen();
}
+ // Tell the Genie widget to load new data from the network.
+ 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);
+ }
+
private boolean supportsWeather() {
return (mGenieResources != null);
}
- private void scheduleWeatherFetchDelayed(long delay) {
- if (mWeatherFetchScheduled) return;
+ private void scheduleWeatherQueryDelayed(long delay) {
+ // cancel any existing scheduled queries
+ unscheduleWeatherQuery();
if (DEBUG) Log.d(LOG_TAG, "scheduling weather fetch message for " + delay + "ms from now");
- mWeatherFetchScheduled = true;
-
- mHandy.sendEmptyMessageDelayed(FETCH_WEATHER_DATA_MSG, delay);
+ mHandy.sendEmptyMessageDelayed(QUERY_WEATHER_DATA_MSG, delay);
}
- private void unscheduleWeatherFetch() {
- mWeatherFetchScheduled = false;
+ private void unscheduleWeatherQuery() {
+ mHandy.removeMessages(QUERY_WEATHER_DATA_MSG);
}
- private void fetchWeatherData() {
+ private void queryWeatherData() {
// if we couldn't load the weather widget's resources, we simply
// assume it's not present on the device.
if (mGenieResources == null) return;
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 = "Weather data unavailable."; // TODO: internationalize
+ mWeatherLocationString = getString(R.string.weather_fetch_failure);
+ mWeatherCurrentTemperatureString =
+ mWeatherHighTemperatureString =
+ mWeatherLowTemperatureString = "";
}
mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG);
private void refreshWeather() {
if (supportsWeather())
- scheduleWeatherFetchDelayed(0);
+ scheduleWeatherQueryDelayed(0);
updateWeatherDisplay(); // in case we have it cached
}
final boolean pluggedIn = (plugStatus == BATTERY_STATUS_CHARGING || plugStatus == BATTERY_STATUS_FULL);
if (pluggedIn != mPluggedIn) {
setWakeLock(pluggedIn);
+
+ if (pluggedIn) {
+ // policy: update weather info when attaching to power
+ requestWeatherDataFetch();
+ }
}
if (pluggedIn != mPluggedIn || batteryLevel != mBatteryLevel) {
mBatteryLevel = batteryLevel;
if (mDimmed) {
winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
- winParams.dimAmount = 0.67f; // pump up contrast in dim mode
+ winParams.dimAmount = DIM_BEHIND_AMOUNT_DIMMED;
// show the window tint
tintView.startAnimation(AnimationUtils.loadAnimation(this,
: R.anim.dim_instant));
} else {
winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
- winParams.dimAmount = 0.5f; // lower contrast in normal mode
+ winParams.dimAmount = DIM_BEHIND_AMOUNT_NORMAL;
// hide the window tint
tintView.startAnimation(AnimationUtils.loadAnimation(this,
am.setRepeating(AlarmManager.RTC, today.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mMidnightIntent);
registerReceiver(mIntentReceiver, filter);
+ // un-dim when resuming
+ mDimmed = false;
doDim(false);
- restoreScreen();
+
+ restoreScreen(); // disable screen saver
refreshAll(); // will schedule periodic weather fetch
setWakeLock(mPluggedIn);
Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG, mIdleTimeoutEpoch, 0),
SCREEN_SAVER_TIMEOUT);
- final boolean launchedFromDock
+ final boolean launchedFromDock
= getIntent().hasCategory(Intent.CATEGORY_DESK_DOCK);
if (supportsWeather() && launchedFromDock && !mInDock) {
+ // policy: fetch weather if launched via dock connection
if (DEBUG) Log.d(LOG_TAG, "Device now docked; forcing weather to refresh right now");
- sendBroadcast(
- new Intent("com.google.android.apps.genie.REFRESH")
- .putExtra("requestWeather", true));
+ requestWeatherDataFetch();
}
mInDock = launchedFromDock;
unregisterReceiver(mIntentReceiver);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.cancel(mMidnightIntent);
- unscheduleWeatherFetch();
+ unscheduleWeatherQuery();
super.onPause();
}
mWeatherLocation = (TextView) findViewById(R.id.weather_location);
mWeatherIcon = (ImageView) findViewById(R.id.weather_icon);
- mNextAlarm = (TextView) findViewById(R.id.nextAlarm);
-
- final ImageButton alarmButton = (ImageButton) findViewById(R.id.alarm_button);
- alarmButton.setOnClickListener(new View.OnClickListener() {
+ final View.OnClickListener alarmClickListener = new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(DeskClock.this, AlarmClock.class));
}
- });
+ };
+
+ mNextAlarm = (TextView) findViewById(R.id.nextAlarm);
+ mNextAlarm.setOnClickListener(alarmClickListener);
+
+ final ImageButton alarmButton = (ImageButton) findViewById(R.id.alarm_button);
+ alarmButton.setOnClickListener(alarmClickListener);
final ImageButton galleryButton = (ImageButton) findViewById(R.id.gallery_button);
galleryButton.setOnClickListener(new View.OnClickListener() {
Intent.ACTION_VIEW,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
.putExtra("slideshow", true)
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP));
} catch (android.content.ActivityNotFoundException e) {
Log.e(LOG_TAG, "Couldn't launch image browser", e);
}
try {
Intent musicAppQuery = getPackageManager()
.getLaunchIntentForPackage(MUSIC_PACKAGE_ID)
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (musicAppQuery != null) {
startActivity(musicAppQuery);
}
public void onClick(View v) {
startActivity(
new Intent(Intent.ACTION_MAIN)
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)
.addCategory(Intent.CATEGORY_HOME));
}
});
Intent genieAppQuery = getPackageManager()
.getLaunchIntentForPackage(GENIE_PACKAGE_ID)
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (genieAppQuery != null) {
startActivity(genieAppQuery);
}
if (item.getItemId() == R.id.menu_item_alarms) {
startActivity(new Intent(DeskClock.this, AlarmClock.class));
return true;
+ } else if (item.getItemId() == R.id.menu_item_add_alarm) {
+ AlarmClock.addNewAlarm(this);
+ return true;
}
return false;
}