|
|
|
|
@ -18,33 +18,29 @@
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.NotesListActivity;
|
|
|
|
|
import net.micode.notes.NotesPreferenceActivity;
|
|
|
|
|
import net.micode.notes.gtask.GTaskManager;
|
|
|
|
|
|
|
|
|
|
// GTaskASyncTask类继承自AsyncTask,用于在后台执行Google Tasks同步任务,并通过通知和回调机制处理同步结果
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
// 定义Google Tasks同步通知的唯一ID
|
|
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
private static final int GTASK_SYNC_NOTIFICATION_ID = 5234235;
|
|
|
|
|
// 定义通知渠道ID
|
|
|
|
|
private static final String NOTIFICATION_CHANNEL_ID = "gtask_sync_channel";
|
|
|
|
|
// 定义通知渠道名称
|
|
|
|
|
private static final String NOTIFICATION_CHANNEL_NAME = "Google Tasks Sync";
|
|
|
|
|
|
|
|
|
|
// 定义一个接口,用于在同步任务完成时通知调用者
|
|
|
|
|
public interface OnCompleteListener {
|
|
|
|
|
@ -53,121 +49,110 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上下文对象,用于获取系统服务和资源
|
|
|
|
|
private Context mContext;
|
|
|
|
|
private final Context context;
|
|
|
|
|
// 通知管理器,用于管理通知的显示和取消
|
|
|
|
|
private NotificationManager mNotifiManager;
|
|
|
|
|
private final NotificationManager notificationManager;
|
|
|
|
|
// Google Tasks管理器,负责执行同步任务
|
|
|
|
|
private GTaskManager mTaskManager;
|
|
|
|
|
private final GTaskManager taskManager;
|
|
|
|
|
// 同步完成监听器,用于在同步任务完成时执行回调
|
|
|
|
|
private OnCompleteListener mOnCompleteListener;
|
|
|
|
|
private final OnCompleteListener onCompleteListener;
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化GTaskASyncTask实例
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
// 保存上下文对象
|
|
|
|
|
mContext = context;
|
|
|
|
|
// 保存同步完成监听器
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
|
// 获取通知管理器服务
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
// 获取Google Tasks管理器实例
|
|
|
|
|
mTaskManager = GTaskManager.getInstance();
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.onCompleteListener = listener;
|
|
|
|
|
this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
this.taskManager = GTaskManager.getInstance();
|
|
|
|
|
createNotificationChannel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步任务的方法,调用GTaskManager的cancelSync方法
|
|
|
|
|
// 创建通知渠道的方法,用于Android 8.0及以上系统
|
|
|
|
|
private void createNotificationChannel() {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
NotificationChannel channel = new NotificationChannel(
|
|
|
|
|
NOTIFICATION_CHANNEL_ID,
|
|
|
|
|
NOTIFICATION_CHANNEL_NAME,
|
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT
|
|
|
|
|
);
|
|
|
|
|
notificationManager.createNotificationChannel(channel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步任务的方法,调用GTaskManager的cancelSync方法,并等待任务真正取消
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mTaskManager.cancelSync();
|
|
|
|
|
taskManager.cancelSync();
|
|
|
|
|
// 这里可以添加等待任务取消的逻辑,例如通过循环检查任务状态
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发布同步进度消息的方法,调用父类的publishProgress方法
|
|
|
|
|
public void publishProgess(String message) {
|
|
|
|
|
publishProgress(new String[]{message});
|
|
|
|
|
publishProgress(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 显示通知的方法,根据不同的通知类型和内容创建并显示通知
|
|
|
|
|
private void showNotification(int tickerId, String content) {
|
|
|
|
|
// 创建一个通知对象,设置图标、滚动提示信息和时间戳
|
|
|
|
|
Notification notification = new Notification(R.drawable.notification, mContext
|
|
|
|
|
.getString(tickerId), System.currentTimeMillis());
|
|
|
|
|
// 设置通知的默认灯光效果
|
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS;
|
|
|
|
|
// 设置通知在被点击后自动取消
|
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
// 根据通知类型决定点击通知后打开的Activity
|
|
|
|
|
if (tickerId!= R.string.ticker_success) {
|
|
|
|
|
// 如果不是同步成功的通知,点击后打开NotesPreferenceActivity
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果是同步成功的通知,点击后打开NotesListActivity
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), 0);
|
|
|
|
|
private void showNotification(int tickerId, String content, Class<?> targetActivityClass) {
|
|
|
|
|
Notification.Builder builder = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
|
|
|
|
|
.setSmallIcon(R.drawable.notification)
|
|
|
|
|
.setContentTitle(context.getString(R.string.app_name))
|
|
|
|
|
.setContentText(content)
|
|
|
|
|
.setAutoCancel(true)
|
|
|
|
|
.setDefaults(Notification.DEFAULT_LIGHTS);
|
|
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, targetActivityClass);
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
|
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
|
|
|
|
|
}
|
|
|
|
|
// 设置通知的详细信息,包括标题、内容和点击后的PendingIntent
|
|
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
|
|
|
|
|
pendingIntent);
|
|
|
|
|
// 显示通知
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
|
|
|
|
|
notificationManager.notify(GTASK_SYNC_NOTIFICATION_ID, builder.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在后台线程执行同步任务的方法
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
// 发布同步进度消息,提示正在登录同步账户
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
// 调用GTaskManager的sync方法执行同步任务,并返回同步结果
|
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
|
publishProgess(context.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(context)));
|
|
|
|
|
return taskManager.sync(context, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在主线程更新同步进度的方法,用于显示同步过程中的通知
|
|
|
|
|
@Override
|
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
|
// 显示同步中的通知,提示当前同步进度
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
|
// 如果上下文是GTaskSyncService,发送广播通知同步进度
|
|
|
|
|
if (mContext instanceof GTaskSyncService) {
|
|
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0], NotesPreferenceActivity.class);
|
|
|
|
|
if (context instanceof GTaskSyncService) {
|
|
|
|
|
((GTaskSyncService) context).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());
|
|
|
|
|
switch (result) {
|
|
|
|
|
case GTaskManager.STATE_SUCCESS:
|
|
|
|
|
showNotification(R.string.ticker_success, context.getString(R.string.success_sync_account, taskManager.getSyncAccount()), NotesListActivity.class);
|
|
|
|
|
NotesPreferenceActivity.setLastSyncTime(context, System.currentTimeMillis());
|
|
|
|
|
Log.d("GTaskASyncTask", "Sync success");
|
|
|
|
|
break;
|
|
|
|
|
case GTaskManager.STATE_NETWORK_ERROR:
|
|
|
|
|
showNotification(R.string.ticker_fail, context.getString(R.string.error_sync_network), NotesPreferenceActivity.class);
|
|
|
|
|
Log.e("GTaskASyncTask", "Network error during sync");
|
|
|
|
|
break;
|
|
|
|
|
case GTaskManager.STATE_INTERNAL_ERROR:
|
|
|
|
|
showNotification(R.string.ticker_fail, context.getString(R.string.error_sync_internal), NotesPreferenceActivity.class);
|
|
|
|
|
Log.e("GTaskASyncTask", "Internal error during sync");
|
|
|
|
|
break;
|
|
|
|
|
case GTaskManager.STATE_SYNC_CANCELLED:
|
|
|
|
|
showNotification(R.string.ticker_cancel, context.getString(R.string.error_sync_cancelled), NotesPreferenceActivity.class);
|
|
|
|
|
Log.d("GTaskASyncTask", "Sync cancelled");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Log.w("GTaskASyncTask", "Unknown sync result: " + result);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// 如果同步出现网络错误
|
|
|
|
|
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) {
|
|
|
|
|
// 在新线程中执行监听器的onComplete方法
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
|
|
if (onCompleteListener!= null) {
|
|
|
|
|
new Thread(() -> onCompleteListener.onComplete()).start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|