|
|
@ -1,4 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -15,7 +14,9 @@
|
|
|
|
* limitations under the License.
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
//这个类封装了Google任务同步的所有逻辑,包括后台执行、进度通知、错误处理和结果反馈,为应用提供了完整的同步功能实现。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;//导入包
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.NotificationManager;
|
|
|
@ -28,23 +29,32 @@ import net.micode.notes.R;
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Google任务同步的异步任务类,继承自AsyncTask
|
|
|
|
|
|
|
|
* 用于在后台执行同步操作并显示通知
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 同步通知的唯一ID
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 同步完成回调接口
|
|
|
|
|
|
|
|
*/
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
void onComplete();
|
|
|
|
void onComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private Context mContext; // 上下文对象
|
|
|
|
|
|
|
|
private NotificationManager mNotifiManager; // 通知管理器
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
private GTaskManager mTaskManager; // Google任务管理器
|
|
|
|
|
|
|
|
private OnCompleteListener mOnCompleteListener; // 完成监听器
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数
|
|
|
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
|
|
|
* @param listener 完成监听器
|
|
|
|
|
|
|
|
*/
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
mContext = context;
|
|
|
|
mContext = context;
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
mOnCompleteListener = listener;
|
|
|
@ -53,67 +63,112 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 取消同步操作
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void cancelSync() {
|
|
|
|
public void cancelSync() {
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 发布进度更新
|
|
|
|
|
|
|
|
* @param message 进度消息
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
publishProgress(new String[] {
|
|
|
|
publishProgress(new String[] { message });
|
|
|
|
message
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 显示通知
|
|
|
|
|
|
|
|
* @param tickerId 通知标题资源ID
|
|
|
|
|
|
|
|
* @param content 通知内容
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
|
Notification notification = new Notification(R.drawable.notification, mContext
|
|
|
|
// 创建通知对象
|
|
|
|
.getString(tickerId), System.currentTimeMillis());
|
|
|
|
Notification notification = new Notification(R.drawable.notification,
|
|
|
|
|
|
|
|
mContext.getString(tickerId), System.currentTimeMillis());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置通知属性
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS;
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS;
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据通知类型设置不同的PendingIntent
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
|
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 {
|
|
|
|
} 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);
|
|
|
|
// 设置通知内容
|
|
|
|
|
|
|
|
notification.setLatestEventInfo(mContext,
|
|
|
|
|
|
|
|
mContext.getString(R.string.app_name), content, pendingIntent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 显示通知
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 后台执行同步任务
|
|
|
|
|
|
|
|
* @param unused 未使用的参数
|
|
|
|
|
|
|
|
* @return 同步结果状态码
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
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);
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 进度更新回调
|
|
|
|
|
|
|
|
* @param progress 进度消息数组
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
|
|
|
|
// 显示同步中的通知
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是服务上下文,发送广播
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 同步完成后回调
|
|
|
|
|
|
|
|
* @param result 同步结果状态码
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
|
|
|
|
// 根据不同的结果状态显示不同的通知
|
|
|
|
if (result == GTaskManager.STATE_SUCCESS) {
|
|
|
|
if (result == GTaskManager.STATE_SUCCESS) {
|
|
|
|
|
|
|
|
// 同步成功
|
|
|
|
showNotification(R.string.ticker_success, mContext.getString(
|
|
|
|
showNotification(R.string.ticker_success, mContext.getString(
|
|
|
|
R.string.success_sync_account, mTaskManager.getSyncAccount()));
|
|
|
|
R.string.success_sync_account, mTaskManager.getSyncAccount()));
|
|
|
|
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
|
|
|
|
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
|
|
|
|
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
|
|
|
|
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
|
|
|
|
|
|
|
|
// 网络错误
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
|
|
|
|
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
|
|
|
|
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
|
|
|
|
|
|
|
|
// 内部错误
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
|
|
|
|
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
|
|
|
|
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
|
|
|
|
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
|
|
|
|
|
|
|
|
// 同步取消
|
|
|
|
showNotification(R.string.ticker_cancel, mContext
|
|
|
|
showNotification(R.string.ticker_cancel, mContext
|
|
|
|
.getString(R.string.error_sync_cancelled));
|
|
|
|
.getString(R.string.error_sync_cancelled));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 调用完成监听器
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|