|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
* 版权所有,MiCode开源社区。
|
|
|
*/
|
|
|
/**
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* 根据Apache License 2.0版本(“许可证”)授权;
|
|
|
*/
|
|
|
/**
|
|
|
* 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
|
|
|
* 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; // 导入Notification类。
|
|
|
import android.app.NotificationManager; // 导入NotificationManager类。
|
|
|
import android.app.PendingIntent; // 导入PendingIntent类。
|
|
|
import android.content.Context; // 导入Context类。
|
|
|
import android.content.Intent; // 导入Intent类。
|
|
|
import android.os.AsyncTask; // 导入AsyncTask类。
|
|
|
import net.micode.notes.R; // 导入应用资源。
|
|
|
import net.micode.notes.ui.NotesListActivity; // 导入NotesListActivity。
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity; // 导入NotesPreferenceActivity。
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> { // 定义异步任务类。
|
|
|
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 同步通知的ID。
|
|
|
|
|
|
public interface OnCompleteListener { // 定义任务完成的监听器接口。
|
|
|
void onComplete(); // 当任务完成时调用的方法。
|
|
|
}
|
|
|
private Context mContext; // 存储上下文对象。
|
|
|
private NotificationManager mNotifiManager; // 存储通知管理器对象。
|
|
|
private GTaskManager mTaskManager; // 存储GTask管理器对象。
|
|
|
private OnCompleteListener mOnCompleteListener; // 存储任务完成监听器对象。
|
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) { // 构造函数。
|
|
|
mContext = context; // 初始化上下文对象。
|
|
|
mOnCompleteListener = listener; // 初始化任务完成监听器对象。
|
|
|
mNotifiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); // 获取通知管理器服务。
|
|
|
mTaskManager = GTaskManager.getInstance(); // 获取GTask管理器实例。
|
|
|
}
|
|
|
|
|
|
public void cancelSync() { // 提供取消同步的方法。
|
|
|
mTaskManager.cancelSync(); // 调用GTask管理器的取消同步方法。
|
|
|
}
|
|
|
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()); // 创建通知对象。
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS; // 设置通知的默认灯光。
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; // 设置通知的自动取消标志。
|
|
|
PendingIntent pendingIntent; // 声明PendingIntent对象。
|
|
|
if (tickerId != R.string.ticker_success) { // 如果通知ID不是成功ID。
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0); // 创建PendingIntent,指向设置活动。
|
|
|
} else { // 否则。
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0); // 创建PendingIntent,指向笔记列表活动。
|
|
|
}
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent); // 设置通知的最新事件信息。
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); // 发送通知。
|
|
|
}
|
|
|
@Override
|
|
|
protected Integer doInBackground(Void... unused) { // 重写doInBackground方法,执行后台任务。
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(mContext))); // 发布登录进度。
|
|
|
return mTaskManager.sync(mContext, this); // 执行同步操作并返回结果。
|
|
|
}
|
|
|
@Override
|
|
|
protected void onProgressUpdate(String... progress) { // 重写onProgressUpdate方法,处理进度更新。
|
|
|
showNotification(R.string.ticker_syncing, progress[0]); // 显示同步中的通知。
|
|
|
if (mContext instanceof GTaskSyncService) { // 如果上下文是GTaskSyncService的实例。
|
|
|
((GTaskSyncService) mContext).sendBroadcast(progress[0]); // 发送进度广播。
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
protected void onPostExecute(Integer result) { // 重写onPostExecute方法,处理任务执行完毕后的操作。
|
|
|
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() { // 运行任务完成监听器的onComplete方法。
|
|
|
mOnCompleteListener.onComplete(); // 调用onComplete方法。
|
|
|
}
|
|
|
}).start(); // 启动线程。
|
|
|
}
|
|
|
}
|
|
|
} |