diff --git a/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/PeopleEventsUpdater.java b/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/PeopleEventsUpdater.java index 987b2057..cbafd433 100644 --- a/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/PeopleEventsUpdater.java +++ b/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/PeopleEventsUpdater.java @@ -1,6 +1,7 @@ package com.alexstyl.specialdates.events.peopleevents; import com.alexstyl.specialdates.ErrorTracker; +import com.alexstyl.specialdates.ExternalWidgetRefresher; import com.alexstyl.specialdates.events.namedays.NamedayDatabaseRefresher; import com.alexstyl.specialdates.permissions.PermissionChecker; import com.alexstyl.specialdates.upcoming.NamedaySettingsMonitor; @@ -16,20 +17,22 @@ class PeopleEventsUpdater { private final ContactsObserver contactsObserver; private final PermissionChecker permissionChecker; private final NamedayDatabaseRefresher namedayDatabaseRefresher; + private final ExternalWidgetRefresher widgetRefresher; PeopleEventsUpdater(PeopleEventsDatabaseRefresher peopleEventsDatabaseRefresher, NamedayDatabaseRefresher namedayDatabaseRefresher, EventPreferences eventPreferences, ContactsObserver contactsObserver, NamedaySettingsMonitor namedayMonitor, - PermissionChecker permissionChecker - ) { + PermissionChecker permissionChecker, + ExternalWidgetRefresher widgetRefresher) { this.peopleEventsDatabaseRefresher = peopleEventsDatabaseRefresher; this.namedayDatabaseRefresher = namedayDatabaseRefresher; this.eventPreferences = eventPreferences; this.contactsObserver = contactsObserver; this.namedayMonitor = namedayMonitor; this.permissionChecker = permissionChecker; + this.widgetRefresher = widgetRefresher; } void updateEventsIfNeeded() { @@ -41,6 +44,7 @@ void updateEventsIfNeeded() { synchronized (REFRESH_LOCK) { if (isFirstTimeRunning()) { peopleEventsDatabaseRefresher.refreshEvents(); + widgetRefresher.refreshAllWidgets(); namedayDatabaseRefresher.refreshNamedaysIfEnabled(); eventPreferences.markEventsAsInitialised(); } else { @@ -59,6 +63,7 @@ private void updateEventsIfSettingsChanged() { if (wereContactsUpdated) { peopleEventsDatabaseRefresher.refreshEvents(); + widgetRefresher.refreshAllWidgets(); } if (wereContactsUpdated || wereNamedaysSettingsUpdated) { namedayDatabaseRefresher.refreshNamedaysIfEnabled(); diff --git a/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/StaticEventsContentProvider.java b/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/StaticEventsContentProvider.java index faedab00..444523cc 100644 --- a/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/StaticEventsContentProvider.java +++ b/mobile/src/main/java/com/alexstyl/specialdates/events/peopleevents/StaticEventsContentProvider.java @@ -13,6 +13,7 @@ import android.os.Handler; import android.support.annotation.NonNull; +import com.alexstyl.specialdates.ExternalWidgetRefresher; import com.alexstyl.specialdates.contact.AndroidContactsProvider; import com.alexstyl.specialdates.events.database.DatabaseContract; import com.alexstyl.specialdates.events.database.EventColumns; @@ -51,13 +52,15 @@ public boolean onCreate() { ContactEventsMarshaller mementoMarshaller = new ContactEventsMarshaller(EventColumns.SOURCE_MEMENTO); NamedayCalendarProvider namedayCalendarProvider = NamedayCalendarProvider.newInstance(resources); PeopleNamedaysCalculator calculator = new PeopleNamedaysCalculator(namedayPreferences, namedayCalendarProvider, contactsProvider); + ExternalWidgetRefresher widgetRefresher = ExternalWidgetRefresher.get(context); peopleEventsUpdater = new PeopleEventsUpdater( new PeopleEventsDatabaseRefresher(repository, deviceEventsMarshaller, peopleEventsPersister), new NamedayDatabaseRefresher(namedayPreferences, peopleEventsPersister, mementoMarshaller, calculator), new EventPreferences(context), new ContactsObserver(contentResolver, new Handler()), new NamedaySettingsMonitor(namedayPreferences), - new PermissionChecker(context) + new PermissionChecker(context), + widgetRefresher ); peopleEventsUpdater.register(); uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); diff --git a/mobile/src/main/java/com/alexstyl/specialdates/permissions/PermissionChecker.java b/mobile/src/main/java/com/alexstyl/specialdates/permissions/PermissionChecker.java index 90bf91fb..111e4185 100644 --- a/mobile/src/main/java/com/alexstyl/specialdates/permissions/PermissionChecker.java +++ b/mobile/src/main/java/com/alexstyl/specialdates/permissions/PermissionChecker.java @@ -24,9 +24,9 @@ public boolean canReadExternalStorage() { return hasPermission(context, READ_EXTERNAL_STORAGE); } - private static boolean hasPermission(Context context, String readContacts) { + private static boolean hasPermission(Context context, String permission) { try { - return ActivityCompat.checkSelfPermission(context, readContacts) == PackageManager.PERMISSION_GRANTED; + return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; } catch (RuntimeException ex) { // some devices randomly throw an exception to this point. just treat as if the permission is not there ErrorTracker.track(ex); diff --git a/mobile/src/main/java/com/alexstyl/specialdates/upcoming/widget/list/UpcomingEventsAppWidgetProvider.java b/mobile/src/main/java/com/alexstyl/specialdates/upcoming/widget/list/UpcomingEventsAppWidgetProvider.java index 78c75ce1..6ece340c 100644 --- a/mobile/src/main/java/com/alexstyl/specialdates/upcoming/widget/list/UpcomingEventsAppWidgetProvider.java +++ b/mobile/src/main/java/com/alexstyl/specialdates/upcoming/widget/list/UpcomingEventsAppWidgetProvider.java @@ -12,13 +12,24 @@ import com.alexstyl.specialdates.date.Date; import com.alexstyl.specialdates.date.DateDisplayStringCreator; import com.alexstyl.specialdates.datedetails.DateDetailsActivity; +import com.alexstyl.specialdates.permissions.PermissionChecker; import com.alexstyl.specialdates.ui.activity.MainActivity; public class UpcomingEventsAppWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + super.onUpdate(context, appWidgetManager, appWidgetIds); + + PermissionChecker permissionChecker = new PermissionChecker(context); + if (permissionChecker.canReadAndWriteContacts()) { + showUpcomingEvents(context, appWidgetManager, appWidgetIds); + } else { + askForContactReadPermission(context, appWidgetManager, appWidgetIds); + } + } + private void showUpcomingEvents(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Date date = Date.today(); String dateLabel = DateDisplayStringCreator.INSTANCE.fullyFormattedDate(date); @@ -33,16 +44,25 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a remoteViews.setPendingIntentTemplate(R.id.widget_upcoming_events_list, listPendingIntent); remoteViews.setTextViewText(R.id.widget_upcoming_events_date, dateLabel); - PendingIntent todayDatePendingIntent = pendingIntentFor(context); + PendingIntent todayDatePendingIntent = pendingIntentToMain(context); remoteViews.setOnClickPendingIntent(R.id.widget_upcoming_events_date, todayDatePendingIntent); appWidgetManager.updateAppWidget(appWidgetId, remoteViews); } - super.onUpdate(context, appWidgetManager, appWidgetIds); } - private PendingIntent pendingIntentFor(Context context) { + private PendingIntent pendingIntentToMain(Context context) { Intent clickIntent = new Intent(context, MainActivity.class); return PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); } + + private void askForContactReadPermission(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + for (int appWidgetId : appWidgetIds) { + + RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_upcoming_events_need_permissions); + remoteViews.setOnClickPendingIntent(R.id.widget_upcoming_events_permission_background, pendingIntentToMain(context)); + appWidgetManager.updateAppWidget(appWidgetId, remoteViews); + } + + } } diff --git a/mobile/src/main/java/com/alexstyl/specialdates/wear/WidgetRefresher.java b/mobile/src/main/java/com/alexstyl/specialdates/wear/WidgetRefresher.java index ea154c31..6cac5950 100644 --- a/mobile/src/main/java/com/alexstyl/specialdates/wear/WidgetRefresher.java +++ b/mobile/src/main/java/com/alexstyl/specialdates/wear/WidgetRefresher.java @@ -1,6 +1,5 @@ package com.alexstyl.specialdates.wear; public interface WidgetRefresher { - void refreshWidget(); } diff --git a/mobile/src/main/res/layout/widget_upcoming_events_need_permissions.xml b/mobile/src/main/res/layout/widget_upcoming_events_need_permissions.xml new file mode 100644 index 00000000..fbe7a8f8 --- /dev/null +++ b/mobile/src/main/res/layout/widget_upcoming_events_need_permissions.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + +