|
|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
@ -28,96 +27,185 @@ import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTaskASyncTask 类
|
|
|
|
|
* 继承自AsyncTask,用于在后台执行Google Tasks同步操作
|
|
|
|
|
* 提供同步进度通知和同步结果处理功能
|
|
|
|
|
*
|
|
|
|
|
* AsyncTask参数说明:
|
|
|
|
|
* Void - 执行任务时传入的参数类型(不需要参数)
|
|
|
|
|
* String - 进度更新时使用的参数类型(进度消息)
|
|
|
|
|
* Integer - 任务执行结果的类型(同步状态码)
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步通知的唯一ID
|
|
|
|
|
* 用于标识Google Tasks同步相关的通知,避免与其他通知冲突
|
|
|
|
|
*/
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步完成监听器接口
|
|
|
|
|
* 用于在同步完成后执行回调操作
|
|
|
|
|
*/
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
/**
|
|
|
|
|
* 同步完成时的回调方法
|
|
|
|
|
*/
|
|
|
|
|
void onComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上下文对象
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
// 通知管理器,用于显示同步进度通知
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
|
|
|
|
|
// Google Tasks管理器,执行实际的同步操作
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
|
|
|
|
|
// 同步完成监听器
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param listener 同步完成监听器
|
|
|
|
|
*/
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
|
// 获取通知管理器服务
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
// 获取Google Tasks管理器单例
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消同步操作
|
|
|
|
|
* 调用GTaskManager的cancelSync方法取消正在进行的同步
|
|
|
|
|
*/
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布同步进度
|
|
|
|
|
* 包装publishProgress方法,简化进度更新操作
|
|
|
|
|
*
|
|
|
|
|
* @param message 进度消息
|
|
|
|
|
*/
|
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
|
publishProgress(new String[] {
|
|
|
|
|
message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 显示通知
|
|
|
|
|
* 根据同步状态显示不同类型的通知
|
|
|
|
|
*
|
|
|
|
|
* @param tickerId 通知标题资源ID
|
|
|
|
|
* @param content 通知内容文本
|
|
|
|
|
*/
|
|
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
|
|
// 创建通知对象
|
|
|
|
|
Notification notification = new Notification(R.drawable.notification, mContext
|
|
|
|
|
.getString(tickerId), System.currentTimeMillis());
|
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS;
|
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS; // 默认灯光效果
|
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击后自动取消
|
|
|
|
|
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
// 根据通知类型设置不同的点击意图
|
|
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
|
|
// 同步失败或进行中:点击跳转到设置页面
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 同步成功:点击跳转到笔记列表页面
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), 0);
|
|
|
|
|
}
|
|
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
|
|
|
|
|
pendingIntent);
|
|
|
|
|
|
|
|
|
|
// 设置通知的详细内容
|
|
|
|
|
notification.setLatestEventInfo(mContext,
|
|
|
|
|
mContext.getString(R.string.app_name), // 应用名称作为标题
|
|
|
|
|
content, // 同步状态作为内容
|
|
|
|
|
pendingIntent); // 点击意图
|
|
|
|
|
|
|
|
|
|
// 显示通知
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后台执行同步任务
|
|
|
|
|
* AsyncTask的核心方法,在后台线程中执行
|
|
|
|
|
*
|
|
|
|
|
* @param unused 可变参数(未使用)
|
|
|
|
|
* @return 同步结果状态码
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
// 发布登录进度消息
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
|
|
|
|
|
// 执行同步操作,将当前异步任务作为进度更新回调
|
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 进度更新回调
|
|
|
|
|
* 在UI线程中执行,用于更新同步进度
|
|
|
|
|
*
|
|
|
|
|
* @param progress 进度消息数组
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
|
// 显示同步进行中的通知
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
|
|
|
|
|
|
// 如果上下文是GTaskSyncService,发送广播通知进度
|
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步任务执行完成回调
|
|
|
|
|
* 在UI线程中执行,处理同步结果
|
|
|
|
|
*
|
|
|
|
|
* @param result 同步结果状态码
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
|
// 根据同步结果显示不同的通知
|
|
|
|
|
if (result == GTaskManager.STATE_SUCCESS) {
|
|
|
|
|
// 同步成功
|
|
|
|
|
showNotification(R.string.ticker_success, mContext.getString(
|
|
|
|
|
R.string.success_sync_account, mTaskManager.getSyncAccount()));
|
|
|
|
|
// 记录最后同步时间
|
|
|
|
|
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
|
|
|
|
|
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
|
|
|
|
|
// 网络错误
|
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
|
|
|
|
|
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
|
|
|
|
|
// 内部错误
|
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
|
|
|
|
|
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
|
|
|
|
|
// 同步被取消
|
|
|
|
|
showNotification(R.string.ticker_cancel, mContext
|
|
|
|
|
.getString(R.string.error_sync_cancelled));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用同步完成监听器
|
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
// 在新线程中执行完成回调,避免阻塞UI线程
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|