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