@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License .
* limitations under the License .
* /
* /
package net.micode.notes.gtask.remote ;
package net.micode.notes.gtask.remote ;
import android.accounts.Account ;
import android.accounts.Account ;
import android.accounts.AccountManager ;
import android.accounts.AccountManager ;
import android.accounts.AccountManagerFuture ;
import android.accounts.AccountManagerFuture ;
@ -23,7 +23,7 @@ import android.app.Activity;
import android.os.Bundle ;
import android.os.Bundle ;
import android.text.TextUtils ;
import android.text.TextUtils ;
import android.util.Log ;
import android.util.Log ;
import net.micode.notes.gtask.data.Node ;
import net.micode.notes.gtask.data.Node ;
import net.micode.notes.gtask.data.Task ;
import net.micode.notes.gtask.data.Task ;
import net.micode.notes.gtask.data.TaskList ;
import net.micode.notes.gtask.data.TaskList ;
@ -31,7 +31,7 @@ import net.micode.notes.gtask.exception.ActionFailureException;
import net.micode.notes.gtask.exception.NetworkFailureException ;
import net.micode.notes.gtask.exception.NetworkFailureException ;
import net.micode.notes.tool.GTaskStringUtils ;
import net.micode.notes.tool.GTaskStringUtils ;
import net.micode.notes.ui.NotesPreferenceActivity ;
import net.micode.notes.ui.NotesPreferenceActivity ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpResponse ;
import org.apache.http.HttpResponse ;
import org.apache.http.client.ClientProtocolException ;
import org.apache.http.client.ClientProtocolException ;
@ -49,7 +49,7 @@ import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray ;
import org.json.JSONArray ;
import org.json.JSONException ;
import org.json.JSONException ;
import org.json.JSONObject ;
import org.json.JSONObject ;
import java.io.BufferedReader ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.InputStream ;
@ -59,37 +59,39 @@ import java.util.List;
import java.util.zip.GZIPInputStream ;
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 ;
// GTaskClient 类用于与 Google Tasks 远程服务进行交互
public class GTaskClient {
public class GTaskClient {
private static final String TAG = GTaskClient . class . getSimpleName ( ) ;
private static final String TAG = GTaskClient . class . getSimpleName ( ) ;
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 ;
private DefaultHttpClient mHttpClient ;
private DefaultHttpClient mHttpClient ;
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 ;
private int mActionId ;
private int mActionId ;
private Account mAccount ;
private Account mAccount ;
private JSONArray mUpdateArray ;
private JSONArray mUpdateArray ;
// 私有构造函数,确保只能通过 getInstance 方法获取实例
private GTaskClient ( ) {
private GTaskClient ( ) {
mHttpClient = null ;
mHttpClient = null ;
mGetUrl = GTASK_GET_URL ;
mGetUrl = GTASK_GET_URL ;
@ -101,14 +103,16 @@ public class GTaskClient {
mAccount = null ;
mAccount = null ;
mUpdateArray = null ;
mUpdateArray = null ;
}
}
// 获取 GTaskClient 的单例实例
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 ;
}
}
// 登录 Google Tasks, 如果已登录则检查登录状态是否过期或账户是否切换
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
@ -116,26 +120,26 @@ public class GTaskClient {
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 ( ) ;
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
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" ) ) ) {
@ -145,12 +149,12 @@ public class GTaskClient {
url . append ( suffix + "/" ) ;
url . append ( suffix + "/" ) ;
mGetUrl = url . toString ( ) + "ig" ;
mGetUrl = url . toString ( ) + "ig" ;
mPostUrl = url . toString ( ) + "r/ig" ;
mPostUrl = url . toString ( ) + "r/ig" ;
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
if ( ! mLoggedin ) {
if ( ! mLoggedin ) {
mGetUrl = GTASK_GET_URL ;
mGetUrl = GTASK_GET_URL ;
@ -159,21 +163,22 @@ public class GTaskClient {
return false ;
return false ;
}
}
}
}
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 ) ;
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 ) {
@ -188,7 +193,7 @@ public class GTaskClient {
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
// 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 ) ;
@ -203,10 +208,11 @@ public class GTaskClient {
Log . e ( TAG , "get auth token failed" ) ;
Log . e ( TAG , "get auth token failed" ) ;
authToken = null ;
authToken = null ;
}
}
return authToken ;
return authToken ;
}
}
// 尝试登录 Google Tasks, 如果第一次失败则尝试失效认证令牌后重新登录
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
@ -216,7 +222,7 @@ public class GTaskClient {
Log . e ( TAG , "login google account failed" ) ;
Log . e ( TAG , "login google account failed" ) ;
return false ;
return false ;
}
}
if ( ! loginGtask ( authToken ) ) {
if ( ! loginGtask ( authToken ) ) {
Log . e ( TAG , "login gtask failed" ) ;
Log . e ( TAG , "login gtask failed" ) ;
return false ;
return false ;
@ -224,7 +230,8 @@ public class GTaskClient {
}
}
return true ;
return true ;
}
}
// 使用认证令牌登录 Google Tasks 并获取客户端版本
private boolean loginGtask ( String authToken ) {
private boolean loginGtask ( String authToken ) {
int timeoutConnection = 10000 ;
int timeoutConnection = 10000 ;
int timeoutSocket = 15000 ;
int timeoutSocket = 15000 ;
@ -235,14 +242,14 @@ public class GTaskClient {
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
try {
try {
String loginUrl = mGetUrl + "?auth=" + authToken ;
String loginUrl = mGetUrl + "?auth=" + authToken ;
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
List < Cookie > cookies = mHttpClient . getCookieStore ( ) . getCookies ( ) ;
List < Cookie > cookies = mHttpClient . getCookieStore ( ) . getCookies ( ) ;
boolean hasAuthCookie = false ;
boolean hasAuthCookie = false ;
@ -254,7 +261,7 @@ public class GTaskClient {
if ( ! hasAuthCookie ) {
if ( ! hasAuthCookie ) {
Log . w ( TAG , "it seems that there is no auth cookie" ) ;
Log . w ( TAG , "it seems that there is no auth cookie" ) ;
}
}
// get the client version
// get the client version
String resString = getResponseContent ( response . getEntity ( ) ) ;
String resString = getResponseContent ( response . getEntity ( ) ) ;
String jsBegin = "_setup(" ;
String jsBegin = "_setup(" ;
@ -276,28 +283,31 @@ public class GTaskClient {
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 + + ;
}
}
// 创建一个用于发送 POST 请求的 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 ;
}
}
// 从 HttpEntity 中读取响应内容,并根据内容编码进行解压
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 ) {
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 ( ) ;
if ( contentEncoding ! = null & & contentEncoding . equalsIgnoreCase ( "gzip" ) ) {
if ( contentEncoding ! = null & & contentEncoding . equalsIgnoreCase ( "gzip" ) ) {
input = new GZIPInputStream ( entity . getContent ( ) ) ;
input = new GZIPInputStream ( entity . getContent ( ) ) ;
@ -305,12 +315,12 @@ public class GTaskClient {
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 ( ) ;
while ( true ) {
while ( true ) {
String buff = br . readLine ( ) ;
String buff = br . readLine ( ) ;
if ( buff = = null ) {
if ( buff = = null ) {
@ -322,25 +332,26 @@ public class GTaskClient {
input . close ( ) ;
input . close ( ) ;
}
}
}
}
// 发送 POST 请求并获取响应的 JSONObject
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 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 ) ;
} catch ( ClientProtocolException e ) {
} catch ( ClientProtocolException e ) {
Log . e ( TAG , e . toString ( ) ) ;
Log . e ( TAG , e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
@ -359,70 +370,73 @@ public class GTaskClient {
throw new ActionFailureException ( "error occurs when posting request" ) ;
throw new ActionFailureException ( "error occurs when posting request" ) ;
}
}
}
}
// 创建一个新的任务,并发送相应的 POST 请求到 Google Tasks
public void createTask ( Task task ) throws NetworkFailureException {
public void createTask ( Task task ) throws NetworkFailureException {
commitUpdate ( ) ;
commitUpdate ( ) ;
try {
try {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONArray actionList = new JSONArray ( ) ;
// action_list
// action_list
actionList . put ( task . getCreateAction ( getActionId ( ) ) ) ;
actionList . put ( task . getCreateAction ( getActionId ( ) ) ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
// client _ version
// client version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
// post
// post
JSONObject jsResponse = postRequest ( jsPost ) ;
JSONObject jsResponse = postRequest ( jsPost ) ;
JSONObject jsResult = ( JSONObject ) jsResponse . getJSONArray (
JSONObject jsResult = ( JSONObject ) jsResponse . getJSONArray (
GTaskStringUtils . GTASK_JSON_RESULTS ) . get ( 0 ) ;
GTaskStringUtils . GTASK_JSON_RESULTS ) . get ( 0 ) ;
task . setGid ( jsResult . getString ( GTaskStringUtils . GTASK_JSON_NEW_ID ) ) ;
task . setGid ( jsResult . getString ( GTaskStringUtils . GTASK_JSON_NEW_ID ) ) ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
Log . e ( TAG , e . toString ( ) ) ;
Log . e ( TAG , e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
throw new ActionFailureException ( "create task: handing jsonobject failed" ) ;
throw new ActionFailureException ( "create task: handing jsonobject failed" ) ;
}
}
}
}
// 创建一个新的任务列表,并发送相应的 POST 请求到 Google Tasks
public void createTaskList ( TaskList tasklist ) throws NetworkFailureException {
public void createTaskList ( TaskList tasklist ) throws NetworkFailureException {
commitUpdate ( ) ;
commitUpdate ( ) ;
try {
try {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONArray actionList = new JSONArray ( ) ;
// action_list
// action_list
actionList . put ( tasklist . getCreateAction ( getActionId ( ) ) ) ;
actionList . put ( tasklist . getCreateAction ( getActionId ( ) ) ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
// client version
// client version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
// post
// post
JSONObject jsResponse = postRequest ( jsPost ) ;
JSONObject jsResponse = postRequest ( jsPost ) ;
JSONObject jsResult = ( JSONObject ) jsResponse . getJSONArray (
JSONObject jsResult = ( JSONObject ) jsResponse . getJSONArray (
GTaskStringUtils . GTASK_JSON_RESULTS ) . get ( 0 ) ;
GTaskStringUtils . GTASK_JSON_RESULTS ) . get ( 0 ) ;
tasklist . setGid ( jsResult . getString ( GTaskStringUtils . GTASK_JSON_NEW_ID ) ) ;
tasklist . setGid ( jsResult . getString ( GTaskStringUtils . GTASK_JSON_NEW_ID ) ) ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
Log . e ( TAG , e . toString ( ) ) ;
Log . e ( TAG , e . toString ( ) ) ;
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
throw new ActionFailureException ( "create tasklist: handing jsonobject failed" ) ;
throw new ActionFailureException ( "create tasklist: handing jsonobject failed" ) ;
}
}
}
}
// 提交更新列表中的所有动作到 Google Tasks
public void commitUpdate ( ) throws NetworkFailureException {
public void commitUpdate ( ) throws NetworkFailureException {
if ( mUpdateArray ! = null ) {
if ( mUpdateArray ! = null ) {
try {
try {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
// action_list
// action_list
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , mUpdateArray ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , mUpdateArray ) ;
// client_version
// client_version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
postRequest ( jsPost ) ;
postRequest ( jsPost ) ;
mUpdateArray = null ;
mUpdateArray = null ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
@ -432,7 +446,8 @@ public class GTaskClient {
}
}
}
}
}
}
// 添加一个节点的更新动作到更新列表中
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
@ -440,13 +455,14 @@ public class GTaskClient {
if ( mUpdateArray ! = null & & mUpdateArray . length ( ) > 10 ) {
if ( mUpdateArray ! = null & & mUpdateArray . length ( ) > 10 ) {
commitUpdate ( ) ;
commitUpdate ( ) ;
}
}
if ( mUpdateArray = = null )
if ( mUpdateArray = = null )
mUpdateArray = new JSONArray ( ) ;
mUpdateArray = new JSONArray ( ) ;
mUpdateArray . put ( node . getUpdateAction ( getActionId ( ) ) ) ;
mUpdateArray . put ( node . getUpdateAction ( getActionId ( ) ) ) ;
}
}
}
}
// 移动一个任务到另一个任务列表,并发送相应的 POST 请求到 Google Tasks
public void moveTask ( Task task , TaskList preParent , TaskList curParent )
public void moveTask ( Task task , TaskList preParent , TaskList curParent )
throws NetworkFailureException {
throws NetworkFailureException {
commitUpdate ( ) ;
commitUpdate ( ) ;
@ -454,7 +470,7 @@ public class GTaskClient {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONObject action = new JSONObject ( ) ;
JSONObject action = new JSONObject ( ) ;
// action_list
// action_list
action . put ( GTaskStringUtils . GTASK_JSON_ACTION_TYPE ,
action . put ( GTaskStringUtils . GTASK_JSON_ACTION_TYPE ,
GTaskStringUtils . GTASK_JSON_ACTION_TYPE_MOVE ) ;
GTaskStringUtils . GTASK_JSON_ACTION_TYPE_MOVE ) ;
@ -473,33 +489,34 @@ public class GTaskClient {
}
}
actionList . put ( action ) ;
actionList . put ( action ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
// client_version
// client_version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
postRequest ( jsPost ) ;
postRequest ( jsPost ) ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
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" ) ;
}
}
}
}
// 删除一个节点,并发送相应的 POST 请求到 Google Tasks
public void deleteNode ( Node node ) throws NetworkFailureException {
public void deleteNode ( Node node ) throws NetworkFailureException {
commitUpdate ( ) ;
commitUpdate ( ) ;
try {
try {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONArray actionList = new JSONArray ( ) ;
// action_list
// action_list
node . setDeleted ( true ) ;
node . setDeleted ( true ) ;
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 ) ;
// client_version
// client_version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
postRequest ( jsPost ) ;
postRequest ( jsPost ) ;
mUpdateArray = null ;
mUpdateArray = null ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
@ -508,18 +525,19 @@ public class GTaskClient {
throw new ActionFailureException ( "delete node: handing jsonobject failed" ) ;
throw new ActionFailureException ( "delete node: handing jsonobject failed" ) ;
}
}
}
}
// 获取所有任务列表,并返回相应的 JSONArray
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" ) ;
throw new ActionFailureException ( "not logged in" ) ;
throw new ActionFailureException ( "not logged in" ) ;
}
}
try {
try {
HttpGet httpGet = new HttpGet ( mGetUrl ) ;
HttpGet httpGet = new HttpGet ( mGetUrl ) ;
HttpResponse response = null ;
HttpResponse response = null ;
response = mHttpClient . execute ( httpGet ) ;
response = mHttpClient . execute ( httpGet ) ;
// get the task list
// get the task list
String resString = getResponseContent ( response . getEntity ( ) ) ;
String resString = getResponseContent ( response . getEntity ( ) ) ;
String jsBegin = "_setup(" ;
String jsBegin = "_setup(" ;
@ -546,14 +564,15 @@ public class GTaskClient {
throw new ActionFailureException ( "get task lists: handing jasonobject failed" ) ;
throw new ActionFailureException ( "get task lists: handing jasonobject failed" ) ;
}
}
}
}
// 获取指定任务列表中的所有任务,并返回相应的 JSONArray
public JSONArray getTaskList ( String listGid ) throws NetworkFailureException {
public JSONArray getTaskList ( String listGid ) throws NetworkFailureException {
commitUpdate ( ) ;
commitUpdate ( ) ;
try {
try {
JSONObject jsPost = new JSONObject ( ) ;
JSONObject jsPost = new JSONObject ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONArray actionList = new JSONArray ( ) ;
JSONObject action = new JSONObject ( ) ;
JSONObject action = new JSONObject ( ) ;
// action_list
// action_list
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 ) ;
@ -562,10 +581,10 @@ public class GTaskClient {
action . put ( GTaskStringUtils . GTASK_JSON_GET_DELETED , false ) ;
action . put ( GTaskStringUtils . GTASK_JSON_GET_DELETED , false ) ;
actionList . put ( action ) ;
actionList . put ( action ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_ACTION_LIST , actionList ) ;
// client_version
// client_version
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
jsPost . put ( GTaskStringUtils . GTASK_JSON_CLIENT_VERSION , mClientVersion ) ;
JSONObject jsResponse = postRequest ( jsPost ) ;
JSONObject jsResponse = postRequest ( jsPost ) ;
return jsResponse . getJSONArray ( GTaskStringUtils . GTASK_JSON_TASKS ) ;
return jsResponse . getJSONArray ( GTaskStringUtils . GTASK_JSON_TASKS ) ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
@ -574,12 +593,14 @@ public class GTaskClient {
throw new ActionFailureException ( "get task list: handing jsonobject failed" ) ;
throw new ActionFailureException ( "get task list: handing jsonobject failed" ) ;
}
}
}
}
// 获取当前同步的 Google 账户
public Account getSyncAccount ( ) {
public Account getSyncAccount ( ) {
return mAccount ;
return mAccount ;
}
}
// 重置更新列表
public void resetUpdateArray ( ) {
public void resetUpdateArray ( ) {
mUpdateArray = null ;
mUpdateArray = null ;
}
}
}
}