You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiangmuyi/GTaskASyncTask.java

170 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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;
// GTaskASyncTask类继承自AsyncTask用于在后台执行与GTask相关的任务并根据任务进度和结果进行相应的界面反馈如显示通知等同时支持任务取消等操作
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
// 定义一个静态的整数作为GTask同步相关通知在系统中的唯一标识符用于区分不同的通知
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
// 定义一个接口,用于定义任务完成时需要执行的回调方法,外部类实现此接口来处理任务完成后的逻辑
public interface OnCompleteListener {
void onComplete();
}
// 用于保存应用的上下文环境,方便后续获取系统服务、资源等操作
private Context mContext;
// 用于管理和显示通知的系统服务对象,通过它可以创建、更新和取消通知
private NotificationManager mNotifiManager;
// GTask管理类的实例负责具体的GTask相关业务逻辑比如执行同步操作等
private GTaskManager mTaskManager;
// 实现了OnCompleteListener接口的对象引用用于在任务完成后进行回调通知
private OnCompleteListener mOnCompleteListener;
// 构造函数用于创建GTaskASyncTask实例传入上下文和任务完成监听器对象
public GTaskASyncTask(Context context, OnCompleteListener listener) {
// 将传入的上下文赋值给成员变量,方便后续使用
mContext = context;
// 保存传入的任务完成监听器对象
mOnCompleteListener = listener;
// 获取系统的通知管理服务,以便后续可以发送通知
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
// 获取GTaskManager的单例实例用于执行具体的GTask相关任务操作
mTaskManager = GTaskManager.getInstance();
}
// 用于取消正在进行的GTask同步任务通过调用GTaskManager中的取消同步方法来实现
public void cancelSync() {
mTaskManager.cancelSync();
}
// 对外提供的方法用于发布任务执行的进度信息实际上是调用AsyncTask的publishProgress方法来通知主线程更新进度显示
public void publishProgess(String message) {
// 创建一个包含要发布消息的字符串数组并调用publishProgress方法传递该数组
publishProgress(new String[] {
message
});
}
// 私有方法用于创建并显示通知根据传入的提示文本ID和具体内容来构建通知并发送到通知栏
private void showNotification(int tickerId, String content) {
PendingIntent pendingIntent;
// 根据传入的提示文本ID判断如果不是表示成功的提示文本ID通常对应不同的后续操作意图
if (tickerId!= R.string.ticker_success) {
// 创建一个PendingIntent用于当用户点击通知时启动NotesPreferenceActivity设置为不可变遵循Android系统的一些安全和兼容性要求
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesPreferenceActivity.class), PendingIntent.FLAG_IMMUTABLE);
} else {
// 如果是表示成功的提示文本ID则创建一个PendingIntent用于当用户点击通知时启动NotesListActivity同样设置为不可变
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), PendingIntent.FLAG_IMMUTABLE);
}
// 使用Notification.Builder来构建通知对象设置通知的一系列属性
Notification.Builder builder = new Notification.Builder(mContext)
// 设置通知点击后自动取消
.setAutoCancel(true)
// 设置通知的标题,从资源文件中获取应用名称作为标题
.setContentTitle(mContext.getString(R.string.app_name))
// 设置通知的内容文本,传入具体的内容字符串
.setContentText(content)
// 设置通知的点击意图即用户点击通知后要执行的操作由前面创建的PendingIntent决定
.setContentIntent(pendingIntent)
// 设置通知触发的时间,使用当前系统时间
.setWhen(System.currentTimeMillis())
// 设置通知为持续进行状态(比如在同步过程中一直显示,直到任务完成等情况)
.setOngoing(true);
// 获取构建好的通知对象在不同的Android版本中获取方式可能有所不同这里是一种常见的方式
Notification notification = builder.getNotification();
// 通过通知管理器发送通知使用之前定义的通知ID来标识该通知确保唯一性
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
// 在后台线程中执行的方法用于执行实际的GTask同步任务相关逻辑同时发布初始的进度信息
@Override
protected Integer doInBackground(Void... unused) {
// 发布同步登录进度信息从资源文件中获取格式化后的字符串其中包含同步账号名称通过NotesPreferenceActivity获取
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
// 调用GTaskManager的sync方法执行同步操作并返回同步结果通常是表示不同状态的整数
return mTaskManager.sync(mContext, this);
}
// 在主线程中执行的方法,用于处理任务执行过程中的进度更新情况,显示相应的通知,并在特定条件下发送广播通知相关组件
@Override
protected void onProgressUpdate(String... progress) {
// 根据进度信息显示相应的通知使用表示正在同步的提示文本ID和传入的具体进度消息内容
showNotification(R.string.ticker_syncing, progress[0]);
// 如果当前上下文对象是GTaskSyncService类型可能是在特定的服务环境中运行该任务
if (mContext instanceof GTaskSyncService) {
// 则通过该服务发送广播,广播内容为进度消息,用于通知其他相关组件任务进度情况
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
// 在主线程中执行的方法,用于处理任务执行完成后的逻辑,根据不同的任务结果显示相应的通知,并在有任务完成监听器的情况下进行回调通知
@Override
protected void onPostExecute(Integer result) {
// 如果任务结果表示成功通常对应GTaskManager中定义的表示成功的状态常量
if (result == GTaskManager.STATE_SUCCESS) {
// 显示表示成功的通知通知内容包含成功同步的账号信息从资源文件中获取格式化后的字符串包含账号信息通过GTaskManager获取账号
showNotification(R.string.ticker_success, mContext.getString(
R.string.success_sync_account, mTaskManager.getSyncAccount()));
// 在NotesPreferenceActivity中记录最后同步时间传入当前系统时间
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));
}
// 如果存在任务完成监听器对象即外部传入了实现了OnCompleteListener接口的对象
if (mOnCompleteListener!= null) {
// 创建一个新线程,用于执行任务完成的回调方法,避免阻塞主线程等情况
new Thread(new Runnable() {
public void run() {
// 调用任务完成监听器的onComplete方法通知外部任务已完成由外部实现具体的后续逻辑
mOnCompleteListener.onComplete();
}
}).start();
}
}
}