|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
@ -29,114 +28,137 @@ 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 int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 同步通知的唯一标识
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步完成监听器接口
|
|
|
|
|
* 用于在同步任务完成后回调通知上层应用
|
|
|
|
|
*/
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
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);
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消同步操作
|
|
|
|
|
*/
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发布同步进度消息
|
|
|
|
|
* @param message 进度提示信息
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
//}
|
|
|
|
|
/**
|
|
|
|
|
* 显示同步状态通知
|
|
|
|
|
* @param tickerId 通知标题资源ID
|
|
|
|
|
* @param content 通知内容
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0,
|
|
|
|
|
new Intent(mContext, NotesPreferenceActivity.class),
|
|
|
|
|
PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
} else {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
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))
|
|
|
|
|
.setContentText(content)
|
|
|
|
|
.setContentIntent(pendingIntent)
|
|
|
|
|
.setWhen(System.currentTimeMillis())
|
|
|
|
|
.setOngoing(true);
|
|
|
|
|
Notification notification=builder.getNotification();
|
|
|
|
|
.setAutoCancel(true) // 点击后自动取消通知
|
|
|
|
|
.setContentTitle(mContext.getString(R.string.app_name)) // 通知标题
|
|
|
|
|
.setContentText(content) // 通知内容
|
|
|
|
|
.setContentIntent(pendingIntent) // 点击通知后的跳转意图
|
|
|
|
|
.setWhen(System.currentTimeMillis()) // 通知时间
|
|
|
|
|
.setOngoing(true); // 设置为正在进行的通知
|
|
|
|
|
Notification notification = builder.getNotification();
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后台执行同步任务
|
|
|
|
|
* @param unused 未使用的参数
|
|
|
|
|
* @return 同步结果状态码(来自GTaskManager)
|
|
|
|
|
*/
|
|
|
|
|
@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)));
|
|
|
|
|
// 调用GTaskManager执行同步操作并返回状态
|
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理进度更新
|
|
|
|
|
* @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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步任务完成后的处理
|
|
|
|
|
* @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));
|
|
|
|
|
showNotification(R.string.ticker_cancel, mContext.getString(R.string.error_sync_cancelled));
|
|
|
|
|
}
|
|
|
|
|
// 在新线程中回调完成监听器(避免UI线程阻塞)
|
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|