2016-12-28 21:11:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright © Ricki Hirner (bitfire web engineering).
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
*/
|
|
|
|
|
2017-02-27 12:57:57 +00:00
|
|
|
package com.etesync.syncadapter;
|
2016-12-28 21:11:47 +00:00
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2016-12-30 13:05:23 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.annotation.NonNull;
|
2016-12-28 21:11:47 +00:00
|
|
|
|
2017-02-27 12:57:57 +00:00
|
|
|
import com.etesync.syncadapter.model.ServiceDB;
|
|
|
|
import com.etesync.syncadapter.model.ServiceDB.Services;
|
|
|
|
import com.etesync.syncadapter.resource.LocalTaskList;
|
2016-12-28 21:11:47 +00:00
|
|
|
import at.bitfire.ical4android.TaskProvider;
|
|
|
|
import lombok.Cleanup;
|
|
|
|
|
|
|
|
public class PackageChangedReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) ||
|
2016-12-30 13:05:23 +00:00
|
|
|
Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction()))
|
|
|
|
updateTaskSync(context);
|
|
|
|
}
|
2016-12-28 21:11:47 +00:00
|
|
|
|
2016-12-30 13:05:23 +00:00
|
|
|
static void updateTaskSync(@NonNull Context context) {
|
|
|
|
boolean tasksInstalled = LocalTaskList.tasksProviderAvailable(context);
|
|
|
|
App.log.info("Package (un)installed; OpenTasks provider now available = " + tasksInstalled);
|
2016-12-28 21:11:47 +00:00
|
|
|
|
2016-12-30 13:05:23 +00:00
|
|
|
// 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();
|
2016-12-28 21:11:47 +00:00
|
|
|
|
2016-12-30 13:05:23 +00:00
|
|
|
@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);
|
2016-12-28 21:11:47 +00:00
|
|
|
|
2016-12-30 13:05:23 +00:00
|
|
|
if (tasksInstalled) {
|
|
|
|
if (ContentResolver.getIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority) <= 0) {
|
2016-12-28 21:11:47 +00:00
|
|
|
ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
|
2016-12-30 13:05:23 +00:00
|
|
|
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);
|
2016-12-28 21:11:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|