VrListenerService.java revision e24b9a6cfa4d565d7f49c9ae8f3aeca737d93312
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 17package android.service.vr; 18 19import android.annotation.NonNull; 20import android.annotation.SdkConstant; 21import android.app.ActivityManager; 22import android.app.Service; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.os.IBinder; 27 28/** 29 * A service that is bound from the system while running in virtual reality (VR) mode. 30 * 31 * <p>To extend this class, you must declare the service in your manifest file with 32 * the {@link android.Manifest.permission#BIND_VR_LISTENER_SERVICE} permission 33 * and include an intent filter with the {@link #SERVICE_INTERFACE} action. For example:</p> 34 * <pre> 35 * <service android:name=".VrListener" 36 * android:label="@string/service_name" 37 * android:permission="android.permission.BIND_VR_LISTENER_SERVICE"> 38 * <intent-filter> 39 * <action android:name="android.service.vr.VrListenerService" /> 40 * </intent-filter> 41 * </service> 42 * </pre> 43 * 44 * <p> 45 * This service is bound when the system enters VR mode and is unbound when the system leaves VR 46 * mode. 47 * {@see android.app.Activity#setVrMode(boolean)} 48 * </p> 49 */ 50public abstract class VrListenerService extends Service { 51 52 /** 53 * The {@link Intent} that must be declared as handled by the service. 54 */ 55 @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) 56 public static final String SERVICE_INTERFACE = "android.service.vr.VrListenerService"; 57 58 /** 59 * @hide 60 */ 61 public static class VrListenerBinder extends IVrListener.Stub { 62 } 63 64 private final VrListenerBinder mBinder = new VrListenerBinder(); 65 66 @Override 67 public IBinder onBind(Intent intent) { 68 return mBinder; 69 } 70 71 /** 72 * Check if the given package is available to be enabled/disabled in VR mode settings. 73 * 74 * @param context the {@link Context} to use for looking up the requested component. 75 * @param requestedComponent the name of the component that implements 76 * {@link android.service.vr.VrListenerService} to check. 77 * 78 * @return {@code true} if this package is enabled in settings. 79 */ 80 public static final boolean isVrModePackageEnabled(@NonNull Context context, 81 @NonNull ComponentName requestedComponent) { 82 ActivityManager am = context.getSystemService(ActivityManager.class); 83 if (am == null) { 84 return false; 85 } 86 return am.isVrModePackageEnabled(requestedComponent); 87 } 88} 89