Commit 5c56e082 authored by Marcin Wiacek's avatar Marcin Wiacek Committed by Commit Bot

Migrate BandwidthType to @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: I4cc6bde5c2d738bf5bc908735110ac420325e86c
Reviewed-on: https://chromium-review.googlesource.com/1183663Reviewed-by: default avatarTheresa <twellington@chromium.org>
Commit-Queue: Marcin Wiącek <marcin@mwiacek.com>
Cr-Commit-Position: refs/heads/master@{#585551}
parent e75f9cdd
......@@ -4,31 +4,34 @@
package org.chromium.chrome.browser.preferences.privacy;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Bandwidth options available based on network.
*/
public enum BandwidthType {
// Still using "prerender" in the strings for historical reasons: we don't want to break
// existing users' settings.
NEVER_PRERENDER ("never_prefetch"),
PRERENDER_ON_WIFI ("prefetch_on_wifi"),
ALWAYS_PRERENDER ("always_prefetch");
public static final BandwidthType DEFAULT = PRERENDER_ON_WIFI;
private final String mTitle;
BandwidthType(String title) {
mTitle = title;
public class BandwidthType {
@IntDef({Type.NEVER_PRERENDER, Type.PRERENDER_ON_WIFI, Type.ALWAYS_PRERENDER})
@Retention(RetentionPolicy.SOURCE)
public @interface Type {
// Values are numbered from 0 and can't have gaps.
int NEVER_PRERENDER = 0;
int PRERENDER_ON_WIFI = 1; // Default option.
int ALWAYS_PRERENDER = 2;
int NUM_ENTRIES = 3;
}
private final static String[] TITLES = {
"never_prefetch", "prefetch_on_wifi", "always_prefetch"};
/**
* Returns the title of the bandwidthType.
* @return title
*/
public String title() {
return mTitle;
public static String title(@Type int type) {
return TITLES[type];
}
/**
......@@ -36,11 +39,11 @@ public enum BandwidthType {
* @param title
* @return BandwidthType
*/
public static BandwidthType getBandwidthFromTitle(String title) {
for (BandwidthType b : BandwidthType.values()) {
if (b.mTitle.equals(title)) return b;
public static @BandwidthType.Type int getBandwidthFromTitle(String title) {
for (@BandwidthType.Type int i = Type.NEVER_PRERENDER; i < Type.NUM_ENTRIES; i++) {
if (TITLES[i].equals(title)) return i;
}
assert false;
return DEFAULT;
return Type.PRERENDER_ON_WIFI;
}
}
......@@ -178,7 +178,8 @@ public class PrivacyPreferencesManager implements CrashReportingPermissionManage
// Migrate if the old preferences are at their default values.
// (Note that for PREF_BANDWIDTH*, if the setting is default, then there is no way to tell
// whether the user has set it.)
final String prefBandwidthDefault = BandwidthType.PRERENDER_ON_WIFI.title();
final String prefBandwidthDefault =
BandwidthType.title(BandwidthType.Type.PRERENDER_ON_WIFI);
final String prefBandwidth =
mSharedPreferences.getString(PREF_BANDWIDTH_OLD, prefBandwidthDefault);
boolean prefBandwidthNoCellularDefault = true;
......@@ -191,12 +192,13 @@ public class PrivacyPreferencesManager implements CrashReportingPermissionManage
// Observe PREF_BANDWIDTH on mobile network capable devices.
if (isMobileNetworkCapable()) {
if (mSharedPreferences.contains(PREF_BANDWIDTH_OLD)) {
BandwidthType prefetchBandwidthTypePref = BandwidthType.getBandwidthFromTitle(
prefBandwidth);
if (BandwidthType.NEVER_PRERENDER.equals(prefetchBandwidthTypePref)) {
@BandwidthType.Type
int prefetchBandwidthTypePref =
BandwidthType.getBandwidthFromTitle(prefBandwidth);
if (BandwidthType.Type.NEVER_PRERENDER == prefetchBandwidthTypePref) {
newValue = false;
} else if (BandwidthType.PRERENDER_ON_WIFI.equals(prefetchBandwidthTypePref)
|| BandwidthType.ALWAYS_PRERENDER.equals(prefetchBandwidthTypePref)) {
} else if (BandwidthType.Type.PRERENDER_ON_WIFI == prefetchBandwidthTypePref
|| BandwidthType.Type.ALWAYS_PRERENDER == prefetchBandwidthTypePref) {
newValue = true;
}
}
......
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