Home | History | Annotate | Download | only in sensoroperations
      1 /*
      2  * Copyright (C) 2014 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.hardware.cts.helpers.sensoroperations;
     18 
     19 import android.content.Context;
     20 import android.hardware.cts.helpers.reporting.ISensorTestNode;
     21 import android.os.PowerManager;
     22 import android.os.PowerManager.WakeLock;
     23 
     24 /**
     25  * An {@link SensorOperation} which holds a wake-lock while performing another
     26  * {@link SensorOperation}.
     27  */
     28 public class WakeLockOperation extends SensorOperation {
     29     private static final String TAG = "WakeLockOperation";
     30 
     31     private final SensorOperation mOperation;
     32     private final Context mContext;
     33     private final int mWakeLockFlags;
     34 
     35     /**
     36      * Constructor for {@link WakeLockOperation}.
     37      *
     38      * @param operation the child {@link SensorOperation} to perform after the delay
     39      * @param context the context used to access the power manager
     40      * @param wakeLockFlags the flags used when acquiring the wake-lock
     41      */
     42     public WakeLockOperation(SensorOperation operation, Context context, int wakeLockFlags) {
     43         super(operation.getStats());
     44         mOperation = operation;
     45         mContext = context;
     46         mWakeLockFlags = wakeLockFlags;
     47     }
     48 
     49     /**
     50      * Constructor for {@link WakeLockOperation}.
     51      *
     52      * @param operation the child {@link SensorOperation} to perform after the delay
     53      * @param context the context used to access the power manager
     54      */
     55     public WakeLockOperation(SensorOperation operation, Context context) {
     56         this(operation, context, PowerManager.PARTIAL_WAKE_LOCK);
     57     }
     58 
     59     /**
     60      * {@inheritDoc}
     61      */
     62     @Override
     63     public void execute(ISensorTestNode parent) throws Exception {
     64         PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
     65         WakeLock wakeLock = pm.newWakeLock(mWakeLockFlags, TAG);
     66         wakeLock.acquire();
     67         try {
     68             mOperation.execute(asTestNode(parent));
     69         } finally {
     70             wakeLock.release();
     71         }
     72     }
     73 
     74     /**
     75      * {@inheritDoc}
     76      */
     77     @Override
     78     public SensorOperation clone() {
     79         return new WakeLockOperation(mOperation.clone(), mContext, mWakeLockFlags);
     80     }
     81 }
     82