1 /* 2 * Copyright (C) 2009 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.cts; 18 19 import android.app.IntentService; 20 import android.content.Intent; 21 import android.os.Binder; 22 import android.os.IBinder; 23 import android.view.animation.cts.DelayedCheck; 24 25 public class IntentServiceStub extends IntentService { 26 public IntentServiceStub() { 27 super("IntentServiceStub"); 28 } 29 30 public static String ISS_ADD = "add"; 31 public static String ISS_VALUE = "value"; 32 33 public static int onHandleIntentCalled; 34 public static boolean onBindCalled; 35 public static boolean onCreateCalled; 36 public static boolean onDestroyCalled; 37 public static boolean onStartCalled; 38 public static int accumulator; 39 40 private static Throwable throwable; 41 42 public static void reset() { 43 onHandleIntentCalled = 0; 44 onBindCalled = false; 45 onCreateCalled = false; 46 onDestroyCalled = false; 47 onStartCalled = false; 48 accumulator = 0; 49 throwable = null; 50 } 51 52 public static void waitToFinish(long timeout) throws Throwable { 53 new DelayedCheck(timeout) { 54 @Override 55 protected boolean check() { 56 return IntentServiceStub.onDestroyCalled; 57 } 58 }.run(); 59 if (throwable != null) { 60 throw throwable; 61 } 62 } 63 64 @Override 65 protected void onHandleIntent(Intent intent) { 66 onHandleIntentCalled += 1; 67 try { 68 String action = intent.getAction(); 69 if (action != null && action.equals(ISS_ADD)) { 70 accumulator += intent.getIntExtra(ISS_VALUE, 0); 71 } 72 } catch (Throwable t) { 73 throwable = t; 74 } 75 } 76 77 @Override 78 public IBinder onBind(Intent intent) { 79 onBindCalled = true; 80 return new Binder(); 81 } 82 83 @Override 84 public void onCreate() { 85 onCreateCalled = true; 86 super.onCreate(); 87 } 88 89 @Override 90 public void onDestroy() { 91 onDestroyCalled = true; 92 super.onDestroy(); 93 } 94 95 @Override 96 public void onStart(Intent intent, int startId) { 97 onStartCalled = true; 98 super.onStart(intent, startId); 99 } 100 101 } 102