Polish the new CustomTabsIntent intent extra APIs
Addresses the feedback from the API council:
- Add the extra info to the setters' javadoc
- Replace String with Locale for the language param type
Test: ./gradlew browser:browser:test
Relnote: Revised API signature/Javadoc
Bug: b/296299289
Change-Id: I5a9fbb690d7eaa502e0cb123aab64bc1fd4db675
diff --git a/browser/browser/api/current.txt b/browser/browser/api/current.txt
index 68c6b14..2d7adf9d6 100644
--- a/browser/browser/api/current.txt
+++ b/browser/browser/api/current.txt
@@ -105,7 +105,7 @@
method @Dimension(unit=androidx.annotation.Dimension.PX) public static int getInitialActivityHeightPx(android.content.Intent);
method public static int getMaxToolbarItems();
method @Dimension(unit=androidx.annotation.Dimension.DP) public static int getToolbarCornerRadiusDp(android.content.Intent);
- method public static String? getTranslateLanguage(android.content.Intent);
+ method public static java.util.Locale? getTranslateLocale(android.content.Intent);
method public static boolean isBackgroundInteractionEnabled(android.content.Intent);
method public static boolean isBookmarksButtonEnabled(android.content.Intent);
method public static boolean isDownloadButtonEnabled(android.content.Intent);
@@ -154,7 +154,7 @@
field public static final String EXTRA_TOOLBAR_COLOR = "android.support.customtabs.extra.TOOLBAR_COLOR";
field public static final String EXTRA_TOOLBAR_CORNER_RADIUS_DP = "androidx.browser.customtabs.extra.TOOLBAR_CORNER_RADIUS_DP";
field public static final String EXTRA_TOOLBAR_ITEMS = "android.support.customtabs.extra.TOOLBAR_ITEMS";
- field public static final String EXTRA_TRANSLATE_LANGUAGE = "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE";
+ field public static final String EXTRA_TRANSLATE_LANGUAGE_TAG = "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE_TAG";
field public static final String KEY_DESCRIPTION = "android.support.customtabs.customaction.DESCRIPTION";
field public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
field public static final String KEY_ID = "android.support.customtabs.customaction.ID";
@@ -206,7 +206,7 @@
method public androidx.browser.customtabs.CustomTabsIntent.Builder setStartAnimations(android.content.Context, @AnimRes int, @AnimRes int);
method @Deprecated public androidx.browser.customtabs.CustomTabsIntent.Builder setToolbarColor(@ColorInt int);
method public androidx.browser.customtabs.CustomTabsIntent.Builder setToolbarCornerRadiusDp(@Dimension(unit=androidx.annotation.Dimension.DP) int);
- method public androidx.browser.customtabs.CustomTabsIntent.Builder setTranslateLanguage(String);
+ method public androidx.browser.customtabs.CustomTabsIntent.Builder setTranslateLocale(java.util.Locale);
method public androidx.browser.customtabs.CustomTabsIntent.Builder setUrlBarHidingEnabled(boolean);
}
diff --git a/browser/browser/api/restricted_current.txt b/browser/browser/api/restricted_current.txt
index e978f0e..5b842a4 100644
--- a/browser/browser/api/restricted_current.txt
+++ b/browser/browser/api/restricted_current.txt
@@ -116,7 +116,7 @@
method @Dimension(unit=androidx.annotation.Dimension.PX) public static int getInitialActivityHeightPx(android.content.Intent);
method public static int getMaxToolbarItems();
method @Dimension(unit=androidx.annotation.Dimension.DP) public static int getToolbarCornerRadiusDp(android.content.Intent);
- method public static String? getTranslateLanguage(android.content.Intent);
+ method public static java.util.Locale? getTranslateLocale(android.content.Intent);
method public static boolean isBackgroundInteractionEnabled(android.content.Intent);
method public static boolean isBookmarksButtonEnabled(android.content.Intent);
method public static boolean isDownloadButtonEnabled(android.content.Intent);
@@ -165,7 +165,7 @@
field public static final String EXTRA_TOOLBAR_COLOR = "android.support.customtabs.extra.TOOLBAR_COLOR";
field public static final String EXTRA_TOOLBAR_CORNER_RADIUS_DP = "androidx.browser.customtabs.extra.TOOLBAR_CORNER_RADIUS_DP";
field public static final String EXTRA_TOOLBAR_ITEMS = "android.support.customtabs.extra.TOOLBAR_ITEMS";
- field public static final String EXTRA_TRANSLATE_LANGUAGE = "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE";
+ field public static final String EXTRA_TRANSLATE_LANGUAGE_TAG = "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE_TAG";
field public static final String KEY_DESCRIPTION = "android.support.customtabs.customaction.DESCRIPTION";
field public static final String KEY_ICON = "android.support.customtabs.customaction.ICON";
field public static final String KEY_ID = "android.support.customtabs.customaction.ID";
@@ -217,7 +217,7 @@
method public androidx.browser.customtabs.CustomTabsIntent.Builder setStartAnimations(android.content.Context, @AnimRes int, @AnimRes int);
method @Deprecated public androidx.browser.customtabs.CustomTabsIntent.Builder setToolbarColor(@ColorInt int);
method public androidx.browser.customtabs.CustomTabsIntent.Builder setToolbarCornerRadiusDp(@Dimension(unit=androidx.annotation.Dimension.DP) int);
- method public androidx.browser.customtabs.CustomTabsIntent.Builder setTranslateLanguage(String);
+ method public androidx.browser.customtabs.CustomTabsIntent.Builder setTranslateLocale(java.util.Locale);
method public androidx.browser.customtabs.CustomTabsIntent.Builder setUrlBarHidingEnabled(boolean);
}
diff --git a/browser/browser/src/main/java/androidx/browser/customtabs/CustomTabsIntent.java b/browser/browser/src/main/java/androidx/browser/customtabs/CustomTabsIntent.java
index 53d1504..19aa937 100644
--- a/browser/browser/src/main/java/androidx/browser/customtabs/CustomTabsIntent.java
+++ b/browser/browser/src/main/java/androidx/browser/customtabs/CustomTabsIntent.java
@@ -53,6 +53,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
+import java.util.Locale;
/**
* Class holding the {@link Intent} and start bundle for a Custom Tabs Activity.
@@ -177,10 +178,11 @@
"android.support.customtabs.extra.SEND_TO_EXTERNAL_HANDLER";
/**
- * Extra that specifies the target language the Translate UI should be triggered with.
+ * Extra that specifies the target locale the Translate UI should be triggered with.
+ * The locale is represented as a well-formed IETF BCP 47 language tag.
*/
- public static final String EXTRA_TRANSLATE_LANGUAGE =
- "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE";
+ public static final String EXTRA_TRANSLATE_LANGUAGE_TAG =
+ "androidx.browser.customtabs.extra.TRANSLATE_LANGUAGE_TAG";
/**
* Extra that, when set to false, disables interactions with the background app
@@ -1099,6 +1101,7 @@
* is enabled by default.
*
* @param enabled Whether the start button is enabled.
+ * @see CustomTabsIntent#EXTRA_DISABLE_BOOKMARKS_BUTTON
*/
@NonNull
public Builder setBookmarksButtonEnabled(boolean enabled) {
@@ -1111,6 +1114,7 @@
* is enabled by default.
*
* @param enabled Whether the download button is enabled.
+ * @see CustomTabsIntent#EXTRA_DISABLE_DOWNLOAD_BUTTON
*/
@NonNull
public Builder setDownloadButtonEnabled(boolean enabled) {
@@ -1122,6 +1126,7 @@
* Enables sending initial urls to external handler apps, if possible.
*
* @param enabled Whether to send urls to external handler.
+ * @see CustomTabsIntent#EXTRA_SEND_TO_EXTERNAL_DEFAULT_HANDLER
*/
@NonNull
public Builder setSendToExternalDefaultHandlerEnabled(boolean enabled) {
@@ -1130,14 +1135,16 @@
}
/**
- * Specifies the target language the Translate UI should be triggered with.
+ * Specifies the target locale the Translate UI should be triggered with.
*
- * @param lang Language code for the translate UI. Should be in the format of
- * ISO 639 language code.
+ * @param locale {@link Locale} object that represents the target locale.
+ * @see CustomTabsIntent#EXTRA_TRANSLATE_LANGUAGE_TAG
*/
@NonNull
- public Builder setTranslateLanguage(@NonNull String lang) {
- mIntent.putExtra(EXTRA_TRANSLATE_LANGUAGE, lang);
+ public Builder setTranslateLocale(@NonNull Locale locale) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ setLanguageTag(locale);
+ }
return this;
}
@@ -1147,6 +1154,7 @@
* Enables the interactions with the background app when a Partial Custom Tab is launched.
*
* @param enabled Whether the background interaction is enabled.
+ * @see CustomTabsIntent#EXTRA_ENABLE_BACKGROUND_INTERACTION
*/
@NonNull
public Builder setBackgroundInteractionEnabled(boolean enabled) {
@@ -1160,6 +1168,7 @@
* toolbar.
*
* @param enabled Whether the additional actions can be added to the toolbar.
+ * @see CustomTabsIntent#EXTRA_SHOW_ON_TOOLBAR
*/
@NonNull
public Builder setShowOnToolbarEnabled(boolean enabled) {
@@ -1239,6 +1248,11 @@
}
}
+ @RequiresApi(api = Build.VERSION_CODES.N)
+ private void setLanguageTag(@NonNull Locale locale) {
+ Api21Impl.setLanguageTag(mIntent, locale);
+ }
+
@RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
private void setShareIdentityEnabled() {
if (mActivityOptions == null) {
@@ -1401,14 +1415,24 @@
}
/**
- * Gets the target language for the Translate UI.
+ * Gets the target locale for the Translate UI.
*
- * @return The target language the Translate UI should be triggered with.
- * @see CustomTabsIntent#EXTRA_TRANSLATE_LANGUAGE
+ * @return The target locale the Translate UI should be triggered with.
+ * @see CustomTabsIntent#EXTRA_TRANSLATE_LANGUAGE_TAG
*/
@Nullable
- public static String getTranslateLanguage(@NonNull Intent intent) {
- return intent.getStringExtra(EXTRA_TRANSLATE_LANGUAGE);
+ public static Locale getTranslateLocale(@NonNull Intent intent) {
+ Locale locale = null;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ locale = getLocaleForLanguageTag(intent);
+ }
+ return locale;
+ }
+
+ @RequiresApi(api = Build.VERSION_CODES.N)
+ @Nullable
+ private static Locale getLocaleForLanguageTag(Intent intent) {
+ return Api21Impl.getLocaleForLanguageTag(intent);
}
/**
@@ -1427,6 +1451,21 @@
return intent.getBooleanExtra(EXTRA_SHOW_ON_TOOLBAR, false);
}
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
+ private static class Api21Impl {
+ @DoNotInline
+ static void setLanguageTag(Intent intent, Locale locale) {
+ intent.putExtra(EXTRA_TRANSLATE_LANGUAGE_TAG, locale.toLanguageTag());
+ }
+
+ @DoNotInline
+ @Nullable
+ static Locale getLocaleForLanguageTag(Intent intent) {
+ String languageTag = intent.getStringExtra(EXTRA_TRANSLATE_LANGUAGE_TAG);
+ return languageTag != null ? Locale.forLanguageTag(languageTag) : null;
+ }
+ }
+
@RequiresApi(api = Build.VERSION_CODES.M)
private static class Api23Impl {
@DoNotInline
diff --git a/browser/browser/src/test/java/androidx/browser/customtabs/CustomTabsIntentTest.java b/browser/browser/src/test/java/androidx/browser/customtabs/CustomTabsIntentTest.java
index 9997f73..89d5148 100644
--- a/browser/browser/src/test/java/androidx/browser/customtabs/CustomTabsIntentTest.java
+++ b/browser/browser/src/test/java/androidx/browser/customtabs/CustomTabsIntentTest.java
@@ -42,6 +42,8 @@
import org.robolectric.annotation.Config;
import org.robolectric.annotation.internal.DoNotInstrument;
+import java.util.Locale;
+
/**
* Tests for CustomTabsIntent.
*/
@@ -608,13 +610,15 @@
assertTrue(CustomTabsIntent.isShowOnToolbarEnabled(intent));
}
+ @Config(minSdk = Build.VERSION_CODES.N)
@Test
- public void testTranslateLanguage() {
+ public void testTranslateLocale() {
Intent intent = new CustomTabsIntent.Builder().build().intent;
- assertNull(CustomTabsIntent.getTranslateLanguage(intent));
+ assertNull(CustomTabsIntent.getTranslateLocale(intent));
- intent = new CustomTabsIntent.Builder().setTranslateLanguage("fr").build().intent;
- assertEquals("fr", CustomTabsIntent.getTranslateLanguage(intent));
+ intent = new CustomTabsIntent.Builder().setTranslateLocale(Locale.FRANCE).build().intent;
+ Locale locale = CustomTabsIntent.getTranslateLocale(intent);
+ assertEquals(locale.toLanguageTag(), Locale.FRANCE.toLanguageTag());
}
private void assertNullSessionInExtras(Intent intent) {