PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/android/src/main/java/com/google/samples/apps/iosched/util/AccountUtils.java

https://gitlab.com/adam.lukaitis/iosched
Java | 263 lines | 197 code | 41 blank | 25 comment | 15 complexity | 609c50b3019d24e959e94725befd5be5 MD5 | raw file
  1. /*
  2. * Copyright 2014 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.samples.apps.iosched.util;
  17. import android.accounts.Account;
  18. import android.content.Context;
  19. import android.content.SharedPreferences;
  20. import android.preference.PreferenceManager;
  21. import android.text.TextUtils;
  22. import com.google.samples.apps.iosched.provider.ScheduleContract;
  23. import com.google.android.gms.auth.*;
  24. import com.google.android.gms.common.Scopes;
  25. import java.io.IOException;
  26. import java.util.UUID;
  27. import static com.google.samples.apps.iosched.util.LogUtils.*;
  28. /**
  29. * Account and login utilities. This class manages a local shared preferences object
  30. * that stores which account is currently active, and can store associated information
  31. * such as Google+ profile info (name, image URL, cover URL) and also the auth token
  32. * associated with the account.
  33. */
  34. public class AccountUtils {
  35. private static final String TAG = makeLogTag(AccountUtils.class);
  36. private static final String PREF_ACTIVE_ACCOUNT = "chosen_account";
  37. // these names are are prefixes; the account is appended to them
  38. private static final String PREFIX_PREF_AUTH_TOKEN = "auth_token_";
  39. private static final String PREFIX_PREF_PLUS_PROFILE_ID = "plus_profile_id_";
  40. private static final String PREFIX_PREF_PLUS_NAME = "plus_name_";
  41. private static final String PREFIX_PREF_PLUS_IMAGE_URL = "plus_image_url_";
  42. private static final String PREFIX_PREF_PLUS_COVER_URL = "plus_cover_url_";
  43. private static final String PREFIX_PREF_GCM_KEY = "gcm_key_";
  44. public static final String AUTH_SCOPES[] = {
  45. Scopes.PLUS_LOGIN,
  46. Scopes.DRIVE_APPFOLDER,
  47. "https://www.googleapis.com/auth/userinfo.email"};
  48. static final String AUTH_TOKEN_TYPE;
  49. static {
  50. StringBuilder sb = new StringBuilder();
  51. sb.append("oauth2:");
  52. for (String scope : AUTH_SCOPES) {
  53. sb.append(scope);
  54. sb.append(" ");
  55. }
  56. AUTH_TOKEN_TYPE = sb.toString();
  57. }
  58. private static SharedPreferences getSharedPreferences(final Context context) {
  59. return PreferenceManager.getDefaultSharedPreferences(context);
  60. }
  61. public static boolean hasActiveAccount(final Context context) {
  62. return !TextUtils.isEmpty(getActiveAccountName(context));
  63. }
  64. public static String getActiveAccountName(final Context context) {
  65. SharedPreferences sp = getSharedPreferences(context);
  66. return sp.getString(PREF_ACTIVE_ACCOUNT, null);
  67. }
  68. public static Account getActiveAccount(final Context context) {
  69. String account = getActiveAccountName(context);
  70. if (account != null) {
  71. return new Account(account, GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
  72. } else {
  73. return null;
  74. }
  75. }
  76. public static boolean setActiveAccount(final Context context, final String accountName) {
  77. LOGD(TAG, "Set active account to: " + accountName);
  78. SharedPreferences sp = getSharedPreferences(context);
  79. sp.edit().putString(PREF_ACTIVE_ACCOUNT, accountName).commit();
  80. return true;
  81. }
  82. private static String makeAccountSpecificPrefKey(Context ctx, String prefix) {
  83. return hasActiveAccount(ctx) ? makeAccountSpecificPrefKey(getActiveAccountName(ctx),
  84. prefix) : null;
  85. }
  86. private static String makeAccountSpecificPrefKey(String accountName, String prefix) {
  87. return prefix + accountName;
  88. }
  89. public static String getAuthToken(final Context context) {
  90. SharedPreferences sp = getSharedPreferences(context);
  91. return hasActiveAccount(context) ?
  92. sp.getString(makeAccountSpecificPrefKey(context, PREFIX_PREF_AUTH_TOKEN), null) : null;
  93. }
  94. public static void setAuthToken(final Context context, final String accountName, final String authToken) {
  95. LOGI(TAG, "Auth token of length "
  96. + (TextUtils.isEmpty(authToken) ? 0 : authToken.length()) + " for "
  97. + accountName);
  98. SharedPreferences sp = getSharedPreferences(context);
  99. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_AUTH_TOKEN),
  100. authToken).commit();
  101. LOGV(TAG, "Auth Token: " + authToken);
  102. }
  103. public static void setAuthToken(final Context context, final String authToken) {
  104. if (hasActiveAccount(context)) {
  105. setAuthToken(context, getActiveAccountName(context), authToken);
  106. } else {
  107. LOGE(TAG, "Can't set auth token because there is no chosen account!");
  108. }
  109. }
  110. static void invalidateAuthToken(final Context context) {
  111. GoogleAuthUtil.invalidateToken(context, getAuthToken(context));
  112. setAuthToken(context, null);
  113. }
  114. public static void setPlusProfileId(final Context context, final String accountName, final String profileId) {
  115. SharedPreferences sp = getSharedPreferences(context);
  116. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_PLUS_PROFILE_ID),
  117. profileId).commit();
  118. }
  119. public static String getPlusProfileId(final Context context) {
  120. SharedPreferences sp = getSharedPreferences(context);
  121. return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(context,
  122. PREFIX_PREF_PLUS_PROFILE_ID), null) : null;
  123. }
  124. public static boolean hasPlusInfo(final Context context, final String accountName) {
  125. SharedPreferences sp = getSharedPreferences(context);
  126. return !TextUtils.isEmpty(sp.getString(makeAccountSpecificPrefKey(accountName,
  127. PREFIX_PREF_PLUS_PROFILE_ID), null));
  128. }
  129. public static boolean hasToken(final Context context, final String accountName) {
  130. SharedPreferences sp = getSharedPreferences(context);
  131. return !TextUtils.isEmpty(sp.getString(makeAccountSpecificPrefKey(accountName,
  132. PREFIX_PREF_AUTH_TOKEN), null));
  133. }
  134. public static void setPlusName(final Context context, final String accountName, final String name) {
  135. SharedPreferences sp = getSharedPreferences(context);
  136. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_PLUS_NAME),
  137. name).commit();
  138. }
  139. public static String getPlusName(final Context context) {
  140. SharedPreferences sp = getSharedPreferences(context);
  141. return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(context,
  142. PREFIX_PREF_PLUS_NAME), null) : null;
  143. }
  144. public static void setPlusImageUrl(final Context context, final String accountName, final String imageUrl) {
  145. SharedPreferences sp = getSharedPreferences(context);
  146. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_PLUS_IMAGE_URL),
  147. imageUrl).commit();
  148. }
  149. public static String getPlusImageUrl(final Context context) {
  150. SharedPreferences sp = getSharedPreferences(context);
  151. return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(context,
  152. PREFIX_PREF_PLUS_IMAGE_URL), null) : null;
  153. }
  154. public static String getPlusImageUrl(final Context context, final String accountName) {
  155. SharedPreferences sp = getSharedPreferences(context);
  156. return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(accountName,
  157. PREFIX_PREF_PLUS_IMAGE_URL), null) : null;
  158. }
  159. public static void refreshAuthToken(Context mContext) {
  160. invalidateAuthToken(mContext);
  161. tryAuthenticateWithErrorNotification(mContext, ScheduleContract.CONTENT_AUTHORITY);
  162. }
  163. public static void setPlusCoverUrl(final Context context, final String accountName, String coverPhotoUrl) {
  164. SharedPreferences sp = getSharedPreferences(context);
  165. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_PLUS_COVER_URL),
  166. coverPhotoUrl).commit();
  167. }
  168. public static String getPlusCoverUrl(final Context context) {
  169. SharedPreferences sp = getSharedPreferences(context);
  170. return hasActiveAccount(context) ? sp.getString(makeAccountSpecificPrefKey(context,
  171. PREFIX_PREF_PLUS_COVER_URL), null) : null;
  172. }
  173. static void tryAuthenticateWithErrorNotification(Context context, String syncAuthority) {
  174. try {
  175. String accountName = getActiveAccountName(context);
  176. if (accountName != null) {
  177. LOGI(TAG, "Requesting new auth token (with notification)");
  178. final String token = GoogleAuthUtil.getTokenWithNotification(context, accountName, AUTH_TOKEN_TYPE,
  179. null, syncAuthority, null);
  180. setAuthToken(context, token);
  181. } else {
  182. LOGE(TAG, "Can't try authentication because no account is chosen.");
  183. }
  184. } catch (UserRecoverableNotifiedException e) {
  185. // Notification has already been pushed.
  186. LOGW(TAG, "User recoverable exception. Check notification.", e);
  187. } catch (GoogleAuthException e) {
  188. // This is likely unrecoverable.
  189. LOGE(TAG, "Unrecoverable authentication exception: " + e.getMessage(), e);
  190. } catch (IOException e) {
  191. LOGE(TAG, "transient error encountered: " + e.getMessage());
  192. }
  193. }
  194. public static void setGcmKey(final Context context, final String accountName, final String gcmKey) {
  195. SharedPreferences sp = getSharedPreferences(context);
  196. sp.edit().putString(makeAccountSpecificPrefKey(accountName, PREFIX_PREF_GCM_KEY),
  197. gcmKey).commit();
  198. LOGD(TAG, "GCM key of account " + accountName + " set to: " + sanitizeGcmKey(gcmKey));
  199. }
  200. public static String getGcmKey(final Context context, final String accountName) {
  201. SharedPreferences sp = getSharedPreferences(context);
  202. String gcmKey = sp.getString(makeAccountSpecificPrefKey(accountName,
  203. PREFIX_PREF_GCM_KEY), null);
  204. // if there is no current GCM key, generate a new random one
  205. if (TextUtils.isEmpty(gcmKey)) {
  206. gcmKey = UUID.randomUUID().toString();
  207. LOGD(TAG, "No GCM key on account " + accountName + ". Generating random one: "
  208. + sanitizeGcmKey(gcmKey));
  209. setGcmKey(context, accountName, gcmKey);
  210. }
  211. return gcmKey;
  212. }
  213. public static String sanitizeGcmKey(String key) {
  214. if (key == null) {
  215. return "(null)";
  216. } else if (key.length() > 8) {
  217. return key.substring(0, 4) + "........" + key.substring(key.length() - 4);
  218. } else {
  219. return "........";
  220. }
  221. }
  222. }