|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
/*
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
@ -24,24 +24,22 @@ import android.os.Bundle;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
|
|
|
|
|
public class GTaskSyncService extends Service {
|
|
|
|
|
// 同步动作类型
|
|
|
|
|
public final static String ACTION_STRING_NAME = "sync_action_type";
|
|
|
|
|
|
|
|
|
|
public final static int ACTION_START_SYNC = 0;
|
|
|
|
|
|
|
|
|
|
public final static int ACTION_CANCEL_SYNC = 1;
|
|
|
|
|
|
|
|
|
|
public final static int ACTION_INVALID = 2;
|
|
|
|
|
|
|
|
|
|
// 广播相关常量
|
|
|
|
|
public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service";
|
|
|
|
|
|
|
|
|
|
public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";
|
|
|
|
|
|
|
|
|
|
public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg";
|
|
|
|
|
|
|
|
|
|
// 同步任务和进度信息
|
|
|
|
|
private static GTaskASyncTask mSyncTask = null;
|
|
|
|
|
|
|
|
|
|
private static String mSyncProgress = "";
|
|
|
|
|
|
|
|
|
|
// 启动同步
|
|
|
|
|
private void startSync() {
|
|
|
|
|
if (mSyncTask == null) {
|
|
|
|
|
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
|
|
|
|
@ -56,17 +54,20 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步
|
|
|
|
|
private void cancelSync() {
|
|
|
|
|
if (mSyncTask != null) {
|
|
|
|
|
mSyncTask.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 服务创建时的操作
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
mSyncTask = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 服务启动命令处理
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
@ -86,6 +87,7 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 内存不足时的操作
|
|
|
|
|
@Override
|
|
|
|
|
public void onLowMemory() {
|
|
|
|
|
if (mSyncTask != null) {
|
|
|
|
@ -93,10 +95,12 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 绑定服务
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送广播
|
|
|
|
|
public void sendBroadcast(String msg) {
|
|
|
|
|
mSyncProgress = msg;
|
|
|
|
|
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
|
|
|
|
@ -105,6 +109,7 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
sendBroadcast(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 静态方法:启动同步
|
|
|
|
|
public static void startSync(Activity activity) {
|
|
|
|
|
GTaskManager.getInstance().setActivityContext(activity);
|
|
|
|
|
Intent intent = new Intent(activity, GTaskSyncService.class);
|
|
|
|
@ -112,16 +117,19 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
activity.startService(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 静态方法:取消同步
|
|
|
|
|
public static void cancelSync(Context context) {
|
|
|
|
|
Intent intent = new Intent(context, GTaskSyncService.class);
|
|
|
|
|
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC);
|
|
|
|
|
context.startService(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否正在同步
|
|
|
|
|
public static boolean isSyncing() {
|
|
|
|
|
return mSyncTask != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取同步进度信息
|
|
|
|
|
public static String getProgressString() {
|
|
|
|
|
return mSyncProgress;
|
|
|
|
|
}
|
|
|
|
|