Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.sync.notifier; | 5 package org.chromium.components.sync.notifier; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.util.Base64; | 9 import android.util.Base64; |
| 10 | 10 |
| 11 import com.google.ipc.invalidation.external.client.types.ObjectId; | 11 import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 12 | 12 |
| 13 import org.chromium.base.ContextUtils; | 13 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
| 15 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 @VisibleForTesting | 70 @VisibleForTesting |
| 71 public static final String SYNC_ACCT_NAME = "sync_acct_name"; | 71 public static final String SYNC_ACCT_NAME = "sync_acct_name"; |
| 72 | 72 |
| 73 /** Shared preference key to store the type of account in use. */ | 73 /** Shared preference key to store the type of account in use. */ |
| 74 static final String SYNC_ACCT_TYPE = "sync_acct_type"; | 74 static final String SYNC_ACCT_TYPE = "sync_acct_type"; |
| 75 | 75 |
| 76 /** Shared preference key to store internal notification client library state. */ | 76 /** Shared preference key to store internal notification client library state. */ |
| 77 static final String SYNC_TANGO_INTERNAL_STATE = "sync_tango_internal_sta te"; | 77 static final String SYNC_TANGO_INTERNAL_STATE = "sync_tango_internal_sta te"; |
| 78 } | 78 } |
| 79 | 79 |
| 80 private static final String TAG = "InvalidationPreferences"; | 80 private static final String TAG = "InvalidationPrefs"; |
|
Nicolas Zea
2016/07/18 19:57:31
Why did this change?
maxbogue
2016/07/18 21:26:51
The TAG is limited to 20 characters. It failed a p
| |
| 81 | 81 |
| 82 // Only one commit call can be in progress at a time. | 82 // Only one commit call can be in progress at a time. |
| 83 private static final Object sCommitLock = new Object(); | 83 private static final Object sCommitLock = new Object(); |
| 84 | 84 |
| 85 /** Returns a new {@link EditContext} to modify the preferences managed by t his class. */ | 85 /** Returns a new {@link EditContext} to modify the preferences managed by t his class. */ |
| 86 public EditContext edit() { | 86 public EditContext edit() { |
| 87 return new EditContext(); | 87 return new EditContext(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Applies the changes accumulated in {@code editContext}. Returns whether t hey were | 91 * Applies the changes accumulated in {@code editContext}. Returns whether t hey were |
| 92 * successfully written. | 92 * successfully written. |
| 93 * <p> | 93 * <p> |
| 94 * NOTE: this method performs blocking I/O and must not be called from the U I thread. | 94 * NOTE: this method performs blocking I/O and must not be called from the U I thread. |
| 95 */ | 95 */ |
| 96 public boolean commit(EditContext editContext) { | 96 public boolean commit(EditContext editContext) { |
| 97 synchronized (sCommitLock) { | 97 synchronized (sCommitLock) { |
| 98 if (!editContext.mEditor.commit()) { | 98 if (!editContext.mEditor.commit()) { |
| 99 Log.w(TAG, "Failed to commit invalidation preferences"); | 99 Log.w(TAG, "Failed to commit invalidation preferences"); |
| 100 return false; | 100 return false; |
| 101 } | 101 } |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 /** Returns the saved sync types, or {@code null} if none exist. */ | 106 /** Returns the saved sync types, or {@code null} if none exist. */ |
| 107 @Nullable public Set<String> getSavedSyncedTypes() { | 107 @Nullable |
| 108 public Set<String> getSavedSyncedTypes() { | |
| 108 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 109 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 109 Set<String> syncedTypes = preferences.getStringSet(PrefKeys.SYNC_TANGO_T YPES, null); | 110 Set<String> syncedTypes = preferences.getStringSet(PrefKeys.SYNC_TANGO_T YPES, null); |
| 110 // Wrap with unmodifiableSet to ensure it's never modified. See crbug.co m/568369. | 111 // Wrap with unmodifiableSet to ensure it's never modified. See crbug.co m/568369. |
| 111 return syncedTypes == null ? null : Collections.unmodifiableSet(syncedTy pes); | 112 return syncedTypes == null ? null : Collections.unmodifiableSet(syncedTy pes); |
| 112 } | 113 } |
| 113 | 114 |
| 114 /** Sets the saved sync types to {@code syncTypes} in {@code editContext}. * / | 115 /** Sets the saved sync types to {@code syncTypes} in {@code editContext}. * / |
| 115 public void setSyncTypes(EditContext editContext, Collection<String> syncTyp es) { | 116 public void setSyncTypes(EditContext editContext, Collection<String> syncTyp es) { |
| 116 if (syncTypes == null) throw new NullPointerException("syncTypes is null ."); | 117 if (syncTypes == null) throw new NullPointerException("syncTypes is null ."); |
| 117 Set<String> selectedTypesSet = new HashSet<String>(syncTypes); | 118 Set<String> selectedTypesSet = new HashSet<String>(syncTypes); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 140 public void setObjectIds(EditContext editContext, Collection<ObjectId> objec tIds) { | 141 public void setObjectIds(EditContext editContext, Collection<ObjectId> objec tIds) { |
| 141 if (objectIds == null) throw new NullPointerException("objectIds is null ."); | 142 if (objectIds == null) throw new NullPointerException("objectIds is null ."); |
| 142 Set<String> objectIdStrings = new HashSet<String>(objectIds.size()); | 143 Set<String> objectIdStrings = new HashSet<String>(objectIds.size()); |
| 143 for (ObjectId objectId : objectIds) { | 144 for (ObjectId objectId : objectIds) { |
| 144 objectIdStrings.add(getObjectIdString(objectId)); | 145 objectIdStrings.add(getObjectIdString(objectId)); |
| 145 } | 146 } |
| 146 editContext.mEditor.putStringSet(PrefKeys.TANGO_OBJECT_IDS, objectIdStri ngs); | 147 editContext.mEditor.putStringSet(PrefKeys.TANGO_OBJECT_IDS, objectIdStri ngs); |
| 147 } | 148 } |
| 148 | 149 |
| 149 /** Returns the saved account, or {@code null} if none exists. */ | 150 /** Returns the saved account, or {@code null} if none exists. */ |
| 150 @Nullable public Account getSavedSyncedAccount() { | 151 @Nullable |
| 152 public Account getSavedSyncedAccount() { | |
| 151 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 153 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 152 String accountName = preferences.getString(PrefKeys.SYNC_ACCT_NAME, null ); | 154 String accountName = preferences.getString(PrefKeys.SYNC_ACCT_NAME, null ); |
| 153 String accountType = preferences.getString(PrefKeys.SYNC_ACCT_TYPE, null ); | 155 String accountType = preferences.getString(PrefKeys.SYNC_ACCT_TYPE, null ); |
| 154 if (accountName == null || accountType == null) { | 156 if (accountName == null || accountType == null) { |
| 155 return null; | 157 return null; |
| 156 } | 158 } |
| 157 return new Account(accountName, accountType); | 159 return new Account(accountName, accountType); |
| 158 } | 160 } |
| 159 | 161 |
| 160 /** Sets the saved account to {@code account} in {@code editContext}. */ | 162 /** Sets the saved account to {@code account} in {@code editContext}. */ |
| 161 public void setAccount(EditContext editContext, Account account) { | 163 public void setAccount(EditContext editContext, Account account) { |
| 162 editContext.mEditor.putString(PrefKeys.SYNC_ACCT_NAME, account.name); | 164 editContext.mEditor.putString(PrefKeys.SYNC_ACCT_NAME, account.name); |
| 163 editContext.mEditor.putString(PrefKeys.SYNC_ACCT_TYPE, account.type); | 165 editContext.mEditor.putString(PrefKeys.SYNC_ACCT_TYPE, account.type); |
| 164 } | 166 } |
| 165 | 167 |
| 166 /** Returns the notification client internal state. */ | 168 /** Returns the notification client internal state. */ |
| 167 @Nullable public byte[] getInternalNotificationClientState() { | 169 @Nullable |
| 170 public byte[] getInternalNotificationClientState() { | |
| 168 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 171 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 169 String base64State = preferences.getString(PrefKeys.SYNC_TANGO_INTERNAL_ STATE, null); | 172 String base64State = preferences.getString(PrefKeys.SYNC_TANGO_INTERNAL_ STATE, null); |
| 170 if (base64State == null) { | 173 if (base64State == null) { |
| 171 return null; | 174 return null; |
| 172 } | 175 } |
| 173 return Base64.decode(base64State, Base64.DEFAULT); | 176 return Base64.decode(base64State, Base64.DEFAULT); |
| 174 } | 177 } |
| 175 | 178 |
| 176 /** Sets the notification client internal state to {@code state}. */ | 179 /** Sets the notification client internal state to {@code state}. */ |
| 177 public void setInternalNotificationClientState(EditContext editContext, byte [] state) { | 180 public void setInternalNotificationClientState(EditContext editContext, byte [] state) { |
| 178 editContext.mEditor.putString(PrefKeys.SYNC_TANGO_INTERNAL_STATE, | 181 editContext.mEditor.putString( |
| 179 Base64.encodeToString(state, Base64.DEFAULT)); | 182 PrefKeys.SYNC_TANGO_INTERNAL_STATE, Base64.encodeToString(state, Base64.DEFAULT)); |
| 180 } | 183 } |
| 181 | 184 |
| 182 /** Converts the given object id to a string for storage in preferences. */ | 185 /** Converts the given object id to a string for storage in preferences. */ |
| 183 private String getObjectIdString(ObjectId objectId) { | 186 private String getObjectIdString(ObjectId objectId) { |
| 184 return objectId.getSource() + ":" + new String(objectId.getName()); | 187 return objectId.getSource() + ":" + new String(objectId.getName()); |
| 185 } | 188 } |
| 186 | 189 |
| 187 /** | 190 /** |
| 188 * Converts the given object id string stored in preferences to an object id . | 191 * Converts the given object id string stored in preferences to an object id . |
| 189 * Returns null if the string does not represent a valid object id. | 192 * Returns null if the string does not represent a valid object id. |
| 190 */ | 193 */ |
| 191 private ObjectId getObjectId(String objectIdString) { | 194 private ObjectId getObjectId(String objectIdString) { |
| 192 int separatorPos = objectIdString.indexOf(':'); | 195 int separatorPos = objectIdString.indexOf(':'); |
| 193 // Ensure that the separator is surrounded by at least one character on each side. | 196 // Ensure that the separator is surrounded by at least one character on each side. |
| 194 if (separatorPos < 1 || separatorPos == objectIdString.length() - 1) { | 197 if (separatorPos < 1 || separatorPos == objectIdString.length() - 1) { |
| 195 return null; | 198 return null; |
| 196 } | 199 } |
| 197 int objectSource; | 200 int objectSource; |
| 198 try { | 201 try { |
| 199 objectSource = Integer.parseInt(objectIdString.substring(0, separato rPos)); | 202 objectSource = Integer.parseInt(objectIdString.substring(0, separato rPos)); |
| 200 } catch (NumberFormatException e) { | 203 } catch (NumberFormatException e) { |
| 201 return null; | 204 return null; |
| 202 } | 205 } |
| 203 byte[] objectName = objectIdString.substring(separatorPos + 1).getBytes( ); | 206 byte[] objectName = objectIdString.substring(separatorPos + 1).getBytes( ); |
| 204 return ObjectId.newInstance(objectSource, objectName); | 207 return ObjectId.newInstance(objectSource, objectName); |
| 205 } | 208 } |
| 206 } | 209 } |
| OLD | NEW |