|
|
/*
|
|
|
* 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
|
|
|
*
|
|
|
* <url id="d0vutuf37oq2m9lhs9r0" type="url" status="parsed" title="Apache License, Version 2.0" wc="10467">http://www.apache.org/licenses/LICENSE-2.0</url>
|
|
|
*
|
|
|
* 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;
|
|
|
|
|
|
public class GTaskSyncService extends Service {
|
|
|
// 广播 Action 名称,用于标识同步服务的广播
|
|
|
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";
|
|
|
|
|
|
// 操作类型常量
|
|
|
public final static int ACTION_START_SYNC = 0; // 开始同步
|
|
|
public final static int ACTION_CANCEL_SYNC = 1; // 取消同步
|
|
|
public final static int ACTION_INVALID = 2; // 无效操作
|
|
|
|
|
|
// 操作类型键,用于 Intent 额外数据
|
|
|
public final static String ACTION_STRING_NAME = "sync_action_type";
|
|
|
|
|
|
// 同步任务实例
|
|
|
private static GTaskASyncTask mSyncTask = null;
|
|
|
|
|
|
// 同步进度消息
|
|
|
private static String mSyncProgress = "";
|
|
|
|
|
|
// 开始同步操作
|
|
|
private void startSync() {
|
|
|
if (mSyncTask == null) {
|
|
|
// 创建并启动同步任务
|
|
|
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
|
|
|
public void onComplete() {
|
|
|
mSyncTask = null; // 同步完成,清空任务引用
|
|
|
sendBroadcast(""); // 发送广播通知同步完成
|
|
|
stopSelf(); // 停止服务
|
|
|
}
|
|
|
});
|
|
|
sendBroadcast(""); // 发送广播通知同步开始
|
|
|
mSyncTask.execute(); // 执行同步任务
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 取消同步操作
|
|
|
private void cancelSync() {
|
|
|
if (mSyncTask != null) {
|
|
|
mSyncTask.cancelSync(); // 取消同步任务
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 服务创建时调用
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
mSyncTask = null; // 初始化同步任务为 null
|
|
|
}
|
|
|
|
|
|
// 服务启动时调用
|
|
|
@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; // 返回 sticky 模式,确保服务被系统杀死后重新创建
|
|
|
}
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
|
|
|
|
// 内存不足时调用
|
|
|
@Override
|
|
|
public void onLowMemory() {
|
|
|
if (mSyncTask != null) {
|
|
|
mSyncTask.cancelSync(); // 取消同步任务
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 绑定服务时调用
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
return null; // 返回 null,表示不支持绑定
|
|
|
}
|
|
|
|
|
|
// 发送广播通知
|
|
|
public void sendBroadcast(String msg) {
|
|
|
mSyncProgress = msg; // 更新同步进度消息
|
|
|
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); // 发送广播
|
|
|
}
|
|
|
|
|
|
// 静态方法,启动同步服务
|
|
|
public static void startSync(Activity activity) {
|
|
|
GTaskManager.getInstance().setActivityContext(activity); // 设置活动上下文
|
|
|
Intent intent = new Intent(activity, GTaskSyncService.class); // 创建 Intent
|
|
|
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); // 添加操作类型额外数据
|
|
|
activity.startService(intent); // 启动服务
|
|
|
}
|
|
|
|
|
|
// 静态方法,取消同步服务
|
|
|
public static void cancelSync(Context context) {
|
|
|
Intent intent = new Intent(context, GTaskSyncService.class); // 创建 Intent
|
|
|
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); // 添加操作类型额外数据
|
|
|
context.startService(intent); // 启动服务
|
|
|
}
|
|
|
|
|
|
// 静态方法,检查是否正在同步
|
|
|
public static boolean isSyncing() {
|
|
|
return mSyncTask != null; // 返回同步任务是否为 null
|
|
|
}
|
|
|
|
|
|
// 静态方法,获取同步进度消息
|
|
|
public static String getProgressString() {
|
|
|
return mSyncProgress; // 返回同步进度消息
|
|
|
}
|
|
|
} |