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