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

150 lines
9.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.

/*
* 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用于在后台线程执行与GTask相关的同步任务并在任务执行的不同阶段如执行中、执行完成进行相应的界面交互操作
// 例如发布任务进度、展示通知以及根据任务执行结果进行不同的通知展示等,同时支持取消同步任务以及在任务完成后回调通知外部监听器。
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
// 定义一个静态的整型变量用于作为同步任务相关通知的唯一标识符ID以便区分不同的通知这里给定了一个固定值。
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
// 定义一个内部接口用于定义任务完成后的回调方法外部类实现该接口并传入实例后在任务完成时会调用对应的onComplete方法。
public interface OnCompleteListener {
void onComplete();
}
// 存储上下文对象用于获取系统服务、资源以及启动相关的Activity等操作是与Android系统交互的重要入口。
private Context mContext;
// 用于管理和展示通知的对象,通过上下文获取系统的通知服务来实例化,负责发送和管理与同步任务相关的通知。
private NotificationManager mNotifiManager;
// 用于管理GTask相关操作的对象通过单例模式获取实例负责实际的同步等业务逻辑操作。
private GTaskManager mTaskManager;
// 存储实现了OnCompleteListener接口的外部监听器对象用于在任务完成时通知外部进行相应的后续处理。
private OnCompleteListener mOnCompleteListener;
// 构造函数接收上下文对象和OnCompleteListener监听器对象作为参数用于初始化相关成员变量包括获取通知管理器、GTaskManager实例等。
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();
}
// 用于发布任务进度信息的方法将传入的消息字符串包装成字符串数组后调用父类AsyncTask的publishProgress方法
// 以便触发onProgressUpdate方法来更新界面展示的进度信息等。
public void publishProgess(String message) {
publishProgress(new String[] {
message
});
}
// 私有方法用于创建并展示一个通知根据传入的提示文本资源IDtickerId和具体内容content构建通知对象
// 设置通知的默认属性如灯光闪烁等、取消标志等同时根据不同的提示文本资源ID设置点击通知后启动的PendingIntent指向不同的Activity
// 最后通过通知管理器发送该通知。
private void showNotification(int tickerId, String content) {
// 创建一个通知对象使用指定的图标资源R.drawable.notification、提示文本通过上下文获取对应资源字符串以及当前时间作为时间戳来初始化通知。
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
// 设置通知的默认属性这里设置为默认的灯光闪烁效果具体的默认行为可根据系统设置和API文档进一步了解。
notification.defaults = Notification.DEFAULT_LIGHTS;
// 设置通知的标志,这里设置为自动取消,即用户点击通知后通知会自动消失,方便用户操作和避免重复显示。
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent;
// 根据传入的提示文本资源ID判断如果不是表示成功的资源IDR.string.ticker_success则设置点击通知后启动的PendingIntent指向NotesPreferenceActivity。
if (tickerId!= R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesPreferenceActivity.class), 0);
} else {
// 如果是表示成功的资源ID则设置点击通知后启动的PendingIntent指向NotesListActivity通常用于成功完成同步任务后引导用户查看相关内容。
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
NotesListActivity.class), 0);
}
// 设置通知的详细信息包括标题应用名称通过上下文获取对应资源字符串、内容传入的content参数以及点击通知后要执行的PendingIntent。
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);
// 通过通知管理器发送通知使用之前定义的通知唯一标识符GTASK_SYNC_NOTIFICATION_ID来区分不同的通知确保每个通知有唯一标识。
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
}
// 在后台线程执行的方法继承自AsyncTask这里首先发布一个表示正在登录进行同步的进度消息包含同步账号名称
// 然后调用GTaskManager的sync方法执行实际的同步操作并返回同步结果以整数形式表示不同的状态码
@Override
protected Integer doInBackground(Void... unused) {
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext)));
return mTaskManager.sync(mContext, this);
}
// 在主线程执行的方法用于处理任务执行过程中的进度更新情况继承自AsyncTask会接收到在doInBackground中通过publishProgress方法传递过来的进度消息
// 在这里展示对应的通知并且如果上下文是GTaskSyncService类型还会发送广播传递进度消息可能用于服务内部其他组件获取进度信息
@Override
protected void onProgressUpdate(String... progress) {
showNotification(R.string.ticker_syncing, progress[0]);
if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
// 在主线程执行的方法用于处理任务执行完成后的情况继承自AsyncTask根据返回的不同结果码代表不同的同步状态展示相应的通知
// 如果传入了OnCompleteListener监听器对象则启动一个新线程调用监听器的onComplete方法通知外部任务已完成方便外部进行后续处理。
@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();
}
}
}