1/*
2 * Copyright (C) 2017 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.app;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.io.PrintWriter;
23
24/**
25 * Display properties to be used by VR mode when creating a virtual display.
26 *
27 * @hide
28 */
29public final class Vr2dDisplayProperties implements Parcelable {
30
31    public static final int FLAG_VIRTUAL_DISPLAY_ENABLED = 1;
32
33    /**
34     * The actual width, height and dpi.
35     */
36    private final int mWidth;
37    private final int mHeight;
38    private final int mDpi;
39
40    // Flags describing the virtual display behavior.
41    private final int mAddedFlags;
42    private final int mRemovedFlags;
43
44    public Vr2dDisplayProperties(int width, int height, int dpi) {
45        this(width, height, dpi, 0, 0);
46    }
47
48    private Vr2dDisplayProperties(int width, int height, int dpi, int flags, int removedFlags) {
49        mWidth = width;
50        mHeight = height;
51        mDpi = dpi;
52        mAddedFlags = flags;
53        mRemovedFlags = removedFlags;
54    }
55
56    @Override
57    public int hashCode() {
58        int result = getWidth();
59        result = 31 * result + getHeight();
60        result = 31 * result + getDpi();
61        return result;
62    }
63
64    @Override
65    public String toString() {
66        return "Vr2dDisplayProperties{"
67                + "mWidth=" + mWidth
68                + ", mHeight=" + mHeight
69                + ", mDpi=" + mDpi
70                + ", flags=" + toReadableFlags(mAddedFlags)
71                + ", removed_flags=" + toReadableFlags(mRemovedFlags)
72                + "}";
73    }
74
75    @Override
76    public boolean equals(Object o) {
77        if (this == o) return true;
78        if (o == null || getClass() != o.getClass()) return false;
79
80        Vr2dDisplayProperties that = (Vr2dDisplayProperties) o;
81
82        if (getFlags() != that.getFlags()) return false;
83        if (getRemovedFlags() != that.getRemovedFlags()) return false;
84        if (getWidth() != that.getWidth()) return false;
85        if (getHeight() != that.getHeight()) return false;
86        return getDpi() == that.getDpi();
87    }
88
89    @Override
90    public int describeContents() {
91        return 0;
92    }
93
94    @Override
95    public void writeToParcel(Parcel dest, int flags) {
96        dest.writeInt(mWidth);
97        dest.writeInt(mHeight);
98        dest.writeInt(mDpi);
99        dest.writeInt(mAddedFlags);
100        dest.writeInt(mRemovedFlags);
101    }
102
103    public static final Parcelable.Creator<Vr2dDisplayProperties> CREATOR
104            = new Parcelable.Creator<Vr2dDisplayProperties>() {
105        @Override
106        public Vr2dDisplayProperties createFromParcel(Parcel source) {
107            return new Vr2dDisplayProperties(source);
108        }
109
110        @Override
111        public Vr2dDisplayProperties[] newArray(int size) {
112            return new Vr2dDisplayProperties[size];
113        }
114    };
115
116    private Vr2dDisplayProperties(Parcel source) {
117        mWidth = source.readInt();
118        mHeight = source.readInt();
119        mDpi = source.readInt();
120        mAddedFlags = source.readInt();
121        mRemovedFlags = source.readInt();
122    }
123
124    public void dump(PrintWriter pw, String prefix) {
125        pw.println(prefix + toString());
126    }
127
128    public int getWidth() {
129        return mWidth;
130    }
131
132    public int getHeight() {
133        return mHeight;
134    }
135
136    public int getDpi() {
137        return mDpi;
138    }
139
140    public int getFlags() {
141        return mAddedFlags;
142    }
143
144    public int getRemovedFlags() {
145        return mRemovedFlags;
146    }
147
148    private static String toReadableFlags(int flags) {
149        String retval = "{";
150        if ((flags & FLAG_VIRTUAL_DISPLAY_ENABLED) == FLAG_VIRTUAL_DISPLAY_ENABLED) {
151            retval += "enabled";
152        }
153        return retval + "}";
154    }
155
156    /**
157     * Convenience class for creating Vr2dDisplayProperties.
158     */
159    public static class Builder {
160        private int mAddedFlags = 0;
161        private int mRemovedFlags = 0;
162
163        // Negative values are translated as an "ignore" to VrManagerService.
164        private int mWidth = -1;
165        private int mHeight = -1;
166        private int mDpi = -1;
167
168        public Builder() {
169        }
170
171        /**
172         * Sets the dimensions to use for the virtual display.
173         */
174        public Builder setDimensions(int width, int height, int dpi) {
175            mWidth = width;
176            mHeight = height;
177            mDpi = dpi;
178            return this;
179        }
180
181        /**
182         * Toggles the virtual display functionality for 2D activities in VR.
183         */
184        public Builder setEnabled(boolean enabled) {
185            if (enabled) {
186                addFlags(FLAG_VIRTUAL_DISPLAY_ENABLED);
187            } else {
188                removeFlags(FLAG_VIRTUAL_DISPLAY_ENABLED);
189            }
190            return this;
191        }
192
193        /**
194         * Adds property flags.
195         */
196        public Builder addFlags(int flags) {
197            mAddedFlags |= flags;
198            mRemovedFlags &= ~flags;
199            return this;
200        }
201
202        /**
203         * Removes property flags.
204         */
205        public Builder removeFlags(int flags) {
206            mRemovedFlags |= flags;
207            mAddedFlags &= ~flags;
208            return this;
209        }
210
211        /**
212         * Builds the Vr2dDisplayProperty instance.
213         */
214        public Vr2dDisplayProperties build() {
215            return new Vr2dDisplayProperties(mWidth, mHeight, mDpi, mAddedFlags, mRemovedFlags);
216        }
217    }
218}
219