Permalink
Browse files

Changed the alarms to trigger a broadcast instead of a service, and m…

…ake a static class to hold the wake lock so that it can be acquired in the broadcast receiver and released in the service.

Added icons.
  • Loading branch information...
jjc1138
jjc1138 committed Dec 6, 2008
1 parent 00fb7dd commit 0a942645a86d11482acf43c06504aacd1f44193e
View
@@ -21,6 +21,7 @@
</activity>
<service android:name=".Fetcher"
android:permission="net.jjc1138.android.twitter.privateservices" />
<receiver android:name=".AlarmReceiver" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
View
@@ -0,0 +1 @@
notification_icon_status_bar.png is desaturated by luminosity in GIMP.
View
340 Icon.svg

Large diffs are not rendered by default.

Oops, something went wrong.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,19 @@
package net.jjc1138.android.twitter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
// The API documentation says that AlarmManager can only fire broadcasts, not
// start services. Starting services does seem to work sometimes, but it doesn't
// seem to be reliable that the device will stay alive long enough to acquire
// the wake lock.
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
FetcherWakeLock.acquire(context);
context.startService(new Intent(context, Fetcher.class));
}
}
@@ -61,7 +61,6 @@
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
@@ -82,7 +81,6 @@
private FetcherThread fetcherThread;
private Handler handler;
private PowerManager.WakeLock wakeLock;
@Override
public IBinder onBind(Intent intent) {
@@ -93,11 +91,6 @@ public IBinder onBind(Intent intent) {
public void onCreate() {
super.onCreate();
wakeLock = ((PowerManager) getSystemService(POWER_SERVICE))
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
wakeLock.setReferenceCounted(false);
wakeLock.acquire();
prefs = getSharedPreferences(TwitterConfig.PREFS, 0);
handler = new Handler();
}
@@ -120,8 +113,8 @@ public void onStart(Intent intent, int startId) {
(prefs.getInt("interval",
TwitterConfig.INTERVALS[
TwitterConfig.DEFAULT_INTERVAL_INDEX]) * 60 * 1000),
PendingIntent.getService(this, 0,
new Intent(this, Fetcher.class), 0));
PendingIntent.getBroadcast(this, 0,
new Intent(this, AlarmReceiver.class), 0));
Log.d(LOG_TAG, "Scheduled next run.");
if (fetcherThread != null && fetcherThread.inProgress()) {
@@ -195,7 +188,7 @@ private HttpEntity download(DefaultHttpClient client, URI uri)
if (status == HttpStatus.SC_UNAUTHORIZED) {
Notification n = new Notification();
n.icon = R.drawable.icon; // TODO proper error icon
n.icon = R.drawable.notification_icon_status_bar;
Intent i = new Intent(
Fetcher.this, TwitterConfig.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -678,7 +671,7 @@ public int compare(Tweet t1, Tweet t2) {
for (final Tweet t : tweets) {
final Notification n = new Notification();
n.icon = R.drawable.icon; // TODO proper notification icon
n.icon = R.drawable.notification_icon_status_bar;
final String screenName = t.getScreenName();
final long id = t.getID();
final boolean message = t instanceof Message;
@@ -777,7 +770,7 @@ private void stopIfIdle() {
Log.d(LOG_TAG, "Not stopping service because thread is running.");
return;
}
wakeLock.release();
FetcherWakeLock.release();
Log.d(LOG_TAG, "Stopping service.");
stopSelf();
}
@@ -0,0 +1,28 @@
package net.jjc1138.android.twitter;
import android.content.Context;
import android.os.PowerManager;
// This is modeled after the way the Android alarm clock application handles
// its WakeLock.
class FetcherWakeLock {
private static PowerManager.WakeLock wakeLock;
static void acquire(Context context) {
PowerManager.WakeLock old = wakeLock;
wakeLock = ((PowerManager) context.getSystemService(
Context.POWER_SERVICE)).newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, Fetcher.LOG_TAG);
wakeLock.acquire();
if (old != null) {
old.release();
}
}
static void release() {
if (wakeLock != null) {
wakeLock.release();
wakeLock = null;
}
}
}
@@ -178,9 +178,12 @@ public void onClick(View v) {
uiToPrefs(prefs);
settingsChanged();
// Trigger the AlarmReceiver instead of starting the service
// directly so that the wake lock gets acquired.
// The service will take care of rescheduling itself
// appropriately.
startService(new Intent(TwitterConfig.this, Fetcher.class));
sendBroadcast(
new Intent(TwitterConfig.this, AlarmReceiver.class));
}
});
((Button) findViewById(R.id.revert)).setOnClickListener(

0 comments on commit 0a94264

Please sign in to comment.