1/* 2 * Copyright (C) 2006 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 17package com.android.internal.telephony.mocks; 18 19import android.os.Handler; 20import android.os.Looper; 21import android.os.Registrant; 22import android.os.RegistrantList; 23 24import com.android.internal.annotations.VisibleForTesting; 25import com.android.internal.telephony.PhoneSwitcher; 26 27import java.util.concurrent.atomic.AtomicBoolean; 28 29public class PhoneSwitcherMock extends PhoneSwitcher { 30 private final int mNumPhones; 31 private final RegistrantList mActivePhoneRegistrants[]; 32 private final AtomicBoolean mIsActive[]; 33 34 public PhoneSwitcherMock(int numPhones, Looper looper) { 35 super(looper); 36 37 mNumPhones = numPhones; 38 mActivePhoneRegistrants = new RegistrantList[numPhones]; 39 mIsActive = new AtomicBoolean[numPhones]; 40 for(int i = 0; i < numPhones; i++) { 41 mActivePhoneRegistrants[i] = new RegistrantList(); 42 mIsActive[i] = new AtomicBoolean(false); 43 } 44 } 45 46 @Override 47 public void resendDataAllowed(int phoneId) { 48 throw new RuntimeException("resendPhone not implemented"); 49 } 50 51 @Override 52 public boolean isPhoneActive(int phoneId) { 53 return mIsActive[phoneId].get(); 54 } 55 56 @Override 57 public void registerForActivePhoneSwitch(int phoneId, Handler h, int what, Object o) { 58 validatePhoneId(phoneId); 59 Registrant r = new Registrant(h, what, o); 60 mActivePhoneRegistrants[phoneId].add(r); 61 r.notifyRegistrant(); 62 } 63 64 @Override 65 public void unregisterForActivePhoneSwitch(int phoneId, Handler h) { 66 validatePhoneId(phoneId); 67 mActivePhoneRegistrants[phoneId].remove(h); 68 } 69 70 private void validatePhoneId(int phoneId) { 71 if (phoneId < 0 || phoneId >= mNumPhones) { 72 throw new IllegalArgumentException("Invalid PhoneId"); 73 } 74 } 75 76 @VisibleForTesting 77 public void setPhoneActive(int phoneId, boolean active) { 78 validatePhoneId(phoneId); 79 if (mIsActive[phoneId].getAndSet(active) != active) { 80 mActivePhoneRegistrants[phoneId].notifyRegistrants(); 81 } 82 } 83} 84