|
|
|
|
@ -62,35 +62,39 @@ import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GTaskClient {
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// 定义日志TAG,方便调试输出
|
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
// 定义Google Tasks服务的基本URL
|
|
|
|
|
private static final String GTASK_URL = "https://mail.google.com/tasks/";
|
|
|
|
|
|
|
|
|
|
// 定义获取任务信息的GET请求URL
|
|
|
|
|
private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
|
|
|
|
|
|
|
|
|
|
// 定义提交任务更新等POST请求URL
|
|
|
|
|
private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
|
|
|
|
|
|
|
|
|
|
// 单例模式,确保全局只有一个GTaskClient实例
|
|
|
|
|
private static GTaskClient mInstance = null;
|
|
|
|
|
|
|
|
|
|
// HTTP客户端实例,用于发起网络请求
|
|
|
|
|
private DefaultHttpClient mHttpClient;
|
|
|
|
|
|
|
|
|
|
// 存储GET请求的实际URL
|
|
|
|
|
private String mGetUrl;
|
|
|
|
|
|
|
|
|
|
// 存储POST请求的实际URL
|
|
|
|
|
private String mPostUrl;
|
|
|
|
|
|
|
|
|
|
// 客户端版本号
|
|
|
|
|
private long mClientVersion;
|
|
|
|
|
|
|
|
|
|
// 登录状态标志位,标识是否已登录
|
|
|
|
|
private boolean mLoggedin;
|
|
|
|
|
|
|
|
|
|
// 最后登录时间戳
|
|
|
|
|
private long mLastLoginTime;
|
|
|
|
|
|
|
|
|
|
// 行动(操作)ID,用于跟踪每个请求的唯一性
|
|
|
|
|
private int mActionId;
|
|
|
|
|
|
|
|
|
|
// Google账户对象,当前正在使用的同步账户
|
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
// 用于缓存待更新的任务数据的JSONArray
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
|
|
// 私有构造函数,防止外部直接实例化
|
|
|
|
|
private GTaskClient() {
|
|
|
|
|
// 初始化各个成员变量
|
|
|
|
|
mHttpClient = null;
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
|
@ -102,40 +106,45 @@ public class GTaskClient {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 静态工厂方法,获取GTaskClient的单例
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
|
// 如果实例尚未创建,则创建并返回一个新的GTaskClient实例
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
|
}
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录方法,尝试登录Google账户并验证GTask服务访问权限
|
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
|
// then we need to re-login
|
|
|
|
|
// 判断上次登录时间是否超过5分钟,若超过则需要重新登录
|
|
|
|
|
final long interval = 1000 * 60 * 5;
|
|
|
|
|
if (mLastLoginTime + interval < System.currentTimeMillis()) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断是否切换了同步账户,如果切换则需重新登录
|
|
|
|
|
// need to re-login after account switch
|
|
|
|
|
if (mLoggedin
|
|
|
|
|
&& !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
.getSyncAccountName(activity))) {
|
|
|
|
|
mLoggedin = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果已登录则直接返回true
|
|
|
|
|
if (mLoggedin) {
|
|
|
|
|
Log.d(TAG, "already logged in");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新最后登录时间
|
|
|
|
|
mLastLoginTime = System.currentTimeMillis();
|
|
|
|
|
// 获取Google账户授权令牌
|
|
|
|
|
String authToken = loginGoogleAccount(activity, false);
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 对于非Gmail或Googlemail域的账户,尝试使用自定义域名登录GTask服务
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase()
|
|
|
|
|
.endsWith("googlemail.com"))) {
|
|
|
|
|
@ -151,6 +160,7 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 若未登录成功,则尝试使用官方URL登录GTask服务
|
|
|
|
|
// try to login with google official url
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
|
@ -159,47 +169,56 @@ public class GTaskClient {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录成功,设置登录状态为已登录
|
|
|
|
|
mLoggedin = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 辅助方法:从Google账户获取授权令牌
|
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
String authToken;
|
|
|
|
|
// 获取AccountManager实例,用于管理账户认证
|
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
// 获取所有类型为"com.google"的账户
|
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
|
|
// 检查是否存在可用的Google账户
|
|
|
|
|
if (accounts.length == 0) {
|
|
|
|
|
Log.e(TAG, "there is no available google account");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取用户在设置中选择的同步账户名
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
|
Account account = null;
|
|
|
|
|
// 查找与设置中所选账户名相匹配的账户
|
|
|
|
|
for (Account a : accounts) {
|
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
|
account = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果找到匹配的账户,则设置当前账户为找到的账户
|
|
|
|
|
if (account != null) {
|
|
|
|
|
mAccount = account;
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Log.e(TAG, "unable to get an account with the same name in the settings");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从账户管理器中获取授权令牌
|
|
|
|
|
// get the token now
|
|
|
|
|
AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
|
|
|
|
|
"goanna_mobile", null, activity, null, null);
|
|
|
|
|
try {
|
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
// 如果需要刷新令牌,则先使当前令牌失效,然后重新获取
|
|
|
|
|
if (invalidateToken) {
|
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "get auth token failed");
|
|
|
|
|
authToken = null;
|
|
|
|
|
}
|
|
|
|
|
@ -207,54 +226,69 @@ public class GTaskClient {
|
|
|
|
|
return authToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这个方法尝试登录GTask服务。首先使用提供的authToken进行登录,如果失败,则尝试刷新并重新登录。
|
|
|
|
|
// 登录成功后返回true,否则返回false。
|
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
|
// 使用给定的authToken尝试登录GTask
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
// 如果登录失败(可能是authToken过期),则尝试通过Google账号刷新并获取新的authToken
|
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
|
// token and try again
|
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
|
// 如果刷新并获取新的authToken失败
|
|
|
|
|
if (authToken == null) {
|
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
|
return false;
|
|
|
|
|
return false;// 返回登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用新的authToken再次尝试登录GTask
|
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
|
Log.e(TAG, "login gtask failed");
|
|
|
|
|
return false;
|
|
|
|
|
Log.e(TAG, "login gtask failed");// 记录错误日志
|
|
|
|
|
return false;// 返回登录失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 登录成功
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这个私有方法用于执行实际的GTask登录过程,根据提供的authToken发送HTTP请求,并检查响应中的认证Cookie是否存在。
|
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
|
// 设置HTTP连接和读取超时时间
|
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters); // 创建一个HTTP客户端实例
|
|
|
|
|
// 创建并设置Cookie存储对象
|
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
|
// 设置HTTP协议参数
|
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
|
try {
|
|
|
|
|
// 构建登录URL并发起GET请求
|
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
|
// 执行HTTP请求并获取响应
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// 检查响应中是否包含认证Cookie
|
|
|
|
|
// get the cookie now
|
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (cookie.getName().contains("GTL")) {
|
|
|
|
|
hasAuthCookie = true;
|
|
|
|
|
hasAuthCookie = true;// 如果找到名为"GTL"的Cookie,则表示存在认证Cookie
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!hasAuthCookie) {
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");
|
|
|
|
|
Log.w(TAG, "it seems that there is no auth cookie");// 若未发现认证Cookie则记录警告日志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从响应内容中解析客户端版本信息
|
|
|
|
|
// get the client version
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
@ -266,166 +300,216 @@ public class GTaskClient {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
mClientVersion = js.getLong("v");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
mClientVersion = js.getLong("v");// 获取并保存客户端版本号
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());// 记录JSON解析异常的日志
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false;// JSON解析出错,返回登录失败
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {// 捕获所有其他可能的异常
|
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
|
return false;
|
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");// 记录HTTP GET请求失败的日志
|
|
|
|
|
return false;// HTTP请求出错,返回登录失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果执行到此处,说明登录过程顺利完成
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这是一个辅助方法,用于生成并返回唯一的动作ID。
|
|
|
|
|
private int getActionId() {
|
|
|
|
|
return mActionId++;
|
|
|
|
|
return mActionId++;// 自增并返回mActionId的当前值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这个私有方法用于创建并配置一个HttpPost对象,指定目标URL和相关HTTP头部信息。
|
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
|
// 创建一个新的HttpPost对象指向预定义的POST请求URL
|
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
|
// 设置HTTP头部Content-Type字段,表明发送的数据格式为application/x-www-form-urlencoded,并采用UTF-8编码
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
|
// 添加自定义HTTP头部信息,这里设定了一个名为"AT"的字段,其值为"1"
|
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
|
// 返回已配置好的HttpPost对象
|
|
|
|
|
return httpPost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这个私有方法用于从HttpEntity中获取并解析响应内容,支持gzip和deflate压缩格式。
|
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
|
// 获取实体的Content-Encoding属性,即数据的压缩编码方式
|
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);// 输出编码方式至日志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取实体内容的输入流
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
// 根据Content-Encoding选择合适的解压方式
|
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
|
// 如果数据被gzip压缩,使用GZIPInputStream解压
|
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
}
|
|
|
|
|
else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
|
// 如果数据被deflate压缩,使用InflaterInputStream解压,注意这里的Inflater构造函数传入true启用ZLIB RFC1950格式
|
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用Reader和BufferedReader逐行读取并拼接响应内容
|
|
|
|
|
try {
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);// 使用UTF-8编码创建StreamReader
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
// 循环读取每一行内容直到结束
|
|
|
|
|
while (true) {
|
|
|
|
|
String buff = br.readLine();
|
|
|
|
|
if (buff == null) {
|
|
|
|
|
// 当读取到最后一行(null)时,返回累积的字符串内容
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
sb = sb.append(buff);
|
|
|
|
|
sb = sb.append(buff);// 将读取到的一行追加到StringBuilder中
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
// 在所有操作结束后确保关闭输入流
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 这个私有方法用于执行一个POST请求并将JSON对象作为请求体发送。若发生网络错误或无法将响应转换为JSON对象,将抛出异常
|
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
|
// 验证用户是否已登录,未登录则抛出自定义异常
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建并初始化HttpPost对象
|
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
|
try {
|
|
|
|
|
// 创建一个包含请求参数的链表,参数名为'r',值为传入的JSON对象的字符串形式
|
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
|
// 创建一个UrlEncodedFormEntity对象,编码格式为UTF-8,用于封装请求参数
|
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
|
// 执行POST请求
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
|
// 从响应中获取内容并转换为JSON对象
|
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
|
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (ClientProtocolException e) {
|
|
|
|
|
// 记录并抛出网络协议错误异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
// 记录并抛出JSON解析错误异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("postRequest failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
// 记录并抛出任何未知错误异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("unable to convert response content to jsonobject");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 公共方法,用于创建一个新的任务
|
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
|
// 提交更新前的操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建一个用于发送的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONArray actionList = new JSONArray();// 创建一个JSONArray存放动作列表
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 将新任务的动作信息添加到action_list中
|
|
|
|
|
actionList.put(task.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// 添加客户端版本信息到JSON对象
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 调用postRequest方法执行POST请求
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
// 从响应的JSON对象中提取结果数组的第一个元素
|
|
|
|
|
JSONObject jsResult = (JSONObject)jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 从响应结果中获取新创建的任务ID,并将其设置到task对象中
|
|
|
|
|
task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
// 记录并抛出JSON处理异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 此公共方法用于创建一个新的任务列表,将任务列表的创建动作添加到请求体中并提交至服务器
|
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
|
// 在更新数据库前进行必要的提交操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
// 创建用于发送请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONArray actionList = new JSONArray();// 创建一个JSONArray来存储动作列表
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将新建任务列表的动作信息加入到动作列表中
|
|
|
|
|
// action_list
|
|
|
|
|
actionList.put(tasklist.getCreateAction(getActionId()));
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);// 将动作列表放入JSON对象
|
|
|
|
|
// 添加客户端版本信息到JSON对象
|
|
|
|
|
// client version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
// post
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
|
|
|
|
|
// 从响应的JSON对象中获取结果数组的第一个元素
|
|
|
|
|
JSONObject jsResult = (JSONObject)jsResponse.getJSONArray(
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
|
|
|
|
|
// 从响应结果中取出新创建的任务列表ID,并设置到tasklist对象中
|
|
|
|
|
tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
// 处理JSON异常并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 此公共方法用于提交当前待更新的动作列表至服务器
|
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
|
// 如果有待提交的更新数组(动作列表)
|
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONObject jsPost = new JSONObject();// 创建用于发送请求的JSON对象
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);// 将待提交的动作列表放入JSON对象
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 添加客户端版本信息到JSON对象
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
postRequest(jsPost);// 发送POST请求
|
|
|
|
|
mUpdateArray = null; // 清空已提交的更新数组
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {// 处理JSON异常并抛出操作失败异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("commit update: handing jsonobject failed");
|
|
|
|
|
@ -433,94 +517,102 @@ public class GTaskClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (node != null) {
|
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {// 此公共方法用于向待更新的动作列表中添加一个节点的更新动作
|
|
|
|
|
if (node != null) {// 如果节点非空
|
|
|
|
|
// 避免一次性更新过多导致错误,限制最多允许10个更新项
|
|
|
|
|
// too many update items may result in an error
|
|
|
|
|
// set max to 10 items
|
|
|
|
|
if (mUpdateArray != null && mUpdateArray.length() > 10) {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
commitUpdate();// 超过10个则先提交现有的更新
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mUpdateArray == null)
|
|
|
|
|
mUpdateArray = new JSONArray();
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
|
mUpdateArray = new JSONArray(); // 如果待更新数组为空,则创建一个新的JSONArray
|
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));// 将节点的更新动作添加到待更新数组中
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
|
throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)// 此方法用于移动一个任务到另一个任务列表中,包括在同一任务列表内调整位置和跨任务列表移动
|
|
|
|
|
throws NetworkFailureException{
|
|
|
|
|
commitUpdate();// 在移动任务之前先提交任何待更新的操作
|
|
|
|
|
try {// 创建用于发送请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 构建动作列表
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
|
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);// 动作类型:移动
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());// 动作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());// 目标任务ID
|
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {// 如果当前任务列表与原父任务列表相同,并且该任务不是第一个子任务,则添加前置兄弟任务ID
|
|
|
|
|
// put prioring_sibing_id only if moving within the tasklist and
|
|
|
|
|
// it is not the first one
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
|
}
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
|
|
|
|
|
if (preParent != curParent) {
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());// 添加原父任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());// 添加目标父任务列表ID
|
|
|
|
|
|
|
|
|
|
if (preParent != curParent) {// 如果任务要从一个任务列表移动到另一个任务列表,则添加目标任务列表ID
|
|
|
|
|
// put the dest_list only if moving between tasklists
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
|
}
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
actionList.put(action);// 将移动动作添加到动作列表中
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 添加客户端版本信息
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 此方法用于删除指定的节点(任务或任务列表)
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
commitUpdate();// 提交任何待更新的操作
|
|
|
|
|
try {// 创建用于发送请求的JSON对象
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
node.setDeleted(true);// 将节点标记为已删除,并将其更新动作添加到动作列表中
|
|
|
|
|
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);// 添加客户端版本信息
|
|
|
|
|
|
|
|
|
|
postRequest(jsPost);
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
postRequest(jsPost);// 发送POST请求
|
|
|
|
|
mUpdateArray = null;// 清空已提交的更新数组
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
|
if (!mLoggedin) {
|
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {// 此方法用于获取所有的任务列表
|
|
|
|
|
if (!mLoggedin) {// 检查用户是否已登录,未登录则抛出异常
|
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
try {// 创建并执行HttpGet请求
|
|
|
|
|
HttpGet httpGet = new HttpGet(mGetUrl);
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
|
// 从响应内容中解析出任务列表数据
|
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
|
@ -530,56 +622,67 @@ public class GTaskClient {
|
|
|
|
|
if (begin != -1 && end != -1 && begin < end) {
|
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
|
}
|
|
|
|
|
// 将解析出的JSON字符串转换为JSONObject,并从中提取任务列表数据
|
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (ClientProtocolException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new NetworkFailureException("gettasklists: httpget failed");
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 此方法用于获取指定任务列表ID下的所有任务
|
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
|
// 在获取任务列表前先提交任何待更新的操作
|
|
|
|
|
commitUpdate();
|
|
|
|
|
try {
|
|
|
|
|
try {// 创建用于发送请求的JSON对象及动作列表
|
|
|
|
|
JSONObject jsPost = new JSONObject();
|
|
|
|
|
JSONArray actionList = new JSONArray();
|
|
|
|
|
JSONObject action = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
|
// 构建动作详情,动作类型为获取全部任务
|
|
|
|
|
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);
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());// 设置动作ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);// 设置要获取的任务列表ID
|
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);// 不获取已删除的任务
|
|
|
|
|
actionList.put(action);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 将动作列表放入JSON请求体
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
|
// 添加客户端版本信息到JSON请求体
|
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
|
|
|
|
|
|
|
|
|
|
// 发送POST请求并获取响应
|
|
|
|
|
JSONObject jsResponse = postRequest(jsPost);
|
|
|
|
|
// 从响应的JSON对象中提取任务列表(tasks)并返回
|
|
|
|
|
return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
}
|
|
|
|
|
catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());// 记录并打印异常信息
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");
|
|
|
|
|
throw new ActionFailureException("get task list: handing jsonobject failed");// 抛出操作失败异常
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
|
return mAccount;
|
|
|
|
|
public Account getSyncAccount() {// 此方法用于获取同步账户对象
|
|
|
|
|
return mAccount;// 直接返回当前持有的同步账户对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
|
mUpdateArray = null;
|
|
|
|
|
public void resetUpdateArray() {// 此方法用于重置待更新动作数组
|
|
|
|
|
mUpdateArray = null;// 将待更新动作数组设为null,表示清空所有待更新项
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|