File: DeviceFeatures.java

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,084 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 120; sed: 19
file content (395 lines) | stat: -rw-r--r-- 14,929 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright (C) 2021 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.hardware.hdmi;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;

/**
 * Immutable class that stores support status for features in the [Device Features] operand.
 * Each feature may be supported, be not supported, or have an unknown support status.
 *
 * @hide
 */
public class DeviceFeatures {

    @IntDef({
            FEATURE_NOT_SUPPORTED,
            FEATURE_SUPPORTED,
            FEATURE_SUPPORT_UNKNOWN
    })
    public @interface FeatureSupportStatus {};

    public static final int FEATURE_NOT_SUPPORTED = 0;
    public static final int FEATURE_SUPPORTED = 1;
    public static final int FEATURE_SUPPORT_UNKNOWN = 2;

    /**
     * Instance representing no knowledge of any feature's support.
     */
    @NonNull
    public static final DeviceFeatures ALL_FEATURES_SUPPORT_UNKNOWN =
            new Builder(FEATURE_SUPPORT_UNKNOWN).build();

    /**
     * Instance representing no support for any feature.
     */
    @NonNull
    public static final DeviceFeatures NO_FEATURES_SUPPORTED =
            new Builder(FEATURE_NOT_SUPPORTED).build();

    @FeatureSupportStatus private final int mRecordTvScreenSupport;
    @FeatureSupportStatus private final int mSetOsdStringSupport;
    @FeatureSupportStatus private final int mDeckControlSupport;
    @FeatureSupportStatus private final int mSetAudioRateSupport;
    @FeatureSupportStatus private final int mArcTxSupport;
    @FeatureSupportStatus private final int mArcRxSupport;
    @FeatureSupportStatus private final int mSetAudioVolumeLevelSupport;

    private DeviceFeatures(@NonNull Builder builder) {
        this.mRecordTvScreenSupport = builder.mRecordTvScreenSupport;
        this.mSetOsdStringSupport = builder.mOsdStringSupport;
        this.mDeckControlSupport = builder.mDeckControlSupport;
        this.mSetAudioRateSupport = builder.mSetAudioRateSupport;
        this.mArcTxSupport = builder.mArcTxSupport;
        this.mArcRxSupport = builder.mArcRxSupport;
        this.mSetAudioVolumeLevelSupport = builder.mSetAudioVolumeLevelSupport;
    }

    /**
     * Converts an instance to a builder.
     */
    public Builder toBuilder() {
        return new Builder(this);
    }

    /**
     * Constructs an instance from a [Device Features] operand.
     *
     * Bit 7 of [Device Features] is currently ignored. It indicates whether the operand spans more
     * than one byte, but only the first byte contains information as of CEC 2.0.
     *
     * @param deviceFeaturesOperand The [Device Features] operand to parse.
     * @return Instance representing the [Device Features] operand.
     */
    @NonNull
    public static DeviceFeatures fromOperand(@NonNull byte[] deviceFeaturesOperand) {
        Builder builder = new Builder(FEATURE_SUPPORT_UNKNOWN);

        // Read feature support status from the bits of [Device Features]
        if (deviceFeaturesOperand.length >= 1) {
            byte b = deviceFeaturesOperand[0];
            builder
                    .setRecordTvScreenSupport(bitToFeatureSupportStatus(((b >> 6) & 1) == 1))
                    .setSetOsdStringSupport(bitToFeatureSupportStatus(((b >> 5) & 1) == 1))
                    .setDeckControlSupport(bitToFeatureSupportStatus(((b >> 4) & 1) == 1))
                    .setSetAudioRateSupport(bitToFeatureSupportStatus(((b >> 3) & 1) == 1))
                    .setArcTxSupport(bitToFeatureSupportStatus(((b >> 2) & 1) == 1))
                    .setArcRxSupport(bitToFeatureSupportStatus(((b >> 1) & 1) == 1))
                    .setSetAudioVolumeLevelSupport(bitToFeatureSupportStatus((b & 1) == 1));
        }
        return builder.build();
    }

    /**
     * Returns the input that is not {@link #FEATURE_SUPPORT_UNKNOWN}. If neither is equal to
     * {@link #FEATURE_SUPPORT_UNKNOWN}, returns the second input.
     */
    private static @FeatureSupportStatus int updateFeatureSupportStatus(
            @FeatureSupportStatus int oldStatus, @FeatureSupportStatus int newStatus) {
        if (newStatus == FEATURE_SUPPORT_UNKNOWN) {
            return oldStatus;
        } else {
            return newStatus;
        }
    }

