/* * 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 { // 定义一个静态的整型变量,用于作为同步任务相关通知的唯一标识符(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 }); } // 私有方法,用于创建并展示一个通知,根据传入的提示文本资源ID(tickerId)和具体内容(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判断,如果不是表示成功的资源ID(R.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(); } } }