99import android .content .Intent ;
1010import android .content .SharedPreferences ;
1111import android .preference .PreferenceManager ;
12- import android .support .annotation .VisibleForTesting ;
13- import android .util .Log ;
1412import android .view .View ;
1513import android .widget .RemoteViews ;
1614
1715import java .util .Calendar ;
1816
19- //import static android.content.ContentValues.TAG;
20-
2117public class ClockWidgetProvider extends AppWidgetProvider {
22-
23- static String TAG = "info" ;
18+
2419 static final int houroverhang = 1 ;
2520
2621 public void onUpdate (Context context , AppWidgetManager appWidgetManager , int [] appWidgetIds ) {
27- Log .i ("info" , "onUpdate: i have been called" );
28- //int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
29- //int minutes = Calendar.getInstance().get(Calendar.MINUTE);
30- //int seconds = Calendar.getInstance().get(Calendar.SECOND);
31- for (int i = 0 ; i <appWidgetIds .length ; i ++) {
22+ for (int appWidgetId : appWidgetIds ) {
3223 RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
33- //remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes);
34- //appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
35- Log .i ("info" , "onUpdate: updated widget " +i );
36-
37- int appWidgetId = appWidgetIds [i ];
3824 appWidgetManager .updateAppWidget (appWidgetId , remoteViews );
3925 updateAppWidget (context , appWidgetManager , appWidgetId );
4026 }
4127 }
4228
43- /**
44- * Custom Intent name that is used by the AlarmManager to tell us to update the clock once per second.
45- */
4629 public static int overhang ;
4730 public static boolean controlsvisible = false ;
4831 public static String CLOCK_WIDGET_UPDATE = "com.JJ.hangoverclock.widgetupdate" ;
@@ -60,7 +43,6 @@ public void onReceive(Context context, Intent intent) {
6043 }
6144
6245 if (controlbutton .equals (intent .getAction ().split ("#" )[0 ])) {
63- Log .i (TAG , "onReceive: controlbutton pressed on widget " + intent .getAction ().split ("#" )[1 ]);
6446 ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
6547 AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
6648 int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
@@ -71,7 +53,6 @@ public void onReceive(Context context, Intent intent) {
7153 String timebefore = sharedPreferences .getString ("time" + appWidgetID , "" );
7254 updateAppWidget (context , appWidgetManager , appWidgetID );
7355 String timeafter = sharedPreferences .getString ("time" + appWidgetID , "" );
74- Log .i (TAG , "onReceive: comparing widget " + appWidgetID + ", before " + timebefore + " with after " + timeafter + ", result: " + !timebefore .equals (timeafter ));
7556 if (!timebefore .equals (timeafter )) switchcheck = false ;
7657 }
7758 }
@@ -81,59 +62,46 @@ public void onReceive(Context context, Intent intent) {
8162 SharedPreferences .Editor editor = sharedPreferences .edit ();
8263 editor .putBoolean ("controlsvisible" + intent .getAction ().split ("#" )[1 ], controlsvisible );
8364 editor .apply ();
84- Log .i (TAG , "onReceive: new controlbutton state: " + controlsvisible );
8565 RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
8666 if (controlsvisible ) {
8767 remoteViews .setViewVisibility (R .id .plus , View .VISIBLE );
8868 remoteViews .setViewVisibility (R .id .minus , View .VISIBLE );
8969 remoteViews .setTextViewText (R .id .clock , "+" + overhang );
90- Log .i (TAG , "onReceive: enabled controls" );
9170 } else {
9271 remoteViews .setViewVisibility (R .id .plus , View .GONE );
9372 remoteViews .setViewVisibility (R .id .minus , View .GONE );
9473 updateAppWidget (context , appWidgetManager , Integer .valueOf (intent .getAction ().split ("#" )[1 ]));
95- Log .i (TAG , "onReceive: disabled controls" );
9674 }
9775 appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
9876 }
9977 if (plusbutton .equals (intent .getAction ().split ("#" )[0 ])) {
100- Log .i (TAG , "onReceive: plusbutton pressed" );
10178 if (overhang >=60 ) overhang += 60 ;
10279 if (overhang <60 ) overhang += 10 ;
10380 //overhang = Math.min(overhang, 60); //Limit of 1 hour
10481 //overhang = Math.min(overhang, 60*24); //Limit of a day
105- ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
10682 AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
107- int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
10883 RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
10984 remoteViews .setTextViewText (R .id .clock , "+" + overhang );
11085 appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
11186 SharedPreferences .Editor editor = sharedPreferences .edit ();
11287 editor .putInt ("overhang" + Integer .valueOf (intent .getAction ().split ("#" )[1 ]), overhang );
11388 editor .apply ();
114- Log .i (TAG , "onReceive: new overhang " + Integer .valueOf (intent .getAction ().split ("#" )[1 ]) + " value: " + overhang );
11589 }
11690 if (minusbutton .equals (intent .getAction ().split ("#" )[0 ])) {
117- Log .i (TAG , "onReceive: minusbutton pressed" );
11891 if (overhang <=60 ) overhang -= 10 ;
11992 if (overhang >60 ) overhang -= 60 ;
12093 overhang = Math .max (overhang , 0 );
121- ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
12294 AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
123- int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
12495 RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
12596 remoteViews .setTextViewText (R .id .clock , "+" + overhang );
12697 appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
12798 SharedPreferences .Editor editor = sharedPreferences .edit ();
12899 editor .putInt ("overhang" + Integer .valueOf (intent .getAction ().split ("#" )[1 ]), overhang );
129100 editor .apply ();
130- Log .i (TAG , "onReceive: new overhang " + Integer .valueOf (intent .getAction ().split ("#" )[1 ]) + " value: " + overhang );
131101 }
132102
133103 if (CLOCK_WIDGET_UPDATE .equals (intent .getAction ())) {
134- Log .i (TAG , "Clock update" );
135- // Get the widget manager and ids for this widget provider, then call the shared
136- // clock update method.
104+ // Get the widget manager and ids for this widget provider, then call the shared clock update method.
137105 ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
138106 AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
139107 int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
@@ -156,7 +124,6 @@ protected PendingIntent getPendingSelfIntent(Context context, String action) {
156124 @ Override
157125 public void onDisabled (Context context ) {
158126 super .onDisabled (context );
159- Log .i (TAG , "Widget Provider disabled. Turning off timer and deleting all values" );
160127 AlarmManager alarmManager = (AlarmManager )context .getSystemService (Context .ALARM_SERVICE );
161128 alarmManager .cancel (createClockTickIntent (context ));
162129 SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
@@ -168,9 +135,6 @@ public void onDisabled(Context context) {
168135 @ Override
169136 public void onEnabled (Context context ) {
170137 super .onEnabled (context );
171- //SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
172- //overhang = sharedPreferences.getInt("overhang", overhang);
173- Log .i (TAG , "Widget Provider enabled. Starting timer to update widget every minute" );
174138 AlarmManager alarmManager = (AlarmManager )context .getSystemService (Context .ALARM_SERVICE );
175139 Calendar calendar = Calendar .getInstance ();
176140 calendar .setTimeInMillis (System .currentTimeMillis ());
@@ -184,53 +148,33 @@ public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
184148 controlsvisible = sharedPreferences .getBoolean ("controlsvisible" + appWidgetId , false );
185149 int hour = Calendar .getInstance ().get (Calendar .HOUR_OF_DAY );
186150 int minutes = Calendar .getInstance ().get (Calendar .MINUTE );
187- //int seconds = Calendar.getInstance().get(Calendar.SECOND);
188151 RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
189- //remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes);
190152 String time = calculatetime ((double )hour *60 *60 +minutes *60 ,overhang );
191153 if (controlsvisible ) {
192154 remoteViews .setViewVisibility (R .id .plus , View .GONE );
193155 remoteViews .setViewVisibility (R .id .minus , View .GONE );
194156 controlsvisible = false ;
195- Log .i (TAG , "updateAppWidget: disabled controls" );
196157 }
197158 remoteViews .setTextViewText (R .id .clock , time );
198159 SharedPreferences .Editor editor = sharedPreferences .edit ();
199160 editor .putString ("time" + appWidgetId , time );
200161 editor .apply ();
201- //remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes + ":" + seconds);
202162 remoteViews .setOnClickPendingIntent (R .id .controlbutton , getPendingSelfIntent (context , controlbutton + "#" + appWidgetId ));
203163 remoteViews .setOnClickPendingIntent (R .id .plus , getPendingSelfIntent (context , plusbutton + "#" + appWidgetId ));
204164 remoteViews .setOnClickPendingIntent (R .id .minus , getPendingSelfIntent (context , minusbutton + "#" + appWidgetId ));
205165 appWidgetManager .updateAppWidget (appWidgetId , remoteViews );
206- Log .i ("info" , "updateAppWidget: updated widget " + appWidgetId );
207166 }
208167
209168 public static String calculatetime (double time , int overhang ) {
210169 //inputs: double time in seconds
211170 // int overhang in seconds
212- //if (overhang>60) overhang = 60;
213171 int h = (int ) Math .floor (time / 60 / 60 );
214172 int m = (int ) Math .floor (time / 60 ) - (h *60 );
215- //int s = (int) Math.floor(time) - (m*60) - (h*60*60);
216- //int ms = (int) ((time - (h*60*60) - (m*60) - s) * 100);
217- //final String timetext2 = String.format();
218- //final String timetext2 = String.format("%02d", h)+":"+String.format("%02d", m)+":"+String.format("%02d", s)+"."+String.format("%02d", ms);
219- /*if (h>0 & m<overhang) {
220- m = m+60;
221- if(m>=60) h--;
222- }/*
223- /*if (m>0 & s<overhang) {
224- s = s+60;
225- if(s>=60) m--;
226- }*/
227173 while (m <overhang ) {
228174 m = m +60 ;
229175 if (m >=60 ) h --;
230176 if (h <houroverhang ) h +=24 ;
231177 }
232- //final String timetext = h+":"+m+":"+s+"."+ms;
233- //final String timetext = String.format("%02d", h)+":"+String.format("%02d", m)+":"+String.format("%02d", s)+"."+String.format("%02d", ms);
234178 return String .format ("%02d" , h )+":" +String .format ("%02d" , m );
235179 }
236180
0 commit comments