|
|
|
@ -0,0 +1,156 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010 - 2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* 遵循 Apache 许可证 2.0 版(“许可证”);
|
|
|
|
|
* 除非遵守许可证,否则不得使用此文件。
|
|
|
|
|
* 你可以在以下网址获取许可证副本:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* 除非适用法律要求或书面同意,
|
|
|
|
|
* 根据许可证分发的软件按“原样”分发,
|
|
|
|
|
* 不附带任何明示或暗示的保证或条件。
|
|
|
|
|
* 请参阅许可证,了解具体的权限和限制。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
// GTaskASyncTask 类继承自 AsyncTask,用于在后台执行与 GTask 同步相关的任务
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
// 用于标识 GTask 同步通知的 ID
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
|
|
|
|
|
// 定义一个接口,用于在任务完成时通知调用者
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
// 定义任务完成时的回调方法
|
|
|
|
|
void onComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上下文对象,用于获取系统服务等
|
|
|
|
|
private Context mContext;
|
|
|
|
|
// 通知管理器,用于管理通知
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
// GTask 管理器,用于执行 GTask 相关的同步操作
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
// 任务完成监听器,用于接收任务完成的通知
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化 GTaskASyncTask 实例
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
// 保存上下文对象
|
|
|
|
|
mContext = context;
|
|
|
|
|
// 保存任务完成监听器
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
|
// 获取通知管理器
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
// 获取 GTask 管理器的单例实例
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步操作的方法
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
// 调用 GTask 管理器的取消同步方法
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发布任务进度的方法,该方法名称有误,应为 publishProgress
|
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
|
// 调用父类的 publishProgress 方法发布任务进度
|
|
|
|
|
publishProgress(new String[]{
|
|
|
|
|
message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示通知的方法
|
|
|
|
|
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;
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
// 根据 tickerId 判断设置不同的点击通知后的跳转意图
|
|
|
|
|
if (tickerId!= R.string.ticker_success) {
|
|
|
|
|
// 如果不是同步成功的通知,点击后跳转到 NotesPreferenceActivity
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// 如果是同步成功的通知,点击后跳转到 NotesListActivity
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), 0);
|
|
|
|
|
}
|
|
|
|
|
// 设置通知的最新事件信息
|
|
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
|
|
|
|
|
pendingIntent);
|
|
|
|
|
// 显示通知
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在后台执行的任务方法,该方法运行在子线程中
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
// 发布任务进度,显示正在登录同步账户的信息
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
// 调用 GTask 管理器的 sync 方法执行同步操作,并返回同步结果
|
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当任务进度更新时调用的方法,运行在主线程中
|
|
|
|
|
@Override
|
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
|
// 显示同步中的通知
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
|
// 如果上下文是 GTaskSyncService 类型,发送广播通知进度
|
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当任务执行完成后调用的方法,运行在主线程中
|
|
|
|
|
@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));
|
|
|
|
|
}
|
|
|
|
|
// 如果任务完成监听器不为空,在新线程中调用 onComplete 方法
|
|
|
|
|
if (mOnCompleteListener!= null) {
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|