1e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt/*
2e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * Copyright (C) 2006 The Android Open Source Project
3e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt *
4e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
5e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * you may not use this file except in compliance with the License.
6e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * You may obtain a copy of the License at
7e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt *
8e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
9e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt *
10e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * Unless required by applicable law or agreed to in writing, software
11e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
12e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
13e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * See the License for the specific language governing permissions and
14e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt * limitations under the License.
15e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt */
16e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
17e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltpackage com.android.internal.telephony.mocks;
18e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
19e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport android.os.Handler;
20e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport android.os.Looper;
21e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport android.os.Registrant;
22e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport android.os.RegistrantList;
23e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
24e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport com.android.internal.annotations.VisibleForTesting;
25e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport com.android.internal.telephony.PhoneSwitcher;
26e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
27e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport java.util.concurrent.atomic.AtomicBoolean;
28e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
29e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltpublic class PhoneSwitcherMock extends PhoneSwitcher {
30e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final int mNumPhones;
31e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final RegistrantList mActivePhoneRegistrants[];
32e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final AtomicBoolean mIsActive[];
33e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
34e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public PhoneSwitcherMock(int numPhones, Looper looper) {
35e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        super(looper);
36e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
37e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mNumPhones = numPhones;
38e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mActivePhoneRegistrants = new RegistrantList[numPhones];
39e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mIsActive = new AtomicBoolean[numPhones];
40e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        for(int i = 0; i < numPhones; i++) {
41e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            mActivePhoneRegistrants[i] = new RegistrantList();
42e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            mIsActive[i] = new AtomicBoolean(false);
43e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        }
44e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
45e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
46e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
47e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void resendDataAllowed(int phoneId) {
48e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        throw new RuntimeException("resendPhone not implemented");
49e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
50e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
51e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
52e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public boolean isPhoneActive(int phoneId) {
53e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        return mIsActive[phoneId].get();
54e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
55e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
56e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
57e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void registerForActivePhoneSwitch(int phoneId, Handler h, int what, Object o) {
58e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
59e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        Registrant r = new Registrant(h, what, o);
60e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mActivePhoneRegistrants[phoneId].add(r);
61e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        r.notifyRegistrant();
62e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
63e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
64e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
65e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void unregisterForActivePhoneSwitch(int phoneId, Handler h) {
66e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
67e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mActivePhoneRegistrants[phoneId].remove(h);
68e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
69e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
70e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private void validatePhoneId(int phoneId) {
71e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        if (phoneId < 0 || phoneId >= mNumPhones) {
72e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            throw new IllegalArgumentException("Invalid PhoneId");
73e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        }
74e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
75e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
76e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @VisibleForTesting
77e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void setPhoneActive(int phoneId, boolean active) {
78e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
79e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        if (mIsActive[phoneId].getAndSet(active) != active) {
80e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            mActivePhoneRegistrants[phoneId].notifyRegistrants();
81e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        }
82e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
83e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt}
84