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.content.cts; 18 19 import android.app.Service; 20 import android.content.ContextWrapper; 21 import android.content.Intent; 22 import android.os.Binder; 23 import android.os.Handler; 24 import android.os.IBinder; 25 import android.os.Message; 26 27 /** 28 * This class is used for {@link ContextTest}. 29 */ 30 public class MockContextService extends Service { 31 private static boolean mHadCalledOnBind = false; 32 private static boolean mHadCalledOnUnbind = false; 33 private static boolean mHadCalledOnStart = false; 34 private static boolean mHadCalledOnDestory = false; 35 private static final int TEST_MESSAGE_WHAT = 1; 36 37 private final IBinder mBinder = new Binder(); 38 39 private Handler mHandler = new Handler() { 40 public void handleMessage(Message msg) { 41 mHandler.sendMessageDelayed(mHandler.obtainMessage(TEST_MESSAGE_WHAT), 1000); 42 } 43 }; 44 45 @Override onCreate()46 public void onCreate() { 47 mHandler.sendMessageDelayed(mHandler.obtainMessage(TEST_MESSAGE_WHAT), 1000); 48 } 49 50 @Override onDestroy()51 public void onDestroy() { 52 mHadCalledOnDestory = true; 53 mHandler.removeMessages(1); 54 } 55 56 @Override onUnbind(Intent intent)57 public boolean onUnbind(Intent intent) { 58 mHadCalledOnUnbind = true; 59 return true; 60 } 61 62 @Override onBind(Intent intent)63 public IBinder onBind(Intent intent) { 64 mHadCalledOnBind = true; 65 return mBinder; 66 } 67 68 @Override onStart(Intent intent, int startId)69 public void onStart(Intent intent, int startId) { 70 mHadCalledOnStart = true; 71 } 72 reset()73 public static void reset() { 74 mHadCalledOnBind = false; 75 mHadCalledOnUnbind = false; 76 mHadCalledOnStart = false; 77 mHadCalledOnDestory = false; 78 } 79 hadCalledOnBind()80 public static boolean hadCalledOnBind() { 81 return mHadCalledOnBind; 82 } 83 hadCalledOnUnbind()84 public static boolean hadCalledOnUnbind() { 85 return mHadCalledOnUnbind; 86 } 87 hadCalledOnStart()88 public static boolean hadCalledOnStart() { 89 return mHadCalledOnStart; 90 } 91 hadCalledOnDestory()92 public static boolean hadCalledOnDestory() { 93 return mHadCalledOnDestory; 94 } 95 } 96 97