blob: e118edd8dbb83e1af738e50d08781acdaa682910 [file] [log] [blame]
Zak Cohen56345f42017-01-26 13:54:28 -08001package android.app;
2
rongliu55492662018-03-21 14:34:58 -07003import android.annotation.CallbackExecutor;
Santos Cordone6d77232017-08-08 17:11:41 -07004import android.annotation.NonNull;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06005import android.annotation.RequiresPermission;
Zak Cohen56345f42017-01-26 13:54:28 -08006import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06007import android.annotation.SystemService;
Tarandeep Singh89a6c482017-11-21 14:26:11 -08008import android.annotation.TestApi;
Ruben Brunk52ea6622017-10-02 23:51:25 -07009import android.content.ComponentName;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060010import android.content.Context;
Zak Cohen56345f42017-01-26 13:54:28 -080011import android.os.RemoteException;
Santos Cordone6d77232017-08-08 17:11:41 -070012import android.service.vr.IPersistentVrStateCallbacks;
Zak Cohen56345f42017-01-26 13:54:28 -080013import android.service.vr.IVrManager;
Santos Cordone6d77232017-08-08 17:11:41 -070014import android.service.vr.IVrStateCallbacks;
15import android.util.ArrayMap;
rongliu55492662018-03-21 14:34:58 -070016import android.view.Display;
Santos Cordone6d77232017-08-08 17:11:41 -070017
18import java.util.Map;
rongliu55492662018-03-21 14:34:58 -070019import java.util.concurrent.Executor;
Zak Cohen56345f42017-01-26 13:54:28 -080020
21/**
22 * Used to control aspects of a devices Virtual Reality (VR) capabilities.
Zak Cohen56345f42017-01-26 13:54:28 -080023 * @hide
24 */
25@SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026@SystemService(Context.VR_SERVICE)
Zak Cohen56345f42017-01-26 13:54:28 -080027public class VrManager {
Santos Cordone6d77232017-08-08 17:11:41 -070028
29 private static class CallbackEntry {
30 final IVrStateCallbacks mStateCallback = new IVrStateCallbacks.Stub() {
31 @Override
32 public void onVrStateChanged(boolean enabled) {
rongliu55492662018-03-21 14:34:58 -070033 mExecutor.execute(() -> mCallback.onVrStateChanged(enabled));
Santos Cordone6d77232017-08-08 17:11:41 -070034 }
35
36 };
37 final IPersistentVrStateCallbacks mPersistentStateCallback =
38 new IPersistentVrStateCallbacks.Stub() {
39 @Override
40 public void onPersistentVrStateChanged(boolean enabled) {
rongliu55492662018-03-21 14:34:58 -070041 mExecutor.execute(() -> mCallback.onPersistentVrStateChanged(enabled));
Santos Cordone6d77232017-08-08 17:11:41 -070042 }
43 };
44 final VrStateCallback mCallback;
rongliu55492662018-03-21 14:34:58 -070045 final Executor mExecutor;
Santos Cordone6d77232017-08-08 17:11:41 -070046
rongliu55492662018-03-21 14:34:58 -070047 CallbackEntry(VrStateCallback callback, Executor executor) {
Santos Cordone6d77232017-08-08 17:11:41 -070048 mCallback = callback;
rongliu55492662018-03-21 14:34:58 -070049 mExecutor = executor;
Santos Cordone6d77232017-08-08 17:11:41 -070050 }
51 }
52
Zak Cohen56345f42017-01-26 13:54:28 -080053 private final IVrManager mService;
Santos Cordone6d77232017-08-08 17:11:41 -070054 private Map<VrStateCallback, CallbackEntry> mCallbackMap = new ArrayMap<>();
Zak Cohen56345f42017-01-26 13:54:28 -080055
56 /**
57 * {@hide}
58 */
59 public VrManager(IVrManager service) {
60 mService = service;
61 }
62
63 /**
Santos Cordone6d77232017-08-08 17:11:41 -070064 * Registers a callback to be notified of changes to the VR Mode state.
65 *
66 * @param callback The callback to register.
Santos Cordone6d77232017-08-08 17:11:41 -070067 */
Santos Cordon01a4ea52017-08-31 16:36:01 -070068 @RequiresPermission(anyOf = {
69 android.Manifest.permission.RESTRICTED_VR_ACCESS,
70 android.Manifest.permission.ACCESS_VR_STATE
71 })
rongliu55492662018-03-21 14:34:58 -070072 public void registerVrStateCallback(@NonNull @CallbackExecutor Executor executor,
73 VrStateCallback callback) {
Santos Cordone6d77232017-08-08 17:11:41 -070074 if (callback == null || mCallbackMap.containsKey(callback)) {
75 return;
76 }
77
rongliu55492662018-03-21 14:34:58 -070078 CallbackEntry entry = new CallbackEntry(callback, executor);
Santos Cordone6d77232017-08-08 17:11:41 -070079 mCallbackMap.put(callback, entry);
80 try {
81 mService.registerListener(entry.mStateCallback);
82 mService.registerPersistentVrStateListener(entry.mPersistentStateCallback);
83 } catch (RemoteException e) {
84 try {
85 unregisterVrStateCallback(callback);
86 } catch (Exception ignore) {
87 e.rethrowFromSystemServer();
88 }
89 }
90 }
91
92 /**
93 * Deregisters VR State callbacks.
94 *
95 * @param callback The callback to deregister.
Santos Cordone6d77232017-08-08 17:11:41 -070096 */
Santos Cordon01a4ea52017-08-31 16:36:01 -070097 @RequiresPermission(anyOf = {
98 android.Manifest.permission.RESTRICTED_VR_ACCESS,
99 android.Manifest.permission.ACCESS_VR_STATE
100 })
Santos Cordone6d77232017-08-08 17:11:41 -0700101 public void unregisterVrStateCallback(VrStateCallback callback) {
102 CallbackEntry entry = mCallbackMap.remove(callback);
103 if (entry != null) {
104 try {
105 mService.unregisterListener(entry.mStateCallback);
106 } catch (RemoteException ignore) {
107 // Dont rethrow exceptions from requests to unregister.
108 }
109
110 try {
111 mService.unregisterPersistentVrStateListener(entry.mPersistentStateCallback);
112 } catch (RemoteException ignore) {
113 // Dont rethrow exceptions from requests to unregister.
114 }
115 }
116 }
117
118 /**
119 * Returns the current VrMode state.
Santos Cordone6d77232017-08-08 17:11:41 -0700120 */
Santos Cordon01a4ea52017-08-31 16:36:01 -0700121 @RequiresPermission(anyOf = {
122 android.Manifest.permission.RESTRICTED_VR_ACCESS,
123 android.Manifest.permission.ACCESS_VR_STATE
124 })
rongliu55492662018-03-21 14:34:58 -0700125 public boolean isVrModeEnabled() {
Santos Cordone6d77232017-08-08 17:11:41 -0700126 try {
127 return mService.getVrModeState();
128 } catch (RemoteException e) {
129 e.rethrowFromSystemServer();
130 }
131 return false;
132 }
133
134 /**
135 * Returns the current VrMode state.
Santos Cordone6d77232017-08-08 17:11:41 -0700136 */
Santos Cordon01a4ea52017-08-31 16:36:01 -0700137 @RequiresPermission(anyOf = {
138 android.Manifest.permission.RESTRICTED_VR_ACCESS,
139 android.Manifest.permission.ACCESS_VR_STATE
140 })
rongliu55492662018-03-21 14:34:58 -0700141 public boolean isPersistentVrModeEnabled() {
Santos Cordone6d77232017-08-08 17:11:41 -0700142 try {
143 return mService.getPersistentVrModeEnabled();
144 } catch (RemoteException e) {
145 e.rethrowFromSystemServer();
146 }
147 return false;
148 }
149
150 /**
Zak Cohen56345f42017-01-26 13:54:28 -0800151 * Sets the persistent VR mode state of a device. When a device is in persistent VR mode it will
152 * remain in VR mode even if the foreground does not specify Vr mode being enabled. Mainly used
153 * by VR viewers to indicate that a device is placed in a VR viewer.
154 *
Zak Cohen56345f42017-01-26 13:54:28 -0800155 * @see Activity#setVrModeEnabled(boolean, ComponentName)
156 * @param enabled true if the device should be placed in persistent VR mode.
157 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600158 @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS)
Zak Cohen56345f42017-01-26 13:54:28 -0800159 public void setPersistentVrModeEnabled(boolean enabled) {
160 try {
161 mService.setPersistentVrModeEnabled(enabled);
162 } catch (RemoteException e) {
163 e.rethrowFromSystemServer();
164 }
165 }
Kevin Schoedel28eae8e2017-03-22 11:40:38 -0400166
167 /**
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700168 * Sets the resolution and DPI of the vr2d virtual display used to display 2D
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700169 * applications in VR mode.
170 *
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700171 * @param vr2dDisplayProp properties to be set to the virtual display for
172 * 2D applications in VR mode.
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700173 *
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700174 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600175 @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS)
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700176 public void setVr2dDisplayProperties(
177 Vr2dDisplayProperties vr2dDisplayProp) {
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700178 try {
Karthik Ravi Shankar2b9aaed2017-05-01 01:34:19 -0700179 mService.setVr2dDisplayProperties(vr2dDisplayProp);
Karthik Ravi Shankarcdf9ce72017-04-12 15:31:20 -0700180 } catch (RemoteException e) {
181 e.rethrowFromSystemServer();
182 }
183 }
Ruben Brunk52ea6622017-10-02 23:51:25 -0700184
185 /**
186 * Set the component name of the compositor service to bind.
187 *
188 * @param componentName ComponentName of a Service in the application's compositor process to
189 * bind to, or null to clear the current binding.
190 */
191 @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS)
192 public void setAndBindVrCompositor(ComponentName componentName) {
193 try {
194 mService.setAndBindCompositor(
195 (componentName == null) ? null : componentName.flattenToString());
196 } catch (RemoteException e) {
197 e.rethrowFromSystemServer();
198 }
199 }
Steven Thomas1356ec92017-09-07 11:26:38 -0700200
201 /**
202 * Sets the current standby status of the VR device. Standby mode is only used on standalone vr
203 * devices. Standby mode is a deep sleep state where it's appropriate to turn off vr mode.
204 *
205 * @param standby True if the device is entering standby, false if it's exiting standby.
Steven Thomas1356ec92017-09-07 11:26:38 -0700206 */
207 @RequiresPermission(android.Manifest.permission.ACCESS_VR_MANAGER)
208 public void setStandbyEnabled(boolean standby) {
209 try {
210 mService.setStandbyEnabled(standby);
211 } catch (RemoteException e) {
212 e.rethrowFromSystemServer();
213 }
214 }
Tarandeep Singh89a6c482017-11-21 14:26:11 -0800215
216 /**
217 * Start VR Input method for the packageName in {@link ComponentName}.
218 * This method notifies InputMethodManagerService to use VR IME instead of
219 * regular phone IME.
220 * @param componentName ComponentName of a VR InputMethod that should be set as selected
221 * input by InputMethodManagerService.
Tarandeep Singh89a6c482017-11-21 14:26:11 -0800222 */
223 @TestApi
224 @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS)
225 public void setVrInputMethod(ComponentName componentName) {
226 try {
227 mService.setVrInputMethod(componentName);
228 } catch (RemoteException e) {
229 e.rethrowFromSystemServer();
230 }
231 }
rongliu55492662018-03-21 14:34:58 -0700232
233 /**
234 * Returns the display id of VR's {@link VirtualDisplay}.
235 *
236 * @see DisplayManager#getDisplay(int)
237 */
238 @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS)
239 public int getVr2dDisplayId() {
240 try {
241 return mService.getVr2dDisplayId();
242 } catch (RemoteException e) {
243 e.rethrowFromSystemServer();
244 }
245 return Display.INVALID_DISPLAY;
246 }
Zak Cohen56345f42017-01-26 13:54:28 -0800247}