/* * 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 类用于异步执行Google任务(GTask)同步操作。 * 它继承自 AsyncTask,以便在后台线程中执行耗时的网络操作, * 并通过主线程更新用户界面或发送通知。 */ public class GTaskASyncTask extends AsyncTask { // 用于同步操作的通知ID,确保唯一性。 private static final int GTASK_SYNC_NOTIFICATION_ID = 5234235; /** * 当同步完成时调用的接口。 */ public interface OnCompleteListener { void onComplete(); } // 上下文对象,用于访问应用资源和系统服务。 private Context mContext; // 管理通知的服务。 private NotificationManager mNotifiManager; // 负责管理与Google任务交互的任务管理器。 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(); } /** * 取消正在进行的同步操作。 */ public void cancelSync() { mTaskManager.cancelSync(); } /** * 更新进度信息。 * @param message 进度消息文本。 */ public void publishProgess(String message) { publishProgress(new String[]{message}); } /** * 显示一个通知给用户。 * @param tickerId 通知栏提示文字的资源ID。 * @param content 通知内容文本。 */ 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 pendingIntent; 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); } /** * 在后台线程中执行的实际同步逻辑。 */ @Override protected Integer doInBackground(Void... unused) { // 发布登录进度信息 publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(mContext))); // 执行同步操作并返回状态码 return mTaskManager.sync(mContext, this); } /** * 更新UI线程上的进度。 */ @Override protected void onProgressUpdate(String... progress) { // 显示同步中的通知 showNotification(R.string.ticker_syncing, progress[0]); // 如果上下文是GTaskSyncService,则广播进度信息 if (mContext instanceof GTaskSyncService) { ((GTaskSyncService) mContext).sendBroadcast(progress[0]); } } /** * 同步完成后,在UI线程上执行此方法。 */ @Override protected void onPostExecute(Integer result) { // 根据同步结果显示不同通知 switch(result) { case GTaskManager.STATE_SUCCESS: showNotification(R.string.ticker_success, mContext.getString(R.string.success_sync_account, mTaskManager.getSyncAccount())); NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); break; case GTaskManager.STATE_NETWORK_ERROR: showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network)); break; case GTaskManager.STATE_INTERNAL_ERROR: showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal)); break; case GTaskManager.STATE_SYNC_CANCELLED: showNotification(R.string.ticker_cancel, mContext.getString(R.string.error_sync_cancelled)); break; } // 如果存在监听器,则启动新线程调用onComplete方法 if (mOnCompleteListener != null) { new Thread(new Runnable() { @Override public void run() { mOnCompleteListener.onComplete(); } }).start(); } } }