|
|
|
@ -28,97 +28,136 @@ import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Google Tasks同步异步任务类
|
|
|
|
|
* 负责在后台执行Google Tasks同步操作,并通过通知和回调机制反馈状态
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
private static final int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 同步通知的唯一标识
|
|
|
|
|
|
|
|
|
|
// 完成监听器接口,用于同步完成后回调
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
void onComplete();
|
|
|
|
|
void onComplete(); // 同步完成回调方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
private Context mContext; // 上下文
|
|
|
|
|
private NotificationManager mNotifiManager; // 通知管理器
|
|
|
|
|
private GTaskManager mTaskManager; // Google Tasks管理类实例
|
|
|
|
|
private OnCompleteListener mOnCompleteListener; // 完成监听器
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造方法
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param listener 同步完成监听器
|
|
|
|
|
*/
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
// 获取通知管理器服务
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
// 获取Google Tasks管理类的单例实例
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消同步操作
|
|
|
|
|
*/
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
mTaskManager.cancelSync(); // 调用GTaskManager的取消同步方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布进度消息(包装为数组以适配AsyncTask的泛型参数)
|
|
|
|
|
* @param message 进度消息内容
|
|
|
|
|
*/
|
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
|
publishProgress(new 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 notification = new Notification(R.drawable.notification,
|
|
|
|
|
mContext.getString(tickerId), System.currentTimeMillis());
|
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS; // 设置默认灯光效果
|
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; // 设置通知自动取消
|
|
|
|
|
|
|
|
|
|
// 创建点击通知时的PendingIntent
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
|
|
|
|
|
// 失败/进行中的通知点击后跳转到设置页
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0,
|
|
|
|
|
new Intent(mContext, NotesPreferenceActivity.class), 0);
|
|
|
|
|
} else {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), 0);
|
|
|
|
|
// 成功的通知点击后跳转到笔记列表页
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0,
|
|
|
|
|
new Intent(mContext, NotesListActivity.class), 0);
|
|
|
|
|
}
|
|
|
|
|
// notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
|
|
|
|
|
// pendingIntent);
|
|
|
|
|
|
|
|
|
|
// 设置通知内容(已弃用的setLatestEventInfo,改用contentIntent)
|
|
|
|
|
notification.contentIntent = pendingIntent;
|
|
|
|
|
// 显示通知
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后台执行同步操作(异步任务核心逻辑)
|
|
|
|
|
* @param unused 未使用的参数
|
|
|
|
|
* @return 同步结果状态码(来自GTaskManager的STATE_*常量)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
// 发布登录进度消息(显示正在使用的同步账号)
|
|
|
|
|
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]);
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]); // 显示同步中的通知
|
|
|
|
|
// 如果上下文是GTaskSyncService,发送广播通知进度
|
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理后台任务完成(在主线程执行,用于处理结果和通知用户)
|
|
|
|
|
* @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()));
|
|
|
|
|
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));
|
|
|
|
|
showNotification(R.string.ticker_cancel, mContext.getString(R.string.error_sync_cancelled));
|
|
|
|
|
}
|
|
|
|
|
// 在子线程中执行完成回调,避免阻塞主线程
|
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|