|
|
|
|
|
/*
|
|
|
* 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,用于在后台执行Google Tasks同步操作
|
|
|
*
|
|
|
* 该类负责在后台线程中执行Google Tasks的同步工作,并在UI线程中更新同步进度和结果
|
|
|
* 主要功能包括:
|
|
|
* - 在后台执行同步操作
|
|
|
* - 显示同步进度通知
|
|
|
* - 发送同步状态广播
|
|
|
* - 处理同步完成后的回调
|
|
|
* - 支持取消同步操作
|
|
|
*
|
|
|
* @author MiCode Open Source Community
|
|
|
*/
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
/**
|
|
|
* 同步通知的唯一标识符
|
|
|
*/
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
|
/**
|
|
|
* OnCompleteListener接口 - 同步完成事件的监听器
|
|
|
*
|
|
|
* 该接口用于在同步操作完成后接收回调通知
|
|
|
*/
|
|
|
public interface OnCompleteListener {
|
|
|
/**
|
|
|
* 同步完成时调用的方法
|
|
|
*/
|
|
|
void onComplete();
|
|
|
}
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消正在进行的同步操作
|
|
|
*
|
|
|
* 该方法通过调用GTaskManager的cancelSync方法来取消同步
|
|
|
*/
|
|
|
public void cancelSync() {
|
|
|
mTaskManager.cancelSync();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发布同步进度消息
|
|
|
*
|
|
|
* 该方法用于向UI线程发送同步进度更新
|
|
|
*
|
|
|
* @param message 进度消息内容
|
|
|
*/
|
|
|
public void publishProgess(String message) {
|
|
|
publishProgress(new String[] {
|
|
|
message
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 显示同步通知
|
|
|
*
|
|
|
* 该方法用于显示同步状态的通知消息,包括进度通知和结果通知
|
|
|
*
|
|
|
* @param tickerId 通知标题的资源ID
|
|
|
* @param content 通知内容
|
|
|
*/
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
// 1. 创建通知构建器(替代旧构造函数)
|
|
|
Notification.Builder builder = new Notification.Builder(mContext)
|
|
|
.setSmallIcon(R.drawable.notification) // 替代旧的第一个参数
|
|
|
.setTicker(mContext.getString(tickerId)) // 替代旧的第二个参数
|
|
|
.setWhen(System.currentTimeMillis()) // 替代旧的第三个参数
|
|
|
.setDefaults(Notification.DEFAULT_LIGHTS) // 替代 notification.defaults
|
|
|
.setAutoCancel(true) // 替代 notification.flags = FLAG_AUTO_CANCEL
|
|
|
.setContentTitle(mContext.getString(R.string.app_name))
|
|
|
.setContentText(content);
|
|
|
|
|
|
// 2. 创建 PendingIntent
|
|
|
Intent intent;
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
intent = new Intent(mContext, NotesPreferenceActivity.class);
|
|
|
} else {
|
|
|
intent = new Intent(mContext, NotesListActivity.class);
|
|
|
}
|
|
|
|
|
|
// Android 12+ 需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(
|
|
|
mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE);
|
|
|
|
|
|
// 3. 设置意图
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
|
|
// 4. 构建并发送通知
|
|
|
Notification notification = builder.build();
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在后台线程中执行同步操作
|
|
|
*
|
|
|
* 该方法是AsyncTask的核心方法,在后台线程中执行Google Tasks的同步工作
|
|
|
*
|
|
|
* @param unused 未使用的参数
|
|
|
* @return 同步结果状态码,对应GTaskManager中的状态常量
|
|
|
*/
|
|
|
@Override
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
.getSyncAccountName(mContext)));
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在UI线程中更新同步进度
|
|
|
*
|
|
|
* 当后台线程调用publishProgress方法时,该方法会在UI线程中被调用
|
|
|
*
|
|
|
* @param progress 进度消息数组,包含最新的进度消息
|
|
|
*/
|
|
|
@Override
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在UI线程中处理同步完成后的结果
|
|
|
*
|
|
|
* 当后台线程的doInBackground方法执行完成后,该方法会在UI线程中被调用
|
|
|
*
|
|
|
* @param result 同步结果状态码,对应GTaskManager中的状态常量
|
|
|
*/
|
|
|
@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() {
|
|
|
mOnCompleteListener.onComplete();
|
|
|
}
|
|
|
}).start();
|
|
|
}
|
|
|
}
|
|
|
}
|