main
zhangzhuwei 7 months ago
parent ee2aedb5cf
commit 5c2c3cf910

@ -1,4 +1,3 @@
/* /*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
@ -6,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -15,109 +14,132 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.gtask.remote; package net.micode.notes.gtask.remote;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesListActivity;
import net.micode.notes.ui.NotesPreferenceActivity; import net.micode.notes.ui.NotesPreferenceActivity;
/**
public class GTaskASyncTask extends AsyncTask<Void, String, Integer> { * Google
*/
private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
public interface OnCompleteListener { private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; // 同步通知的ID
void onComplete();
} /**
*
private Context mContext; */
public interface OnCompleteListener {
private NotificationManager mNotifiManager; void onComplete(); // 当同步完成时调用
}
private GTaskManager mTaskManager;
private Context mContext; // 应用程序上下文
private OnCompleteListener mOnCompleteListener; private NotificationManager mNotifiManager; // 通知管理器
private GTaskManager mTaskManager; // Google任务管理器
public GTaskASyncTask(Context context, OnCompleteListener listener) { private OnCompleteListener mOnCompleteListener; // 完成监听器
mContext = context;
mOnCompleteListener = listener; /**
mNotifiManager = (NotificationManager) mContext *
.getSystemService(Context.NOTIFICATION_SERVICE); * @param context
mTaskManager = GTaskManager.getInstance(); * @param listener
} */
public GTaskASyncTask(Context context, OnCompleteListener listener) {
public void cancelSync() { mContext = context;
mTaskManager.cancelSync(); mOnCompleteListener = listener;
} mNotifiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mTaskManager = GTaskManager.getInstance();
public void publishProgess(String message) { }
publishProgress(new String[] {
message /**
}); *
} */
public void cancelSync() {
private void showNotification(int tickerId, String content) { mTaskManager.cancelSync();
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; * @param message
if (tickerId != R.string.ticker_success) { */
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, public void publishProgess(String message) {
NotesPreferenceActivity.class), 0); publishProgress(new String[] { message });
}
} else {
pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, /**
NotesListActivity.class), 0); *
} * @param tickerId ID
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, * @param content
pendingIntent); */
mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); private void showNotification(int tickerId, String content) {
} Notification notification = new Notification(R.drawable.notification, mContext.getString(tickerId), System.currentTimeMillis());
notification.defaults = Notification.DEFAULT_LIGHTS; // 设置默认灯光
@Override notification.flags = Notification.FLAG_AUTO_CANCEL; // 设置通知自动取消
protected Integer doInBackground(Void... unused) { PendingIntent pendingIntent; // 待决意图
publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
.getSyncAccountName(mContext))); // 根据tickerId设置不同的待决意图
return mTaskManager.sync(mContext, this); if (tickerId != R.string.ticker_success) {
} pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesPreferenceActivity.class), 0);
} else {
@Override pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0);
protected void onProgressUpdate(String... progress) { }
showNotification(R.string.ticker_syncing, progress[0]); notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent); // 设置通知信息
if (mContext instanceof GTaskSyncService) { mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); // 显示通知
((GTaskSyncService) mContext).sendBroadcast(progress[0]); }
}
} /**
*
@Override * @return
protected void onPostExecute(Integer result) { */
if (result == GTaskManager.STATE_SUCCESS) { @Override
showNotification(R.string.ticker_success, mContext.getString( protected Integer doInBackground(Void... unused) {
R.string.success_sync_account, mTaskManager.getSyncAccount())); publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity.getSyncAccountName(mContext)));
NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); return mTaskManager.sync(mContext, this); // 同步任务
} 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) { * @param progress
showNotification(R.string.ticker_cancel, mContext */
.getString(R.string.error_sync_cancelled)); @Override
} protected void onProgressUpdate(String... progress) {
if (mOnCompleteListener != null) { showNotification(R.string.ticker_syncing, progress[0]); // 显示同步中的通知
new Thread(new Runnable() { if (mContext instanceof GTaskSyncService) {
((GTaskSyncService) mContext).sendBroadcast(progress[0]); // 如果上下文是GTaskSyncService发送广播
public void run() { }
mOnCompleteListener.onComplete(); }
}
}).start(); /**
} *
} * @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()));
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));
}
// 如果设置了完成监听器则在新线程中调用onComplete方法
if (mOnCompleteListener != null) {
new Thread(new Runnable() {
public void run() {
mOnCompleteListener.onComplete();
}
}).start();
}
}
}

@ -62,34 +62,43 @@ import java.util.zip.InflaterInputStream;
public class GTaskClient { public class GTaskClient {
// 类名的简写,用于日志输出
private static final String TAG = GTaskClient.class.getSimpleName(); private static final String TAG = GTaskClient.class.getSimpleName();
// Google任务的URL地址
private static final String GTASK_URL = "https://mail.google.com/tasks/"; private static final String GTASK_URL = "https://mail.google.com/tasks/";
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
// 单例对象
private static GTaskClient mInstance = null; private static GTaskClient mInstance = null;
// HTTP客户端
private DefaultHttpClient mHttpClient; private DefaultHttpClient mHttpClient;
// 获取和发布任务的URL
private String mGetUrl; private String mGetUrl;
private String mPostUrl; private String mPostUrl;
// 客户端版本号
private long mClientVersion; private long mClientVersion;
// 登录状态
private boolean mLoggedin; private boolean mLoggedin;
// 上次登录时间
private long mLastLoginTime; private long mLastLoginTime;
// 动作ID
private int mActionId; private int mActionId;
// 账户信息
private Account mAccount; private Account mAccount;
// 更新数组
private JSONArray mUpdateArray; private JSONArray mUpdateArray;
// 私有构造函数,防止外部直接创建对象
private GTaskClient() { private GTaskClient() {
mHttpClient = null; mHttpClient = null;
mGetUrl = GTASK_GET_URL; mGetUrl = GTASK_GET_URL;
@ -102,6 +111,7 @@ public class GTaskClient {
mUpdateArray = null; mUpdateArray = null;
} }
// 获取单例对象
public static synchronized GTaskClient getInstance() { public static synchronized GTaskClient getInstance() {
if (mInstance == null) { if (mInstance == null) {
mInstance = new GTaskClient(); mInstance = new GTaskClient();
@ -109,34 +119,37 @@ public class GTaskClient {
return mInstance; return mInstance;
} }
// 登录方法
public boolean login(Activity activity) { public boolean login(Activity activity) {
// we suppose that the cookie would expire after 5 minutes // 假设cookie会在5分钟后过期需要重新登录
// then we need to re-login
final long interval = 1000 * 60 * 5; final long interval = 1000 * 60 * 5;
if (mLastLoginTime + interval < System.currentTimeMillis()) { if (mLastLoginTime + interval < System.currentTimeMillis()) {
mLoggedin = false; mLoggedin = false;
} }
// need to re-login after account switch // 切换账户后需要重新登录
if (mLoggedin if (mLoggedin
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
.getSyncAccountName(activity))) { .getSyncAccountName(activity))) {
mLoggedin = false; mLoggedin = false;
} }
// 如果已经登录,则直接返回
if (mLoggedin) { if (mLoggedin) {
Log.d(TAG, "already logged in"); Log.d(TAG, "already logged in");
return true; return true;
} }
// 更新登录时间
mLastLoginTime = System.currentTimeMillis(); mLastLoginTime = System.currentTimeMillis();
// 获取Google账户的认证令牌
String authToken = loginGoogleAccount(activity, false); String authToken = loginGoogleAccount(activity, false);
if (authToken == null) { if (authToken == null) {
Log.e(TAG, "login google account failed"); Log.e(TAG, "login google account failed");
return false; return false;
} }
// login with custom domain if necessary // 如果账户不是Gmail或Googlemail则尝试使用自定义域名登录
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
.endsWith("googlemail.com"))) { .endsWith("googlemail.com"))) {
StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
@ -146,12 +159,13 @@ public class GTaskClient {
mGetUrl = url.toString() + "ig"; mGetUrl = url.toString() + "ig";
mPostUrl = url.toString() + "r/ig"; mPostUrl = url.toString() + "r/ig";
// 尝试使用自定义域名登录Google任务
if (tryToLoginGtask(activity, authToken)) { if (tryToLoginGtask(activity, authToken)) {
mLoggedin = true; mLoggedin = true;
} }
} }
// try to login with google official url // 如果登录失败则尝试使用Google官方URL登录
if (!mLoggedin) { if (!mLoggedin) {
mGetUrl = GTASK_GET_URL; mGetUrl = GTASK_GET_URL;
mPostUrl = GTASK_POST_URL; mPostUrl = GTASK_POST_URL;
@ -160,426 +174,502 @@ public class GTaskClient {
} }
} }
// 登录成功
mLoggedin = true; mLoggedin = true;
return true; return true;
} }
// 获取Google账户的认证令牌
private String loginGoogleAccount(Activity activity, boolean invalidateToken) { private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
String authToken; String authToken;
// 获取账户管理器
AccountManager accountManager = AccountManager.get(activity); AccountManager accountManager = AccountManager.get(activity);
// 获取所有Google账户
Account[] accounts = accountManager.getAccountsByType("com.google"); Account[] accounts = accountManager.getAccountsByType("com.google");
// 如果没有Google账户则返回null
if (accounts.length == 0) { if (accounts.length == 0) {
Log.e(TAG, "there is no available google account"); Log.e(TAG, "there is no available google account");
return null; return null;
} }
// 获取同步账户的名称
String accountName = NotesPreferenceActivity.getSyncAccountName(activity); String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
Account account = null; Account account = null;
// 查找与设置中账户名称相同的账户
for (Account a : accounts) { for (Account a : accounts) {
if (a.name.equals(accountName)) { if (a.name.equals(accountName)) {
account = a; account = a;
break; break;
} }
} }
// 如果找到账户,则保存账户信息
if (account != null) { if (account != null) {
mAccount = account; mAccount = account;
} else { } else {
// 如果没有找到账户则返回null
Log.e(TAG, "unable to get an account with the same name in the settings"); Log.e(TAG, "unable to get an account with the same name in the settings");
return null; return null;
} }
// get the token now // 获取认证令牌
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account, AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
"goanna_mobile", null, activity, null, null); "goanna_mobile", null, activity, null, null);
try { try {
// 获取认证令牌的Bundle对象
Bundle authTokenBundle = accountManagerFuture.getResult(); Bundle authTokenBundle = accountManagerFuture.getResult();
// 获取认证令牌
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
// 如果需要,则使令牌无效并重新登录
if (invalidateToken) { if (invalidateToken) {
accountManager.invalidateAuthToken("com.google", authToken); accountManager.invalidateAuthToken("com.google", authToken);
loginGoogleAccount(activity, false); loginGoogleAccount(activity, false);
} }
} catch (Exception e) { } catch (Exception e) {
// 如果获取认证令牌失败则返回null
Log.e(TAG, "get auth token failed"); Log.e(TAG, "get auth token failed");
authToken = null; authToken = null;
} }
return authToken; return authToken;
} }
}
private boolean tryToLoginGtask(Activity activity, String authToken) { // 尝试使用给定的认证令牌登录Google任务
if (!loginGtask(authToken)) { private boolean tryToLoginGtask(Activity activity, String authToken) {
// maybe the auth token is out of date, now let's invalidate the // 如果使用给定的认证令牌登录失败
// token and try again if (!loginGtask(authToken)) {
authToken = loginGoogleAccount(activity, true); // 可能认证令牌已过期,现在使令牌无效并再次尝试登录
if (authToken == null) { authToken = loginGoogleAccount(activity, true);
Log.e(TAG, "login google account failed"); // 如果获取认证令牌失败
return false; if (authToken == null) {
} Log.e(TAG, "login google account failed");
return false;
if (!loginGtask(authToken)) {
Log.e(TAG, "login gtask failed");
return false;
}
} }
return true;
}
private boolean loginGtask(String authToken) {
int timeoutConnection = 10000;
int timeoutSocket = 15000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
mHttpClient = new DefaultHttpClient(httpParameters);
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
mHttpClient.setCookieStore(localBasicCookieStore);
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
// login gtask
try {
String loginUrl = mGetUrl + "?auth=" + authToken;
HttpGet httpGet = new HttpGet(loginUrl);
HttpResponse response = null;
response = mHttpClient.execute(httpGet);
// get the cookie now
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
boolean hasAuthCookie = false;
for (Cookie cookie : cookies) {
if (cookie.getName().contains("GTL")) {
hasAuthCookie = true;
}
}
if (!hasAuthCookie) {
Log.w(TAG, "it seems that there is no auth cookie");
}
// get the client version // 如果使用新的认证令牌登录失败
String resString = getResponseContent(response.getEntity()); if (!loginGtask(authToken)) {
String jsBegin = "_setup("; Log.e(TAG, "login gtask failed");
String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin);
int end = resString.lastIndexOf(jsEnd);
String jsString = null;
if (begin != -1 && end != -1 && begin < end) {
jsString = resString.substring(begin + jsBegin.length(), end);
}
JSONObject js = new JSONObject(jsString);
mClientVersion = js.getLong("v");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return false;
} catch (Exception e) {
// simply catch all exceptions
Log.e(TAG, "httpget gtask_url failed");
return false; return false;
} }
return true;
}
private int getActionId() {
return mActionId++;
} }
return true;
}
private HttpPost createHttpPost() { // 使用给定的认证令牌登录Google任务
HttpPost httpPost = new HttpPost(mPostUrl); private boolean loginGtask(String authToken) {
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 设置连接超时和套接字超时的时间
httpPost.setHeader("AT", "1"); int timeoutConnection = 10000;
return httpPost; int timeoutSocket = 15000;
} HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
private String getResponseContent(HttpEntity entity) throws IOException { HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
String contentEncoding = null; // 创建HTTP客户端
if (entity.getContentEncoding() != null) { mHttpClient = new DefaultHttpClient(httpParameters);
contentEncoding = entity.getContentEncoding().getValue(); // 创建本地Cookie存储
Log.d(TAG, "encoding: " + contentEncoding); BasicCookieStore localBasicCookieStore = new BasicCookieStore();
} mHttpClient.setCookieStore(localBasicCookieStore);
// 设置HTTP协议参数
InputStream input = entity.getContent(); HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
input = new GZIPInputStream(entity.getContent()); // 登录Google任务
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { try {
Inflater inflater = new Inflater(true); // 构建登录URL
input = new InflaterInputStream(entity.getContent(), inflater); String loginUrl = mGetUrl + "?auth=" + authToken;
} HttpGet httpGet = new HttpGet(loginUrl);
// 执行HTTP GET请求
try { HttpResponse response = mHttpClient.execute(httpGet);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr); // 获取Cookie
StringBuilder sb = new StringBuilder(); List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
boolean hasAuthCookie = false;
while (true) { for (Cookie cookie : cookies) {
String buff = br.readLine(); // 检查是否存在认证Cookie
if (buff == null) { if (cookie.getName().contains("GTL")) {
return sb.toString(); hasAuthCookie = true;
}
sb = sb.append(buff);
} }
} finally {
input.close();
} }
} if (!hasAuthCookie) {
Log.w(TAG, "it seems that there is no auth cookie");
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
if (!mLoggedin) {
Log.e(TAG, "please login first");
throw new ActionFailureException("not logged in");
} }
HttpPost httpPost = createHttpPost(); // 获取客户端版本
try { String resString = getResponseContent(response.getEntity());
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>(); String jsBegin = "_setup(";
list.add(new BasicNameValuePair("r", js.toString())); String jsEnd = ")}</script>";
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); int begin = resString.indexOf(jsBegin);
httpPost.setEntity(entity); int end = resString.lastIndexOf(jsEnd);
String jsString = null;
// execute the post if (begin != -1 && end != -1 && begin < end) {
HttpResponse response = mHttpClient.execute(httpPost); jsString = resString.substring(begin + jsBegin.length(), end);
String jsString = getResponseContent(response.getEntity());
return new JSONObject(jsString);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("postRequest failed");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("unable to convert response content to jsonobject");
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("error occurs when posting request");
} }
JSONObject js = new JSONObject(jsString);
mClientVersion = js.getLong("v");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return false;
} catch (Exception e) {
// 捕获所有异常
Log.e(TAG, "httpget gtask_url failed");
return false;
} }
public void createTask(Task task) throws NetworkFailureException { return true;
commitUpdate(); }
try {
JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
// action_list
actionList.put(task.getCreateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version // 获取动作ID
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); private int getActionId() {
// 返回并递增动作ID
return mActionId++;
}
// post // 创建HTTP POST请求
JSONObject jsResponse = postRequest(jsPost); private HttpPost createHttpPost() {
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( HttpPost httpPost = new HttpPost(mPostUrl);
GTaskStringUtils.GTASK_JSON_RESULTS).get(0); // 设置请求头
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.setHeader("AT", "1");
return httpPost;
}
} catch (JSONException e) { // 获取HTTP响应的内容
Log.e(TAG, e.toString()); private String getResponseContent(HttpEntity entity) throws IOException {
e.printStackTrace(); String contentEncoding = null;
throw new ActionFailureException("create task: handing jsonobject failed"); // 获取内容编码
} if (entity.getContentEncoding() != null) {
contentEncoding = entity.getContentEncoding().getValue();
Log.d(TAG, "encoding: " + contentEncoding);
} }
public void createTaskList(TaskList tasklist) throws NetworkFailureException { InputStream input = entity.getContent();
commitUpdate(); // 根据内容编码解压缩响应内容
try { if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
JSONObject jsPost = new JSONObject(); input = new GZIPInputStream(entity.getContent());
JSONArray actionList = new JSONArray(); } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
Inflater inflater = new Inflater(true);
// action_list input = new InflaterInputStream(entity.getContent(), inflater);
actionList.put(tasklist.getCreateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client version
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// post
JSONObject jsResponse = postRequest(jsPost);
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("create tasklist: handing jsonobject failed");
}
} }
public void commitUpdate() throws NetworkFailureException { try {
if (mUpdateArray != null) { // 读取响应内容
try { InputStreamReader isr = new InputStreamReader(input);
JSONObject jsPost = new JSONObject(); BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
// action_list while (true) {
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); String buff = br.readLine();
if (buff == null) {
// client_version return sb.toString();
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
postRequest(jsPost);
mUpdateArray = null;
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("commit update: handing jsonobject failed");
} }
sb = sb.append(buff);
} }
} finally {
// 关闭输入流
input.close();
} }
}
public void addUpdateNode(Node node) throws NetworkFailureException { // 发送POST请求
if (node != null) { private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
// too many update items may result in an error // 如果未登录,则抛出异常
// set max to 10 items if (!mLoggedin) {
if (mUpdateArray != null && mUpdateArray.length() > 10) { Log.e(TAG, "please login first");
commitUpdate(); throw new ActionFailureException("not logged in");
}
if (mUpdateArray == null)
mUpdateArray = new JSONArray();
mUpdateArray.put(node.getUpdateAction(getActionId()));
}
} }
public void moveTask(Task task, TaskList preParent, TaskList curParent) // 创建HTTP POST请求
throws NetworkFailureException { HttpPost httpPost = createHttpPost();
commitUpdate(); try {
try { // 创建参数列表
JSONObject jsPost = new JSONObject(); LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
JSONArray actionList = new JSONArray(); list.add(new BasicNameValuePair("r", js.toString()));
JSONObject action = new JSONObject(); // 创建URL编码的表单实体
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
// action_list httpPost.setEntity(entity);
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); // 执行POST请求
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); HttpResponse response = mHttpClient.execute(httpPost);
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); // 获取响应内容
if (preParent == curParent && task.getPriorSibling() != null) { String jsString = getResponseContent(response.getEntity());
// put prioring_sibing_id only if moving within the tasklist and // 将响应内容转换为JSON对象
// it is not the first one return new JSONObject(jsString);
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
} } catch (ClientProtocolException e) {
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); Log.e(TAG, e.toString());
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); e.printStackTrace();
if (preParent != curParent) { throw new NetworkFailureException("postRequest failed");
// put the dest_list only if moving between tasklists } catch (IOException e) {
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); Log.e(TAG, e.toString());
} e.printStackTrace();
actionList.put(action); throw new NetworkFailureException("postRequest failed");
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); } catch (JSONException e) {
Log.e(TAG, e.toString());
// client_version e.printStackTrace();
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); throw new ActionFailureException("unable to convert response content to jsonobject");
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("error occurs when posting request");
}
}
postRequest(jsPost); // 创建任务
public void createTask(Task task) throws NetworkFailureException {
// 提交更新
commitUpdate();
try {
// 创建JSON对象
JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
// 添加创建任务的动作
actionList.put(task.getCreateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// 设置客户端版本
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// 发送POST请求
JSONObject jsResponse = postRequest(jsPost);
// 获取响应结果
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
// 设置任务的GID
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("create task: handing jsonobject failed");
}
}
} catch (JSONException e) { // 创建任务列表
Log.e(TAG, e.toString()); public void createTaskList(TaskList tasklist) throws NetworkFailureException {
e.printStackTrace(); // 提交更新
throw new ActionFailureException("move task: handing jsonobject failed"); commitUpdate();
} try {
// 创建JSON对象
JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
// 添加创建任务列表的动作
actionList.put(tasklist.getCreateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// 设置客户端版本
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// 发送POST请求
JSONObject jsResponse = postRequest(jsPost);
// 获取响应结果
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
// 设置任务列表的GID
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("create tasklist: handing jsonobject failed");
} }
}
public void deleteNode(Node node) throws NetworkFailureException { // 提交更新
commitUpdate(); public void commitUpdate() throws NetworkFailureException {
// 如果存在更新数组
if (mUpdateArray != null) {
try { try {
// 创建JSON对象
JSONObject jsPost = new JSONObject(); JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
// action_list // 添加更新动作列表
node.setDeleted(true); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
actionList.put(node.getUpdateAction(getActionId()));
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// client_version // 设置客户端版本
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// 发送POST请求
postRequest(jsPost); postRequest(jsPost);
// 清空更新数组
mUpdateArray = null; mUpdateArray = null;
} catch (JSONException e) { } catch (JSONException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
e.printStackTrace(); e.printStackTrace();
throw new ActionFailureException("delete node: handing jsonobject failed"); throw new ActionFailureException("commit update: handing jsonobject failed");
} }
} }
}
public JSONArray getTaskLists() throws NetworkFailureException { // 添加更新节点
if (!mLoggedin) { public void addUpdateNode(Node node) throws NetworkFailureException {
Log.e(TAG, "please login first"); // 如果节点不为空
throw new ActionFailureException("not logged in"); if (node != null) {
// 如果更新项过多,则提交更新
if (mUpdateArray != null && mUpdateArray.length() > 10) {
commitUpdate();
} }
try { // 如果更新数组为空,则创建新的数组
HttpGet httpGet = new HttpGet(mGetUrl); if (mUpdateArray == null)
HttpResponse response = null; mUpdateArray = new JSONArray();
response = mHttpClient.execute(httpGet); // 添加更新动作
mUpdateArray.put(node.getUpdateAction(getActionId()));
// get the task list }
String resString = getResponseContent(response.getEntity()); }
String jsBegin = "_setup(";
String jsEnd = ")}</script>"; // 移动任务
int begin = resString.indexOf(jsBegin); public void moveTask(Task task, TaskList preParent, TaskList curParent)
int end = resString.lastIndexOf(jsEnd); throws NetworkFailureException {
String jsString = null; // 提交更新
if (begin != -1 && end != -1 && begin < end) { commitUpdate();
jsString = resString.substring(begin + jsBegin.length(), end); try {
} // 创建JSON对象
JSONObject js = new JSONObject(jsString); JSONObject jsPost = new JSONObject();
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); JSONArray actionList = new JSONArray();
} catch (ClientProtocolException e) { JSONObject action = new JSONObject();
Log.e(TAG, e.toString());
e.printStackTrace(); // 添加移动任务的动作
throw new NetworkFailureException("gettasklists: httpget failed"); action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
} catch (IOException e) { GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
Log.e(TAG, e.toString()); action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
e.printStackTrace(); action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
throw new NetworkFailureException("gettasklists: httpget failed"); if (preParent == curParent && task.getPriorSibling() != null) {
} catch (JSONException e) { // 如果在同一个任务列表中移动且不是第一个则添加前一个兄弟节点的ID
Log.e(TAG, e.toString()); action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
e.printStackTrace();
throw new ActionFailureException("get task lists: handing jasonobject failed");
} }
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
if (preParent != curParent) {
// 如果在不同任务列表之间移动则添加目标列表的ID
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
}
actionList.put(action);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// 设置客户端版本
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// 发送POST请求
postRequest(jsPost);
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("move task: handing jsonobject failed");
} }
}
public JSONArray getTaskList(String listGid) throws NetworkFailureException { // 删除节点
commitUpdate(); public void deleteNode(Node node) throws NetworkFailureException {
try { // 提交所有挂起的更新
JSONObject jsPost = new JSONObject(); commitUpdate();
JSONArray actionList = new JSONArray(); try {
JSONObject action = new JSONObject(); // 创建JSON对象来构建请求体
JSONObject jsPost = new JSONObject();
// action_list JSONArray actionList = new JSONArray();
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); // 设置节点为已删除,并添加更新动作到动作列表中
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); node.setDeleted(true);
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); actionList.put(node.getUpdateAction(getActionId()));
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
actionList.put(action);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 设置客户端版本号
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// client_version
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 发送请求
postRequest(jsPost);
// 清空更新数组
mUpdateArray = null;
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("delete node: handing jsonobject failed");
}
}
JSONObject jsResponse = postRequest(jsPost); // 获取任务列表
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); public JSONArray getTaskLists() throws NetworkFailureException {
} catch (JSONException e) { // 如果未登录,则抛出异常
Log.e(TAG, e.toString()); if (!mLoggedin) {
e.printStackTrace(); Log.e(TAG, "please login first");
throw new ActionFailureException("get task list: handing jsonobject failed"); throw new ActionFailureException("not logged in");
}
} }
public Account getSyncAccount() { try {
return mAccount; // 创建HTTP GET请求
HttpGet httpGet = new HttpGet(mGetUrl);
HttpResponse response = null;
response = mHttpClient.execute(httpGet);
// 获取任务列表
String resString = getResponseContent(response.getEntity());
String jsBegin = "_setup(";
String jsEnd = ")}</script>";
int begin = resString.indexOf(jsBegin);
int end = resString.lastIndexOf(jsEnd);
String jsString = null;
if (begin != -1 && end != -1 && begin < end) {
jsString = resString.substring(begin + jsBegin.length(), end);
}
JSONObject js = new JSONObject(jsString);
// 返回任务列表的JSON数组
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("gettasklists: httpget failed");
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new NetworkFailureException("gettasklists: httpget failed");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("get task lists: handing jasonobject failed");
} }
}
public void resetUpdateArray() { // 获取特定任务列表中的任务
mUpdateArray = null; public JSONArray getTaskList(String listGid) throws NetworkFailureException {
// 提交所有挂起的更新
commitUpdate();
try {
// 创建JSON对象来构建请求体
JSONObject jsPost = new JSONObject();
JSONArray actionList = new JSONArray();
JSONObject action = new JSONObject();
// 构建获取任务的动作
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
actionList.put(action);
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
// 设置客户端版本号
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
// 发送请求并获取响应
JSONObject jsResponse = postRequest(jsPost);
// 返回任务的JSON数组
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("get task list: handing jsonobject failed");
} }
} }
// 获取用于同步的账户
public Account getSyncAccount() {
return mAccount;
}
// 重置更新数组
public void resetUpdateArray() {
mUpdateArray = null;
}

File diff suppressed because it is too large Load Diff

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -14,148 +14,198 @@
* limitations under the License. * limitations under the License.
*/ */
package net.micode.notes.gtask.remote; package net.micode.notes.gtask.remote;
import android.app.Activity; import android.app.Activity;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
/**
* /**
* GTaskSyncService GTask
*
* @ProjectName: minode * @ProjectName: minode
* @Package: net.micode.notes.gtask.remote * @Package: net.micode.notes.gtask.remote
* @ClassName: GTaskSyncService * @ClassName: GTaskSyncService
* @Description: Gtask * @Description: GTask
* @Author: * @Author:
* @Date: 2023-12-17 23:38 * @Date: 2023-12-17 23:38
*/ */
public class GTaskSyncService extends Service { public class GTaskSyncService extends Service {
public final static String ACTION_STRING_NAME = "sync_action_type"; // 定义广播的 Action 字符串
public final static String ACTION_STRING_NAME = "sync_action_type";
public final static int ACTION_START_SYNC = 0;
// 定义同步操作的类型常量
public final static int ACTION_CANCEL_SYNC = 1; public final static int ACTION_START_SYNC = 0;
public final static int ACTION_CANCEL_SYNC = 1;
public final static int ACTION_INVALID = 2; public final static int ACTION_INVALID = 2;
public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service"; // 定义广播的名称
public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service";
public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";
// 定义广播的额外信息键
public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg"; public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";
public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg";
private static GTaskASyncTask mSyncTask = null;
// 同步任务对象
private static String mSyncProgress = ""; private static GTaskASyncTask mSyncTask = null;
// 同步进度字符串
private static String mSyncProgress = "";
/** /**
* @method startSync * startSync GTask
* @description Gtask *
* @date: 2023-12-17 23:42 * @date: 2023-12-17 23:42
* @author: * @author:
* @return void
*/ */
private void startSync() { private void startSync() {
if (mSyncTask == null) { // 如果同步任务为空,则创建并启动同步任务
mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { if (mSyncTask == null) {
public void onComplete() { mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
mSyncTask = null; public void onComplete() {
sendBroadcast(""); // 同步完成时,将同步任务置空并发送广播通知
stopSelf(); mSyncTask = null;
} sendBroadcast("");
}); stopSelf();
sendBroadcast(""); }
// 使同步任务以单线程队列方式开启运行 });
// 2023-12-17 23:46 // 发送广播通知同步开始
mSyncTask.execute(); sendBroadcast("");
} // 以单线程队列方式启动同步任务
} mSyncTask.execute();
}
}
/** /**
* @method cancelSync * cancelSync GTask
* @description Gtask *
* @date: 2023-12-17 23:49 * @date: 2023-12-17 23:49
* @author: * @author:
* @return void
*/ */
private void cancelSync() { private void cancelSync() {
if (mSyncTask != null) { // 如果同步任务不为空,则取消同步任务
mSyncTask.cancelSync(); if (mSyncTask != null) {
} mSyncTask.cancelSync();
} }
}
@Override
public void onCreate() { @Override
mSyncTask = null; public void onCreate() {
} // 在服务创建时,将同步任务置空
mSyncTask = null;
}
/** /**
* @method onStartCommand * onStartCommand 使 GTaskSyncService
* @description 使GtaskSyncService *
* @date: 2023-12-17 23:53 * @date: 2023-12-17 23:53
* @author: * @author:
* @return int
*/ */
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Bundle bundle = intent.getExtras(); // 获取传递过来的 Bundle 对象
if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { Bundle bundle = intent.getExtras();
switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { // 如果 Bundle 对象不为空且包含同步操作类型键,则根据操作类型执行相应操作
// 开始同步或取消同步 if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {
// 2023-12-17 23:52 switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) {
case ACTION_START_SYNC: // 开始同步
startSync(); case ACTION_START_SYNC:
break; startSync();
case ACTION_CANCEL_SYNC: break;
cancelSync(); // 取消同步
break; case ACTION_CANCEL_SYNC:
default: cancelSync();
break; break;
} default:
return START_STICKY; break;
} }
return super.onStartCommand(intent, flags, startId); // 返回 START_STICKY表示如果服务被杀死系统会尝试重新创建服务
} return START_STICKY;
}
@Override // 如果 Bundle 对象为空或不包含同步操作类型键,则调用父类的 onStartCommand 方法
public void onLowMemory() { return super.onStartCommand(intent, flags, startId);
if (mSyncTask != null) { }
mSyncTask.cancelSync();
} @Override
} public void onLowMemory() {
// 在内存不足时,取消同步任务
public IBinder onBind(Intent intent) { if (mSyncTask != null) {
return null; mSyncTask.cancelSync();
} }
}
public void sendBroadcast(String msg) {
mSyncProgress = msg; public IBinder onBind(Intent intent) {
Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); // 返回 null表示不支持绑定
intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); return null;
intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); }
sendBroadcast(intent);
} /**
* sendBroadcast 广
public static void startSync(Activity activity) { *
GTaskManager.getInstance().setActivityContext(activity); * @param msg
Intent intent = new Intent(activity, GTaskSyncService.class); */
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); public void sendBroadcast(String msg) {
activity.startService(intent); // 更新同步进度字符串
} mSyncProgress = msg;
// 创建 Intent 对象
public static void cancelSync(Context context) { Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME);
Intent intent = new Intent(context, GTaskSyncService.class); // 将是否正在同步和同步进度消息作为额外信息添加到 Intent 对象中
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null);
context.startService(intent); intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg);
} // 发送广播通知
sendBroadcast(intent);
public static boolean isSyncing() { }
return mSyncTask != null;
} /**
* startSync GTask
public static String getProgressString() { *
return mSyncProgress; * @param activity Activity
} */
} public static void startSync(Activity activity) {
// 设置 Activity 上下文
GTaskManager.getInstance().setActivityContext(activity);
// 创建 Intent 对象
Intent intent = new Intent(activity, GTaskSyncService.class);
// 将同步操作类型设置为开始同步
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC);
// 启动服务
activity.startService(intent);
}
/**
* cancelSync GTask
*
* @param context Context
*/
public static void cancelSync(Context context) {
// 创建 Intent 对象
Intent intent = new Intent(context, GTaskSyncService.class);
// 将同步操作类型设置为取消同步
intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC);
// 启动服务
context.startService(intent);
}
/**
* isSyncing
*
* @return
*/
public static boolean isSyncing() {
// 如果同步任务不为空,则表示正在同步
return mSyncTask != null;
}
/**
* getProgressString
*
* @return
*/
public static String getProgressString() {
// 返回同步进度字符串
return mSyncProgress;
}
}
Loading…
Cancel
Save