blob: 5bdbae39b5dd3b4f975ddc93c639492cb82ccdaa [file] [log] [blame]
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.hardware.input;
18
Yohei Yukawa5660fad2016-01-27 22:04:09 -080019import android.annotation.NonNull;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070020import android.os.LocaleList;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070021import android.os.Parcel;
22import android.os.Parcelable;
23
Michael Wright07483422015-10-30 16:20:13 +000024import java.util.Locale;
25
Jeff Brown9f25b7f2012-04-10 14:30:49 -070026/**
27 * Describes a keyboard layout.
28 *
29 * @hide
30 */
31public final class KeyboardLayout implements Parcelable,
32 Comparable<KeyboardLayout> {
33 private final String mDescriptor;
34 private final String mLabel;
Jeff Brownd9fec5d2012-05-17 16:01:54 -070035 private final String mCollection;
Michael Wright8ebac232014-09-18 18:29:49 -070036 private final int mPriority;
Yohei Yukawa5660fad2016-01-27 22:04:09 -080037 @NonNull
38 private final LocaleList mLocales;
Michael Wright07483422015-10-30 16:20:13 +000039 private final int mVendorId;
40 private final int mProductId;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070041
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070042 public static final @android.annotation.NonNull Parcelable.Creator<KeyboardLayout> CREATOR =
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043 new Parcelable.Creator<KeyboardLayout>() {
44 public KeyboardLayout createFromParcel(Parcel source) {
45 return new KeyboardLayout(source);
46 }
47 public KeyboardLayout[] newArray(int size) {
48 return new KeyboardLayout[size];
49 }
50 };
51
Michael Wright07483422015-10-30 16:20:13 +000052 public KeyboardLayout(String descriptor, String label, String collection, int priority,
Yohei Yukawa5660fad2016-01-27 22:04:09 -080053 LocaleList locales, int vid, int pid) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -070054 mDescriptor = descriptor;
55 mLabel = label;
Jeff Brownd9fec5d2012-05-17 16:01:54 -070056 mCollection = collection;
Michael Wright8ebac232014-09-18 18:29:49 -070057 mPriority = priority;
Yohei Yukawa5660fad2016-01-27 22:04:09 -080058 mLocales = locales;
Michael Wright07483422015-10-30 16:20:13 +000059 mVendorId = vid;
60 mProductId = pid;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070061 }
62
63 private KeyboardLayout(Parcel source) {
64 mDescriptor = source.readString();
65 mLabel = source.readString();
Jeff Brownd9fec5d2012-05-17 16:01:54 -070066 mCollection = source.readString();
Michael Wright8ebac232014-09-18 18:29:49 -070067 mPriority = source.readInt();
Yohei Yukawa5660fad2016-01-27 22:04:09 -080068 mLocales = LocaleList.CREATOR.createFromParcel(source);
Michael Wright07483422015-10-30 16:20:13 +000069 mVendorId = source.readInt();
70 mProductId = source.readInt();
Jeff Brown9f25b7f2012-04-10 14:30:49 -070071 }
72
73 /**
74 * Gets the keyboard layout descriptor, which can be used to retrieve
75 * the keyboard layout again later using
76 * {@link InputManager#getKeyboardLayout(String)}.
77 *
78 * @return The keyboard layout descriptor.
79 */
80 public String getDescriptor() {
81 return mDescriptor;
82 }
83
84 /**
85 * Gets the keyboard layout descriptive label to show in the user interface.
86 * @return The keyboard layout descriptive label.
87 */
88 public String getLabel() {
89 return mLabel;
90 }
91
Jeff Brownd9fec5d2012-05-17 16:01:54 -070092 /**
93 * Gets the name of the collection to which the keyboard layout belongs. This is
94 * the label of the broadcast receiver or application that provided the keyboard layout.
95 * @return The keyboard layout collection name.
96 */
97 public String getCollection() {
98 return mCollection;
99 }
100
Michael Wright07483422015-10-30 16:20:13 +0000101 /**
102 * Gets the locales that this keyboard layout is intended for.
103 * This may be empty if a locale has not been assigned to this keyboard layout.
104 * @return The keyboard layout's intended locale.
105 */
Yohei Yukawa5660fad2016-01-27 22:04:09 -0800106 public LocaleList getLocales() {
Michael Wright07483422015-10-30 16:20:13 +0000107 return mLocales;
108 }
109
110 /**
111 * Gets the vendor ID of the hardware device this keyboard layout is intended for.
112 * Returns -1 if this is not specific to any piece of hardware.
113 * @return The hardware vendor ID of the keyboard layout's intended device.
114 */
115 public int getVendorId() {
116 return mVendorId;
117 }
118
119 /**
120 * Gets the product ID of the hardware device this keyboard layout is intended for.
121 * Returns -1 if this is not specific to any piece of hardware.
122 * @return The hardware product ID of the keyboard layout's intended device.
123 */
124 public int getProductId() {
125 return mProductId;
126 }
127
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700128 @Override
129 public int describeContents() {
130 return 0;
131 }
132
133 @Override
134 public void writeToParcel(Parcel dest, int flags) {
135 dest.writeString(mDescriptor);
136 dest.writeString(mLabel);
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700137 dest.writeString(mCollection);
Michael Wright8ebac232014-09-18 18:29:49 -0700138 dest.writeInt(mPriority);
Yohei Yukawa5660fad2016-01-27 22:04:09 -0800139 mLocales.writeToParcel(dest, 0);
Michael Wright07483422015-10-30 16:20:13 +0000140 dest.writeInt(mVendorId);
141 dest.writeInt(mProductId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700142 }
143
144 @Override
145 public int compareTo(KeyboardLayout another) {
Michael Wright8ebac232014-09-18 18:29:49 -0700146 // Note that these arguments are intentionally flipped since you want higher priority
147 // keyboards to be listed before lower priority keyboards.
148 int result = Integer.compare(another.mPriority, mPriority);
149 if (result == 0) {
150 result = mLabel.compareToIgnoreCase(another.mLabel);
151 }
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700152 if (result == 0) {
153 result = mCollection.compareToIgnoreCase(another.mCollection);
154 }
155 return result;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700156 }
157
158 @Override
159 public String toString() {
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700160 if (mCollection.isEmpty()) {
161 return mLabel;
162 }
163 return mLabel + " - " + mCollection;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700164 }
Michael Wright8ebac232014-09-18 18:29:49 -0700165}