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