/* * 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; // 定义一个异步任务类,继承自AsyncTask,用于执行后台任务 public class GTaskASyncTask extends AsyncTask { // 定义一个常量,用作通知的唯一标识 private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 定义一个接口,用于通知任务完成 public interface OnCompleteListener { void onComplete(); } private Context mContext; // 上下文对象,用于获取系统服务等 private NotificationManager mNotifiManager; // 通知管理器,用于管理通知 private GTaskManager mTaskManager; // 任务管理器,用于处理具体的任务 private OnCompleteListener mOnCompleteListener; // 完成监听器,用于通知任务完成 // 构造方法,初始化上下文和完成监听器,获取通知管理器和任务管理器的实例 public GTaskASyncTask(Context context, OnCompleteListener listener) { mContext = context; mOnCompleteListener = listener; mNotifiManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); mTaskManager = GTaskManager.getInstance(); } // 取消同步任务的方法 public void cancelSync() { mTaskManager.cancelSync(); } // 发布进度信息的方法,将字符串消息转换为字符串数组,然后调用publishProgress public void publishProgess(String message) { publishProgress(new String[] { message }); } // 私有方法,用于显示通知 private void showNotification(int tickerId, String content) { // 创建一个通知,指定图标、提示文本和时间戳 Notification notification = new Notification(R.drawable.notification, mContext .getString(tickerId), System.currentTimeMillis()); // 设置通知的默认行为,这里是点亮LED灯 notification.defaults = Notification.DEFAULT_LIGHTS; // 设置通知的标志,这里是自动取消 notification.flags = Notification.FLAG_AUTO_CANCEL; // 创建一个待定意图,用于点击通知后的跳转 PendingIntent pendingIntent; // 根据不同的tickerId,设置不同的跳转活动 if (tickerId != R.string.ticker_success) { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0); } else { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0); } // 设置通知的详细信息 notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent); // 通过通知管理器发送通知 mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); } // 重写doInBackground方法,该方法在后台线程中执行异步任务 @Override protected Integer doInBackground(Void... unused) { // 发布进度更新,显示登录进度 publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity .getSyncAccountName(mContext))); // 执行同步操作,并返回结果 return mTaskManager.sync(mContext, this); } // 重写onProgressUpdate方法,该方法在主线程中执行,用于更新进度 @Override protected void onProgressUpdate(String... progress) { // 显示同步中的通知 showNotification(R.string.ticker_syncing, progress[0]); // 如果上下文是GTaskSyncService的实例,则发送广播 if (mContext instanceof GTaskSyncService) { ((GTaskSyncService) mContext).sendBroadcast(progress[0]); } } // 重写onPostExecute方法,该方法在异步任务执行完毕后被调用 @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() { // 实现Runnable接口的run方法 public void run() { mOnCompleteListener.onComplete(); } }).start(); } } }