/* * 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(); // 定义 GTASK 的 URL 地址 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_POST_URL = "https://mail.google.com/tasks/r/ig"; private static GTaskClient mInstance = null; // 单例对象 private DefaultHttpClient mHttpClient; // HTTP 客户端 private String mGetUrl; // GET 请求 URL private String mPostUrl; // POST 请求 URL private long mClientVersion; // 客户端版本 private boolean mLoggedin; // 登录状态 private long mLastLoginTime; // 上次登录时间 private int mActionId; // 操作 ID,用于唯一标识每个操作 private Account mAccount; // 当前登录的 Google 帐号 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; } // 登录方法,返回是否成功 public boolean login(Activity activity) { // 每 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, "已经登录"); return true; } mLastLoginTime = System.currentTimeMillis(); String authToken = loginGoogleAccount(activity, false); if (authToken == null) { Log.e(TAG, "Google 帐号登录失败"); return false; } // 如果是自定义域名帐号,需要构建对应的 URL 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; } } // 尝试使用 Google 官方 URL 登录 if (!mLoggedin) { mGetUrl = GTASK_GET_URL; mPostUrl = GTASK_POST_URL; if (!tryToLoginGtask(activity, authToken)) { return false; } } mLoggedin = true; return true; } // 获取授权令牌 private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken; AccountManager accountManager = AccountManager.get(activity); Account[] accounts = accountManager.getAccountsByType("com.google"); if (accounts.length == 0) { Log.e(TAG, "没有可用的 Google 帐号"); 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, "无法找到相同名称的帐户"); 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, "获取授权令牌失败"); authToken = null; } return authToken; } // 尝试登录 Google 任务 private boolean tryToLoginGtask(Activity activity, String authToken) { if (!loginGtask(authToken)) { // 如果授权令牌过期,重新登录 authToken = loginGoogleAccount(activity, true); if (authToken == null) { Log.e(TAG, "Google 帐号登录失败"); return false; } if (!loginGtask(authToken)) { Log.e(TAG, "登录 Google Tasks 失败"); return false; } } return true; } // 通过授权令牌登录 Google Tasks private boolean loginGtask(String authToken) { int timeoutConnection = 10000; int timeoutSocket = 15000; 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); // 登录 Google 任务 try { String loginUrl = mGetUrl + "?auth=" + authToken; HttpGet httpGet = new HttpGet(loginUrl); HttpResponse 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, "未找到授权 Cookie"); } // 获取客户端版本 String resString = getResponseContent(response.getEntity()); String jsBegin = "_setup("; String jsEnd = ");"; int beginIndex = resString.indexOf(jsBegin); int endIndex = resString.indexOf(jsEnd); if (beginIndex != -1 && endIndex != -1) { String jsonString = resString.substring(beginIndex + jsBegin.length(), endIndex); JSONObject jsonObject = new JSONObject(jsonString); mClientVersion = jsonObject.optLong("clientVersion", -1); } return true; } catch (Exception e) { Log.e(TAG, "登录失败:" + e.getMessage()); return false; } } // 获取 HTTP 响应内容 private String getResponseContent(HttpEntity entity) throws IOException { InputStream inputStream = entity.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } return stringBuilder.toString(); } // 获取当前同步的 Google 帐号 public Account getSyncAccount() { return mAccount; } // 获取已登录状态 public boolean isLoggedin() { return mLoggedin; } }