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.
note/GTaskASyncTask.java

174 lines
6.3 KiB

2 months ago
/*
* 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 GoogleGTask
* AsyncTask便线
* 线
*/
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
// 用于同步操作的通知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();
}
}
}