Skip to content

Commit

Permalink
refactor AlarmHelper & QuoteWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Dec 17, 2023
1 parent ae0c9d6 commit ba3ffe6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/phone/vishnu/quotes/helper/AlarmHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
38 changes: 19 additions & 19 deletions app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit ba3ffe6

Please sign in to comment.