1/*
2 * Copyright (C) 2015 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.bluetooth;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.Log;
22
23import java.util.HashMap;
24import java.util.Map;
25
26/**
27 * Class used to identify settings associated with the player on AG.
28 *
29 * {@hide}
30 */
31public final class BluetoothAvrcpPlayerSettings implements Parcelable {
32    public static final String TAG = "BluetoothAvrcpPlayerSettings";
33
34    /**
35     * Equalizer setting.
36     */
37    public static final int SETTING_EQUALIZER = 0x01;
38
39    /**
40     * Repeat setting.
41     */
42    public static final int SETTING_REPEAT = 0x02;
43
44    /**
45     * Shuffle setting.
46     */
47    public static final int SETTING_SHUFFLE = 0x04;
48
49    /**
50     * Scan mode setting.
51     */
52    public static final int SETTING_SCAN = 0x08;
53
54    /**
55     * Invalid state.
56     *
57     * Used for returning error codes.
58     */
59    public static final int STATE_INVALID = -1;
60
61    /**
62     * OFF state.
63     *
64     * Denotes a general OFF state. Applies to all settings.
65     */
66    public static final int STATE_OFF = 0x00;
67
68    /**
69     * ON state.
70     *
71     * Applies to {@link SETTING_EQUALIZER}.
72     */
73    public static final int STATE_ON = 0x01;
74
75    /**
76     * Single track repeat.
77     *
78     * Applies only to {@link SETTING_REPEAT}.
79     */
80    public static final int STATE_SINGLE_TRACK = 0x02;
81
82    /**
83     * All track repeat/shuffle.
84     *
85     * Applies to {@link #SETTING_REPEAT}, {@link #SETTING_SHUFFLE} and {@link #SETTING_SCAN}.
86     */
87    public static final int STATE_ALL_TRACK = 0x03;
88
89    /**
90     * Group repeat/shuffle.
91     *
92     * Applies to {@link #SETTING_REPEAT}, {@link #SETTING_SHUFFLE} and {@link #SETTING_SCAN}.
93     */
94    public static final int STATE_GROUP = 0x04;
95
96    /**
97     * List of supported settings ORed.
98     */
99    private int mSettings;
100
101    /**
102     * Hash map of current capability values.
103     */
104    private Map<Integer, Integer> mSettingsValue = new HashMap<Integer, Integer>();
105
106    @Override
107    public int describeContents() {
108        return 0;
109    }
110
111    @Override
112    public void writeToParcel(Parcel out, int flags) {
113        out.writeInt(mSettings);
114        out.writeInt(mSettingsValue.size());
115        for (int k : mSettingsValue.keySet()) {
116            out.writeInt(k);
117            out.writeInt(mSettingsValue.get(k));
118        }
119    }
120
121    public static final Parcelable.Creator<BluetoothAvrcpPlayerSettings> CREATOR =
122            new Parcelable.Creator<BluetoothAvrcpPlayerSettings>() {
123        public BluetoothAvrcpPlayerSettings createFromParcel(Parcel in) {
124            return new BluetoothAvrcpPlayerSettings(in);
125        }
126
127        public BluetoothAvrcpPlayerSettings[] newArray(int size) {
128            return new BluetoothAvrcpPlayerSettings[size];
129        }
130    };
131
132    private BluetoothAvrcpPlayerSettings(Parcel in) {
133        mSettings = in.readInt();
134        int numSettings = in.readInt();
135        for (int i = 0; i < numSettings; i++) {
136            mSettingsValue.put(in.readInt(), in.readInt());
137        }
138    }
139
140    /**
141     * Create a new player settings object.
142     *
143     * @param settings a ORed value of SETTINGS_* defined above.
144     */
145    public BluetoothAvrcpPlayerSettings(int settings) {
146        mSettings = settings;
147    }
148
149    /**
150     * Get the supported settings.
151     *
152     * @return int ORed value of supported settings.
153     */
154    public int getSettings() {
155        return mSettings;
156    }
157
158    /**
159     * Add a setting value.
160     *
161     * The setting must be part of possible settings in {@link getSettings()}.
162     *
163     * @param setting setting config.
164     * @param value value for the setting.
165     * @throws IllegalStateException if the setting is not supported.
166     */
167    public void addSettingValue(int setting, int value) {
168        if ((setting & mSettings) == 0) {
169            Log.e(TAG, "Setting not supported: " + setting + " " + mSettings);
170            throw new IllegalStateException("Setting not supported: " + setting);
171        }
172        mSettingsValue.put(setting, value);
173    }
174
175    /**
176     * Get a setting value.
177     *
178     * The setting must be part of possible settings in {@link getSettings()}.
179     *
180     * @param setting setting config.
181     * @return value value for the setting.
182     * @throws IllegalStateException if the setting is not supported.
183     */
184    public int getSettingValue(int setting) {
185        if ((setting & mSettings) == 0) {
186            Log.e(TAG, "Setting not supported: " + setting + " " + mSettings);
187            throw new IllegalStateException("Setting not supported: " + setting);
188        }
189        Integer i = mSettingsValue.get(setting);
190        if (i == null) return -1;
191        return i;
192    }
193}
194