|
|
@ -15,7 +15,10 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 主要功能:实现GTASK的登录操作,进行GTASK任务的创建,创建任务列表,从网络上获取任务和任务列表的内容
|
|
|
|
|
|
|
|
* 主要使用类或技术:accountManager JSONObject HttpParams authToken Gid
|
|
|
|
|
|
|
|
*/
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
@ -100,7 +103,10 @@ public class GTaskClient {
|
|
|
|
mActionId = 1;
|
|
|
|
mActionId = 1;
|
|
|
|
mAccount = null;
|
|
|
|
mAccount = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|
}
|
|
|
|
}/*用来获取的实例化对象
|
|
|
|
|
|
|
|
* 使用 getInstance()
|
|
|
|
|
|
|
|
* 返回mInstance这个实例化对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
public static synchronized GTaskClient getInstance() {
|
|
|
|
if (mInstance == null) {
|
|
|
|
if (mInstance == null) {
|
|
|
@ -108,7 +114,11 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mInstance;
|
|
|
|
return mInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*用来实现登录操作的函数,传入的参数是一个Activity
|
|
|
|
|
|
|
|
* 设置登录操作限制时间,如果超时则需要重新登录
|
|
|
|
|
|
|
|
* 有两种登录方式,使用用户自己的URL登录或者使用谷歌官方的URL登录
|
|
|
|
|
|
|
|
* 返回true或者false,即最后是否登陆成功
|
|
|
|
|
|
|
|
*/
|
|
|
|
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
|
|
|
@ -117,41 +127,44 @@ public class GTaskClient {
|
|
|
|
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();//更新最后登录时间,改为系统当前的时间
|
|
|
|
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
|
|
|
|
// 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/");
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;
|
|
|
|
int index = mAccount.name.indexOf('@') + 1;
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
String suffix = mAccount.name.substring(index);
|
|
|
|
url.append(suffix + "/");
|
|
|
|
url.append(suffix + "/");
|
|
|
|
mGetUrl = url.toString() + "ig";
|
|
|
|
mGetUrl = url.toString() + "ig";//设置用户对应的getUrl
|
|
|
|
mPostUrl = url.toString() + "r/ig";
|
|
|
|
mPostUrl = url.toString() + "r/ig";//设置用户对应的postUrl
|
|
|
|
|
|
|
|
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
if (tryToLoginGtask(activity, authToken)) {
|
|
|
|
mLoggedin = true;
|
|
|
|
mLoggedin = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// try to login with google official url
|
|
|
|
// try to login with google official url
|
|
|
|
|
|
|
|
//如果用户账户无法登录,则使用谷歌官方的URI进行登录
|
|
|
|
if (!mLoggedin) {
|
|
|
|
if (!mLoggedin) {
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mGetUrl = GTASK_GET_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
|
mPostUrl = GTASK_POST_URL;
|
|
|
@ -165,9 +178,9 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);//AccountManager这个类给用户提供了集中注册账号的接口
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");
|
|
|
|
Account[] accounts = accountManager.getAccountsByType("com.google");//获取全部以com.google结尾的account
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
@ -176,6 +189,7 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
|
|
|
|
Account account = null;
|
|
|
|
Account account = null;
|
|
|
|
|
|
|
|
//遍历获得的accounts信息,寻找已经记录过的账户信息
|
|
|
|
for (Account a : accounts) {
|
|
|
|
for (Account a : accounts) {
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
if (a.name.equals(accountName)) {
|
|
|
|
account = a;
|
|
|
|
account = a;
|
|
|
@ -190,11 +204,13 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the token now
|
|
|
|
// 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 authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
Bundle authTokenBundle = accountManagerFuture.getResult();
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
|
|
|
|
//如果是invalidateToken,那么需要调用invalidateAuthToken(String, String)方法废除这个无效token
|
|
|
|
if (invalidateToken) {
|
|
|
|
if (invalidateToken) {
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
accountManager.invalidateAuthToken("com.google", authToken);
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
|
loginGoogleAccount(activity, false);
|
|
|
@ -206,11 +222,12 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
|
|
return authToken;
|
|
|
|
return authToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//尝试登陆Gtask,这只是一个预先判断令牌是否是有效以及是否能登上GTask的方法,而不是具体实现登陆的方法
|
|
|
|
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 authTokedate, now let's invalidate the
|
|
|
|
// token and try again
|
|
|
|
// token and try again
|
|
|
|
|
|
|
|
//删除过一个无效的authToken,申请一个新的后再次尝试登陆
|
|
|
|
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");
|
|
|
@ -225,25 +242,28 @@ public class GTaskClient {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//实现登录GTask的具体操作
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
private boolean loginGtask(String authToken) {
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
int timeoutConnection = 10000;
|
|
|
|
int timeoutSocket = 15000;
|
|
|
|
int timeoutSocket = 15000;//socket是一种通信连接实现数据的交换的端口
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();
|
|
|
|
HttpParams httpParameters = new BasicHttpParams();//实例化一个新的HTTP参数类
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
|
|
|
|
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);//设置连接超时时间
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
|
|
|
|
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);//设置设置端口超时时间
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
mHttpClient = new DefaultHttpClient(httpParameters);
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();
|
|
|
|
BasicCookieStore localBasicCookieStore = new BasicCookieStore();//设置本地cookie
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
mHttpClient.setCookieStore(localBasicCookieStore);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
|
|
|
|
|
|
|
|
|
|
|
|
// login gtask
|
|
|
|
// login gtask
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;
|
|
|
|
String loginUrl = mGetUrl + "?auth=" + authToken;//设置登录的url
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);
|
|
|
|
HttpGet httpGet = new HttpGet(loginUrl);//通过登录的uri实例化网页上资源的查找
|
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response = null;
|
|
|
|
HttpResponse response = null;
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
response = mHttpClient.execute(httpGet);
|
|
|
|
|
|
|
|
|
|
|
|
// get the cookie now
|
|
|
|
// get the cookie now
|
|
|
|
|
|
|
|
//获取CookieStore里存放的cookie,看如果存有“GTL(不知道什么意思)”,则说明有验证成功的有效的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 +276,7 @@ public class GTaskClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the client version
|
|
|
|
// get the client version
|
|
|
|
|
|
|
|
//获取client的内容,具体操作是在返回的Content中截取从_setup(开始到)}</script>中间的字符串内容,也就是gtask_url的内容
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String resString = getResponseContent(response.getEntity());
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsBegin = "_setup(";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
|
String jsEnd = ")}</script>";
|
|
|
@ -283,14 +304,20 @@ public class GTaskClient {
|
|
|
|
private int getActionId() {
|
|
|
|
private int getActionId() {
|
|
|
|
return mActionId++;
|
|
|
|
return mActionId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*实例化创建一个用于向网络传输数据的对象
|
|
|
|
|
|
|
|
* 使用HttpPost类
|
|
|
|
|
|
|
|
* 返回一个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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*通过URL获取响应后返回的数据,也就是网络上的数据和资源
|
|
|
|
|
|
|
|
* 使用getContentEncoding()获取网络上的资源和数据
|
|
|
|
|
|
|
|
* 返回值就是获取到的资源
|
|
|
|
|
|
|
|
*/
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
private String getResponseContent(HttpEntity entity) throws IOException {
|
|
|
|
String contentEncoding = null;
|
|
|
|
String contentEncoding = null;
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
|
if (entity.getContentEncoding() != null) {
|
|
|
@ -322,21 +349,27 @@ public class GTaskClient {
|
|
|
|
input.close();
|
|
|
|
input.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*通过JSON发送请求
|
|
|
|
|
|
|
|
* 请求的具体内容在json的实例化对象js中然后传入
|
|
|
|
|
|
|
|
* 利用UrlEncodedFormEntity entity和httpPost.setEntity(entity)方法把js中的内容放置到httpPost中
|
|
|
|
|
|
|
|
* 执行请求后使用getResponseContent方法得到返回的数据和资源
|
|
|
|
|
|
|
|
* 将资源再次放入json后返回
|
|
|
|
|
|
|
|
*/
|
|
|
|
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");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
throw new ActionFailureException("not logged in");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//实例化一个httpPost的对象用来向服务器传输数据,在这里就是发送请求,而请求的内容在js里
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
HttpPost httpPost = createHttpPost();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
list.add(new BasicNameValuePair("r", js.toString()));
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
httpPost.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
|
|
// execute the post
|
|
|
|
// execute the post
|
|
|
|
|
|
|
|
//执行这个请求
|
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
HttpResponse response = mHttpClient.execute(httpPost);
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
String jsString = getResponseContent(response.getEntity());
|
|
|
|
return new JSONObject(jsString);
|
|
|
|
return new JSONObject(jsString);
|
|
|
@ -359,7 +392,12 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
throw new ActionFailureException("error occurs when posting request");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*创建单个任务
|
|
|
|
|
|
|
|
* 传入参数是一个.gtask.data.Task包里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,9 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("create task: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 创建一个任务列表,与createTask几乎一样,区别就是最后设置的是tasklist的gid
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
public void createTaskList(TaskList tasklist) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -411,7 +451,11 @@ 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 +476,10 @@ 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 +493,12 @@ public class GTaskClient {
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
mUpdateArray.put(node.getUpdateAction(getActionId()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 移动task,比如讲task移动到不同的task列表中去
|
|
|
|
|
|
|
|
* 通过getGid获取task所属列表的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 +515,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 +525,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
|
|
|
@ -483,7 +537,11 @@ public class GTaskClient {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("move task: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}/*
|
|
|
|
|
|
|
|
* 删除操作节点
|
|
|
|
|
|
|
|
* 还是利用JSON
|
|
|
|
|
|
|
|
* 删除过后使用postRequest发送删除后的结果
|
|
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
|
public void deleteNode(Node node) throws NetworkFailureException {
|
|
|
@ -494,7 +552,7 @@ public class GTaskClient {
|
|
|
|
|
|
|
|
|
|
|
|
// action_list
|
|
|
|
// action_list
|
|
|
|
node.setDeleted(true);
|
|
|
|
node.setDeleted(true);
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));
|
|
|
|
actionList.put(node.getUpdateAction(getActionId()));//这里会获取到删除操作的ID,加入到actionLiast中
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
|
|
|
|
|
|
|
|
|
|
|
|
// client_version
|
|
|
|
// client_version
|
|
|
@ -508,7 +566,11 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
throw new ActionFailureException("delete node: handing jsonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 获取任务列表
|
|
|
|
|
|
|
|
* 首先通过GetURI使用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 +583,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>";
|
|
|
@ -531,6 +594,7 @@ public class GTaskClient {
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
jsString = resString.substring(begin + jsBegin.length(), end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
JSONObject js = new JSONObject(jsString);
|
|
|
|
|
|
|
|
//获取GTASK_JSON_LISTS
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
} catch (ClientProtocolException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
@ -546,7 +610,9 @@ public class GTaskClient {
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
throw new ActionFailureException("get task lists: handing jasonobject failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* 通过传入的TASKList的gid,从网络上获取相应属于这个任务列表的任务
|
|
|
|
|
|
|
|
*/
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
public JSONArray getTaskList(String listGid) throws NetworkFailureException {
|
|
|
|
commitUpdate();
|
|
|
|
commitUpdate();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -578,6 +644,7 @@ public class GTaskClient {
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
public Account getSyncAccount() {
|
|
|
|
return mAccount;
|
|
|
|
return mAccount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//重置更新的内容
|
|
|
|
|
|
|
|
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
public void resetUpdateArray() {
|
|
|
|
mUpdateArray = null;
|
|
|
|
mUpdateArray = null;
|
|
|
|