@@ -54,60 +54,66 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
54
54
public void onReceive (Context context , Intent intent ) {
55
55
super .onReceive (context , intent );
56
56
SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
57
- overhang = sharedPreferences .getInt ("overhang" , overhang );
57
+ if (intent .getAction ().split ("#" ).length != 1 ) {
58
+ overhang = sharedPreferences .getInt ("overhang" + intent .getAction ().split ("#" )[1 ], overhang );
59
+ controlsvisible = sharedPreferences .getBoolean ("controlsvisible" + intent .getAction ().split ("#" )[1 ], false );
60
+ }
58
61
59
- if (controlbutton .equals (intent .getAction ())) {
60
- Log .i (TAG , "onReceive: controlbutton pressed" );
62
+ if (controlbutton .equals (intent .getAction (). split ( "#" )[ 0 ] )) {
63
+ Log .i (TAG , "onReceive: controlbutton pressed on widget " + intent . getAction (). split ( "#" )[ 1 ] );
61
64
ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
62
65
AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
63
66
int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
64
67
if (!controlsvisible ) {
65
68
boolean switchcheck = true ;
66
69
for (int appWidgetID : ids ) {
67
- String timebefore = sharedPreferences .getString ("time" , "" );
68
- updateAppWidget (context , appWidgetManager , appWidgetID );
69
- String timeafter = sharedPreferences .getString ("time" , "" );
70
- if (timebefore != timeafter ) switchcheck = false ;
70
+ if (!sharedPreferences .getBoolean ("controlsvisible" + appWidgetID , false )) {
71
+ String timebefore = sharedPreferences .getString ("time" + appWidgetID , "" );
72
+ updateAppWidget (context , appWidgetManager , appWidgetID );
73
+ String timeafter = sharedPreferences .getString ("time" + appWidgetID , "" );
74
+ Log .i (TAG , "onReceive: comparing widget " + appWidgetID + ", before " + timebefore + " with after " + timeafter + ", result: " + !timebefore .equals (timeafter ));
75
+ if (!timebefore .equals (timeafter )) switchcheck = false ;
76
+ }
71
77
}
72
78
if (!switchcheck ) return ;
73
79
}
74
80
controlsvisible = !controlsvisible ;
81
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
82
+ editor .putBoolean ("controlsvisible" + intent .getAction ().split ("#" )[1 ], controlsvisible );
83
+ editor .apply ();
75
84
Log .i (TAG , "onReceive: new controlbutton state: " + controlsvisible );
76
85
RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
77
- for (int appWidgetID : ids ) {
78
- if (controlsvisible ) {
79
- remoteViews .setViewVisibility (R .id .plus , View .VISIBLE );
80
- remoteViews .setViewVisibility (R .id .minus , View .VISIBLE );
81
- remoteViews .setTextViewText (R .id .clock , "+" + overhang );
82
- Log .i (TAG , "onReceive: enabled controls" );
83
- } else {
84
- remoteViews .setViewVisibility (R .id .plus , View .GONE );
85
- remoteViews .setViewVisibility (R .id .minus , View .GONE );
86
- remoteViews .setTextViewText (R .id .clock , calculatetime ((double )Calendar .getInstance ().get (Calendar .HOUR_OF_DAY )*60 *60 +Calendar .getInstance ().get (Calendar .MINUTE )*60 ,overhang ));
87
- Log .i (TAG , "onReceive: disabled controls" );
88
- }
89
- appWidgetManager .updateAppWidget (appWidgetID , remoteViews );
86
+ if (controlsvisible ) {
87
+ remoteViews .setViewVisibility (R .id .plus , View .VISIBLE );
88
+ remoteViews .setViewVisibility (R .id .minus , View .VISIBLE );
89
+ remoteViews .setTextViewText (R .id .clock , "+" + overhang );
90
+ Log .i (TAG , "onReceive: enabled controls" );
91
+ } else {
92
+ remoteViews .setViewVisibility (R .id .plus , View .GONE );
93
+ remoteViews .setViewVisibility (R .id .minus , View .GONE );
94
+ updateAppWidget (context , appWidgetManager , Integer .valueOf (intent .getAction ().split ("#" )[1 ]));
95
+ Log .i (TAG , "onReceive: disabled controls" );
90
96
}
97
+ appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
91
98
}
92
- if (plusbutton .equals (intent .getAction ())) {
99
+ if (plusbutton .equals (intent .getAction (). split ( "#" )[ 0 ] )) {
93
100
Log .i (TAG , "onReceive: plusbutton pressed" );
94
101
if (overhang >=60 ) overhang += 60 ;
95
102
if (overhang <60 ) overhang += 10 ;
96
- //overhang = Math.min(overhang, 60);
103
+ //overhang = Math.min(overhang, 60); //Limit of 1 hour
104
+ //overhang = Math.min(overhang, 60*24); //Limit of a day
97
105
ComponentName thisAppWidget = new ComponentName (context .getPackageName (), getClass ().getName ());
98
106
AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
99
107
int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
100
108
RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
101
- for (int appWidgetID : ids ) {
102
- remoteViews .setTextViewText (R .id .clock , "+" + overhang );
103
- appWidgetManager .updateAppWidget (appWidgetID , remoteViews );
104
- }
105
- SharedPreferences .Editor editor = sharedPreferences .edit ();
106
- editor .putInt ("overhang" , overhang );
109
+ remoteViews .setTextViewText (R .id .clock , "+" + overhang );
110
+ appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
111
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
112
+ editor .putInt ("overhang" + Integer .valueOf (intent .getAction ().split ("#" )[1 ]), overhang );
107
113
editor .apply ();
108
- Log .i (TAG , "onReceive: new overhang value: " + overhang );
114
+ Log .i (TAG , "onReceive: new overhang " + Integer . valueOf ( intent . getAction (). split ( "#" )[ 1 ]) + " value: " + overhang );
109
115
}
110
- if (minusbutton .equals (intent .getAction ())) {
116
+ if (minusbutton .equals (intent .getAction (). split ( "#" )[ 0 ] )) {
111
117
Log .i (TAG , "onReceive: minusbutton pressed" );
112
118
if (overhang <=60 ) overhang -= 10 ;
113
119
if (overhang >60 ) overhang -= 60 ;
@@ -116,14 +122,12 @@ public void onReceive(Context context, Intent intent) {
116
122
AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
117
123
int [] ids = appWidgetManager .getAppWidgetIds (thisAppWidget );
118
124
RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
119
- for (int appWidgetID : ids ) {
120
- remoteViews .setTextViewText (R .id .clock , "+" + overhang );
121
- appWidgetManager .updateAppWidget (appWidgetID , remoteViews );
122
- }
125
+ remoteViews .setTextViewText (R .id .clock , "+" + overhang );
126
+ appWidgetManager .updateAppWidget (Integer .valueOf (intent .getAction ().split ("#" )[1 ]), remoteViews );
123
127
SharedPreferences .Editor editor = sharedPreferences .edit ();
124
- editor .putInt ("overhang" , overhang );
128
+ editor .putInt ("overhang" + Integer . valueOf ( intent . getAction (). split ( "#" )[ 1 ]) , overhang );
125
129
editor .apply ();
126
- Log .i (TAG , "onReceive: new overhang value: " + overhang );
130
+ Log .i (TAG , "onReceive: new overhang " + Integer . valueOf ( intent . getAction (). split ( "#" )[ 1 ]) + " value: " + overhang );
127
131
}
128
132
129
133
if (CLOCK_WIDGET_UPDATE .equals (intent .getAction ())) {
@@ -152,16 +156,20 @@ protected PendingIntent getPendingSelfIntent(Context context, String action) {
152
156
@ Override
153
157
public void onDisabled (Context context ) {
154
158
super .onDisabled (context );
155
- Log .i (TAG , "Widget Provider disabled. Turning off timer" );
159
+ Log .i (TAG , "Widget Provider disabled. Turning off timer and deleting all values " );
156
160
AlarmManager alarmManager = (AlarmManager )context .getSystemService (Context .ALARM_SERVICE );
157
161
alarmManager .cancel (createClockTickIntent (context ));
162
+ SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
163
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
164
+ editor .clear ();
165
+ editor .commit ();
158
166
}
159
167
160
168
@ Override
161
169
public void onEnabled (Context context ) {
162
170
super .onEnabled (context );
163
- SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
164
- overhang = sharedPreferences .getInt ("overhang" , overhang );
171
+ // SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
172
+ // overhang = sharedPreferences.getInt("overhang", overhang);
165
173
Log .i (TAG , "Widget Provider enabled. Starting timer to update widget every minute" );
166
174
AlarmManager alarmManager = (AlarmManager )context .getSystemService (Context .ALARM_SERVICE );
167
175
Calendar calendar = Calendar .getInstance ();
@@ -171,23 +179,29 @@ public void onEnabled(Context context) {
171
179
}
172
180
173
181
public void updateAppWidget (Context context , AppWidgetManager appWidgetManager , int appWidgetId ) {
174
- Log .i ("info" , "updateAppWidget: i have been called" );
175
182
SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences (context );
176
- overhang = sharedPreferences .getInt ("overhang" , overhang );
183
+ overhang = sharedPreferences .getInt ("overhang" + appWidgetId , overhang );
184
+ controlsvisible = sharedPreferences .getBoolean ("controlsvisible" + appWidgetId , false );
177
185
int hour = Calendar .getInstance ().get (Calendar .HOUR_OF_DAY );
178
186
int minutes = Calendar .getInstance ().get (Calendar .MINUTE );
179
187
//int seconds = Calendar.getInstance().get(Calendar.SECOND);
180
188
RemoteViews remoteViews = new RemoteViews (context .getPackageName (), R .layout .widget );
181
189
//remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes);
182
190
String time = calculatetime ((double )hour *60 *60 +minutes *60 ,overhang );
191
+ if (controlsvisible ) {
192
+ remoteViews .setViewVisibility (R .id .plus , View .GONE );
193
+ remoteViews .setViewVisibility (R .id .minus , View .GONE );
194
+ controlsvisible = false ;
195
+ Log .i (TAG , "updateAppWidget: disabled controls" );
196
+ }
183
197
remoteViews .setTextViewText (R .id .clock , time );
184
- SharedPreferences .Editor editor = sharedPreferences .edit ();
185
- editor .putString ("time" , time );
198
+ SharedPreferences .Editor editor = sharedPreferences .edit ();
199
+ editor .putString ("time" + appWidgetId , time );
186
200
editor .apply ();
187
201
//remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes + ":" + seconds);
188
- remoteViews .setOnClickPendingIntent (R .id .controlbutton , getPendingSelfIntent (context , controlbutton ));
189
- remoteViews .setOnClickPendingIntent (R .id .plus , getPendingSelfIntent (context , plusbutton ));
190
- remoteViews .setOnClickPendingIntent (R .id .minus , getPendingSelfIntent (context , minusbutton ));
202
+ remoteViews .setOnClickPendingIntent (R .id .controlbutton , getPendingSelfIntent (context , controlbutton + "#" + appWidgetId ));
203
+ remoteViews .setOnClickPendingIntent (R .id .plus , getPendingSelfIntent (context , plusbutton + "#" + appWidgetId ));
204
+ remoteViews .setOnClickPendingIntent (R .id .minus , getPendingSelfIntent (context , minusbutton + "#" + appWidgetId ));
191
205
appWidgetManager .updateAppWidget (appWidgetId , remoteViews );
192
206
Log .i ("info" , "updateAppWidget: updated widget " + appWidgetId );
193
207
}
0 commit comments