Skip to content

Commit

Permalink
seperated widgets
Browse files Browse the repository at this point in the history
and fixed some minor issues regarding widget interconnection
  • Loading branch information
programminghoch10 committed Jul 22, 2019
1 parent dd65630 commit 8d93b99
Showing 1 changed file with 60 additions and 46 deletions.
106 changes: 60 additions & 46 deletions app/src/main/java/com/jj/hangoverclock/ClockWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,60 +54,66 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
overhang = sharedPreferences.getInt("overhang", overhang);
if (intent.getAction().split("#").length != 1) {
overhang = sharedPreferences.getInt("overhang" + intent.getAction().split("#")[1], overhang);
controlsvisible = sharedPreferences.getBoolean("controlsvisible" + intent.getAction().split("#")[1], false);
}

if (controlbutton.equals(intent.getAction())) {
Log.i(TAG, "onReceive: controlbutton pressed");
if (controlbutton.equals(intent.getAction().split("#")[0])) {
Log.i(TAG, "onReceive: controlbutton pressed on widget " + intent.getAction().split("#")[1]);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
if(!controlsvisible) {
boolean switchcheck = true;
for (int appWidgetID : ids) {
String timebefore = sharedPreferences.getString("time", "");
updateAppWidget(context, appWidgetManager, appWidgetID);
String timeafter = sharedPreferences.getString("time", "");
if (timebefore != timeafter) switchcheck = false;
if (!sharedPreferences.getBoolean("controlsvisible" + appWidgetID, false)) {
String timebefore = sharedPreferences.getString("time" + appWidgetID, "");
updateAppWidget(context, appWidgetManager, appWidgetID);
String timeafter = sharedPreferences.getString("time" + appWidgetID, "");
Log.i(TAG, "onReceive: comparing widget " + appWidgetID + ", before " + timebefore + " with after " + timeafter + ", result: " + !timebefore.equals(timeafter));
if (!timebefore.equals(timeafter)) switchcheck = false;
}
}
if (!switchcheck) return;
}
controlsvisible = !controlsvisible;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("controlsvisible" + intent.getAction().split("#")[1], controlsvisible);
editor.apply();
Log.i(TAG, "onReceive: new controlbutton state: " + controlsvisible);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int appWidgetID: ids) {
if (controlsvisible) {
remoteViews.setViewVisibility(R.id.plus, View.VISIBLE);
remoteViews.setViewVisibility(R.id.minus, View.VISIBLE);
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
Log.i(TAG, "onReceive: enabled controls");
} else {
remoteViews.setViewVisibility(R.id.plus, View.GONE);
remoteViews.setViewVisibility(R.id.minus, View.GONE);
remoteViews.setTextViewText(R.id.clock, calculatetime((double)Calendar.getInstance().get(Calendar.HOUR_OF_DAY)*60*60+Calendar.getInstance().get(Calendar.MINUTE)*60,overhang));
Log.i(TAG, "onReceive: disabled controls");
}
appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
if (controlsvisible) {
remoteViews.setViewVisibility(R.id.plus, View.VISIBLE);
remoteViews.setViewVisibility(R.id.minus, View.VISIBLE);
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
Log.i(TAG, "onReceive: enabled controls");
} else {
remoteViews.setViewVisibility(R.id.plus, View.GONE);
remoteViews.setViewVisibility(R.id.minus, View.GONE);
updateAppWidget(context, appWidgetManager, Integer.valueOf(intent.getAction().split("#")[1]));
Log.i(TAG, "onReceive: disabled controls");
}
appWidgetManager.updateAppWidget(Integer.valueOf(intent.getAction().split("#")[1]), remoteViews);
}
if (plusbutton.equals(intent.getAction())) {
if (plusbutton.equals(intent.getAction().split("#")[0])) {
Log.i(TAG, "onReceive: plusbutton pressed");
if (overhang>=60) overhang += 60;
if (overhang<60) overhang += 10;
//overhang = Math.min(overhang, 60);
//overhang = Math.min(overhang, 60); //Limit of 1 hour
//overhang = Math.min(overhang, 60*24); //Limit of a day
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int appWidgetID: ids) {
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
}
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putInt("overhang", overhang);
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
appWidgetManager.updateAppWidget(Integer.valueOf(intent.getAction().split("#")[1]), remoteViews);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("overhang" + Integer.valueOf(intent.getAction().split("#")[1]), overhang);
editor.apply();
Log.i(TAG, "onReceive: new overhang value: " + overhang);
Log.i(TAG, "onReceive: new overhang " + Integer.valueOf(intent.getAction().split("#")[1]) + " value: " + overhang);
}
if (minusbutton.equals(intent.getAction())) {
if (minusbutton.equals(intent.getAction().split("#")[0])) {
Log.i(TAG, "onReceive: minusbutton pressed");
if (overhang<=60) overhang -= 10;
if (overhang>60) overhang -= 60;
Expand All @@ -116,14 +122,12 @@ public void onReceive(Context context, Intent intent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] ids = appWidgetManager.getAppWidgetIds(thisAppWidget);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
for (int appWidgetID: ids) {
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
appWidgetManager.updateAppWidget(appWidgetID, remoteViews);
}
remoteViews.setTextViewText(R.id.clock, "+" + overhang);
appWidgetManager.updateAppWidget(Integer.valueOf(intent.getAction().split("#")[1]), remoteViews);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putInt("overhang", overhang);
editor.putInt("overhang" + Integer.valueOf(intent.getAction().split("#")[1]), overhang);
editor.apply();
Log.i(TAG, "onReceive: new overhang value: " + overhang);
Log.i(TAG, "onReceive: new overhang " + Integer.valueOf(intent.getAction().split("#")[1]) + " value: " + overhang);
}

if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
Expand Down Expand Up @@ -152,16 +156,20 @@ protected PendingIntent getPendingSelfIntent(Context context, String action) {
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.i(TAG, "Widget Provider disabled. Turning off timer");
Log.i(TAG, "Widget Provider disabled. Turning off timer and deleting all values");
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.clear();
editor.commit();
}

@Override
public void onEnabled(Context context) {
super.onEnabled(context);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
overhang = sharedPreferences.getInt("overhang", overhang);
//SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//overhang = sharedPreferences.getInt("overhang", overhang);
Log.i(TAG, "Widget Provider enabled. Starting timer to update widget every minute");
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Expand All @@ -171,23 +179,29 @@ public void onEnabled(Context context) {
}

public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.i("info", "updateAppWidget: i have been called");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
overhang = sharedPreferences.getInt("overhang", overhang);
overhang = sharedPreferences.getInt("overhang" + appWidgetId, overhang);
controlsvisible = sharedPreferences.getBoolean("controlsvisible" + appWidgetId, false);
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int minutes = Calendar.getInstance().get(Calendar.MINUTE);
//int seconds = Calendar.getInstance().get(Calendar.SECOND);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
//remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes);
String time = calculatetime((double)hour*60*60+minutes*60,overhang);
if (controlsvisible) {
remoteViews.setViewVisibility(R.id.plus, View.GONE);
remoteViews.setViewVisibility(R.id.minus, View.GONE);
controlsvisible = false;
Log.i(TAG, "updateAppWidget: disabled controls");
}
remoteViews.setTextViewText(R.id.clock, time);
SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putString("time", time);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("time" + appWidgetId, time);
editor.apply();
//remoteViews.setTextViewText(R.id.clock, hour + ":" + minutes + ":" + seconds);
remoteViews.setOnClickPendingIntent(R.id.controlbutton, getPendingSelfIntent(context, controlbutton));
remoteViews.setOnClickPendingIntent(R.id.plus, getPendingSelfIntent(context, plusbutton));
remoteViews.setOnClickPendingIntent(R.id.minus, getPendingSelfIntent(context, minusbutton));
remoteViews.setOnClickPendingIntent(R.id.controlbutton, getPendingSelfIntent(context, controlbutton + "#" + appWidgetId));
remoteViews.setOnClickPendingIntent(R.id.plus, getPendingSelfIntent(context, plusbutton + "#" + appWidgetId));
remoteViews.setOnClickPendingIntent(R.id.minus, getPendingSelfIntent(context, minusbutton + "#" + appWidgetId));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
Log.i("info", "updateAppWidget: updated widget " + appWidgetId);
}
Expand Down

0 comments on commit 8d93b99

Please sign in to comment.