Permalink
Browse files

clock now updates properly every minute.

  • Loading branch information...
1 parent e4ed795 commit 2c44bb02a4c73882a0f43b5cf57fac540d06b841 @gregkh committed Jun 9, 2010
View
@@ -5,6 +5,8 @@
android:versionName="001">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:description="@string/app_descrip">
+ <service android:name=".UpdateService" />
+
<receiver android:name=".MedAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
@@ -11,7 +11,7 @@
<style name="Text.Loading">
<item name="android:textSize">10sp</item>
- <item name="android:textColor">@color/semi_white</item>
+ <item name="android:textColor">@color/white</item>
</style>
</resources>
@@ -22,7 +22,7 @@
public class Configure extends Activity {
- public static final String MODULE = "ConfigureScreen";
+ public static final String MODULE = "GREGKHConfigureScreen";
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private Button okButton;
@@ -1,15 +1,14 @@
package com.kroah.clock;
-import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
-import android.widget.RemoteViews;
public class MedAppWidget extends AppWidgetProvider {
+
@Override
public void onUpdate(Context context,
AppWidgetManager appWidgetManager,
@@ -18,23 +17,34 @@ public void onUpdate(Context context,
appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, MedAppWidget.class));
}
- final int num_ids = appWidgetIds.length;
- for (int i = 0; i < num_ids; i++) {
- int appWidgetId = appWidgetIds[i];
-
- // Create a remote view
- RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_loading);
-
- // Tie clicking on the button to bring up our configure screen
- Intent intent = new Intent(context, Configure.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
- views.setOnClickPendingIntent(R.id.loading, pendingIntent);
-
- // TODO update the UI
-
- appWidgetManager.updateAppWidget(appWidgetId, views);
- }
+// UpdateService.requestUpdate(appWidgetIds);
+ context.startService(new Intent(context, UpdateService.class));
}
+
+// final int num_ids = appWidgetIds.length;
+// for (int i = 0; i < num_ids; i++) {
+//
+// int appWidgetId = appWidgetIds[i];
+//
+// Timer timer = new Timer();
+// timer.scheduleAtFixedRate(new myTime(context, appWidgetManager), 1, 10000);
+//
+// // Create a remote view
+// RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_loading);
+
+// // Tie clicking on the button to bring up our configure screen
+// Intent intent = new Intent(context, Configure.class);
+// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
+// views.setOnClickPendingIntent(R.id.loading, pendingIntent);
+//
+// // set the time
+//// DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
+//// views.setTextViewText(R.id.loading, format.format(new Date()));
+// // TODO update the UI
+//
+// appWidgetManager.updateAppWidget(appWidgetId, views);
+// }
+// }
}
@@ -0,0 +1,90 @@
+package com.kroah.clock;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+import android.text.format.DateUtils;
+import android.text.format.Time;
+import android.util.Log;
+import android.widget.RemoteViews;
+
+
+public class UpdateService extends Service implements Runnable {
+ private static final String MODULE = "GREGKHUpdateService";
+
+ private static final long UPDATE_INTERVAL = DateUtils.MINUTE_IN_MILLIS;
+
+ // Our lock for the list of times.
+ private static Object lock = new Object();
+ private static boolean thread_running;
+
+ public static final String ACTION_UPDATE_ALL = "com.kroah.clock.UPDATE_ALL";
+
+ @Override
+ public void onStart(Intent intent, int startId) {
+ RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.widget_loading);
+ Date date = new Date();
+ DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
+
+ updateViews.setTextViewText(R.id.loading, format.format(date));
+
+ ComponentName thisWidget = new ComponentName(this, MedAppWidget.class);
+ AppWidgetManager manager = AppWidgetManager.getInstance(this);
+ manager.updateAppWidget(thisWidget, updateViews);
+ synchronized (lock) {
+ if (!thread_running) {
+ thread_running = true;
+ new Thread(this).start();
+ }
+ }
+ }
+
+ public void run() {
+ Log.d(MODULE, "Processing thread started");
+
+ AppWidgetManager manager = AppWidgetManager.getInstance(this);
+// ContentResolver resolve = getContentResolver();
+
+ // Draw the updated time
+ RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.widget_loading);
+ Date date = new Date();
+ DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
+
+ updateViews.setTextViewText(R.id.loading, format.format(date));
+
+ ComponentName thisWidget = new ComponentName(this, MedAppWidget.class);
+ manager.updateAppWidget(thisWidget, updateViews);
+
+ // Schedule our next update
+ Time time = new Time();
+ time.set(System.currentTimeMillis() + UPDATE_INTERVAL);
+ time.second = 0;
+ long nextUpdate = time.toMillis(false);
+
+ Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
+ updateIntent.setClass(this, UpdateService.class);
+ PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
+
+ AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
+ alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdate, pendingIntent);
+
+ thread_running = false;
+ stopSelf();
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}

0 comments on commit 2c44bb0

Please sign in to comment.