|
|
@ -60,10 +60,13 @@ import java.util.zip.GZIPInputStream;
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
import java.util.zip.Inflater;
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
import java.util.zip.InflaterInputStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
*实现GTask登录操作,创建GTask任务,与谷歌服务联网获取任务和任务列表
|
|
|
|
|
|
|
|
*主要用类:accountManager JSONObject HttpParams authToken Gid
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class GTaskClient {
|
|
|
|
public class GTaskClient {
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
private static final String TAG = GTaskClient.class.getSimpleName();
|
|
|
|
|
|
|
|
//指定登录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";
|
|
|
@ -89,7 +92,7 @@ public class GTaskClient {
|
|
|
|
private Account mAccount;
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
private JSONArray mUpdateArray;
|
|
|
|
|
|
|
|
// 创建GTask构造方法
|
|
|
|
private GTaskClient() {
|
|
|
|
private GTaskClient() {
|
|
|
|
mHttpClient = null;
|
|
|
|
mHttpClient = null;
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
@ -101,35 +104,38 @@ public class GTaskClient {
|
|
|
|
mAccount = null;
|
|
|
|
mAccount = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 实例化并锁定GTaskClient,使用getInstance()返回mInstance
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
if (mInstance == null) {
|
|
|
|
if (mInstance == null) {
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
mInstance = new GTaskClient();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mInstance;
|
|
|
|
return mInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 用于实现登录操作的布尔方法,提供了账号URL密码登录和谷歌官方邮箱URL登录两种登陆方法
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
public boolean login(Activity activity) {
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
// we suppose that the cookie would expire after 5 minutes
|
|
|
|
// then we need to re-login
|
|
|
|
// 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
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// 返回的Token为空则登陆失败
|
|
|
|
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");
|
|
|
@ -137,6 +143,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// login with custom domain if necessary
|
|
|
|
// login with custom domain if necessary
|
|
|
|
|
|
|
|
// 必要时使用本地输入谷歌邮箱登录
|
|
|
|
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/");
|
|
|
@ -152,6 +159,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
// try to login with google official url
|
|
|
|
|
|
|
|
// 调取谷歌邮箱官方URL登录
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
@ -163,25 +171,30 @@ public class GTaskClient {
|
|
|
|
mLoggedin = true;
|
|
|
|
mLoggedin = true;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 实现谷歌登录的方法,以AccountManager管理账号,Token登录
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
|
|
|
|
|
|
|
|
// 登录Token
|
|
|
|
String authToken;
|
|
|
|
String authToken;
|
|
|
|
|
|
|
|
// 账号管理,提供账号注册接口
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
AccountManager accountManager = AccountManager.get(activity);
|
|
|
|
|
|
|
|
// 获取以com.google结尾的账号列表
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
|
|
|
|
// 如果账号长度为空则返回无可用账号
|
|
|
|
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 {
|
|
|
@ -190,11 +203,13 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
// get the token now
|
|
|
|
|
|
|
|
// 取得登录Token
|
|
|
|
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 authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
// 若Token无效,则通过invalidateAuthToken方法废除该Token
|
|
|
|
if (invalidateToken) {
|
|
|
|
if (invalidateToken) {
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
@ -206,11 +221,12 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
return authToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 尝试登录的布尔方法需要预先判断Token是否有效
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
private boolean tryToLoginGtask(Activity activity, String authToken) {
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
if (!loginGtask(authToken)) {
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
// maybe the auth token is out of date, now let's invalidate the
|
|
|
|
// token and try again
|
|
|
|
// token and try again
|
|
|
|
|
|
|
|
// 若Token则废弃Token且重试
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
authToken = loginGoogleAccount(activity, true);
|
|
|
|
if (authToken == null) {
|
|
|
|
if (authToken == null) {
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
|
Log.e(TAG, "login google account failed");
|
|
|
@ -224,26 +240,35 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 登录GTask具体操作
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
|
|
|
|
// socket为一种通信数据交换的端口
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
|
|
|
|
// 实现一个新的HTTP参数类
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
|
|
|
|
// 设置链接超时时间
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
|
|
|
|
// 设置设置端口超时时间
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
|
|
|
|
// 存储新的本地cookie
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
// login gtask
|
|
|
|
|
|
|
|
// 登录GTask具体操作
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 设置登录URL
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
|
|
|
|
// 通过已实例化URL网页中的资源查找
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
HttpResponse response = null;
|
|
|
|
HttpResponse response = null;
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
// get the cookie now
|
|
|
|
|
|
|
|
// 遍历已存储的cookie以验证是否与登录cookie相符,若相符则匹配
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
boolean hasAuthCookie = false;
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
@ -256,6 +281,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
// get the client version
|
|
|
|
|
|
|
|
// 以脚本获取返回的Content中GTask_Url的内容
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
@ -273,40 +299,47 @@ public class GTaskClient {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
// simply catch all exceptions
|
|
|
|
// simply catch all exceptions
|
|
|
|
|
|
|
|
// 仅捕获到异常则返回捕获失败
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
Log.e(TAG, "httpget gtask_url failed");
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取行为ID
|
|
|
|
private int getActionId() {
|
|
|
|
private int getActionId() {
|
|
|
|
return mActionId++;
|
|
|
|
return mActionId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 实例化创建用于网络传输的对向,使用HTTPpost类来创建并返回一个空的的对向
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
private HttpPost createHttpPost() {
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
HttpPost httpPost = new HttpPost(mPostUrl);
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
httpPost.setHeader("AT", "1");
|
|
|
|
return httpPost;
|
|
|
|
return httpPost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取响应的资源目录
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
String contentEncoding = null;
|
|
|
|
String contentEncoding = null;
|
|
|
|
|
|
|
|
// 荣国URL获得HttpEntity对象,若返回值不为空,则创建数据流获取返回对象
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
contentEncoding = entity.getContentEncoding().getValue();
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
Log.d(TAG, "encoding: " + contentEncoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
InputStream input = entity.getContent();
|
|
|
|
|
|
|
|
// Gzip为使用DEFLATE压缩数据的另一个压缩库
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
input = new GZIPInputStream(entity.getContent());
|
|
|
|
} else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deflate为一个无专利的无损数据压缩算法
|
|
|
|
|
|
|
|
else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
Inflater inflater = new Inflater(true);
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
input = new InflaterInputStream(entity.getContent(), inflater);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 包装类,用于提高读取的运行效率
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
InputStreamReader isr = new InputStreamReader(input);
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
@ -322,7 +355,10 @@ public class GTaskClient {
|
|
|
|
input.close();
|
|
|
|
input.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 以json发送请求,请求的内容在json中的实例化对象传入,利用json获取task中内容
|
|
|
|
|
|
|
|
* 并创捷对应jspost,利用postrequest得到返回的任务信息并用task_setGid设置task的新id
|
|
|
|
|
|
|
|
*/
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
@ -360,6 +396,8 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建单个任务,传入task类的对象、使用json获取task中的内容,并创建相应的jspost,利用postRequest获得任务返回信
|
|
|
|
|
|
|
|
// 息并且使用task.setGid设置task的new_id
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
public void createTask(Task task) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -385,7 +423,7 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建任务列表,设置tasklist_Gid
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -411,7 +449,8 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("create tasklist: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 提交更新,使用JSONobject进行存储,使用jsPost.put,Put的信息包括UpdateArray和ClientVersion,使用
|
|
|
|
|
|
|
|
// postRequest发送这个jspost,进行处理
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
public void commitUpdate() throws NetworkFailureException {
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
if (mUpdateArray != null) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -432,7 +471,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 添加更新事项,通过调用 commitUpdate()实现
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
public void addUpdateNode(Node node) throws NetworkFailureException {
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) {
|
|
|
|
// too many update items may result in an error
|
|
|
|
// too many update items may result in an error
|
|
|
@ -446,7 +485,9 @@ public class GTaskClient {
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 移动task至不同的任务列表中去,通过getgid获取所属不同任务列表的gid,通过
|
|
|
|
|
|
|
|
// JSONObject.put(String name, Object value)函数设置移动后的task的相关属性值,从而达到移动的目的
|
|
|
|
|
|
|
|
// 最后通过postRequest进行更新后的任务列表的发送
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
public void moveTask(Task task, TaskList preParent, TaskList curParent)
|
|
|
|
throws NetworkFailureException {
|
|
|
|
throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
@ -463,6 +504,7 @@ public class GTaskClient {
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
if (preParent == curParent && task.getPriorSibling() != null) {
|
|
|
|
// put prioring_sibing_id only if moving within the tasklist and
|
|
|
|
// put prioring_sibing_id only if moving within the tasklist and
|
|
|
|
// it is not the first one
|
|
|
|
// it is not the first one
|
|
|
|
|
|
|
|
//设置优先级ID,只有当移动是发生在文件中
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
|
|
|
@ -472,6 +514,7 @@ public class GTaskClient {
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actionList.put(action);
|
|
|
|
actionList.put(action);
|
|
|
|
|
|
|
|
//最后将ACTION_LIST加入到jsPost中
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// client_version
|
|
|
@ -486,6 +529,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除操作节点,利用JSON,删除后使用postRequest发送删除后的结果
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -494,6 +538,7 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// action_list
|
|
|
|
node.setDeleted(true);
|
|
|
|
node.setDeleted(true);
|
|
|
|
|
|
|
|
// 这里会获取到删除操作的ID,加入到actionLiast中
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
@ -508,7 +553,8 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取任务列表,首先通过GetURL使用getResponseContent联网获取数据,然后筛选出"_setup("到)}</script>的部分,
|
|
|
|
|
|
|
|
// 并且从中获取GTASK_JSON_LISTS的内容返回
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
public JSONArray getTaskLists() throws NetworkFailureException {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
|
Log.e(TAG, "please login first");
|
|
|
@ -521,6 +567,7 @@ public class GTaskClient {
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
// get the task list
|
|
|
|
// get the task list
|
|
|
|
|
|
|
|
// 获取任务列表并存储进jsString中
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
@ -547,6 +594,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过传入的TASKList的gid,从网络上获取相应属于这个任务列表的任务
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -558,6 +606,7 @@ public class GTaskClient {
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
|
|
|
|
|
|
|
|
// 设置为传入的listGid
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
|
|
|
|
actionList.put(action);
|
|
|
|
actionList.put(action);
|
|
|
@ -578,7 +627,7 @@ public class GTaskClient {
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
return mAccount;
|
|
|
|
return mAccount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重装更新后的内容
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|