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