From ba3ffe66c70d060b4dfe7920d229a87d289a528f Mon Sep 17 00:00:00 2001 From: VishnuSanal Date: Sun, 17 Dec 2023 16:15:50 +0530 Subject: [PATCH] refactor AlarmHelper & QuoteWidget --- .../vishnu/quotes/helper/AlarmHelper.java | 8 ++-- .../vishnu/quotes/receiver/QuoteWidget.java | 38 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/phone/vishnu/quotes/helper/AlarmHelper.java b/app/src/main/java/phone/vishnu/quotes/helper/AlarmHelper.java index 2a5a43e..f5a8d76 100644 --- a/app/src/main/java/phone/vishnu/quotes/helper/AlarmHelper.java +++ b/app/src/main/java/phone/vishnu/quotes/helper/AlarmHelper.java @@ -207,11 +207,11 @@ public static void checkWidgetAlarm(Context context, Quote widgetQuote) { } } - public static void scheduleWidgetUpdate(Context context, String QUOTE_WIDGET_UPDATE) { + public static void scheduleWidgetUpdate(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, QuoteWidget.class); - intent.setAction(QUOTE_WIDGET_UPDATE); + intent.setAction(Constants.WIDGET_UPDATE_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast( context, @@ -237,11 +237,11 @@ public static void scheduleWidgetUpdate(Context context, String QUOTE_WIDGET_UPD pendingIntent); } - public static void removeWidgetUpdate(Context context, String QUOTE_WIDGET_UPDATE) { + public static void removeWidgetUpdate(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, QuoteWidget.class); - intent.setAction(QUOTE_WIDGET_UPDATE); + intent.setAction(Constants.WIDGET_UPDATE_ACTION); PendingIntent pendingIntent = PendingIntent.getBroadcast( context, diff --git a/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java b/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java index 5a8c5db..f3a4724 100644 --- a/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java +++ b/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java @@ -48,10 +48,26 @@ public class QuoteWidget extends AppWidgetProvider { + public static void updateWidget(Context context) { + try { + PendingIntent.getBroadcast( + context, + 0, + new Intent(context, QuoteWidget.class) + .setAction(Constants.WIDGET_UPDATE_ACTION), + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) + ? PendingIntent.FLAG_IMMUTABLE + : PendingIntent.FLAG_UPDATE_CURRENT) + .send(); + } catch (PendingIntent.CanceledException e) { + e.printStackTrace(); + } + } + @Override public void onEnabled(Context context) { super.onEnabled(context); - AlarmHelper.scheduleWidgetUpdate(context, Constants.WIDGET_UPDATE_ACTION); + AlarmHelper.scheduleWidgetUpdate(context); Quote widgetQuote = new SharedPreferenceHelper(context).getWidgetQuote(); if (widgetQuote != null) updateQuoteWidget(context, widgetQuote); else initAppWidget(context); @@ -61,14 +77,14 @@ public void onEnabled(Context context) { public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); new SharedPreferenceHelper(context).deleteWidgetQuote(); - AlarmHelper.removeWidgetUpdate(context, Constants.WIDGET_UPDATE_ACTION); + AlarmHelper.removeWidgetUpdate(context); } @Override public void onDisabled(Context context) { super.onDisabled(context); new SharedPreferenceHelper(context).deleteWidgetQuote(); - AlarmHelper.removeWidgetUpdate(context, Constants.WIDGET_UPDATE_ACTION); + AlarmHelper.removeWidgetUpdate(context); } @Override @@ -219,20 +235,4 @@ private void widgetShareButtonClicked(Context context) { .setAction(Constants.WIDGET_SHARE_ACTION) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } - - public static void updateWidget(Context context) { - try { - PendingIntent.getBroadcast( - context, - 0, - new Intent(context, QuoteWidget.class) - .setAction(Constants.WIDGET_UPDATE_ACTION), - (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) - ? PendingIntent.FLAG_IMMUTABLE - : PendingIntent.FLAG_UPDATE_CURRENT) - .send(); - } catch (PendingIntent.CanceledException e) { - e.printStackTrace(); - } - } }