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.
ReadMiNotes/标注/GTaskASyncTask.java

150 lines
5.3 KiB

1 year 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.
1 year ago
*/
1 year ago
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;
// 添加中文注释后
1 year ago
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
1 year ago
// 通知ID
1 year ago
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
1 year ago
// 完成监听器
1 year ago
public interface OnCompleteListener {
void onComplete();
}
1 year ago
// 上下文
1 year ago
private Context mContext;
1 year ago
// 通知管理器
1 year ago
private NotificationManager mNotifiManager;
1 year ago
// 任务管理器
1 year ago
private GTaskManager mTaskManager;
1 year ago
// 完成监听器
1 year ago
private OnCompleteListener mOnCompleteListener;
1 year ago
// 构造函数
1 year ago
public GTaskASyncTask(Context context, OnCompleteListener listener) {
mContext = context;
mOnCompleteListener = listener;
mNotifiManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
}
1 year ago
// 取消同步
1 year ago
public void cancelSync() {
mTaskManager.cancelSync();
}
1 year ago
// 发布进度
public void publishProgess(String message) {
1 year ago
publishProgress(new String[] {
message
});
}
1 year ago
private void showNotification(int tickerId, String content) {
// 创建一个新的通知
1 year ago
Notification notification = new Notification(R.drawable.notification, mContext
.getString(tickerId), System.currentTimeMillis());
1 year ago
// 设置默认的提示音
notification.defaults = Notification.DEFAULT_LIGHTS;
// 设置自动取消
notification.flags = Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingIntent;
// 如果不是成功提示则设置跳转到NotesPreferenceActivity
1 year ago
if (tickerId != R.string.ticker_success) {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
1 year ago
NotesPreferenceActivity.class), 0);
// 如果是成功提示则设置跳转到NotesListActivity
1 year ago
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
1 year ago
NotesListActivity.class), 0);
1 year ago
}
1 year ago
// 设置通知的内容
1 year ago
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
pendingIntent);
1 year ago
// 发送通知
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
1 year ago
}
1 year ago
@Override
1 year ago
protected Integer doInBackground(Void... unused) {
1 year ago
// 发布进度,提示正在登录
1 year ago
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
1 year ago
.getSyncAccountName(mContext)));
// 返回任务管理器的结果
return mTaskManager.sync(mContext, this);
1 year ago
}
1 year ago
1 year ago
@Override
protected void onProgressUpdate(String... progress) {
1 year ago
// 显示进度条
1 year ago
showNotification(R.string.ticker_syncing, progress[0]);
1 year ago
// 如果上下文是GTaskSyncService则发送广播
if (mContext instanceof GTaskSyncService) {
1 year ago
((GTaskSyncService) mContext).sendBroadcast(progress[0]);
}
}
1 year ago
@Override
protected void onPostExecute(Integer result) {
// 判断执行结果
1 year ago
if (result == GTaskManager.STATE_SUCCESS) {
1 year ago
// 执行成功
1 year ago
showNotification(R.string.ticker_success, mContext.getString(
R.string.success_sync_account, mTaskManager.getSyncAccount()));
1 year ago
// 设置上一次同步时间
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());
1 year ago
} else if (result == GTaskManager.STATE_NETWORK_ERROR) {
1 year ago
// 网络错误
1 year ago
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
} else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
1 year ago
// 内部错误
1 year ago
showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
} else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
1 year ago
// 取消同步
1 year ago
showNotification(R.string.ticker_cancel, mContext
.getString(R.string.error_sync_cancelled));
1 year ago
}
// 执行完成回调
1 year ago
if (mOnCompleteListener != null) {
1 year ago
new Thread(new Runnable() {
public void run() {
mOnCompleteListener.onComplete();
1 year ago
}
}).start();
}
}
1 year ago
}