1 /*
2  * Copyright (C) 2012 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.tradefed.utils;
18 
19 import android.app.Activity;
20 import android.app.KeyguardManager;
21 import android.app.KeyguardManager.KeyguardLock;
22 import android.app.Service;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.IBinder;
26 import android.os.PowerManager;
27 import android.provider.Settings;
28 import android.util.Log;
29 
30 /**
31  * A service that sets up the device for testing by dismissing the keyguard
32  * and keep the screen on.
33  */
34 public class DeviceSetupService extends Service {
35     PowerManager.WakeLock wl;
36 
37     private static final int SCREEN_OFF_TIMEOUT = -1; //never timeout
38 
39     @Override
onCreate()40     public void onCreate() {
41         Log.v(DeviceSetupIntentReceiver.TAG, "Setup service started.");
42         // dismiss keyguard
43         KeyguardManager keyguardManager = (KeyguardManager) getSystemService(
44                 Activity.KEYGUARD_SERVICE);
45         KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
46         lock.disableKeyguard();
47 
48         // change screen off timeout
49         Settings.System.putInt(
50                 getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, SCREEN_OFF_TIMEOUT);
51     }
52 
53     @Override
onStartCommand(Intent intent, int flags, int startId)54     public int onStartCommand(Intent intent, int flags, int startId) {
55         // keep screen on
56         Log.v(DeviceSetupIntentReceiver.TAG, "Acquiring wake lock.");
57         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
58         wl = pm.newWakeLock(
59                 PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
60                 DeviceSetupIntentReceiver.TAG);
61         wl.acquire();
62         return START_STICKY;
63     }
64 
65     @Override
onBind(Intent intent)66     public IBinder onBind(Intent intent) {
67         return null;
68     }
69 
70     @Override
onDestroy()71     public void onDestroy() {
72         if (wl != null){
73             wl.release();
74         }
75         Log.v(DeviceSetupIntentReceiver.TAG, "Setup service stopped.");
76     }
77 }
78