|
|
|
@ -14,7 +14,6 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
@ -28,67 +27,86 @@ import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.ui.NotesListActivity;
|
|
|
|
|
import net.micode.notes.ui.NotesPreferenceActivity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 异步任务类,用于处理Google任务(GTask)的同步操作。
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
|
|
|
|
|
// 定义同步通知的ID
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化上下文、通知管理器、任务管理器和回调接口。
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param listener 回调接口
|
|
|
|
|
*/
|
|
|
|
|
public GTaskASyncTask(Context context, OnCompleteListener listener) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mOnCompleteListener = listener;
|
|
|
|
|
mNotifiManager = (NotificationManager) mContext
|
|
|
|
|
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
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 notification = new Notification(R.drawable.notification, mContext.getString(tickerId), System.currentTimeMillis());
|
|
|
|
|
notification.defaults = Notification.DEFAULT_LIGHTS;
|
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL;
|
|
|
|
|
PendingIntent pendingIntent;
|
|
|
|
|
if (tickerId != R.string.ticker_success) {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesPreferenceActivity.class), 0);
|
|
|
|
|
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0);
|
|
|
|
|
} else {
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
|
|
|
|
|
NotesListActivity.class), 0);
|
|
|
|
|
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0);
|
|
|
|
|
}
|
|
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
|
|
|
|
|
pendingIntent);
|
|
|
|
|
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent);
|
|
|
|
|
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后台执行同步操作的方法。
|
|
|
|
|
* @param unused 未使用的参数
|
|
|
|
|
* @return 同步操作的结果
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(mContext)));
|
|
|
|
|
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(mContext)));
|
|
|
|
|
return mTaskManager.sync(mContext, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 进度更新时调用的方法,用于显示进度通知。
|
|
|
|
|
* @param progress 进度数组
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onProgressUpdate(String... progress) {
|
|
|
|
|
showNotification(R.string.ticker_syncing, progress[0]);
|
|
|
|
@ -97,23 +115,24 @@ public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步操作完成后调用的方法,用于处理同步结果并通知。
|
|
|
|
|
* @param result 同步操作的结果
|
|
|
|
|
*/
|
|
|
|
|
@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()));
|
|
|
|
|
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));
|
|
|
|
|
showNotification(R.string.ticker_cancel, mContext.getString(R.string.error_sync_cancelled));
|
|
|
|
|
}
|
|
|
|
|
if (mOnCompleteListener != null) {
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
mOnCompleteListener.onComplete();
|
|
|
|
|
}
|
|
|
|
|