    /**
     * Returns the [Device Features] operand corresponding to this instance.
     * {@link #FEATURE_SUPPORT_UNKNOWN} maps to 0, indicating no support.
     *
     * As of CEC 2.0, the returned byte array will always be of length 1.
     */
    @NonNull
    public byte[] toOperand() {
        byte result = 0;

        if (mRecordTvScreenSupport == FEATURE_SUPPORTED) result |= (byte) (1 << 6);
        if (mSetOsdStringSupport == FEATURE_SUPPORTED) result = (byte) (1 << 5);
        if (mDeckControlSupport == FEATURE_SUPPORTED) result = (byte) (1 << 4);
        if (mSetAudioRateSupport == FEATURE_SUPPORTED) result = (byte) (1 << 3);
        if (mArcTxSupport == FEATURE_SUPPORTED) result = (byte) (1 << 2);
        if (mArcRxSupport == FEATURE_SUPPORTED) result = (byte) (1 << 1);
        if (mSetAudioVolumeLevelSupport == FEATURE_SUPPORTED) result = (byte) 1;

        return new byte[]{ result };
    }

    @FeatureSupportStatus
    private static int bitToFeatureSupportStatus(boolean bit) {
        return bit ? FEATURE_SUPPORTED : FEATURE_NOT_SUPPORTED;
    }

    /**
     * Returns whether the device is a TV that supports <Record TV Screen>.
     */
    @FeatureSupportStatus
    public int getRecordTvScreenSupport() {
        return mRecordTvScreenSupport;
    }

    /**
     * Returns whether the device is a TV that supports <Set OSD String>.
     */
    @FeatureSupportStatus
    public int getSetOsdStringSupport() {
        return mSetOsdStringSupport;
    }

    /**
     * Returns whether the device supports being controlled by Deck Control.
     */
    @FeatureSupportStatus
    public int getDeckControlSupport() {
        return mDeckControlSupport;
    }

    /**
     * Returns whether the device is a Source that supports <Set Audio Rate>.
     */
    @FeatureSupportStatus
    public int getSetAudioRateSupport() {
        return mSetAudioRateSupport;
    }

    /**
     * Returns whether the device is a Sink that supports ARC Tx.
     */
    @FeatureSupportStatus
    public int getArcTxSupport() {
        return mArcTxSupport;
    }

    /**
     * Returns whether the device is a Source that supports ARC Rx.
     */
    @FeatureSupportStatus
    public int getArcRxSupport() {
        return mArcRxSupport;
    }

    /**
     * Returns whether the device supports <Set Audio Volume Level>.
     */
    @FeatureSupportStatus
    public int getSetAudioVolumeLevelSupport() {
        return mSetAudioVolumeLevelSupport;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (!(obj instanceof DeviceFeatures)) {
            return false;
        }

        DeviceFeatures other = (DeviceFeatures) obj;
        return mRecordTvScreenSupport == other.mRecordTvScreenSupport
                && mSetOsdStringSupport == other.mSetOsdStringSupport
                && mDeckControlSupport == other.mDeckControlSupport
                && mSetAudioRateSupport == other.mSetAudioRateSupport
                && mArcTxSupport == other.mArcTxSupport
                && mArcRxSupport == other.mArcRxSupport
                && mSetAudioVolumeLevelSupport == other.mSetAudioVolumeLevelSupport;
    }

    @Override
    public int hashCode() {
        return java.util.Objects.hash(
                mRecordTvScreenSupport,
                mSetOsdStringSupport,
                mDeckControlSupport,
                mSetAudioRateSupport,
                mArcTxSupport,
                mArcRxSupport,
                mSetAudioVolumeLevelSupport
        );
    }

