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.Registrant;
21e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport android.os.RegistrantList;
22e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
23e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport com.android.internal.annotations.VisibleForTesting;
24e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltimport com.android.internal.telephony.SubscriptionMonitor;
25e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
26e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwaltpublic class SubscriptionMonitorMock extends SubscriptionMonitor {
27e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final int mNumPhones;
28e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final RegistrantList mSubscriptionsChangedRegistrants[];
29e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private final RegistrantList mDefaultSubscriptionRegistrants[];
30e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
31e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public SubscriptionMonitorMock(int numPhones) {
32e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        super();
33e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mNumPhones = numPhones;
34e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mSubscriptionsChangedRegistrants = new RegistrantList[numPhones];
35e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mDefaultSubscriptionRegistrants = new RegistrantList[numPhones];
36e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
37e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        for (int i = 0; i < numPhones; i++) {
38e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            mSubscriptionsChangedRegistrants[i] = new RegistrantList();
39e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            mDefaultSubscriptionRegistrants[i] = new RegistrantList();
40e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        }
41e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
42e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
43e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
44e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void registerForSubscriptionChanged(int phoneId, Handler h, int what, Object o) {
45e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
46e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        Registrant r = new Registrant(h, what, o);
47e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mSubscriptionsChangedRegistrants[phoneId].add(r);
48e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        r.notifyRegistrant();
49e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
50e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
51e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
52e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void unregisterForSubscriptionChanged(int phoneId, Handler h) {
53e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
54e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mSubscriptionsChangedRegistrants[phoneId].remove(h);
55e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
56e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
57e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
58e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void registerForDefaultDataSubscriptionChanged(int phoneId, Handler h, int what,
59e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            Object o) {
60e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
61e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        Registrant r = new Registrant(h, what, o);
62e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mDefaultSubscriptionRegistrants[phoneId].add(r);
63e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        r.notifyRegistrant();
64e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
65e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
66e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @Override
67e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void unregisterForDefaultDataSubscriptionChanged(int phoneId, Handler h) {
68e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
69e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mDefaultSubscriptionRegistrants[phoneId].remove(h);
70e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
71e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
72e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @VisibleForTesting
73e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void notifySubscriptionChanged(int phoneId) {
74e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
75e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mSubscriptionsChangedRegistrants[phoneId].notifyRegistrants();
76e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
77e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
78e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    @VisibleForTesting
79e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    public void notifyDefaultSubscriptionChanged(int phoneId) {
80e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        validatePhoneId(phoneId);
81e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        mDefaultSubscriptionRegistrants[phoneId].notifyRegistrants();
82e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
83e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt
84e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    private void validatePhoneId(int phoneId) {
85e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        if (phoneId < 0 || phoneId >= mNumPhones) {
86e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt            throw new IllegalArgumentException("Invalid PhoneId");
87e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt        }
88e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt    }
89e9bf1f44cc6a888fff8419c8f35612d503ba3216Robert Greenwalt}
90