1 /* 2 * Copyright (C) 2015 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 com.android.camera.util; 18 19 import android.annotation.TargetApi; 20 import android.app.Activity; 21 import android.app.ActivityManager; 22 import android.app.KeyguardManager; 23 import android.app.NotificationManager; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 import android.hardware.SensorManager; 27 import android.hardware.camera2.CameraManager; 28 import android.hardware.display.DisplayManager; 29 import android.location.LocationManager; 30 import android.media.AudioManager; 31 import android.os.Build.VERSION_CODES; 32 import android.os.PowerManager; 33 import android.os.Vibrator; 34 import android.view.LayoutInflater; 35 import android.view.WindowManager; 36 import android.view.accessibility.AccessibilityManager; 37 38 import com.android.camera.debug.Log; 39 import com.android.camera.debug.Log.Tag; 40 41 /** 42 * Initializable singleton set of android service providers. 43 * Some services may be null 44 */ 45 public class AndroidServices { 46 private static Tag TAG = new Tag("AndroidServices"); 47 /** Log all requests; otherwise will only log long requests. */ 48 private static final boolean LOG_ALL_REQUESTS = false; 49 /** Log requests if this threshold is exceeded. */ 50 private static final int LOG_THRESHOLD_MILLIS = 10; 51 52 private static class Singleton { 53 private static final AndroidServices INSTANCE = 54 new AndroidServices(AndroidContext.instance().get()); 55 } 56 instance()57 public static AndroidServices instance() { 58 return Singleton.INSTANCE; 59 } 60 61 private final Context mContext; AndroidServices(Context context)62 private AndroidServices(Context context) { 63 mContext = context; 64 } 65 provideActivityManager()66 public ActivityManager provideActivityManager() { 67 return (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 68 } 69 provideAudioManager()70 public AudioManager provideAudioManager() { 71 return (AudioManager) getSystemService(Context.AUDIO_SERVICE); 72 } 73 provideAccessibilityManager()74 public AccessibilityManager provideAccessibilityManager() { 75 return (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE); 76 } 77 78 @TargetApi(VERSION_CODES.LOLLIPOP) provideCameraManager()79 public CameraManager provideCameraManager() { 80 CameraManager cameraManager = null; 81 82 try { 83 Object service = ApiHelper.HAS_CAMERA_2_API ? getSystemService(Context.CAMERA_SERVICE) 84 : null; 85 cameraManager = (CameraManager) service; 86 } catch (IllegalStateException ex) { 87 Log.e(TAG, "Could not get the CAMERA_SERVICE"); 88 cameraManager = null; 89 } 90 91 return cameraManager; 92 } 93 provideDevicePolicyManager()94 public DevicePolicyManager provideDevicePolicyManager() { 95 return (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 96 } 97 provideDisplayManager()98 public DisplayManager provideDisplayManager() { 99 return (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); 100 } 101 provideKeyguardManager()102 public KeyguardManager provideKeyguardManager() { 103 return (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 104 } 105 provideLayoutInflater()106 public LayoutInflater provideLayoutInflater() { 107 return (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 108 } 109 provideLocationManager()110 public LocationManager provideLocationManager() { 111 return (LocationManager) getSystemService(Context.LOCATION_SERVICE); 112 } 113 provideNotificationManager()114 public NotificationManager provideNotificationManager() { 115 return (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 116 } 117 providePowerManager()118 public PowerManager providePowerManager() { 119 return (PowerManager) getSystemService(Context.POWER_SERVICE); 120 } 121 provideSensorManager()122 public SensorManager provideSensorManager() { 123 return (SensorManager) getSystemService(Context.SENSOR_SERVICE); 124 } 125 provideVibrator()126 public Vibrator provideVibrator() { 127 return (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 128 } 129 provideWindowManager(Activity context)130 public WindowManager provideWindowManager(Activity context) { 131 return (WindowManager) getSystemService(Context.WINDOW_SERVICE, context); 132 } 133 getSystemService(String service)134 private Object getSystemService(String service) { 135 return getSystemService(service, null); 136 } 137 getSystemService(String service, Context targetContext)138 private Object getSystemService(String service, Context targetContext) { 139 try { 140 long start = System.currentTimeMillis(); 141 if (targetContext == null) targetContext = mContext; 142 143 Object result = targetContext.getSystemService(service); 144 145 long duration = System.currentTimeMillis() - start; 146 if (duration > LOG_THRESHOLD_MILLIS) { 147 Log.w(TAG, "Warning: providing system service " + service + " took " + 148 duration + "ms"); 149 } else if (LOG_ALL_REQUESTS) { 150 Log.v(TAG, "Provided system service " + service + " in " + duration + "ms"); 151 } 152 return result; 153 } catch (Exception e) { 154 return null; 155 } 156 } 157 } 158