mirror of https://github.com/tasks/tasks
Use android-job to sync Google Tasks
parent
a20479d952
commit
dee29eded4
@ -1,21 +1,13 @@
|
|||||||
package org.tasks;
|
package org.tasks;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import org.tasks.preferences.Preferences;
|
|
||||||
|
|
||||||
public class FlavorSetup {
|
public class FlavorSetup {
|
||||||
|
|
||||||
private final CaldavAccountManager caldavAccountManager;
|
|
||||||
private final Preferences preferences;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FlavorSetup(CaldavAccountManager caldavAccountManager, Preferences preferences) {
|
public FlavorSetup() {
|
||||||
this.caldavAccountManager = caldavAccountManager;
|
|
||||||
this.preferences = preferences;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setup() {
|
public void setup() {
|
||||||
caldavAccountManager
|
|
||||||
.setBackgroundSynchronization(preferences.getBoolean(R.string.p_background_sync, true));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,13 @@
|
|||||||
package org.tasks;
|
package org.tasks;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import org.tasks.preferences.Preferences;
|
|
||||||
|
|
||||||
public class FlavorSetup {
|
public class FlavorSetup {
|
||||||
|
|
||||||
private final CaldavAccountManager caldavAccountManager;
|
|
||||||
private final Preferences preferences;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FlavorSetup(CaldavAccountManager caldavAccountManager, Preferences preferences) {
|
public FlavorSetup() {
|
||||||
this.caldavAccountManager = caldavAccountManager;
|
|
||||||
this.preferences = preferences;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setup() {
|
public void setup() {
|
||||||
caldavAccountManager
|
|
||||||
.setBackgroundSynchronization(preferences.getBoolean(R.string.p_background_sync, true));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,41 +1,22 @@
|
|||||||
package org.tasks;
|
package org.tasks;
|
||||||
|
|
||||||
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import org.tasks.billing.InventoryHelper;
|
import org.tasks.billing.InventoryHelper;
|
||||||
import org.tasks.gtasks.GoogleAccountManager;
|
|
||||||
import org.tasks.gtasks.PlayServices;
|
import org.tasks.gtasks.PlayServices;
|
||||||
import org.tasks.jobs.JobManager;
|
|
||||||
import org.tasks.preferences.Preferences;
|
|
||||||
|
|
||||||
public class FlavorSetup {
|
public class FlavorSetup {
|
||||||
|
|
||||||
private final GtasksPreferenceService gtasksPreferenceService;
|
|
||||||
private final InventoryHelper inventoryHelper;
|
private final InventoryHelper inventoryHelper;
|
||||||
private final Preferences preferences;
|
|
||||||
private final PlayServices playServices;
|
private final PlayServices playServices;
|
||||||
private final GoogleAccountManager googleAccountManager;
|
|
||||||
private final JobManager jobManager;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FlavorSetup(GtasksPreferenceService gtasksPreferenceService,
|
public FlavorSetup(InventoryHelper inventoryHelper, PlayServices playServices) {
|
||||||
InventoryHelper inventoryHelper,
|
|
||||||
Preferences preferences, PlayServices playServices,
|
|
||||||
GoogleAccountManager googleAccountManager, JobManager jobManager) {
|
|
||||||
this.gtasksPreferenceService = gtasksPreferenceService;
|
|
||||||
this.inventoryHelper = inventoryHelper;
|
this.inventoryHelper = inventoryHelper;
|
||||||
this.preferences = preferences;
|
|
||||||
this.playServices = playServices;
|
this.playServices = playServices;
|
||||||
this.googleAccountManager = googleAccountManager;
|
|
||||||
this.jobManager = jobManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setup() {
|
public void setup() {
|
||||||
inventoryHelper.initialize();
|
inventoryHelper.initialize();
|
||||||
gtasksPreferenceService.stopOngoing(); // if sync ongoing flag was set, clear it
|
|
||||||
boolean backgroundSyncEnabled = preferences.getBoolean(R.string.p_background_sync, true);
|
|
||||||
googleAccountManager.setBackgroundSynchronization(backgroundSyncEnabled);
|
|
||||||
jobManager.setBackgroundSynchronization(backgroundSyncEnabled);
|
|
||||||
playServices.refresh();
|
playServices.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:accountType="com.google"
|
|
||||||
android:contentAuthority="org.tasks"
|
|
||||||
android:supportsUploading="true"
|
|
||||||
android:isAlwaysSyncable="true"
|
|
||||||
android:userVisible="false" />
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013 Google Inc.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.tasks.gtasks;
|
|
||||||
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.IBinder;
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service to handle sync requests.
|
|
||||||
*
|
|
||||||
* <p>This service is invoked in response to Intents with action android.content.SyncAdapter, and
|
|
||||||
* returns a Binder connection to SyncAdapter.
|
|
||||||
*
|
|
||||||
* <p>For performance, only one sync adapter will be initialized within this application's context.
|
|
||||||
*
|
|
||||||
* <p>Note: The SyncService itself is not notified when a new sync occurs. It's role is to manage
|
|
||||||
* the lifecycle of our {@link GoogleTaskSyncAdapter} and provide a handle to said SyncAdapter to
|
|
||||||
* the OS on request.
|
|
||||||
*/
|
|
||||||
public class GoogleTaskSyncService extends Service {
|
|
||||||
|
|
||||||
private static final Object sSyncAdapterLock = new Object();
|
|
||||||
private static GoogleTaskSyncAdapter sGoogleTaskSyncAdapter = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thread-safe constructor, creates static {@link GoogleTaskSyncAdapter} instance.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void onCreate() {
|
|
||||||
super.onCreate();
|
|
||||||
Timber.d("Service created");
|
|
||||||
synchronized (sSyncAdapterLock) {
|
|
||||||
if (sGoogleTaskSyncAdapter == null) {
|
|
||||||
sGoogleTaskSyncAdapter = new GoogleTaskSyncAdapter(getApplicationContext(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
/**
|
|
||||||
* Logging-only destructor.
|
|
||||||
*/
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
Timber.d("Service destroyed");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return Binder handle for IPC communication with {@link GoogleTaskSyncAdapter}.
|
|
||||||
*
|
|
||||||
* <p>New sync requests will be sent directly to the SyncAdapter using this channel.
|
|
||||||
*
|
|
||||||
* @param intent Calling intent
|
|
||||||
* @return Binder handle for {@link GoogleTaskSyncAdapter}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public IBinder onBind(Intent intent) {
|
|
||||||
return sGoogleTaskSyncAdapter.getSyncAdapterBinder();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
package org.tasks.injection;
|
|
||||||
|
|
||||||
import android.content.AbstractThreadedSyncAdapter;
|
|
||||||
import android.content.Context;
|
|
||||||
|
|
||||||
public abstract class InjectingAbstractThreadedSyncAdapter extends AbstractThreadedSyncAdapter {
|
|
||||||
|
|
||||||
protected InjectingAbstractThreadedSyncAdapter(Context context, boolean autoInitialize) {
|
|
||||||
super(context, autoInitialize);
|
|
||||||
inject(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void inject(Context context) {
|
|
||||||
inject(Dagger.get(context)
|
|
||||||
.getApplicationComponent()
|
|
||||||
.plus(new SyncAdapterModule()));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void inject(SyncAdapterComponent component);
|
|
||||||
}
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
package org.tasks.injection;
|
|
||||||
|
|
||||||
import dagger.Subcomponent;
|
|
||||||
import org.tasks.gtasks.GoogleTaskSyncAdapter;
|
|
||||||
|
|
||||||
@Subcomponent(modules = SyncAdapterModule.class)
|
|
||||||
public interface SyncAdapterComponent {
|
|
||||||
|
|
||||||
void inject(GoogleTaskSyncAdapter googleTaskSyncAdapter);
|
|
||||||
}
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
package org.tasks.injection;
|
|
||||||
|
|
||||||
import dagger.Module;
|
|
||||||
|
|
||||||
@Module
|
|
||||||
public class SyncAdapterModule {
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
package org.tasks.jobs;
|
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import com.evernote.android.job.Job;
|
|
||||||
import org.tasks.caldav.CaldavSynchronizer;
|
|
||||||
|
|
||||||
public class CaldavSyncJob extends Job{
|
|
||||||
|
|
||||||
private final CaldavSynchronizer caldavSynchronizer;
|
|
||||||
|
|
||||||
CaldavSyncJob(CaldavSynchronizer caldavSynchronizer) {
|
|
||||||
this.caldavSynchronizer = caldavSynchronizer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
protected Result onRunJob(@NonNull Params params) {
|
|
||||||
return caldavSynchronizer.sync() ? Result.SUCCESS : Result.FAILURE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package org.tasks.jobs;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import com.evernote.android.job.Job;
|
||||||
|
import org.tasks.caldav.CaldavSynchronizer;
|
||||||
|
import org.tasks.gtasks.GoogleTaskSynchronizer;
|
||||||
|
|
||||||
|
public class SyncJob extends Job{
|
||||||
|
|
||||||
|
private final CaldavSynchronizer caldavSynchronizer;
|
||||||
|
private final GoogleTaskSynchronizer googleTaskSynchronizer;
|
||||||
|
|
||||||
|
SyncJob(CaldavSynchronizer caldavSynchronizer, GoogleTaskSynchronizer googleTaskSynchronizer) {
|
||||||
|
this.caldavSynchronizer = caldavSynchronizer;
|
||||||
|
this.googleTaskSynchronizer = googleTaskSynchronizer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected Result onRunJob(@NonNull Params params) {
|
||||||
|
caldavSynchronizer.sync();
|
||||||
|
googleTaskSynchronizer.sync();
|
||||||
|
return Result.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,31 +0,0 @@
|
|||||||
package org.tasks.receivers;
|
|
||||||
|
|
||||||
import com.todoroo.astrid.data.SyncFlags;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.tasks.jobs.JobManager;
|
|
||||||
import org.tasks.sync.SyncAdapters;
|
|
||||||
|
|
||||||
public class CalDAVPushReceiver {
|
|
||||||
|
|
||||||
private final JobManager jobManager;
|
|
||||||
private final SyncAdapters syncAdapters;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public CalDAVPushReceiver(JobManager jobManager, SyncAdapters syncAdapters) {
|
|
||||||
this.jobManager = jobManager;
|
|
||||||
this.syncAdapters = syncAdapters;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void push(Task task, Task original) {
|
|
||||||
if (!syncAdapters.isCaldavSyncEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
jobManager.syncCaldavNow();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
package org.tasks.receivers;
|
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
|
||||||
import com.todoroo.astrid.data.SyncFlags;
|
|
||||||
import com.todoroo.astrid.data.Task;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.tasks.sync.SyncAdapters;
|
|
||||||
|
|
||||||
public class GoogleTaskPusher {
|
|
||||||
|
|
||||||
private final SyncAdapters syncAdapters;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public GoogleTaskPusher(SyncAdapters syncAdapters) {
|
|
||||||
this.syncAdapters = syncAdapters;
|
|
||||||
}
|
|
||||||
|
|
||||||
void push(Task task, Task original) {
|
|
||||||
if (!syncAdapters.isGoogleTaskSyncEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (original == null ||
|
|
||||||
!task.getTitle().equals(original.getTitle()) ||
|
|
||||||
(Strings.isNullOrEmpty(task.getNotes())
|
|
||||||
? !Strings.isNullOrEmpty(original.getNotes())
|
|
||||||
: !task.getNotes().equals(original.getNotes())) ||
|
|
||||||
!task.getDueDate().equals(original.getDueDate()) ||
|
|
||||||
!task.getCompletionDate().equals(original.getCompletionDate()) ||
|
|
||||||
!task.getDeletionDate().equals(original.getDeletionDate()) ||
|
|
||||||
task.checkAndClearTransitory(SyncFlags.FORCE_SYNC)) {
|
|
||||||
syncAdapters.requestSynchronization();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,21 +1,63 @@
|
|||||||
package org.tasks.receivers;
|
package org.tasks.receivers;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
|
import com.todoroo.astrid.data.SyncFlags;
|
||||||
import com.todoroo.astrid.data.Task;
|
import com.todoroo.astrid.data.Task;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import org.tasks.jobs.JobManager;
|
||||||
|
import org.tasks.sync.SyncAdapters;
|
||||||
|
|
||||||
public class PushReceiver {
|
public class PushReceiver {
|
||||||
|
|
||||||
private final GoogleTaskPusher googleTaskPusher;
|
private final SyncAdapters syncAdapters;
|
||||||
private final CalDAVPushReceiver calDAVPushReceiver;
|
private final JobManager jobManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public PushReceiver(GoogleTaskPusher googleTaskPusher, CalDAVPushReceiver calDAVPushReceiver) {
|
public PushReceiver(SyncAdapters syncAdapters, JobManager jobManager) {
|
||||||
this.googleTaskPusher = googleTaskPusher;
|
this.syncAdapters = syncAdapters;
|
||||||
this.calDAVPushReceiver = calDAVPushReceiver;
|
this.jobManager = jobManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void push(Task task, Task original) {
|
public void push(Task task, Task original) {
|
||||||
googleTaskPusher.push(task, original);
|
if (!pushGoogleTasks(task, original)) {
|
||||||
calDAVPushReceiver.push(task, original);
|
pushCaldav(task, original);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean pushGoogleTasks(Task task, Task original) {
|
||||||
|
if (!syncAdapters.isGoogleTaskSyncEnabled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (original == null ||
|
||||||
|
!task.getTitle().equals(original.getTitle()) ||
|
||||||
|
(Strings.isNullOrEmpty(task.getNotes())
|
||||||
|
? !Strings.isNullOrEmpty(original.getNotes())
|
||||||
|
: !task.getNotes().equals(original.getNotes())) ||
|
||||||
|
!task.getDueDate().equals(original.getDueDate()) ||
|
||||||
|
!task.getCompletionDate().equals(original.getCompletionDate()) ||
|
||||||
|
!task.getDeletionDate().equals(original.getDeletionDate()) ||
|
||||||
|
task.checkAndClearTransitory(SyncFlags.FORCE_SYNC)) {
|
||||||
|
syncAdapters.syncNow();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void pushCaldav(Task task, Task original) {
|
||||||
|
if (!syncAdapters.isCaldavSyncEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
jobManager.syncNow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue