Home | History | Annotate | Download | only in stubs
      1 /*
      2  * Copyright (C) 2008 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.app.stubs;
     18 
     19 import android.app.Service;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Binder;
     23 import android.os.IBinder;
     24 import android.os.SystemClock;
     25 
     26 public class MockService extends Service {
     27     // Extra for start: don't automatically stop itself, if true.
     28     public static final String EXTRA_NO_STOP = "no_stop";
     29 
     30     public static boolean result = false;
     31     private final IBinder mBinder = new MockBinder();
     32 
     33     private static boolean sStarted = false;
     34     private static boolean sDestroyed = false;
     35     private static Object sBlocker = new Object();
     36 
     37     public class MockBinder extends Binder {
     38         MockService getService() {
     39             return MockService.this;
     40         }
     41     }
     42 
     43     /**
     44      * set the result as true when service bind
     45      */
     46     @Override
     47     public IBinder onBind(Intent intent) {
     48         synchronized (sBlocker) {
     49             result = true;
     50             sStarted = true;
     51             sBlocker.notifyAll();
     52         }
     53         return mBinder;
     54     }
     55 
     56     /**
     57      * set the result as true when service start
     58      */
     59     @Override
     60     public void onStart(Intent intent, int startId) {
     61         super.onStart(intent, startId);
     62         synchronized (sBlocker) {
     63             if (!intent.getBooleanExtra(EXTRA_NO_STOP, false)) {
     64                 stopSelf(startId);
     65             }
     66             result = true;
     67             sStarted = true;
     68             sBlocker.notifyAll();
     69         }
     70     }
     71 
     72     @Override
     73     public void onDestroy() {
     74         super.onDestroy();
     75         synchronized (sBlocker) {
     76             sDestroyed = true;
     77             sBlocker.notifyAll();
     78         }
     79     }
     80 
     81     public static void prepareStart() {
     82         synchronized (sBlocker) {
     83             sStarted = false;
     84             result = false;
     85         }
     86     }
     87 
     88     public static boolean waitForStart(long timeout) {
     89         long now = SystemClock.elapsedRealtime();
     90         final long endTime = now + timeout;
     91         synchronized (sBlocker) {
     92             while (!sStarted && now < endTime) {
     93                 try {
     94                     sBlocker.wait(endTime - now);
     95                 } catch (InterruptedException e) {
     96                 }
     97                 now = SystemClock.elapsedRealtime();
     98             }
     99             return sStarted;
    100         }
    101     }
    102 
    103 
    104     public static void prepareDestroy() {
    105         synchronized (sBlocker) {
    106             sDestroyed = false;
    107         }
    108     }
    109 
    110     public static boolean waitForDestroy(long timeout) {
    111         long now = SystemClock.elapsedRealtime();
    112         final long endTime = now + timeout;
    113         synchronized (sBlocker) {
    114             while (!sDestroyed && now < endTime) {
    115                 try {
    116                     sBlocker.wait(endTime - now);
    117                 } catch (InterruptedException e) {
    118                 }
    119                 now = SystemClock.elapsedRealtime();
    120             }
    121             return sDestroyed;
    122         }
    123     }
    124 
    125     public static void stopService(Context context) {
    126         Intent intent = new Intent();
    127         intent.setClass(context, MockService.class);
    128         context.stopService(intent);
    129     }
    130 }
    131