Skip to content

Commit

Permalink
plusonelabs#298 Fix: No calendar updates in Android 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
yvolk committed Jan 19, 2019
1 parent 452f0f1 commit 74ffc14
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {

defaultConfig {
versionCode getCommitCount()
versionName '2.0.0' + "-" + versionCode
versionName '2.0.1' + "-" + versionCode
minSdkVersion 15
targetSdkVersion 27

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@
<action android:name="android.intent.action.DATE_CHANGED"/>
<action android:name="android.intent.action.TIME_SET"/>
<action android:name="android.intent.action.LOCALE_CHANGED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="org.andstatus.todoagenda.action.REFRESH"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"/>

<data android:scheme="content"/>
<data android:host="com.android.calendar"/>
</intent-filter>
<intent-filter>
<action android:name="org.andstatus.todoagenda.action.REFRESH"/>
</intent-filter>
</receiver>
<receiver android:name="org.andstatus.todoagenda.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.EVENT_REMINDER" />
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class EnvironmentChangedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.i(this.getClass().getName(), "Received intent: " + intent);
String action = intent.getAction();
if (action.equals(Intent.ACTION_LOCALE_CHANGED)
|| action.equals(Intent.ACTION_TIME_CHANGED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,42 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.util.PermissionsUtil;

import org.joda.time.DateTime;

import java.util.List;
import java.util.Locale;

import static android.graphics.Color.*;
import static org.andstatus.todoagenda.CalendarIntentUtil.*;
import static org.andstatus.todoagenda.RemoteViewsUtil.*;
import static android.graphics.Color.alpha;
import static android.graphics.Color.blue;
import static android.graphics.Color.green;
import static android.graphics.Color.red;
import static org.andstatus.todoagenda.CalendarIntentUtil.createOpenCalendarAtDayIntent;
import static org.andstatus.todoagenda.CalendarIntentUtil.createOpenCalendarEventPendingIntent;
import static org.andstatus.todoagenda.CalendarIntentUtil.createOpenCalendarPendingIntent;
import static org.andstatus.todoagenda.RemoteViewsUtil.setAlpha;
import static org.andstatus.todoagenda.RemoteViewsUtil.setColorFilter;
import static org.andstatus.todoagenda.RemoteViewsUtil.setImageFromAttr;
import static org.andstatus.todoagenda.RemoteViewsUtil.setTextColorFromAttr;
import static org.andstatus.todoagenda.RemoteViewsUtil.setTextSize;
import static org.andstatus.todoagenda.Theme.themeNameToResId;

public class EventAppWidgetProvider extends AppWidgetProvider {

private static final String PACKAGE = EventAppWidgetProvider.class.getPackage().getName();
public static final String ACTION_REFRESH = PACKAGE + ".action.REFRESH";
private static volatile boolean receiversRegistered = false;

public static int[] getWidgetIds(Context context) {
return AppWidgetManager.getInstance(context)
Expand All @@ -47,6 +58,7 @@ public void onDeleted(Context context, int[] appWidgetIds) {

@Override
public void onUpdate(Context baseContext, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
registerReceivers(baseContext);
for (int widgetId : appWidgetIds) {
InstanceSettings settings = InstanceSettings.fromId(baseContext, widgetId);
AlarmReceiver.scheduleAlarm(settings.getHeaderThemeContext());
Expand Down Expand Up @@ -122,7 +134,8 @@ private static PendingIntent getEmptyPendingIntent(Context context) {
}

private void configureRefresh(Context context, RemoteViews rv) {
Intent intent = new Intent(ACTION_REFRESH);
Intent intent = new Intent(context, EnvironmentChangedReceiver.class);
intent.setAction(ACTION_REFRESH);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
rv.setOnClickPendingIntent(R.id.refresh, pendingIntent);
}
Expand Down Expand Up @@ -163,6 +176,7 @@ private void configureList(InstanceSettings settings, int widgetId, RemoteViews
}

public static void updateEventList(Context context) {
registerReceivers(context);
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(getWidgetIds(context), R.id.event_list);
}

Expand All @@ -173,4 +187,23 @@ public static void updateAllWidgets(Context context) {
context.sendBroadcast(intent);
}

private static void registerReceivers(Context contextIn) {
if (receiversRegistered) return;

Context context = contextIn.getApplicationContext();
EnvironmentChangedReceiver receiver = new EnvironmentChangedReceiver();

IntentFilter providerChanged = new IntentFilter();
providerChanged.addAction("android.intent.action.PROVIDER_CHANGED");
providerChanged.addDataScheme("content");
providerChanged.addDataAuthority("com.android.calendar", null);
context.registerReceiver(receiver, providerChanged);

IntentFilter userPresent = new IntentFilter();
userPresent.addAction("android.intent.action.USER_PRESENT");
context.registerReceiver(receiver, userPresent);

Log.i(EventAppWidgetProvider.class.getName(), "Registered receivers from " + contextIn.getClass().getName());
receiversRegistered = true;
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'org.ajoberstar:grgit:2.1.0'
}
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Sep 25 20:42:50 MSK 2018
#Thu Jan 17 20:51:35 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

0 comments on commit 74ffc14

Please sign in to comment.