1/*
2 * Copyright (C) 2016 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 */
16package android.support.car.navigation;
17
18import android.os.Parcel;
19import android.os.Parcelable;
20import android.support.annotation.IntDef;
21import android.support.car.annotation.VersionDef;
22import android.support.car.os.ExtendableParcelable;
23
24import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26
27/**
28 * Holds options related to navigation for the car's instrument cluster.
29 * @hide
30 */
31public class CarNavigationInstrumentCluster extends ExtendableParcelable {
32
33    private static final int VERSION = 1;
34
35    @Retention(RetentionPolicy.SOURCE)
36    @IntDef({
37            ClusterType.CUSTOM_IMAGES_SUPPORTED,
38            ClusterType.IMAGE_CODES_ONLY
39    })
40    public @interface ClusterType {
41        /** Navigation Next Turn messages contain an image, as well as an enum. */
42        int CUSTOM_IMAGES_SUPPORTED = 1;
43        /** Navigation Next Turn messages only contain an enum. */
44        int IMAGE_CODES_ONLY = 2;
45    }
46
47    @VersionDef(version = 1)
48    private int mMinIntervalMs;
49
50    @VersionDef(version = 1)
51    @ClusterType
52    private int mType;
53
54    @VersionDef(version = 1)
55    private int mImageWidth;
56
57    @VersionDef(version = 1)
58    private int mImageHeight;
59
60    @VersionDef(version = 1)
61    private int mImageColorDepthBits;
62
63    public static final Parcelable.Creator<CarNavigationInstrumentCluster> CREATOR
64            = new Parcelable.Creator<CarNavigationInstrumentCluster>() {
65        public CarNavigationInstrumentCluster createFromParcel(Parcel in) {
66            return new CarNavigationInstrumentCluster(in);
67        }
68
69        public CarNavigationInstrumentCluster[] newArray(int size) {
70            return new CarNavigationInstrumentCluster[size];
71        }
72    };
73
74    public static CarNavigationInstrumentCluster createCluster(int minIntervalMs) {
75        return new CarNavigationInstrumentCluster(minIntervalMs, ClusterType.IMAGE_CODES_ONLY,
76                0, 0, 0);
77    }
78
79    public static CarNavigationInstrumentCluster createCustomImageCluster(int minIntervalMs,
80            int imageWidth, int imageHeight, int imageColorDepthBits) {
81        return new CarNavigationInstrumentCluster(minIntervalMs,
82                ClusterType.CUSTOM_IMAGES_SUPPORTED,
83                imageWidth, imageHeight, imageColorDepthBits);
84    }
85
86    /** Minimum time between instrument cluster updates in milliseconds.*/
87    public int getMinIntervalMs() {
88        return mMinIntervalMs;
89    }
90
91    /** Type of instrument cluster, can be image or enum. */
92    @ClusterType
93    public int getType() {
94        return mType;
95    }
96
97    /** If instrument cluster is image, width of instrument cluster in pixels. */
98    public int getImageWidth() {
99        return mImageWidth;
100    }
101
102    /** If instrument cluster is image, height of instrument cluster in pixels. */
103    public int getImageHeight() {
104        return mImageHeight;
105    }
106
107    /** If instrument cluster is image, number of bits of colour depth it supports (8, 16 or 32). */
108    public int getImageColorDepthBits() {
109        return mImageColorDepthBits;
110    }
111
112    public CarNavigationInstrumentCluster(CarNavigationInstrumentCluster that) {
113      this(that.mMinIntervalMs,
114          that.mType,
115          that.mImageWidth,
116          that.mImageHeight,
117          that.mImageColorDepthBits);
118    }
119
120    public boolean supportsCustomImages() {
121      return mType == ClusterType.CUSTOM_IMAGES_SUPPORTED;
122    }
123
124    /** @hide */
125    CarNavigationInstrumentCluster(
126            int minIntervalMs,
127            @ClusterType int type,
128            int imageWidth,
129            int imageHeight,
130            int imageColorDepthBits) {
131        super(VERSION);
132        this.mMinIntervalMs = minIntervalMs;
133        this.mType = type;
134        this.mImageWidth = imageWidth;
135        this.mImageHeight = imageHeight;
136        this.mImageColorDepthBits = imageColorDepthBits;
137    }
138
139    @Override
140    public int describeContents() {
141        return 0;
142    }
143
144    @Override
145    public void writeToParcel(Parcel dest, int flags) {
146        int startingPosition = writeHeader(dest);
147        dest.writeInt(mMinIntervalMs);
148        dest.writeInt(mType);
149        dest.writeInt(mImageWidth);
150        dest.writeInt(mImageHeight);
151        dest.writeInt(mImageColorDepthBits);
152        completeWriting(dest, startingPosition);
153    }
154
155    private CarNavigationInstrumentCluster(Parcel in) {
156        super(in, VERSION);
157        int lastPosition = readHeader(in);
158        mMinIntervalMs = in.readInt();
159        mType = in.readInt();
160        mImageWidth = in.readInt();
161        mImageHeight = in.readInt();
162        mImageColorDepthBits = in.readInt();
163        completeReading(in, lastPosition);
164    }
165
166    /** Converts to string for debug purpose */
167    @Override
168    public String toString() {
169        return CarNavigationInstrumentCluster.class.getSimpleName() + "{ " +
170                "minIntervalMs: " + mMinIntervalMs + ", " +
171                "type: " + mType + ", " +
172                "imageWidth: " + mImageWidth + ", " +
173                "imageHeight: " + mImageHeight + ", " +
174                "imageColourDepthBits: " + mImageColorDepthBits + " }";
175    }
176}
177