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
17package android.hardware.input;
18
19import android.annotation.NonNull;
20import android.os.LocaleList;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.util.Locale;
25
26/**
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;
35    private final String mCollection;
36    private final int mPriority;
37    @NonNull
38    private final LocaleList mLocales;
39    private final int mVendorId;
40    private final int mProductId;
41
42    public static final Parcelable.Creator<KeyboardLayout> CREATOR =
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
52    public KeyboardLayout(String descriptor, String label, String collection, int priority,
53            LocaleList locales, int vid, int pid) {
54        mDescriptor = descriptor;
55        mLabel = label;
56        mCollection = collection;
57        mPriority = priority;
58        mLocales = locales;
59        mVendorId = vid;
60        mProductId = pid;
61    }
62
63    private KeyboardLayout(Parcel source) {
64        mDescriptor = source.readString();
65        mLabel = source.readString();
66        mCollection = source.readString();
67        mPriority = source.readInt();
68        mLocales = LocaleList.CREATOR.createFromParcel(source);
69        mVendorId = source.readInt();
70        mProductId = source.readInt();
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
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
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     */
106    public LocaleList getLocales() {
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
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);
137        dest.writeString(mCollection);
138        dest.writeInt(mPriority);
139        mLocales.writeToParcel(dest, 0);
140        dest.writeInt(mVendorId);
141        dest.writeInt(mProductId);
142    }
143
144    @Override
145    public int compareTo(KeyboardLayout another) {
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        }
152        if (result == 0) {
153            result = mCollection.compareToIgnoreCase(another.mCollection);
154        }
155        return result;
156    }
157
158    @Override
159    public String toString() {
160        if (mCollection.isEmpty()) {
161            return mLabel;
162        }
163        return mLabel + " - " + mCollection;
164    }
165}
166