|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
@ -29,22 +28,27 @@ import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 异步执行GTask同步任务的类
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
// 同步通知的唯一ID
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
|
|
|
|
|
// 同步完成的回调接口
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
void onComplete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上下文对象
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
// 通知管理器
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
|
|
|
|
|
// GTask管理器
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
|
|
|
|
|
// 完成回调监听器
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
|
|
|
|
|
// 构造方法,初始化上下文和监听器
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
@ -53,36 +57,20 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步任务
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发布进度更新
|
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
|
publishProgress(new String[] {
|
|
|
|
|
message
|
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
|
// 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);
|
|
|
|
|
// mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
// }
|
|
|
|
|
// 显示通知栏消息
|
|
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
// 根据同步状态设置点击通知后的跳转目标
|
|
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
@ -90,6 +78,8 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建通知
|
|
|
|
|
Notification.Builder builder = new Notification.Builder(mContext)
|
|
|
|
|
.setAutoCancel(true)
|
|
|
|
|
.setContentTitle(mContext.getString(R.string.app_name))
|
|
|
|
@ -100,26 +90,35 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
Notification notification=builder.getNotification();
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 后台执行的方法,进行GTask同步
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
// 发布登录进度
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
// 执行同步并返回结果
|
|
|
|
|
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));
|
|
|
|
@ -129,13 +128,14 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|