|
|
|
|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
// 定义一个继承自AsyncTask的异步任务类,用于执行与GTask相关的异步操作
|
|
|
// 泛型参数:Void表示不需要传入参数,String表示在任务执行过程中用于传递进度信息的类型,Integer表示任务执行结果的类型
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
// 定义一个静态的通知ID,用于标识GTask同步相关的通知
|
|
|
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;
|
|
|
|
|
|
// 构造函数,用于初始化GTaskASyncTask对象
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
// 初始化上下文对象
|
|
|
mContext = context;
|
|
|
// 初始化任务完成监听器
|
|
|
mOnCompleteListener = listener;
|
|
|
// 通过上下文获取NotificationManager系统服务
|
|
|
mNotifiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
// 获取GTaskManager的单例实例
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
}
|
|
|
|
|
|
// 取消同步操作的方法
|
|
|
public void cancelSync() {
|
|
|
// 调用GTaskManager的取消同步方法
|
|
|
mTaskManager.cancelSync();
|
|
|
}
|
|
|
|
|
|
// 发布任务进度的方法,这里方法名拼写有误,应为publishProgress
|
|
|
public void publishProgess(String message) {
|
|
|
// 调用父类的publishProgress方法,将进度信息传递出去
|
|
|
publishProgress(new String[]{
|
|
|
message
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 显示通知的方法
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
// 创建一个Notification对象
|
|
|
// 使用传入的tickerId获取对应的字符串作为通知的提示信息,使用系统当前时间作为通知的时间戳
|
|
|
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判断创建不同的PendingIntent
|
|
|
if (tickerId!= R.string.ticker_success) {
|
|
|
// 创建一个PendingIntent,用于在用户点击通知时启动NotesPreferenceActivity
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0);
|
|
|
} else {
|
|
|
// 创建一个PendingIntent,用于在用户点击通知时启动NotesListActivity
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0);
|
|
|
}
|
|
|
// 设置通知的详细信息,包括应用名称、具体内容和PendingIntent
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent);
|
|
|
// 使用NotificationManager显示通知
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
}
|
|
|
|
|
|
// 异步任务在后台执行的方法,此方法在子线程中运行
|
|
|
@Override
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
// 发布任务进度,显示正在登录的信息,其中包含同步账户名称
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(mContext)));
|
|
|
// 调用GTaskManager的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]);
|
|
|
}
|
|
|
}
|
|
|
// 重写AsyncTask的onPostExecute方法,该方法在后台任务执行完成后在主线程中调用
|
|
|
@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));
|
|
|
}
|
|
|
// 如果任务完成监听器不为空
|
|
|
if (mOnCompleteListener!= null) {
|
|
|
// 创建一个新线程
|
|
|
new Thread(new Runnable() {
|
|
|
// 定义线程的执行逻辑
|
|
|
public void run() {
|
|
|
// 调用任务完成监听器的onComplete方法
|
|
|
mOnCompleteListener.onComplete();
|
|
|
}
|
|
|
}).start();
|
|
|
}
|
|
|
} |