Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #97 from alexstyl/widget_permissions
Browse files Browse the repository at this point in the history
Widget permissions
  • Loading branch information
alexstyl authored Apr 19, 2017
2 parents 4972c32 + a3c9600 commit c33e66b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand All @@ -41,6 +44,7 @@ void updateEventsIfNeeded() {
synchronized (REFRESH_LOCK) {
if (isFirstTimeRunning()) {
peopleEventsDatabaseRefresher.refreshEvents();
widgetRefresher.refreshAllWidgets();
namedayDatabaseRefresher.refreshNamedaysIfEnabled();
eventPreferences.markEventsAsInitialised();
} else {
Expand All @@ -59,6 +63,7 @@ private void updateEventsIfSettingsChanged() {

if (wereContactsUpdated) {
peopleEventsDatabaseRefresher.refreshEvents();
widgetRefresher.refreshAllWidgets();
}
if (wereContactsUpdated || wereNamedaysSettingsUpdated) {
namedayDatabaseRefresher.refreshNamedaysIfEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alexstyl.specialdates.wear;

public interface WidgetRefresher {

void refreshWidget();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/widget_upcoming_events_permission_background"
android:layout_height="match_parent"
android:background="#EF9A9A">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:importantForAccessibility="no"
android:src="@mipmap/ic_launcher" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/contact_permission_arrow_horizontal_margin"
android:layout_marginRight="@dimen/contact_permission_arrow_horizontal_margin"
android:importantForAccessibility="no"
android:src="@drawable/ic_action_arrow_light_back" />

<ImageView
android:id="@+id/contact_permission_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:importantForAccessibility="no"
android:src="@drawable/ic_contacts" />
</LinearLayout>

<TextView
android:id="@+id/contact_permission_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="@string/contact_permission_message_request"
android:textColor="#fff" />

</LinearLayout>
</FrameLayout>

0 comments on commit c33e66b

Please sign in to comment.