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

166 lines
8.4 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.
*/
// 版权声明部分说明代码遵循Apache 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;
// 导入相关的Android系统类以及应用内自定义的类用于后续操作中使用这些类的功能
// 例如Notification相关类用于创建和管理通知AsyncTask用于异步任务处理等R类用于获取资源相关的引用等
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
// 定义一个名为GTaskASyncTask的类继承自AsyncTask用于执行异步任务
// 泛型参数分别表示:
// - Void表示异步任务执行时不需要传入参数
// - String表示在任务执行过程中可以传递进度信息的类型这里是字符串形式的进度信息
// - Integer表示异步任务执行完成后返回结果的类型
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
// 定义一个静态的整型变量,用于作为同步任务相关通知的唯一标识符
public interface OnCompleteListener {
void onComplete();
}
// 定义一个内部接口OnCompleteListener用于定义任务完成后的回调方法具体实现由外部类决定
private Context mContext;
// 保存上下文对象,用于后续获取系统服务、资源等操作
private NotificationManager mNotifiManager;
// 用于管理通知的显示、取消等操作的对象
private GTaskManager mTaskManager;
// 与GTask相关的管理类对象可能用于执行具体的同步等任务逻辑
private OnCompleteListener mOnCompleteListener;
// 保存任务完成监听器对象,用于在任务结束后执行相应的回调逻辑
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context;
mOnCompleteListener = listener;
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
}
// 构造函数,接收上下文对象和任务完成监听器
// 在构造函数中:
// - 初始化成员变量mContext和mOnCompleteListener
// - 通过上下文获取系统的通知管理服务赋值给mNotifiManager
// - 获取GTaskManager的单例实例赋值给mTaskManager
public void cancelSync() {
mTaskManager.cancelSync();
}
// 定义一个方法用于取消同步任务实际调用的是GTaskManager中的取消同步方法
public void publishProgess(String message) {
publishProgress(new String[] {
message
});
}
// 对外提供的方法用于发布任务进度信息实际是调用父类AsyncTask的publishProgress方法
// 将传入的单个字符串消息包装成字符串数组后传递给publishProgress方法
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);
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), PendingIntent.FLAG_IMMUTABLE);
}
// 根据传入的tickerId判断要启动的目标Activity创建对应的PendingIntent
// 如果不是表示成功的tickerId就创建启动NotesPreferenceActivity的PendingIntent否则创建启动NotesListActivity的PendingIntent
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.Builder对象用于构建通知
// 设置通知的相关属性如自动取消、标题使用应用名称资源字符串、内容、关联的PendingIntent、触发时间以及设置为正在进行状态
Notification notification = builder.getNotification();
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
// 从构建器获取最终的通知对象,并通过通知管理对象根据通知标识符来显示通知
}
@Override
protected Integer doInBackground(Void... unused) {
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
return mTaskManager.sync(mContext, this);
}
// 重写AsyncTask的doInBackground方法在异步任务执行的后台线程中
// - 首先发布一个表示登录同步进度的消息,消息内容通过格式化资源字符串并传入同步账户名获取
// - 然后调用GTaskManager的sync方法执行同步任务并返回同步结果返回类型为Integer
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
// 重写AsyncTask的onProgressUpdate方法当在后台线程中调用publishProgress方法发布进度时会触发该方法
// - 首先根据固定的表示正在同步的tickerId和传入的进度消息内容来显示通知
// - 然后判断上下文是否是GTaskSyncService类型如果是则发送包含进度消息的广播
@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));
}
// 根据异步任务执行完成后的结果result来显示不同类型的通知
// - 如果是成功状态,显示成功相关的通知,并记录最后同步时间到偏好设置中
// - 如果是网络错误状态,显示网络错误相关通知
// - 如果是内部错误状态,显示内部错误相关通知
// - 如果是同步取消状态,显示同步取消相关通知
if (mOnCompleteListener!= null) {
new Thread(new Runnable() {
public void run() {
mOnCompleteListener.onComplete();
}
}).start();
}
// 如果任务完成监听器不为空则创建一个新线程来执行监听器的onComplete方法确保在合适的线程环境下执行回调逻辑
}
}