/* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.micode.notes.gtask.remote; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AccountManagerFuture; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import net.micode.notes.gtask.data.Node; import net.micode.notes.gtask.data.Task; import net.micode.notes.gtask.data.TaskList; import net.micode.notes.gtask.exception.ActionFailureException; import net.micode.notes.gtask.exception.NetworkFailureException; import net.micode.notes.tool.GTaskStringUtils; import net.micode.notes.ui.NotesPreferenceActivity; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; // GTaskClient 类,用于与 Google Tasks 服务进行交互 public class GTaskClient { // 日志标签,使用类的简单名称,方便日志输出时识别 private static final String TAG = GTaskClient.class.getSimpleName(); // Google Tasks 的基础 URL private static final String GTASK_URL = "https://mail.google.com/tasks/"; // 获取任务列表数据的 URL private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; // 发送任务列表操作的 URL private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; // 单例对象,确保只有一个 GTaskClient 实例 private static GTaskClient mInstance = null; // HTTP 客户端,用于发送 HTTP 请求 private DefaultHttpClient mHttpClient; // 存储获取数据的 URL private String mGetUrl; // 存储发送数据的 URL private String mPostUrl; // 客户端版本号 private long mClientVersion; // 登录状态 private boolean mLoggedin; // 上次登录的时间戳 private long mLastLoginTime; // 操作的唯一标识符,每次操作递增 private int mActionId; // 存储用户的 Google 账户信息 private Account mAccount; // 存储更新操作的 JSON 数组 private JSONArray mUpdateArray; // 构造函数,初始化成员变量 private GTaskClient() { mHttpClient = null; mGetUrl = GTASK_GET_URL; mPostUrl = GTASK_POST_URL; mClientVersion = -1; mLoggedin = false; mLastLoginTime = 0; mActionId = 1; mAccount = null; mUpdateArray = null; } // 获取 GTaskClient 的单例实例 public static synchronized GTaskClient getInstance() { if (mInstance == null) { mInstance = new GTaskClient(); } return mInstance; } // 登录方法,用于登录 Google Tasks 服务 public boolean login(Activity activity) { // 假设 cookie 在 5 分钟后过期,需要重新登录 final long interval = 1000 * 60 * 5; if (mLastLoginTime + interval < System.currentTimeMillis()) { mLoggedin = false; } // 账户切换后需要重新登录 if (mLoggedin &&!TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity.getSyncAccountName(activity))) { mLoggedin = false; } if (mLoggedin) { Log.d(TAG, "already logged in"); return true; } mLastLoginTime = System.currentTimeMillis(); // 调用 loginGoogleAccount 方法获取认证令牌 String authToken = loginGoogleAccount(activity, false); if (authToken == null) { Log.e(TAG, "login google account failed"); return false; } // 如果账户不是以 gmail.com 或 googlemail.com 结尾,使用自定义域名登录 if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase().endsWith("googlemail.com"))) { StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); int index = mAccount.name.indexOf('@') + 1; String suffix = mAccount.name.substring(index); url.append(suffix + "/"); mGetUrl = url.toString() + "ig"; mPostUrl = url.toString() + "r/ig"; if (tryToLoginGtask(activity, authToken)) { mLoggedin = true; } } // 尝试使用谷歌官方 URL 登录 if (!mLoggedin) { mGetUrl = GTASK_GET_URL; mPostUrl = GTASK_POST_URL; if (!tryToLoginGtask(activity, authToken)) { return false; } } mLoggedin = true; return true; } // 登录 Google 账户 private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken; // 获取账户管理器 AccountManager accountManager = AccountManager.get(activity); // 获取所有 Google 账户 Account[] accounts = accountManager.getAccountsByType("com.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 { Log.e(TAG, "unable to get an account with the same name in the settings"); return null; } // 获取账户的认证令牌 AccountManagerFuture 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) { Log.e(TAG, "get auth token failed"); authToken = null; } return authToken; } // 尝试登录 GTask 服务 private boolean tryToLoginGtask(Activity activity, String authToken) { if (!loginGtask(authToken)) { // 令牌可能过期,重新获取令牌并再次尝试登录 authToken = loginGoogleAccount(activity, true); if (authToken == null) { Log.e(TAG, "login google account failed"); return false; } if (!loginGtask(authToken)) { Log.e(TAG, "login gtask failed"); return false; } } return true; } // 执行 GTask 服务的登录操作 private boolean loginGtask(String authToken) { int timeoutConnection = 10000; int timeoutSocket = 15000; // 设置 HTTP 请求的参数,包括连接超时和套接字超时 HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); mHttpClient = new DefaultHttpClient(httpParameters); BasicCookieStore localBasicCookieStore = new BasicCookieStore(); mHttpClient.setCookieStore(localBasicCookieStore); HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); // 执行登录请求 try { String loginUrl = mGetUrl + "?auth=" + authToken; HttpGet httpGet = new HttpGet(loginUrl); HttpResponse response = null; response = mHttpClient.execute(httpGet); // 检查是否获得认证 cookie List cookies = mHttpClient.getCookieStore().getCookies(); boolean hasAuthCookie = false; for (Cookie cookie : cookies) { if (cookie.getName().contains("GTL")) { hasAuthCookie = true; } } if (!hasAuthCookie) { Log.w(TAG, "it seems that there is no auth cookie"); } // 获取响应内容并解析客户端版本号 String resString = getResponseContent(response.getEntity()); String jsBegin = "_setup("; String jsEnd = ")}"; int begin = resString.indexOf(jsBegin); int end = resString.lastIndexOf(jsEnd); String jsString = null; if (begin!= -1 && end!= -1 && begin < end) { jsString = resString.substring(begin + jsBegin.length(), end); } JSONObject js = new JSONObject(jsString); mClientVersion = js.getLong("v"); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); return false; } catch (Exception e) { Log.e(TAG, "httpget gtask_url failed"); return false; } return true; } // 获取下一个操作的唯一标识符 private int getActionId() { return mActionId++; } // 创建 HTTP POST 请求 private HttpPost createHttpPost() { HttpPost httpPost = new HttpPost(mPostUrl); // 设置请求头,指定内容类型和其他属性 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); httpPost.setHeader("AT", "1"); return httpPost; } // 获取 HTTP 响应的内容 private String getResponseContent(HttpEntity entity) throws IOException { String contentEncoding = null; if (entity.getContentEncoding()!= null) { contentEncoding = entity.getContentEncoding().getValue(); Log.d(TAG, "encoding: " + contentEncoding); } InputStream input = entity.getContent(); if (contentEncoding!= null && contentEncoding.equalsIgnoreCase("gzip")) { input = new GZIPInputStream(entity.getContent()); } else if (contentEncoding!= null && contentEncoding.equalsIgnoreCase("deflate")) { Inflater inflater = new Inflater(true); input = new InflaterInputStream(entity.getContent(), inflater); } try { InputStreamReader isr = new InputStreamReader(input); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); // 逐行读取响应内容 while (true) { String buff = br.readLine(); if (buff == null) { return sb.toString(); } sb = sb.append(buff); } } finally { input.close(); } } // 发送 POST 请求 private JSONObject postRequest(JSONObject js) throws NetworkFailureException { if (!mLoggedin) { Log.e(TAG, "please login first"); throw new ActionFailureException("not logged in"); } HttpPost httpPost = createHttpPost(); try { LinkedList list = new LinkedList(); list.add(new BasicNameValuePair("r", js.toString())); // 创建 URL 编码的表单实体 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); httpPost.setEntity(entity); // 执行 POST 请求 HttpResponse response = mHttpClient.execute(httpPost); String jsString = getResponseContent(response.getEntity()); return new JSONObject(jsString); } catch (ClientProtocolException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("postRequest failed"); } catch (IOException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("postRequest failed"); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("unable to convert response content to jsonobject"); } catch (Exception e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("error occurs when posting request"); } } // 创建任务 public void createTask(Task task) throws NetworkFailureException { commitUpdate(); try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); // 将任务的创建操作添加到动作列表中 actionList.put(task.getCreateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 包含客户端版本号 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 发送请求并处理响应 JSONObject jsResponse = postRequest(jsPost); JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("create task: handing jsonobject failed"); } } // 创建任务列表 public void createTaskList(TaskList tasklist) throws NetworkFailureException { commitUpdate(); try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); // 将任务列表的创建操作添加到动作列表中 actionList.put(tasklist.getCreateAction(getActionId())); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 包含客户端版本号 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 发送请求并处理响应 JSONObject jsResponse = postRequest(jsPost); JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0); tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("create tasklist: handing jsonobject failed"); } } // 提交更新操作 public void commitUpdate() throws NetworkFailureException { if (mUpdateArray!= null) { try { JSONObject jsPost = new JSONObject(); // 将更新操作列表添加到请求中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); // 包含客户端版本号 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); postRequest(jsPost); mUpdateArray = null; } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("commit update: handing jsonobject failed"); } } } // 添加节点更新操作 public void addUpdateNode(Node node) throws NetworkFailureException { if (node!= null) { // 为了避免过多更新操作,最多存储 10 个更新项 if (mUpdateArray!= null && mUpdateArray.length() > 10) { commitUpdate(); } if (mUpdateArray == null) mUpdateArray = new JSONArray(); mUpdateArray.put(node.getUpdateAction(getActionId())); } } // 移动任务 public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException { commitUpdate(); try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); // 配置移动任务的操作信息 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) { // 仅在任务列表内移动且不是第一个任务时添加前置兄弟任务的信息 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_DEST_LIST, curParent.getGid()); } actionList.put(action); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 包含客户端版本号 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); postRequest(jsPost); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("move task: handing jsonobject failed"); } } // 该部分代码是 GTaskClient 类的部分方法,主要涉及节点删除、任务列表获取以及一些相关操作 // 删除节点的方法 public void deleteNode(Node node) throws NetworkFailureException { // 首先调用 commitUpdate 方法,可能是为了提交之前的更新操作 commitUpdate(); try { // 创建一个新的 JSON 对象用于存储要发送给服务器的请求信息 JSONObject jsPost = new JSONObject(); // 创建一个 JSON 数组,用于存储操作列表 JSONArray actionList = new JSONArray(); // action_list // 将节点标记为已删除 node.setDeleted(true); // 将节点的更新操作添加到操作列表中,getActionId() 可能是获取一个唯一的操作 ID actionList.put(node.getUpdateAction(getActionId())); // 将操作列表添加到 JSON 对象中,使用了一个常量作为键,可能是约定的键名 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version // 将客户端版本添加到 JSON 对象中,可能是为了服务器端进行兼容性检查等操作 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 调用 postRequest 方法发送请求,将创建的 JSON 对象作为请求参数 postRequest(jsPost); // 将更新数组置为 null,可能是清空之前的更新操作 mUpdateArray = null; } catch (JSONException e) { // 发生 JSON 异常时的处理,输出错误日志并抛出自定义异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("delete node: handing jsonobject failed"); } } // 获取任务列表的方法 public JSONArray getTaskLists() throws NetworkFailureException { // 如果未登录,则输出错误日志并抛出异常 if (!mLoggedin) { Log.e(TAG, "please login first"); throw new ActionFailureException("not logged in"); } try { // 创建一个 HTTP GET 请求 HttpGet httpGet = new HttpGet(mGetUrl); HttpResponse response = null; // 执行请求并获取响应 response = mHttpClient.execute(httpGet); // get the task list // 获取响应内容 String resString = getResponseContent(response.getEntity()); // 定义 JSON 数据的起始和结束标记,可能是为了提取有效 JSON 数据 String jsBegin = "_setup("; String jsEnd = ")}"; int begin = resString.indexOf(jsBegin); int end = resString.lastIndexOf(jsEnd); String jsString = null; // 提取有效的 JSON 字符串 if (begin!= -1 && end!= -1 && begin < end) { jsString = resString.substring(begin + jsBegin.length(), end); } // 将 JSON 字符串解析为 JSON 对象 JSONObject js = new JSONObject(jsString); // 从 JSON 对象中获取任务列表数据,使用了一个常量作为键,可能是约定的键名 return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); } catch (ClientProtocolException e) { // 客户端协议异常处理,输出错误日志并抛出网络异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (IOException e) { // IO 异常处理,输出错误日志并抛出网络异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (JSONException e) { // JSON 异常处理,输出错误日志并抛出自定义异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task lists: handing jasonobject failed"); } } // 根据列表 GID 获取任务列表的方法 public JSONArray getTaskList(String listGid) throws NetworkFailureException { // 调用 commitUpdate 方法,可能是为了提交之前的更新操作 commitUpdate(); try { // 创建一个新的 JSON 对象用于存储请求信息 JSONObject jsPost = new JSONObject(); // 创建一个操作列表的 JSON 数组 JSONArray actionList = new JSONArray(); // 创建一个操作的 JSON 对象 JSONObject action = new JSONObject(); // action_list // 设置操作类型,使用了一个常量作为操作类型,可能是约定的操作类型 action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); // 设置操作的 ID,使用 getActionId() 获取唯一操作 ID action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置列表的 GID action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); // 设置是否获取已删除的任务,这里设置为 false action.put(GTaskStringUtils.GTASK_JSON_GET_DELET OF_NODE); Log.e(TAG, "delete node: handing jsonobject failed"); } } // 获取任务列表 public JSONArray getTaskLists() throws NetworkFailureException { if (!mLoggedin) { Log.e(TAG, "please login first"); throw new ActionFailureException("not logged in"); } try { HttpGet httpGet = new HttpGet(mGetUrl); HttpResponse response = null; response = mHttpClient.execute(httpGet); // 获取任务列表 String resString = getResponseContent(response.getEntity()); String jsBegin = "_setup("; String jsEnd = ")}"; int begin = resString.indexOf(jsBegin); int end = resString.lastIndexOf(jsEnd); String jsString = null; if (begin!= -1 && end!= -1 && begin < end) { jsString = resString.substring(begin + jsBegin.length(), end); } JSONObject js = new JSONObject(jsString); return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); } catch (ClientProtocolException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (IOException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task lists: handing jasonobject failed"); } } // 根据列表 GID 获取任务列表 public JSONArray getTaskList(String listGid) throws NetworkFailureException { commitUpdate(); try { JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); // 配置操作列表 action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); actionList.put(action); jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // 包含客户端版本号 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); JSONObject jsResponse = postRequest(jsPost); return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task list: handing jsonobject failed"); } } // 获取同步账户 public Account getSyncAccount() { return mAccount; } // 重置更新数组 public void resetUpdateArray() { mUpdateArray = null; } }