Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.hardware.input; |
| 18 | |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Yohei Yukawa | 23cbe85 | 2016-05-17 16:42:58 -0700 | [diff] [blame] | 20 | import android.os.LocaleList; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 24 | import java.util.Locale; |
| 25 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Describes a keyboard layout. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
| 31 | public final class KeyboardLayout implements Parcelable, |
| 32 | Comparable<KeyboardLayout> { |
| 33 | private final String mDescriptor; |
| 34 | private final String mLabel; |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 35 | private final String mCollection; |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 36 | private final int mPriority; |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 37 | @NonNull |
| 38 | private final LocaleList mLocales; |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 39 | private final int mVendorId; |
| 40 | private final int mProductId; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 41 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 42 | public static final @android.annotation.NonNull Parcelable.Creator<KeyboardLayout> CREATOR = |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 43 | 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 Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 52 | public KeyboardLayout(String descriptor, String label, String collection, int priority, |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 53 | LocaleList locales, int vid, int pid) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 54 | mDescriptor = descriptor; |
| 55 | mLabel = label; |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 56 | mCollection = collection; |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 57 | mPriority = priority; |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 58 | mLocales = locales; |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 59 | mVendorId = vid; |
| 60 | mProductId = pid; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private KeyboardLayout(Parcel source) { |
| 64 | mDescriptor = source.readString(); |
| 65 | mLabel = source.readString(); |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 66 | mCollection = source.readString(); |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 67 | mPriority = source.readInt(); |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 68 | mLocales = LocaleList.CREATOR.createFromParcel(source); |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 69 | mVendorId = source.readInt(); |
| 70 | mProductId = source.readInt(); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 71 | } |
| 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 Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 92 | /** |
| 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 Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 101 | /** |
| 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 Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 106 | public LocaleList getLocales() { |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 107 | 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 Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 128 | @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 Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 137 | dest.writeString(mCollection); |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 138 | dest.writeInt(mPriority); |
Yohei Yukawa | 5660fad | 2016-01-27 22:04:09 -0800 | [diff] [blame] | 139 | mLocales.writeToParcel(dest, 0); |
Michael Wright | 0748342 | 2015-10-30 16:20:13 +0000 | [diff] [blame] | 140 | dest.writeInt(mVendorId); |
| 141 | dest.writeInt(mProductId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public int compareTo(KeyboardLayout another) { |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 146 | // 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 Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 152 | if (result == 0) { |
| 153 | result = mCollection.compareToIgnoreCase(another.mCollection); |
| 154 | } |
| 155 | return result; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public String toString() { |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 160 | if (mCollection.isEmpty()) { |
| 161 | return mLabel; |
| 162 | } |
| 163 | return mLabel + " - " + mCollection; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 164 | } |
Michael Wright | 8ebac23 | 2014-09-18 18:29:49 -0700 | [diff] [blame] | 165 | } |