-
-
Save shobhitchittora/ae6505c3553beaddbda5 to your computer and use it in GitHub Desktop.
TCW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.r2d.the_clock_widget; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import android.annotation.SuppressLint; | |
import android.app.AlarmManager; | |
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.os.PowerManager; | |
import android.provider.AlarmClock; | |
import android.util.Log; | |
import android.widget.RemoteViews; | |
@SuppressLint({ "SimpleDateFormat", "InlinedApi" }) | |
public class MainActivity extends AppWidgetProvider{ | |
private static final String LOG_TAG = "TheClockWidget"; | |
private static String CLOCK_WIDGET_UPDATE="com.r2d.The_Clock_Widget.WIDGET_UPDATE"; | |
@SuppressLint("SimpleDateFormat") | |
private static final java.text.DateFormat df = new SimpleDateFormat("kk:mm:ss"); | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
// TODO Auto-generated method stub | |
super.onReceive(context, intent); | |
if(CLOCK_WIDGET_UPDATE.equals(intent.getAction())){ | |
Log.d(LOG_TAG, "Clock Update"); | |
ComponentName appwidget = new ComponentName(context.getPackageName(),getClass().getName()); | |
AppWidgetManager appwidgetManager = AppWidgetManager.getInstance(context); | |
int[] appWidgetIds = appwidgetManager.getAppWidgetIds(appwidget); | |
for(int id:appWidgetIds){ //Update all instances of the widget | |
updatetime(context,appwidgetManager,id); | |
} | |
} | |
} | |
private PendingIntent createClockIntent(Context context){ | |
Intent intent = new Intent(CLOCK_WIDGET_UPDATE); | |
PendingIntent pendingInt = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
return pendingInt; | |
} | |
@Override | |
public void onDisabled(Context context) { | |
// TODO Auto-generated method stub | |
super.onDisabled(context); | |
Log.d(LOG_TAG, "Widget Provider disabled. Turning off timer"); | |
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); | |
alarmManager.cancel(createClockIntent(context)); | |
} | |
@Override | |
public void onEnabled(Context context) { | |
// TODO Auto-generated method stub | |
super.onEnabled(context); | |
Log.d(LOG_TAG, "Widget Provider enabled. Starting timer to update widget every second"); | |
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); | |
Calendar cal= Calendar.getInstance(); | |
cal.setTimeInMillis(System.currentTimeMillis()); | |
cal.add(Calendar.SECOND,1); | |
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),1000,createClockIntent(context)); //Get new time every second | |
} | |
@Override | |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, | |
int[] appWidgetIds) { | |
// TODO Auto-generated method stub | |
super.onUpdate(context, appWidgetManager, appWidgetIds); | |
PowerManager pm= (PowerManager)context.getSystemService(Context.POWER_SERVICE); // Keep the CPU on during update | |
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG); | |
wl.acquire(); | |
for(int i=0;i<appWidgetIds.length;i++){ | |
int id=appWidgetIds[i]; | |
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM); | |
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0); | |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main); | |
views.setOnClickPendingIntent(R.id.textView1, pending); // send pending intent to textview1 | |
views.setOnClickPendingIntent(R.id.textView2, pending); // send pending intent to textview2 | |
updatetime(context, appWidgetManager, id); | |
appWidgetManager.updateAppWidget(id, views); | |
} | |
wl.release(); | |
} | |
private static void updatetime(Context context, AppWidgetManager appwidgetManager, int id){ | |
String curTime = df.format(new java.util.Date()); | |
Date d= new Date(); | |
Calendar c= Calendar.getInstance(); | |
c.setTime(d); | |
String monthName = new SimpleDateFormat("MMMM").format(c.getTime()); | |
@SuppressWarnings("deprecation") | |
String date= String.valueOf(d.getDate())+" "+ monthName + " "+ String.valueOf(d.getYear()+1900); | |
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.activity_main); | |
views.setTextViewText(R.id.textView1, curTime); | |
views.setTextViewText(R.id.textView2, date); | |
appwidgetManager.updateAppWidget(id, views); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment