1
0
mirror of https://github.com/etesync/android synced 2024-11-15 20:38:58 +00:00

AccountSettings version 5: enable/disable OpenTasks by availability (Android 7.1.1 fix)

* better handling of setIsSyncable
This commit is contained in:
Ricki Hirner 2016-12-30 14:05:23 +01:00
parent 88b5a26c3b
commit 4b8c44fe5b
3 changed files with 32 additions and 35 deletions

View File

@ -11,8 +11,6 @@ import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
@ -27,8 +25,6 @@ import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;
import java.lang.reflect.Method;
@ -52,7 +48,7 @@ import lombok.Cleanup;
import okhttp3.HttpUrl;
public class AccountSettings {
private final static int CURRENT_VERSION = 4;
private final static int CURRENT_VERSION = 5;
private final static String
KEY_SETTINGS_VERSION = "version",
@ -389,6 +385,13 @@ public class AccountSettings {
setGroupMethod(GroupMethod.CATEGORIES);
}
/* Android 7.1.1 OpenTasks fix */
@SuppressWarnings({ "Recycle", "unused" })
private void update_4_5() {
// call PackageChangedReceiver which then enables/disables OpenTasks sync when it's (not) available
PackageChangedReceiver.updateTaskSync(context);
}
public static class AppUpdatedReceiver extends BroadcastReceiver {

View File

@ -16,6 +16,8 @@ import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import at.bitfire.davdroid.model.ServiceDB;
import at.bitfire.davdroid.model.ServiceDB.Services;
@ -29,31 +31,31 @@ public class PackageChangedReceiver extends BroadcastReceiver {
@SuppressLint("MissingPermission")
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) ||
Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction())) {
Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction()))
updateTaskSync(context);
}
boolean tasksInstalled = LocalTaskList.tasksProviderAvailable(context);
App.log.info("Package (un)installed; OpenTasks provider now available = " + tasksInstalled);
static void updateTaskSync(@NonNull Context context) {
boolean tasksInstalled = LocalTaskList.tasksProviderAvailable(context);
App.log.info("Package (un)installed; OpenTasks provider now available = " + tasksInstalled);
// check all accounts and (de)activate OpenTasks if a CalDAV service is defined
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
// check all accounts and (de)activate OpenTasks if a CalDAV service is defined
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[] { Services.ACCOUNT_NAME },
Services.SERVICE + "=?", new String[] { Services.SERVICE_CALDAV }, null, null, null);
while (cursor.moveToNext()) {
Account account = new Account(cursor.getString(0), Constants.ACCOUNT_TYPE);
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[] { Services.ACCOUNT_NAME },
Services.SERVICE + "=?", new String[] { Services.SERVICE_CALDAV }, null, null, null);
while (cursor.moveToNext()) {
Account account = new Account(cursor.getString(0), Constants.ACCOUNT_TYPE);
if (tasksInstalled) {
if (tasksInstalled) {
if (ContentResolver.getIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority) <= 0) {
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
try {
AccountSettings settings = new AccountSettings(context, account);
settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority, Constants.DEFAULT_SYNC_INTERVAL);
} catch(InvalidAccountException ignored) {
}
} else
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
}
ContentResolver.setSyncAutomatically(account, TaskProvider.ProviderName.OpenTasks.authority, true);
ContentResolver.addPeriodicSync(account, TaskProvider.ProviderName.OpenTasks.authority, new Bundle(), Constants.DEFAULT_SYNC_INTERVAL);
}
} else
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
}
}

View File

@ -140,9 +140,7 @@ public class AccountDetailsFragment extends Fragment {
// enable contact sync
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
settings.setSyncInterval(ContactsContract.AUTHORITY, Constants.DEFAULT_SYNC_INTERVAL);
} else
// disable contact sync when CardDAV is not available
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0);
}
if (config.calDAV != null) {
// insert CalDAV service
@ -161,13 +159,7 @@ public class AccountDetailsFragment extends Fragment {
if (LocalTaskList.tasksProviderAvailable(getContext())) {
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority, Constants.DEFAULT_SYNC_INTERVAL);
} else
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
} else {
// disable calendar and task sync when CalDAV is not available
ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 0);
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
}
}
} catch(InvalidAccountException e) {