mirror of
https://github.com/etesync/android
synced 2025-07-02 04:42:37 +00:00
AccountSettings version 5: enable/disable OpenTasks by availability (Android 7.1.1 fix)
* better handling of setIsSyncable
This commit is contained in:
parent
88b5a26c3b
commit
4b8c44fe5b
@ -11,8 +11,6 @@ import android.accounts.Account;
|
|||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentProviderClient;
|
import android.content.ContentProviderClient;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
@ -27,8 +25,6 @@ import android.provider.CalendarContract;
|
|||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.NotificationManagerCompat;
|
|
||||||
import android.support.v7.app.NotificationCompat;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@ -52,7 +48,7 @@ import lombok.Cleanup;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
|
||||||
public class AccountSettings {
|
public class AccountSettings {
|
||||||
private final static int CURRENT_VERSION = 4;
|
private final static int CURRENT_VERSION = 5;
|
||||||
private final static String
|
private final static String
|
||||||
KEY_SETTINGS_VERSION = "version",
|
KEY_SETTINGS_VERSION = "version",
|
||||||
|
|
||||||
@ -389,6 +385,13 @@ public class AccountSettings {
|
|||||||
setGroupMethod(GroupMethod.CATEGORIES);
|
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 {
|
public static class AppUpdatedReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
@ -16,6 +16,8 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
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;
|
||||||
import at.bitfire.davdroid.model.ServiceDB.Services;
|
import at.bitfire.davdroid.model.ServiceDB.Services;
|
||||||
@ -29,31 +31,31 @@ public class PackageChangedReceiver extends BroadcastReceiver {
|
|||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) ||
|
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);
|
static void updateTaskSync(@NonNull Context context) {
|
||||||
App.log.info("Package (un)installed; OpenTasks provider now available = " + tasksInstalled);
|
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
|
// check all accounts and (de)activate OpenTasks if a CalDAV service is defined
|
||||||
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
|
@Cleanup ServiceDB.OpenHelper dbHelper = new ServiceDB.OpenHelper(context);
|
||||||
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||||
|
|
||||||
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[] { Services.ACCOUNT_NAME },
|
@Cleanup Cursor cursor = db.query(Services._TABLE, new String[] { Services.ACCOUNT_NAME },
|
||||||
Services.SERVICE + "=?", new String[] { Services.SERVICE_CALDAV }, null, null, null);
|
Services.SERVICE + "=?", new String[] { Services.SERVICE_CALDAV }, null, null, null);
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
Account account = new Account(cursor.getString(0), Constants.ACCOUNT_TYPE);
|
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);
|
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
|
||||||
try {
|
ContentResolver.setSyncAutomatically(account, TaskProvider.ProviderName.OpenTasks.authority, true);
|
||||||
AccountSettings settings = new AccountSettings(context, account);
|
ContentResolver.addPeriodicSync(account, TaskProvider.ProviderName.OpenTasks.authority, new Bundle(), Constants.DEFAULT_SYNC_INTERVAL);
|
||||||
settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority, Constants.DEFAULT_SYNC_INTERVAL);
|
}
|
||||||
} catch(InvalidAccountException ignored) {
|
} else
|
||||||
}
|
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
|
||||||
} else
|
|
||||||
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,9 +140,7 @@ public class AccountDetailsFragment extends Fragment {
|
|||||||
// enable contact sync
|
// enable contact sync
|
||||||
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
|
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
|
||||||
settings.setSyncInterval(ContactsContract.AUTHORITY, Constants.DEFAULT_SYNC_INTERVAL);
|
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) {
|
if (config.calDAV != null) {
|
||||||
// insert CalDAV service
|
// insert CalDAV service
|
||||||
@ -161,13 +159,7 @@ public class AccountDetailsFragment extends Fragment {
|
|||||||
if (LocalTaskList.tasksProviderAvailable(getContext())) {
|
if (LocalTaskList.tasksProviderAvailable(getContext())) {
|
||||||
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
|
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
|
||||||
settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority, Constants.DEFAULT_SYNC_INTERVAL);
|
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) {
|
} catch(InvalidAccountException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user