From 789d8fdbd9509e567cc3669c59e9e2dac2b57270 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Thu, 3 Dec 2015 11:27:05 -0800 Subject: [PATCH] Make LocaleList Parcelable. This is a preparation work to pass LocaleList from TextView to IMEs via EditorInfo. Marshalling and unmrshlling LocaleList via Parcel is actually not so difficult. We can reuse its internal data representation "localeTags" as a canonical serialization format. As for implementation, there are two choices. One is making LocaleList Parcelable and the other is having a utility method to do that. This CL uses Parcelable approach so that not only Framework but also application developers can reuse the code. Bug: 22859862 Change-Id: Ib28363bd5ff74228d2abeaa95004ec8bed72bddd --- api/current.txt | 5 ++++- api/system-current.txt | 5 ++++- api/test-current.txt | 5 ++++- core/java/android/util/LocaleList.aidl | 19 ++++++++++++++++ core/java/android/util/LocaleList.java | 30 +++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 core/java/android/util/LocaleList.aidl diff --git a/api/current.txt b/api/current.txt index 8f933618e72cf..f1e1e3c27490f 100644 --- a/api/current.txt +++ b/api/current.txt @@ -38610,10 +38610,11 @@ package android.util { field public static final int RTL = 1; // 0x1 } - public final class LocaleList { + public final class LocaleList implements android.os.Parcelable { ctor public LocaleList(); ctor public LocaleList(java.util.Locale); ctor public LocaleList(java.util.Locale[]); + method public int describeContents(); method public static android.util.LocaleList forLanguageTags(java.lang.String); method public java.util.Locale get(int); method public java.util.Locale getBestMatch(java.lang.String[]); @@ -38623,6 +38624,8 @@ package android.util { method public boolean isEmpty(); method public int size(); method public java.lang.String toLanguageTags(); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; } public final class Log { diff --git a/api/system-current.txt b/api/system-current.txt index 7a9728025b43a..dfd56b41703ba 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -40935,10 +40935,11 @@ package android.util { field public static final int RTL = 1; // 0x1 } - public final class LocaleList { + public final class LocaleList implements android.os.Parcelable { ctor public LocaleList(); ctor public LocaleList(java.util.Locale); ctor public LocaleList(java.util.Locale[]); + method public int describeContents(); method public static android.util.LocaleList forLanguageTags(java.lang.String); method public java.util.Locale get(int); method public java.util.Locale getBestMatch(java.lang.String[]); @@ -40948,6 +40949,8 @@ package android.util { method public boolean isEmpty(); method public int size(); method public java.lang.String toLanguageTags(); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; } public final class Log { diff --git a/api/test-current.txt b/api/test-current.txt index ddb5b06425401..b64928a0c17a0 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -38612,10 +38612,11 @@ package android.util { field public static final int RTL = 1; // 0x1 } - public final class LocaleList { + public final class LocaleList implements android.os.Parcelable { ctor public LocaleList(); ctor public LocaleList(java.util.Locale); ctor public LocaleList(java.util.Locale[]); + method public int describeContents(); method public static android.util.LocaleList forLanguageTags(java.lang.String); method public java.util.Locale get(int); method public java.util.Locale getBestMatch(java.lang.String[]); @@ -38625,6 +38626,8 @@ package android.util { method public boolean isEmpty(); method public int size(); method public java.lang.String toLanguageTags(); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; } public final class Log { diff --git a/core/java/android/util/LocaleList.aidl b/core/java/android/util/LocaleList.aidl new file mode 100644 index 0000000000000..f5de354159997 --- /dev/null +++ b/core/java/android/util/LocaleList.aidl @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.util; + +parcelable LocaleList; diff --git a/core/java/android/util/LocaleList.java b/core/java/android/util/LocaleList.java index c1d23bb9d7e7f..b2ee045db4516 100644 --- a/core/java/android/util/LocaleList.java +++ b/core/java/android/util/LocaleList.java @@ -19,6 +19,8 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.Size; +import android.os.Parcel; +import android.os.Parcelable; import com.android.internal.annotations.GuardedBy; @@ -35,11 +37,12 @@ * LocaleList is an immutable list of Locales, typically used to keep an * ordered user preferences for locales. */ -public final class LocaleList { +public final class LocaleList implements Parcelable { private final Locale[] mList; // This is a comma-separated list of the locales in the LocaleList created at construction time, // basically the result of running each locale's toLanguageTag() method and concatenating them // with commas in between. + @NonNull private final String mStringRepresentation; private static final Locale[] sEmptyList = new Locale[0]; @@ -101,6 +104,16 @@ public String toString() { return sb.toString(); } + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int parcelableFlags) { + dest.writeString(mStringRepresentation); + } + @NonNull public String toLanguageTags() { return mStringRepresentation; @@ -163,10 +176,25 @@ public LocaleList(@Nullable Locale[] list) { } } + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + @Override + public LocaleList createFromParcel(Parcel source) { + return LocaleList.forLanguageTags(source.readString()); + } + + @Override + public LocaleList[] newArray(int size) { + return new LocaleList[size]; + } + }; + + @NonNull public static LocaleList getEmptyLocaleList() { return sEmptyLocaleList; } + @NonNull public static LocaleList forLanguageTags(@Nullable String list) { if (list == null || list.equals("")) { return getEmptyLocaleList();