Commit aed2e26a authored by Marcin Wiacek's avatar Marcin Wiacek Committed by Commit Bot

Replace GoogleServiceAuthError.State enum with @IntDef

@IntDef/@StringDef annotation are preferred way for declaring
set of String/int values.

1. they need less space in APK than enum, see
https://developer.android.com/topic/performance/reduce-apk-size#remove-enums
2. they give more control over allowed values than "static final" values

Main goal of patch is writing "static final" values, enum
and some classes in one common @IntDef/@StringDef form:

1. with @IntDef/@StringDef first, @Retention second
   and related @interface third
2. with values inside @interface
3. with NUM_ENTRIES declaring number of entries if necessary
4. with comment about numbering from 0 without gaps when necessary
5. with @Retention(RetentionPolicy.SOURCE)
6. without "static final" in the @interface

Change-Id: Id545ccd53b49f2fd808c2d6ab32409de73c445d0
Reviewed-on: https://chromium-review.googlesource.com/1172127Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Cr-Commit-Position: refs/heads/master@{#582981}
parent 46582a40
......@@ -96,7 +96,8 @@ public class SyncPreference extends Preference {
}
if (profileSyncService.getAuthError() != GoogleServiceAuthError.State.NONE) {
return res.getString(profileSyncService.getAuthError().getMessage());
return res.getString(
GoogleServiceAuthError.getMessageID(profileSyncService.getAuthError()));
}
if (profileSyncService.getProtocolErrorClientAction()
......
......@@ -4,85 +4,86 @@
package org.chromium.chrome.browser.sync;
import android.support.annotation.IntDef;
import org.chromium.chrome.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* This class mirrors the native GoogleServiceAuthError class State enum from:
* google_apis/gaia/google_service_auth_error.h.
*/
public class GoogleServiceAuthError {
public enum State {
@IntDef({State.NONE, State.INVALID_GAIA_CREDENTIALS, State.USER_NOT_SIGNED_UP,
State.CONNECTION_FAILED, State.CAPTCHA_REQUIRED, State.ACCOUNT_DELETED,
State.ACCOUNT_DISABLED, State.SERVICE_UNAVAILABLE, State.TWO_FACTOR,
State.REQUEST_CANCELED})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
// The user is authenticated.
NONE(0, R.string.sync_error_generic),
int NONE = 0;
// The credentials supplied to GAIA were either invalid, or the locally
// cached credentials have expired.
INVALID_GAIA_CREDENTIALS(1, R.string.sync_error_ga),
int INVALID_GAIA_CREDENTIALS = 1;
// The GAIA user is not authorized to use the service.
USER_NOT_SIGNED_UP(2, R.string.sync_error_generic),
int USER_NOT_SIGNED_UP = 2;
// Could not connect to server to verify credentials. This could be in
// response to either failure to connect to GAIA or failure to connect to
// the service needing GAIA tokens during authentication.
CONNECTION_FAILED(3, R.string.sync_error_connection),
int CONNECTION_FAILED = 3;
// The user needs to satisfy a CAPTCHA challenge to unlock their account.
// If no other information is available, this can be resolved by visiting
// https://www.google.com/accounts/DisplayUnlockCaptcha. Otherwise,
// captcha() will provide details about the associated challenge.
CAPTCHA_REQUIRED(4, R.string.sync_error_generic),
int CAPTCHA_REQUIRED = 4;
// The user account has been deleted.
ACCOUNT_DELETED(5, R.string.sync_error_generic),
int ACCOUNT_DELETED = 5;
// The user account has been disabled.
ACCOUNT_DISABLED(6, R.string.sync_error_generic),
int ACCOUNT_DISABLED = 6;
// The service is not available; try again later.
SERVICE_UNAVAILABLE(7, R.string.sync_error_service_unavailable),
int SERVICE_UNAVAILABLE = 7;
// The password is valid but we need two factor to get a token.
TWO_FACTOR(8, R.string.sync_error_generic),
int TWO_FACTOR = 8;
// The requestor of the authentication step cancelled the request
// prior to completion.
REQUEST_CANCELED(9, R.string.sync_error_generic),
int REQUEST_CANCELED = 9;
// HOSTED accounts are deprecated; left in enumeration to match
// GoogleServiceAuthError enum in histograms.xml.
HOSTED_NOT_ALLOWED_DEPRECATED(10, R.string.sync_error_generic);
private final int mCode;
private final int mMessage;
// int HOSTED_NOT_ALLOWED = 10;
State(int code, int message) {
mCode = code;
mMessage = message;
}
public static State fromCode(int code) {
for (State state : State.values()) {
if (state.mCode == code) {
return state;
}
}
throw new IllegalArgumentException("No state for code: " + code);
}
public int getMessage() {
return mMessage;
}
}
private final State mState;
GoogleServiceAuthError(int code) {
mState = State.fromCode(code);
int NUM_ENTRIES = 11;
}
State getState() {
return mState;
public static int getMessageID(@State int state) {
switch (state) {
case State.INVALID_GAIA_CREDENTIALS:
return R.string.sync_error_ga;
case State.CONNECTION_FAILED:
return R.string.sync_error_connection;
case State.SERVICE_UNAVAILABLE:
return R.string.sync_error_service_unavailable;
// case State.NONE:
// case State.USER_NOT_SIGNED_UP:
// case State.CAPTCHA_REQUIRED:
// case State.ACCOUNT_DELETED:
// case State.ACCOUNT_DISABLED:
// case State.TWO_FACTOR:
// case State.REQUEST_CANCELED:
// case State.HOSTED_NOT_ALLOWED:
default:
return R.string.sync_error_generic;
}
}
}
......@@ -281,9 +281,12 @@ public class ProfileSyncService {
return nativeSetDecryptionPassphrase(mNativeProfileSyncServiceAndroid, passphrase);
}
public GoogleServiceAuthError.State getAuthError() {
public @GoogleServiceAuthError.State int getAuthError() {
int authErrorCode = nativeGetAuthError(mNativeProfileSyncServiceAndroid);
return GoogleServiceAuthError.State.fromCode(authErrorCode);
if (authErrorCode < 0 || authErrorCode >= GoogleServiceAuthError.State.NUM_ENTRIES) {
throw new IllegalArgumentException("No state for code: " + authErrorCode);
}
return authErrorCode;
}
/**
......
......@@ -25,6 +25,7 @@ import org.chromium.chrome.browser.notifications.NotificationManagerProxyImpl;
import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
import org.chromium.chrome.browser.preferences.PreferencesLauncher;
import org.chromium.chrome.browser.sync.GoogleServiceAuthError.State;
import org.chromium.components.sync.AndroidSyncSettings;
/**
......@@ -63,7 +64,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
}
if (shouldSyncAuthErrorBeShown()) {
showSyncNotification(
mProfileSyncService.getAuthError().getMessage(), createSettingsIntent());
GoogleServiceAuthError.getMessageID(mProfileSyncService.getAuthError()),
createSettingsIntent());
} else if (mProfileSyncService.isEngineInitialized()
&& mProfileSyncService.isPassphraseRequiredForDecryption()) {
if (mProfileSyncService.isPassphrasePrompted()) {
......@@ -124,17 +126,17 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
private boolean shouldSyncAuthErrorBeShown() {
switch (mProfileSyncService.getAuthError()) {
case NONE:
case CONNECTION_FAILED:
case SERVICE_UNAVAILABLE:
case REQUEST_CANCELED:
case INVALID_GAIA_CREDENTIALS:
case State.NONE:
case State.CONNECTION_FAILED:
case State.SERVICE_UNAVAILABLE:
case State.REQUEST_CANCELED:
case State.INVALID_GAIA_CREDENTIALS:
return false;
case USER_NOT_SIGNED_UP:
case CAPTCHA_REQUIRED:
case ACCOUNT_DELETED:
case ACCOUNT_DISABLED:
case TWO_FACTOR:
case State.USER_NOT_SIGNED_UP:
case State.CAPTCHA_REQUIRED:
case State.ACCOUNT_DELETED:
case State.ACCOUNT_DISABLED:
case State.TWO_FACTOR:
return true;
default:
Log.w(TAG, "Not showing unknown Auth Error: " + mProfileSyncService.getAuthError());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment