Home | History | Annotate | Download | only in audioquality
      1 /*
      2  * Copyright (C) 2010 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.cts.verifier.audioquality;
     18 
     19 import android.app.Service;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Handler;
     23 import android.os.IBinder;
     24 import android.os.Message;
     25 import android.os.PowerManager;
     26 import android.util.Log;
     27 
     28 import java.util.ArrayList;
     29 
     30 /**
     31  * Launch an experiment.
     32  *
     33  * Experiments are decoupled from the UI both so that they can run on a
     34  * background thread without freezing the UI, and to support experiments
     35  * which take over the screen from the UI (such as invoking Voice Search).
     36  */
     37 public class ExperimentService extends Service implements Terminator {
     38     private static final String TAG = "AudioQualityVerifier";
     39     private static int BACKGROUND_LOAD = 0;
     40 
     41     private ArrayList<Experiment> mExperiments;
     42     private Experiment mExp;
     43 
     44     private TimeoutHandler mHandler = null;
     45     private PowerManager.WakeLock mWakeLock = null;
     46 
     47     private boolean mRunAll;
     48     private int mExpID;
     49 
     50     private boolean mActive;
     51     private LoadGenerator[] mLoadGenerator = null;
     52 
     53     @Override
     54     public IBinder onBind(Intent intent) {
     55         return null;
     56     }
     57 
     58     @Override
     59     public void onCreate() {
     60         mExperiments = VerifierExperiments.getExperiments(this);
     61     }
     62 
     63     @Override
     64     public void onDestroy() {
     65         Log.i(TAG, "Service destroyed");
     66         terminate(true);
     67     }
     68 
     69     /**
     70      * Implements Terminator, to clean up when the experiment indicates it
     71      * has completed.
     72      */
     73     public void terminate(boolean aborted) {
     74         if (mLoadGenerator != null) {
     75             for (LoadGenerator generator : mLoadGenerator) {
     76                 generator.halt();
     77             }
     78             mLoadGenerator = null;
     79         }
     80         if (!mActive) return;
     81         mActive = false;
     82         if (mHandler != null) mHandler.clear();
     83         if (aborted) {
     84             mExp.cancel();
     85         } else {
     86             mExp.stop();
     87         }
     88         Intent intent = new Intent(AudioQualityVerifierActivity.ACTION_EXP_FINISHED);
     89         intent.putExtra(AudioQualityVerifierActivity.EXTRA_EXP_ID, mExpID);
     90         intent.putExtra(AudioQualityVerifierActivity.EXTRA_RUN_ALL, mRunAll);
     91         intent.putExtra(AudioQualityVerifierActivity.EXTRA_ABORTED, aborted);
     92         sendBroadcast(intent);
     93         if (mWakeLock != null) {
     94             mWakeLock.release();
     95             mWakeLock = null;
     96         }
     97     }
     98 
     99     @Override
    100     public int onStartCommand(Intent intent, int flags, int startId) {
    101         mActive = true;
    102 
    103         // Obtain wakelock
    104         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    105         mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG);
    106         mWakeLock.acquire();
    107 
    108         if (BACKGROUND_LOAD > 0) {
    109             mLoadGenerator = new LoadGenerator[BACKGROUND_LOAD];
    110             for (int i = 0; i < BACKGROUND_LOAD; i++) {
    111                 mLoadGenerator[i] = new LoadGenerator();
    112             }
    113         }
    114 
    115         mExpID = intent.getIntExtra(AudioQualityVerifierActivity.EXTRA_EXP_ID, -1);
    116         if (mExpID == -1) {
    117             Log.e(TAG, "Invalid test ID");
    118             System.exit(0);
    119         }
    120         mRunAll = intent.getBooleanExtra(AudioQualityVerifierActivity.EXTRA_RUN_ALL, false);
    121         mExp = mExperiments.get(mExpID);
    122         mExp.start();
    123 
    124         // Inform the VerifierActivity Activity that we have started:
    125         Intent feedback = new Intent(AudioQualityVerifierActivity.ACTION_EXP_STARTED);
    126         feedback.putExtra(AudioQualityVerifierActivity.EXTRA_EXP_ID, mExpID);
    127         sendBroadcast(feedback);
    128 
    129         mHandler = new TimeoutHandler();
    130         mHandler.delay(mExp.getTimeout());
    131         mExp.run(this, this);
    132 
    133         return START_NOT_STICKY;
    134     }
    135 
    136     class TimeoutHandler extends Handler {
    137         @Override
    138         public void handleMessage(Message msg) {
    139             terminate(true);
    140             stopSelf();
    141         }
    142 
    143         public void delay(int secs) {
    144             removeMessages(0);
    145             sendMessageDelayed(obtainMessage(0), secs * 1000);
    146         }
    147 
    148         public void clear() {
    149             removeMessages(0);
    150         }
    151     }
    152 }
    153