From d2f0cccee7093de112552809ec838a9cb67f0502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E6=B7=91=E6=B6=B5?= <1508386176@qq.com> Date: Wed, 25 Dec 2024 02:19:58 +0800 Subject: [PATCH] Signed-off-by: --- GTaskSyncService.txt | 198 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 GTaskSyncService.txt diff --git a/GTaskSyncService.txt b/GTaskSyncService.txt new file mode 100644 index 0000000..a20653c --- /dev/null +++ b/GTaskSyncService.txt @@ -0,0 +1,198 @@ +/* + * 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; +/* + * GTaskSyncService类是一个服务类,用于处理Google任务的同步操作。 + * 该类继承自Service,并在后台执行同步任务。 + */ +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) { + // 创建一个新的GTaskASyncTask对象 + mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { + public void onComplete() { + // 同步任务完成后,将mSyncTask置为null + mSyncTask = null; + // 发送广播通知同步完成 + sendBroadcast(""); + // 停止服务 + stopSelf(); + } + }); + // 发送广播通知同步开始 + sendBroadcast(""); + // 执行同步任务 + mSyncTask.execute(); + } + } +/* + * 取消同步操作。 + */ + private void cancelSync() { + // 如果同步任务正在执行 + if (mSyncTask != null) { + // 调用cancelSync方法取消同步 + mSyncTask.cancelSync(); + } + } +/* + * 服务创建时调用,初始化同步任务。 + */ + @Override + public void onCreate() { + // 将同步任务置为null + mSyncTask = null; + } +/* + * 服务启动时调用,处理启动和取消同步的命令。 + * @param intent 启动服务的意图 + * @param flags 启动标志 + * @param startId 启动ID + * @return 服务启动模式 + */ + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // 获取意图中的额外数据 + Bundle bundle = intent.getExtras(); + // 如果额外数据不为空且包含ACTION_STRING_NAME + if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { + // 根据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; + } + // 返回START_STICKY,表示服务在异常终止后会自动重启 + return START_STICKY; + } + // 如果额外数据为空或不包含ACTION_STRING_NAME,调用父类的onStartCommand方法 + return super.onStartCommand(intent, flags, startId); + } +/* + * 内存不足时调用,取消同步任务。 + */ + @Override + public void onLowMemory() { + // 如果同步任务正在执行 + if (mSyncTask != null) { + // 调用cancelSync方法取消同步 + mSyncTask.cancelSync(); + } + } +/* + * 绑定服务时调用,返回null。 + * @param intent 绑定服务的意图 + * @return null + */ + public IBinder onBind(Intent intent) { + // 返回null,表示不支持绑定服务 + return null; + } +/* + * 发送广播,通知同步状态和进度。 + * @param msg 进度消息 + */ + public void sendBroadcast(String msg) { + // 更新同步进度字符串 + mSyncProgress = msg; + // 创建一个新的Intent对象,用于发送广播 + Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); + // 添加是否正在同步的标志 + intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); + // 添加进度消息 + intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); + // 发送广播 + sendBroadcast(intent); + } +/* + * 启动同步操作的静态方法。 + * @param activity 启动同步的Activity + */ + public static void startSync(Activity activity) { + // 设置活动上下文 + GTaskManager.getInstance().setActivityContext(activity); + // 创建一个新的Intent对象,用于启动服务 + Intent intent = new Intent(activity, GTaskSyncService.class); + // 添加启动同步的命令 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); + // 启动服务 + activity.startService(intent); + } +/* + * 取消同步操作的静态方法。 + * @param context 取消同步的上下文 + */ + public static void cancelSync(Context context) { + // 创建一个新的Intent对象,用于启动服务 + Intent intent = new Intent(context, GTaskSyncService.class); + // 添加取消同步的命令 + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); + // 启动服务 + context.startService(intent); + } +/* + * 检查是否正在同步。 + * @return 如果正在同步,返回true;否则返回false + */ + public static boolean isSyncing() { + // 如果mSyncTask不为null,表示正在同步 + return mSyncTask != null; + } +/* + * 获取同步进度字符串。 + * @return 同步进度字符串 + */ + public static String getProgressString() { + // 返回当前的同步进度字符串 + return mSyncProgress; + } +}