TelephonyTester.java revision 2cc8c148fa4cb6cba5deac6b011268b4174a0b02
1/*
2 * Copyright (C) 2013 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;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.Build;
24import android.telephony.Rlog;
25
26import com.android.internal.telephony.PhoneBase;
27
28/**
29 * Telephony tester receives the following intents where {name} is the phone name
30 *
31 * adb shell am broadcast -a com.android.internal.telephony.{name}.action_detached
32 * adb shell am broadcast -a com.android.internal.telephony.{name}.action_attached
33 */
34public class TelephonyTester {
35    private static final String LOG_TAG = "TelephonyTester";
36    private static final boolean DBG = true;
37
38    private PhoneBase mPhone;
39
40    // The static intent receiver one for all instances and we assume this
41    // is running on the same thread as Dcc.
42    protected BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
43            @Override
44        public void onReceive(Context context, Intent intent) {
45            String action = intent.getAction();
46            if (DBG) log("sIntentReceiver.onReceive: action=" + action);
47            if (action.equals(mPhone.getActionDetached())) {
48                log("simulate detaching");
49                mPhone.getServiceStateTracker().mDetachedRegistrants.notifyRegistrants();
50            } else if (action.equals(mPhone.getActionAttached())) {
51                log("simulate attaching");
52                mPhone.getServiceStateTracker().mAttachedRegistrants.notifyRegistrants();
53            } else {
54                if (DBG) log("onReceive: unknown action=" + action);
55            }
56        }
57    };
58
59    TelephonyTester(PhoneBase phone) {
60        mPhone = phone;
61
62        if (Build.IS_DEBUGGABLE) {
63            IntentFilter filter = new IntentFilter();
64
65            filter.addAction(mPhone.getActionDetached());
66            log("register for intent action=" + mPhone.getActionDetached());
67
68            filter.addAction(mPhone.getActionAttached());
69            log("register for intent action=" + mPhone.getActionAttached());
70
71            phone.getContext().registerReceiver(mIntentReceiver, filter, null, mPhone.getHandler());
72        }
73    }
74
75    void dispose() {
76        mPhone.getContext().unregisterReceiver(mIntentReceiver);
77    }
78
79    private static void log(String s) {
80        Rlog.d(LOG_TAG, s);
81    }
82}
83