|
|
/*
|
|
|
* 版权声明:此处声明代码的版权属于 MiCode 开放源代码社区,并指定了许可协议为 Apache License 2.0。
|
|
|
* 提供了许可协议的获取链接。
|
|
|
* 强调除非适用法律要求或书面同意,否则软件按 “原样” 分发,不提供任何形式的明示或暗示保证。
|
|
|
* 并指出许可协议中关于权限和限制的具体语言。
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
import android.app.Notification; // 导入 Notification 类,用于创建通知
|
|
|
import android.app.NotificationManager; // 导入 NotificationManager 类,用于管理通知
|
|
|
import android.app.PendingIntent; // 导入 PendingIntent 类,用于创建待定意图
|
|
|
import android.content.Context; // 导入 Context 类,用于获取系统服务等
|
|
|
import android.content.Intent; // 导入 Intent 类,用于启动活动等
|
|
|
import android.os.AsyncTask; // 导入 AsyncTask 类,用于在后台线程执行耗时操作
|
|
|
|
|
|
import net.micode.notes.R; // 导入资源文件
|
|
|
import net.micode.notes.ui.NotesListActivity; // 导入笔记列表活动类
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity; // 导入笔记偏好设置活动类
|
|
|
|
|
|
|
|
|
/**
|
|
|
* GTaskASyncTask 类,继承自 AsyncTask,用于在后台线程执行 Google Tasks 同步操作。
|
|
|
*/
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 定义同步通知的 ID
|
|
|
|
|
|
/**
|
|
|
* 定义同步完成的回调接口,用于在同步完成后执行相应操作。
|
|
|
*/
|
|
|
public interface OnCompleteListener {
|
|
|
void onComplete(); // 定义 onComplete 方法,具体实现由调用者提供
|
|
|
}
|
|
|
|
|
|
private Context mContext; // 应用程序上下文
|
|
|
private NotificationManager mNotifiManager; // 通知管理器
|
|
|
private GTaskManager mTaskManager; // Google Tasks 管理器
|
|
|
private OnCompleteListener mOnCompleteListener; // 同步完成回调接口实例
|
|
|
|
|
|
/**
|
|
|
* 构造函数,用于初始化 GTaskASyncTask 对象。
|
|
|
*
|
|
|
* @param context 应用程序上下文
|
|
|
* @param listener 同步完成回调接口实例
|
|
|
*/
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
mContext = context; // 保存上下文
|
|
|
mOnCompleteListener = listener; // 保存回调接口实例
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE); // 获取通知管理器实例
|
|
|
mTaskManager = GTaskManager.getInstance(); // 获取 Google Tasks 管理器实例
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消同步操作。
|
|
|
*/
|
|
|
public void cancelSync() {
|
|
|
mTaskManager.cancelSync(); // 调用 Google Tasks 管理器的 cancelSync 方法取消同步
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发布进度信息。
|
|
|
*
|
|
|
* @param message 进度信息
|
|
|
*/
|
|
|
public void publishProgess(String message) {
|
|
|
publishProgress(new String[]{ // 调用 AsyncTask 的 publishProgress 方法发布进度
|
|
|
message
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示通知。
|
|
|
*
|
|
|
* @param tickerId 通知的 ticker 文本资源 ID
|
|
|
* @param content 通知内容
|
|
|
*/
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
Notification notification = new Notification(R.drawable.notification, mContext // 创建通知对象
|
|
|
.getString(tickerId), System.currentTimeMillis()); // 设置通知图标、ticker 文本和时间
|
|
|
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 的待定意图
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
|
} else { // 如果是成功通知
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, // 创建指向 NotesListActivity 的待定意图
|
|
|
NotesListActivity.class), 0);
|
|
|
}
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, // 设置通知的详细信息
|
|
|
pendingIntent);
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); // 发出通知
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在后台线程执行同步操作。
|
|
|
*
|
|
|
* @param unused 未使用的参数
|
|
|
* @return 同步操作的结果状态码
|
|
|
*/
|
|
|
@Override
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity // 发布登录进度信息
|
|
|
.getSyncAccountName(mContext)));
|
|
|
return mTaskManager.sync(mContext, this); // 执行同步操作并返回结果状态码
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在 UI 线程更新进度。
|
|
|
*
|
|
|
* @param progress 进度信息数组
|
|
|
*/
|
|
|
@Override
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
showNotification(R.string.ticker_syncing, progress[0]); // 显示同步中通知
|
|
|
if (mContext instanceof GTaskSyncService) { // 如果上下文是 GTaskSyncService 实例
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]); // 发送广播更新进度
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在 UI 线程处理同步结果。
|
|
|
*
|
|
|
* @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_n |