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.ims.ImsCall;
27import com.android.ims.ImsConferenceState;
28import com.android.internal.telephony.imsphone.ImsPhone;
29import com.android.internal.telephony.imsphone.ImsPhoneCall;
30import com.android.internal.telephony.test.TestConferenceEventPackageParser;
31
32import java.io.File;
33import java.io.FileInputStream;
34import java.io.FileNotFoundException;
35
36/**
37 * Telephony tester receives the following intents where {name} is the phone name
38 *
39 * adb shell am broadcast -a com.android.internal.telephony.{name}.action_detached
40 * adb shell am broadcast -a com.android.internal.telephony.{name}.action_attached
41 * adb shell am broadcast -a com.android.internal.telephony.TestConferenceEventPackage -e filename
42 *      test_filename.xml
43 */
44public class TelephonyTester {
45    private static final String LOG_TAG = "TelephonyTester";
46    private static final boolean DBG = true;
47
48    /**
49     * Test-only intent used to send a test conference event package to the IMS framework.
50     */
51    private static final String ACTION_TEST_CONFERENCE_EVENT_PACKAGE =
52            "com.android.internal.telephony.TestConferenceEventPackage";
53    private static final String EXTRA_FILENAME = "filename";
54
55    private PhoneBase mPhone;
56
57    // The static intent receiver one for all instances and we assume this
58    // is running on the same thread as Dcc.
59    protected BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
60            @Override
61        public void onReceive(Context context, Intent intent) {
62            String action = intent.getAction();
63            if (DBG) log("sIntentReceiver.onReceive: action=" + action);
64            if (action.equals(mPhone.getActionDetached())) {
65                log("simulate detaching");
66                mPhone.getServiceStateTracker().mDetachedRegistrants.notifyRegistrants();
67            } else if (action.equals(mPhone.getActionAttached())) {
68                log("simulate attaching");
69                mPhone.getServiceStateTracker().mAttachedRegistrants.notifyRegistrants();
70            } else if (action.equals(ACTION_TEST_CONFERENCE_EVENT_PACKAGE)) {
71                log("inject simulated conference event package");
72                handleTestConferenceEventPackage(context, intent.getStringExtra(EXTRA_FILENAME));
73            } else {
74                if (DBG) log("onReceive: unknown action=" + action);
75            }
76        }
77    };
78
79    TelephonyTester(PhoneBase phone) {
80        mPhone = phone;
81
82        if (Build.IS_DEBUGGABLE) {
83            IntentFilter filter = new IntentFilter();
84
85            filter.addAction(mPhone.getActionDetached());
86            log("register for intent action=" + mPhone.getActionDetached());
87
88            filter.addAction(mPhone.getActionAttached());
89            log("register for intent action=" + mPhone.getActionAttached());
90
91            if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_IMS) {
92                log("register for intent action=" + ACTION_TEST_CONFERENCE_EVENT_PACKAGE);
93                filter.addAction(ACTION_TEST_CONFERENCE_EVENT_PACKAGE);
94            }
95
96            phone.getContext().registerReceiver(mIntentReceiver, filter, null, mPhone.getHandler());
97        }
98    }
99
100    void dispose() {
101        if (Build.IS_DEBUGGABLE) {
102            mPhone.getContext().unregisterReceiver(mIntentReceiver);
103        }
104    }
105
106    private static void log(String s) {
107        Rlog.d(LOG_TAG, s);
108    }
109
110    /**
111     * Handles request to send a test conference event package to the active Ims call.
112     *
113     * @see com.android.internal.telephony.test.TestConferenceEventPackageParser
114     * @param context The context.
115     * @param fileName The name of the test conference event package file to read.
116     */
117    private void handleTestConferenceEventPackage(Context context, String fileName) {
118        // Attempt to get the active IMS call before parsing the test XML file.
119        ImsPhone imsPhone = (ImsPhone) mPhone;
120        if (imsPhone == null) {
121            return;
122        }
123
124        ImsPhoneCall imsPhoneCall = imsPhone.getForegroundCall();
125        if (imsPhoneCall == null) {
126            return;
127        }
128
129        ImsCall imsCall = imsPhoneCall.getImsCall();
130        if (imsCall == null) {
131            return;
132        }
133
134        File packageFile = new File(context.getFilesDir(), fileName);
135        final FileInputStream is;
136        try {
137            is = new FileInputStream(packageFile);
138        } catch (FileNotFoundException ex) {
139            log("Test conference event package file not found: " + packageFile.getAbsolutePath());
140            return;
141        }
142
143        TestConferenceEventPackageParser parser = new TestConferenceEventPackageParser(is);
144        ImsConferenceState imsConferenceState = parser.parse();
145        if (imsConferenceState == null) {
146            return;
147        }
148
149        imsCall.conferenceStateUpdated(imsConferenceState);
150    }
151}
152