|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* 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 net.micode.notes.gtask.remote;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.app.Service;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.IBinder;
|
|
|
|
|
|
/**
|
|
|
* Google任务同步服务
|
|
|
*
|
|
|
* 提供后台同步功能,处理与Google Tasks的双向数据同步
|
|
|
* 通过Intent接收同步命令,使用广播机制通知UI同步状态
|
|
|
* 采用单例模式确保服务实例唯一
|
|
|
*
|
|
|
* 服务支持的操作:
|
|
|
* 1. 启动同步
|
|
|
* 2. 取消同步
|
|
|
* 3. 查询同步状态
|
|
|
*
|
|
|
* 同步过程通过GTaskManager执行,服务负责生命周期管理和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 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 String mSyncProgress = ""; // 当前同步进度消息
|
|
|
|
|
|
/**
|
|
|
* 启动Google任务同步
|
|
|
* 此方法将创建并执行异步同步任务
|
|
|
*/
|
|
|
private void startSync() {
|
|
|
// Temporarily disabled sync functionality
|
|
|
sendBroadcast("Sync functionality temporarily disabled");
|
|
|
stopSelf();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消正在进行的同步操作
|
|
|
* 通知GTaskManager停止当前同步任务
|
|
|
*/
|
|
|
private void cancelSync() {
|
|
|
// Temporarily disabled sync functionality
|
|
|
sendBroadcast("Sync cancelled");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 服务创建时调用
|
|
|
* 初始化服务资源,设置同步管理器上下文
|
|
|
*/
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
// Temporarily disabled sync functionality
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理启动服务的请求
|
|
|
* 根据Intent中的操作类型执行相应的同步操作
|
|
|
*
|
|
|
* @param intent 启动服务的Intent,包含操作类型
|
|
|
* @param flags 启动标志
|
|
|
* @param startId 启动ID
|
|
|
* @return 服务重启策略
|
|
|
*/
|
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {
|
|
|
switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) {
|
|
|
case ACTION_START_SYNC:
|
|
|
startSync();
|
|
|
break;
|
|
|
case ACTION_CANCEL_SYNC:
|
|
|
cancelSync();
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
return START_STICKY; // 系统内存不足时自动重启服务
|
|
|
}
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 系统内存不足时调用
|
|
|
* 释放不必要的资源,确保服务稳定运行
|
|
|
*/
|
|
|
@Override
|
|
|
public void onLowMemory() {
|
|
|
// Temporarily disabled sync functionality
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回服务的Binder接口
|
|
|
* 由于本服务不支持绑定,返回null
|
|
|
*/
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送同步状态广播
|
|
|
* 更新当前同步进度并发送广播通知UI
|
|
|
*
|
|
|
* @param msg 同步状态消息
|
|
|
*/
|
|
|
public void sendBroadcast(String msg) {
|
|
|
mSyncProgress = msg;
|
|
|
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
|
|
|
intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, false);
|
|
|
intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg);
|
|
|
sendBroadcast(intent);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 静态方法:启动同步服务
|
|
|
* 方便Activity直接调用启动同步
|
|
|
*
|
|
|
* @param activity 调用此方法的Activity
|
|
|
*/
|
|
|
public static void startSync(Activity activity) {
|
|
|
// Temporarily disabled sync functionality
|
|
|
Intent intent = new Intent(activity, GTaskSyncService.class);
|
|
|
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC);
|
|
|
activity.startService(intent);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 静态方法:取消同步
|
|
|
* 方便任何Context调用取消同步
|
|
|
*
|
|
|
* @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 false; // Temporarily disabled sync functionality
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前同步进度消息
|
|
|
*
|
|
|
* @return 同步进度文本描述
|
|
|
*/
|
|
|
public static String getProgressString() {
|
|
|
return mSyncProgress;
|
|
|
}
|
|
|
}
|