|
|
|
@ -23,25 +23,30 @@ import android.content.Intent;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Google Tasks同步服务
|
|
|
|
|
* 负责在后台执行笔记与Google Tasks的同步操作
|
|
|
|
|
* 通过广播机制通知UI同步状态和进度
|
|
|
|
|
* 支持启动同步、取消同步等操作
|
|
|
|
|
*/
|
|
|
|
|
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 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 static GTaskASyncTask mSyncTask = null; // 同步任务
|
|
|
|
|
private static String mSyncProgress = ""; // 同步进度消息
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 启动同步任务
|
|
|
|
|
*/
|
|
|
|
|
private void startSync() {
|
|
|
|
|
if (mSyncTask == null) {
|
|
|
|
|
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
|
|
|
|
@ -56,17 +61,32 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消同步任务
|
|
|
|
|
*/
|
|
|
|
|
private void cancelSync() {
|
|
|
|
|
if (mSyncTask != null) {
|
|
|
|
|
mSyncTask.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 服务创建时调用
|
|
|
|
|
* 初始化服务状态
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
|
|
|
|
mSyncTask = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理启动服务的请求
|
|
|
|
|
* 根据传入的action执行相应的同步操作
|
|
|
|
|
* @param intent 启动服务的Intent
|
|
|
|
|
* @param flags 启动标志
|
|
|
|
|
* @param startId 启动ID
|
|
|
|
|
* @return 服务启动模式
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
@ -86,6 +106,10 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 系统内存不足时调用
|
|
|
|
|
* 取消正在进行的同步任务以释放资源
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onLowMemory() {
|
|
|
|
|
if (mSyncTask != null) {
|
|
|
|
@ -93,10 +117,20 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回服务的Binder对象
|
|
|
|
|
* 本服务不支持绑定,返回null
|
|
|
|
|
* @param intent 绑定Intent
|
|
|
|
|
* @return null
|
|
|
|
|
*/
|
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送同步状态广播
|
|
|
|
|
* @param msg 同步进度消息
|
|
|
|
|
*/
|
|
|
|
|
public void sendBroadcast(String msg) {
|
|
|
|
|
mSyncProgress = msg;
|
|
|
|
|
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
|
|
|
|
@ -105,6 +139,10 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
sendBroadcast(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 静态方法:启动同步服务
|
|
|
|
|
* @param activity 调用的Activity
|
|
|
|
|
*/
|
|
|
|
|
public static void startSync(Activity activity) {
|
|
|
|
|
GTaskManager.getInstance().setActivityContext(activity);
|
|
|
|
|
Intent intent = new Intent(activity, GTaskSyncService.class);
|
|
|
|
@ -112,17 +150,29 @@ public class GTaskSyncService extends Service {
|
|
|
|
|
activity.startService(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 静态方法:取消同步服务
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 静态方法:检查同步是否正在进行
|
|
|
|
|
* @return 同步状态
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isSyncing() {
|
|
|
|
|
return mSyncTask != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 静态方法:获取当前同步进度消息
|
|
|
|
|
* @return 同步进度字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String getProgressString() {
|
|
|
|
|
return mSyncProgress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|