    @NonNull
    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("Device features: ");
        s.append("record_tv_screen: ")
                .append(featureSupportStatusToString(mRecordTvScreenSupport)).append(" ");
        s.append("set_osd_string: ")
                .append(featureSupportStatusToString(mSetOsdStringSupport)).append(" ");
        s.append("deck_control: ")
                .append(featureSupportStatusToString(mDeckControlSupport)).append(" ");
        s.append("set_audio_rate: ")
                .append(featureSupportStatusToString(mSetAudioRateSupport)).append(" ");
        s.append("arc_tx: ")
                .append(featureSupportStatusToString(mArcTxSupport)).append(" ");
        s.append("arc_rx: ")
                .append(featureSupportStatusToString(mArcRxSupport)).append(" ");
        s.append("set_audio_volume_level: ")
                .append(featureSupportStatusToString(mSetAudioVolumeLevelSupport)).append(" ");
        return s.toString();
    }

    @NonNull
    private static String featureSupportStatusToString(@FeatureSupportStatus int status) {
        switch (status) {
            case FEATURE_SUPPORTED:
                return "Y";
            case FEATURE_NOT_SUPPORTED:
                return "N";
            case FEATURE_SUPPORT_UNKNOWN:
            default:
                return "?";
        }
    }

    /**
     * Builder for {@link DeviceFeatures} instances.
     */
    public static final class Builder {
        @FeatureSupportStatus private int mRecordTvScreenSupport;
        @FeatureSupportStatus private int mOsdStringSupport;
        @FeatureSupportStatus private int mDeckControlSupport;
        @FeatureSupportStatus private int mSetAudioRateSupport;
        @FeatureSupportStatus private int mArcTxSupport;
        @FeatureSupportStatus private int mArcRxSupport;
        @FeatureSupportStatus private int mSetAudioVolumeLevelSupport;

        private Builder(@FeatureSupportStatus int defaultFeatureSupportStatus) {
            mRecordTvScreenSupport = defaultFeatureSupportStatus;
            mOsdStringSupport = defaultFeatureSupportStatus;
            mDeckControlSupport = defaultFeatureSupportStatus;
            mSetAudioRateSupport = defaultFeatureSupportStatus;
            mArcTxSupport = defaultFeatureSupportStatus;
            mArcRxSupport = defaultFeatureSupportStatus;
            mSetAudioVolumeLevelSupport = defaultFeatureSupportStatus;
        }

        private Builder(DeviceFeatures info) {
            mRecordTvScreenSupport = info.getRecordTvScreenSupport();
            mOsdStringSupport = info.getSetOsdStringSupport();
            mDeckControlSupport = info.getDeckControlSupport();
            mSetAudioRateSupport = info.getSetAudioRateSupport();
            mArcTxSupport = info.getArcTxSupport();
            mArcRxSupport = info.getArcRxSupport();
            mSetAudioVolumeLevelSupport = info.getSetAudioVolumeLevelSupport();
        }

        /**
         * Creates a new {@link DeviceFeatures} object.
         */
        public DeviceFeatures build() {
            return new DeviceFeatures(this);
        }

        /**
         * Sets the value for {@link #getRecordTvScreenSupport()}.
         */
        @NonNull
        public Builder setRecordTvScreenSupport(@FeatureSupportStatus int recordTvScreenSupport) {
            mRecordTvScreenSupport = recordTvScreenSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getSetOsdStringSupport()}.
         */
        @NonNull
        public Builder setSetOsdStringSupport(@FeatureSupportStatus int setOsdStringSupport) {
            mOsdStringSupport = setOsdStringSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getDeckControlSupport()}.
         */
        @NonNull
        public Builder setDeckControlSupport(@FeatureSupportStatus int deckControlSupport) {
            mDeckControlSupport = deckControlSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getSetAudioRateSupport()}.
         */
        @NonNull
        public Builder setSetAudioRateSupport(@FeatureSupportStatus int setAudioRateSupport) {
            mSetAudioRateSupport = setAudioRateSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getArcTxSupport()}.
         */
        @NonNull
        public Builder setArcTxSupport(@FeatureSupportStatus int arcTxSupport) {
            mArcTxSupport = arcTxSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getArcRxSupport()}.
         */
        @NonNull
        public Builder setArcRxSupport(@FeatureSupportStatus int arcRxSupport) {
            mArcRxSupport = arcRxSupport;
            return this;
        }

        /**
         * Sets the value for {@link #getSetAudioRateSupport()}.
         */
        @NonNull
        public Builder setSetAudioVolumeLevelSupport(
                @FeatureSupportStatus int setAudioVolumeLevelSupport) {
            mSetAudioVolumeLevelSupport = setAudioVolumeLevelSupport;
            return this;
        }

        /**
         * Updates all fields with those in a 'new' instance of {@link DeviceFeatures}.
         * All fields are replaced with those in the new instance, except when the field is
         * {@link #FEATURE_SUPPORT_UNKNOWN} in the new instance.
         */
        @NonNull
        public Builder update(DeviceFeatures newDeviceFeatures) {
            mRecordTvScreenSupport = updateFeatureSupportStatus(mRecordTvScreenSupport,
                    newDeviceFeatures.getRecordTvScreenSupport());
            mOsdStringSupport = updateFeatureSupportStatus(mOsdStringSupport,
                    newDeviceFeatures.getSetOsdStringSupport());
            mDeckControlSupport = updateFeatureSupportStatus(mDeckControlSupport,
                    newDeviceFeatures.getDeckControlSupport());
            mSetAudioRateSupport = updateFeatureSupportStatus(mSetAudioRateSupport,
                    newDeviceFeatures.getSetAudioRateSupport());
            mArcTxSupport = updateFeatureSupportStatus(mArcTxSupport,
                    newDeviceFeatures.getArcTxSupport());
            mArcRxSupport = updateFeatureSupportStatus(mArcRxSupport,
                    newDeviceFeatures.getArcRxSupport());
            mSetAudioVolumeLevelSupport = updateFeatureSupportStatus(mSetAudioVolumeLevelSupport,
                    newDeviceFeatures.getSetAudioVolumeLevelSupport());
            return this;
        }
    }
}