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.
xiaomi/src/gtask/remote/GTaskASyncTask.java

158 lines
6.1 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.

/*
* 版权所有 (c) 2010-2011MiCode 开源社区 (www.micode.net)
* 根据 Apache 许可证 2.0 版本("许可证")授权;
* 除非符合许可证的规定,否则不得使用本文件。
* 您可以从以下网址获取许可证副本:
* http://www.apache.org/licenses/LICENSE-2.0
* 除非适用法律要求或书面同意,本软件按"原样"分发,
* 没有任何明示或暗示的保证或条件。
* 详见许可证中规定的权限和限制。
* 这是一份标准的Apache许可证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 android.os.Build;
// 导入AndroidX兼容库注解
import androidx.annotation.RequiresApi;
// 导入应用资源
import net.micode.notes.R;
// 导入界面活动类
import net.micode.notes.ui.NotesListActivity;
import net.micode.notes.ui.NotesPreferenceActivity;
// 异步任务类用于处理Google任务同步
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
// 定义同步完成回调接口
public interface OnCompleteListener {
void onComplete(); // 同步完成时触发
}
// 上下文对象
private final Context mContext;
// 通知管理器
private final NotificationManager mNotifiManager;
// Google任务管理器实例
private final GTaskManager mTaskManager;
// 完成监听器
private final OnCompleteListener mOnCompleteListener;
// 构造函数
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context; // 初始化上下文
mOnCompleteListener = listener; // 设置完成监听器
// 获取系统通知服务
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
// 获取任务管理器单例
mTaskManager = GTaskManager.getInstance();
}
// 取消同步操作
public void cancelSync() {
mTaskManager.cancelSync(); // 调用任务管理器取消方法
}
// 发布进度更新
public void publishProgess(String message) {
publishProgress(new String[] {
message // 包装成字符串数组
});
}
// 显示通知的方法兼容新版API
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);
}
// 构建通知对象
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();
// 定义通知ID
int GTASK_SYNC_NOTIFICATION_ID = 5234235;
// 显示通知
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
// 后台执行方法API级别限制
@RequiresApi(api = Build.VERSION_CODES.R)
@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]);
// 如果是服务上下文,发送广播
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));
} 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(mOnCompleteListener::onComplete).start();
}
}
}