blob: bcb8592183e9c910c691d73b3ea82a96f622d46d [file] [log] [blame]
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -07001/*
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
rongliu55492662018-03-21 14:34:58 -070019import android.annotation.IntDef;
20import android.annotation.SystemApi;
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070021import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.io.PrintWriter;
rongliu55492662018-03-21 14:34:58 -070025import java.lang.annotation.Retention;
26import java.lang.annotation.RetentionPolicy;
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070027
28/**
29 * Display properties to be used by VR mode when creating a virtual display.
30 *
31 * @hide
32 */
rongliu55492662018-03-21 14:34:58 -070033@SystemApi
Santos Cordon627a68f2017-06-12 17:57:17 -070034public final class Vr2dDisplayProperties implements Parcelable {
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070035
Santos Cordon627a68f2017-06-12 17:57:17 -070036 public static final int FLAG_VIRTUAL_DISPLAY_ENABLED = 1;
37
Pat Plunkett74d3fa52018-09-04 10:45:25 -070038 /** @hide */
rongliu55492662018-03-21 14:34:58 -070039 @Retention(RetentionPolicy.SOURCE)
40 @IntDef({
41 FLAG_VIRTUAL_DISPLAY_ENABLED
42 })
43 public @interface Vr2dDisplayFlag {}
44
Santos Cordon627a68f2017-06-12 17:57:17 -070045 /**
46 * The actual width, height and dpi.
47 */
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070048 private final int mWidth;
49 private final int mHeight;
50 private final int mDpi;
51
Santos Cordon627a68f2017-06-12 17:57:17 -070052 // Flags describing the virtual display behavior.
53 private final int mAddedFlags;
54 private final int mRemovedFlags;
55
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -070056 public Vr2dDisplayProperties(int width, int height, int dpi) {
Santos Cordon627a68f2017-06-12 17:57:17 -070057 this(width, height, dpi, 0, 0);
58 }
59
60 private Vr2dDisplayProperties(int width, int height, int dpi, int flags, int removedFlags) {
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070061 mWidth = width;
62 mHeight = height;
63 mDpi = dpi;
Santos Cordon627a68f2017-06-12 17:57:17 -070064 mAddedFlags = flags;
65 mRemovedFlags = removedFlags;
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070066 }
67
68 @Override
69 public int hashCode() {
70 int result = getWidth();
71 result = 31 * result + getHeight();
72 result = 31 * result + getDpi();
73 return result;
74 }
75
76 @Override
77 public String toString() {
Santos Cordon627a68f2017-06-12 17:57:17 -070078 return "Vr2dDisplayProperties{"
79 + "mWidth=" + mWidth
80 + ", mHeight=" + mHeight
81 + ", mDpi=" + mDpi
82 + ", flags=" + toReadableFlags(mAddedFlags)
83 + ", removed_flags=" + toReadableFlags(mRemovedFlags)
84 + "}";
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070085 }
86
87 @Override
88 public boolean equals(Object o) {
89 if (this == o) return true;
90 if (o == null || getClass() != o.getClass()) return false;
91
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -070092 Vr2dDisplayProperties that = (Vr2dDisplayProperties) o;
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070093
rongliu55492662018-03-21 14:34:58 -070094 if (getAddedFlags() != that.getAddedFlags()) return false;
Santos Cordon627a68f2017-06-12 17:57:17 -070095 if (getRemovedFlags() != that.getRemovedFlags()) return false;
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -070096 if (getWidth() != that.getWidth()) return false;
97 if (getHeight() != that.getHeight()) return false;
98 return getDpi() == that.getDpi();
99 }
100
101 @Override
102 public int describeContents() {
103 return 0;
104 }
105
106 @Override
107 public void writeToParcel(Parcel dest, int flags) {
108 dest.writeInt(mWidth);
109 dest.writeInt(mHeight);
110 dest.writeInt(mDpi);
Santos Cordon627a68f2017-06-12 17:57:17 -0700111 dest.writeInt(mAddedFlags);
112 dest.writeInt(mRemovedFlags);
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700113 }
114
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700115 public static final @android.annotation.NonNull Parcelable.Creator<Vr2dDisplayProperties> CREATOR
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700116 = new Parcelable.Creator<Vr2dDisplayProperties>() {
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700117 @Override
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700118 public Vr2dDisplayProperties createFromParcel(Parcel source) {
119 return new Vr2dDisplayProperties(source);
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700120 }
121
122 @Override
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700123 public Vr2dDisplayProperties[] newArray(int size) {
124 return new Vr2dDisplayProperties[size];
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700125 }
126 };
127
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700128 private Vr2dDisplayProperties(Parcel source) {
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700129 mWidth = source.readInt();
130 mHeight = source.readInt();
131 mDpi = source.readInt();
Santos Cordon627a68f2017-06-12 17:57:17 -0700132 mAddedFlags = source.readInt();
133 mRemovedFlags = source.readInt();
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700134 }
135
rongliu55492662018-03-21 14:34:58 -0700136 /**
137 * Prints out dump info.
138 */
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700139 public void dump(PrintWriter pw, String prefix) {
Santos Cordon627a68f2017-06-12 17:57:17 -0700140 pw.println(prefix + toString());
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700141 }
142
rongliu55492662018-03-21 14:34:58 -0700143 /**
144 * Returns the width of VR 2d display.
145 */
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700146 public int getWidth() {
147 return mWidth;
148 }
149
rongliu55492662018-03-21 14:34:58 -0700150 /**
151 * Returns the height of VR 2d display.
152 */
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700153 public int getHeight() {
154 return mHeight;
155 }
156
rongliu55492662018-03-21 14:34:58 -0700157 /**
158 * Returns the dpi of VR 2d display.
159 */
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700160 public int getDpi() {
161 return mDpi;
162 }
Santos Cordon627a68f2017-06-12 17:57:17 -0700163
rongliu55492662018-03-21 14:34:58 -0700164 /**
165 * Returns the added flags of VR 2d display. Flags are combined by logic or.
166 */
167 @Vr2dDisplayFlag
168 public int getAddedFlags() {
Santos Cordon627a68f2017-06-12 17:57:17 -0700169 return mAddedFlags;
170 }
171
rongliu55492662018-03-21 14:34:58 -0700172 /**
173 * Returns the removed flags of VR 2d display. Flags are combined by logic or.
174 */
175 @Vr2dDisplayFlag
Santos Cordon627a68f2017-06-12 17:57:17 -0700176 public int getRemovedFlags() {
177 return mRemovedFlags;
178 }
179
180 private static String toReadableFlags(int flags) {
181 String retval = "{";
182 if ((flags & FLAG_VIRTUAL_DISPLAY_ENABLED) == FLAG_VIRTUAL_DISPLAY_ENABLED) {
183 retval += "enabled";
184 }
185 return retval + "}";
186 }
187
188 /**
189 * Convenience class for creating Vr2dDisplayProperties.
190 */
191 public static class Builder {
192 private int mAddedFlags = 0;
193 private int mRemovedFlags = 0;
194
195 // Negative values are translated as an "ignore" to VrManagerService.
196 private int mWidth = -1;
197 private int mHeight = -1;
198 private int mDpi = -1;
199
200 public Builder() {
201 }
202
203 /**
204 * Sets the dimensions to use for the virtual display.
205 */
206 public Builder setDimensions(int width, int height, int dpi) {
207 mWidth = width;
208 mHeight = height;
209 mDpi = dpi;
210 return this;
211 }
212
213 /**
214 * Toggles the virtual display functionality for 2D activities in VR.
215 */
216 public Builder setEnabled(boolean enabled) {
217 if (enabled) {
218 addFlags(FLAG_VIRTUAL_DISPLAY_ENABLED);
219 } else {
220 removeFlags(FLAG_VIRTUAL_DISPLAY_ENABLED);
221 }
222 return this;
223 }
224
225 /**
226 * Adds property flags.
227 */
rongliu55492662018-03-21 14:34:58 -0700228 public Builder addFlags(@Vr2dDisplayFlag int flags) {
Santos Cordon627a68f2017-06-12 17:57:17 -0700229 mAddedFlags |= flags;
230 mRemovedFlags &= ~flags;
231 return this;
232 }
233
234 /**
235 * Removes property flags.
236 */
rongliu55492662018-03-21 14:34:58 -0700237 public Builder removeFlags(@Vr2dDisplayFlag int flags) {
Santos Cordon627a68f2017-06-12 17:57:17 -0700238 mRemovedFlags |= flags;
239 mAddedFlags &= ~flags;
240 return this;
241 }
242
243 /**
244 * Builds the Vr2dDisplayProperty instance.
245 */
246 public Vr2dDisplayProperties build() {
247 return new Vr2dDisplayProperties(mWidth, mHeight, mDpi, mAddedFlags, mRemovedFlags);
248 }
249 }
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700250}