1 /** 2 * Copyright (C) 2016 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.service.vr; 18 19 import android.annotation.NonNull; 20 import android.annotation.SdkConstant; 21 import android.app.ActivityManager; 22 import android.app.Service; 23 import android.compat.annotation.UnsupportedAppUsage; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.Build; 28 import android.os.Handler; 29 import android.os.IBinder; 30 import android.os.Looper; 31 import android.os.Message; 32 33 /** 34 * A service that is bound from the system while running in virtual reality (VR) mode. 35 * 36 * <p>To extend this class, you must declare the service in your manifest file with 37 * the {@link android.Manifest.permission#BIND_VR_LISTENER_SERVICE} permission 38 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p> 39 * <pre> 40 * <service android:name=".VrListener" 41 * android:label="@string/service_name" 42 * android:permission="android.permission.BIND_VR_LISTENER_SERVICE"> 43 * <intent-filter> 44 * <action android:name="android.service.vr.VrListenerService" /> 45 * </intent-filter> 46 * </service> 47 * </pre> 48 * 49 * <p>This service is bound when the system enters VR mode and is unbound when the system leaves VR 50 * mode.</p> 51 * <p>The system will enter VR mode when an application that has previously called 52 * {@link android.app.Activity#setVrModeEnabled} gains user focus. The system will only start this 53 * service if the VR application has specifically targeted this service by specifying 54 * its {@link ComponentName} in the call to {@link android.app.Activity#setVrModeEnabled} and if 55 * this service is installed and enabled in the current user's settings.</p> 56 * 57 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS 58 * @see android.app.Activity#setVrModeEnabled 59 * @see android.R.attr#enableVrMode 60 */ 61 public abstract class VrListenerService extends Service { 62 63 /** 64 * The {@link Intent} that must be declared as handled by the service. 65 */ 66 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 67 public static final String SERVICE_INTERFACE = "android.service.vr.VrListenerService"; 68 69 private final Handler mHandler; 70 71 private static final int MSG_ON_CURRENT_VR_ACTIVITY_CHANGED = 1; 72 73 private final IVrListener.Stub mBinder = new IVrListener.Stub() { 74 @Override 75 public void focusedActivityChanged( 76 ComponentName component, boolean running2dInVr, int pid) { 77 mHandler.obtainMessage(MSG_ON_CURRENT_VR_ACTIVITY_CHANGED, running2dInVr ? 1 : 0, 78 pid, component).sendToTarget(); 79 } 80 }; 81 82 private final class VrListenerHandler extends Handler { VrListenerHandler(Looper looper)83 public VrListenerHandler(Looper looper) { 84 super(looper); 85 } 86 87 @Override handleMessage(Message msg)88 public void handleMessage(Message msg) { 89 switch (msg.what) { 90 case MSG_ON_CURRENT_VR_ACTIVITY_CHANGED: { 91 VrListenerService.this.onCurrentVrActivityChanged( 92 (ComponentName) msg.obj, msg.arg1 == 1, msg.arg2); 93 } break; 94 } 95 } 96 } 97 98 @Override onBind(Intent intent)99 public IBinder onBind(Intent intent) { 100 return mBinder; 101 } 102 VrListenerService()103 public VrListenerService() { 104 mHandler = new VrListenerHandler(Looper.getMainLooper()); 105 } 106 107 /** 108 * Called when the current activity using VR mode has changed. 109 * 110 * <p>This will be called when this service is initially bound, but is not 111 * guaranteed to be called before onUnbind. In general, this is intended to be used to 112 * determine when user focus has transitioned between two VR activities. If both activities 113 * have declared {@link android.R.attr#enableVrMode} with this service (and this 114 * service is present and enabled), this service will not be unbound during the activity 115 * transition.</p> 116 * 117 * @param component the {@link ComponentName} of the VR activity that the system has 118 * switched to, or null if the system is displaying a 2D activity in VR compatibility mode. 119 * 120 * @see android.app.Activity#setVrModeEnabled 121 * @see android.R.attr#enableVrMode 122 */ onCurrentVrActivityChanged(ComponentName component)123 public void onCurrentVrActivityChanged(ComponentName component) { 124 // Override to implement 125 } 126 127 /** 128 * An extended version of onCurrentVrActivityChanged 129 * 130 * <p>This will be called when this service is initially bound, but is not 131 * guaranteed to be called before onUnbind. In general, this is intended to be used to 132 * determine when user focus has transitioned between two VR activities, or between a 133 * VR activity and a 2D activity. This should be overridden instead of the above 134 * onCurrentVrActivityChanged as that version is deprecated.</p> 135 * 136 * @param component the {@link ComponentName} of the VR activity or the 2D intent. 137 * @param running2dInVr true if the component is a 2D component. 138 * @param pid the process the component is running in. 139 * 140 * @see android.app.Activity#setVrModeEnabled 141 * @see android.R.attr#enableVrMode 142 * @hide 143 */ 144 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) onCurrentVrActivityChanged( ComponentName component, boolean running2dInVr, int pid)145 public void onCurrentVrActivityChanged( 146 ComponentName component, boolean running2dInVr, int pid) { 147 // Override to implement. Default to old behaviour of sending null for 2D. 148 onCurrentVrActivityChanged(running2dInVr ? null : component); 149 } 150 151 /** 152 * Checks if the given component is enabled in user settings. 153 * 154 * <p>If this component is not enabled in the user's settings, it will not be started when 155 * the system enters VR mode. The user interface for enabling VrListenerService components 156 * can be started by sending the {@link android.provider.Settings#ACTION_VR_LISTENER_SETTINGS} 157 * intent.</p> 158 * 159 * @param context the {@link Context} to use for looking up the requested component. 160 * @param requestedComponent the name of the component that implements 161 * {@link android.service.vr.VrListenerService} to check. 162 * 163 * @return {@code true} if this component is enabled in settings. 164 * 165 * @see android.provider.Settings#ACTION_VR_LISTENER_SETTINGS 166 */ isVrModePackageEnabled(@onNull Context context, @NonNull ComponentName requestedComponent)167 public static final boolean isVrModePackageEnabled(@NonNull Context context, 168 @NonNull ComponentName requestedComponent) { 169 ActivityManager am = context.getSystemService(ActivityManager.class); 170 if (am == null) { 171 return false; 172 } 173 return am.isVrModePackageEnabled(requestedComponent); 174 } 175 } 176