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.server.telecom.testapps;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.net.Uri;
24import android.os.Bundle;
25import android.support.v4.content.LocalBroadcastManager;
26import android.telecom.PhoneAccountHandle;
27import android.telecom.TelecomManager;
28import android.telecom.VideoProfile;
29import android.util.Log;
30
31/**
32 * This class receives the notification callback intents used to update call states for
33 * {@link TestConnectionService}.
34 */
35public class CallNotificationReceiver extends BroadcastReceiver {
36
37    static final String TAG = CallNotificationReceiver.class.getSimpleName();
38    /**
39     * Exit intent action is sent when the user clicks the "exit" action of the
40     * TestConnectionService notification. Used to cancel (remove) the notification.
41     */
42    static final String ACTION_CALL_SERVICE_EXIT =
43            "com.android.server.telecom.testapps.ACTION_CALL_SERVICE_EXIT";
44    static final String ACTION_REGISTER_PHONE_ACCOUNT =
45            "com.android.server.telecom.testapps.ACTION_REGISTER_PHONE_ACCOUNT";
46    static final String ACTION_SHOW_ALL_PHONE_ACCOUNTS =
47            "com.android.server.telecom.testapps.ACTION_SHOW_ALL_PHONE_ACCOUNTS";
48    static final String ACTION_ONE_WAY_VIDEO_CALL =
49            "com.android.server.telecom.testapps.ACTION_ONE_WAY_VIDEO_CALL";
50    static final String ACTION_TWO_WAY_VIDEO_CALL =
51            "com.android.server.telecom.testapps.ACTION_TWO_WAY_VIDEO_CALL";
52    static final String ACTION_AUDIO_CALL =
53            "com.android.server.telecom.testapps.ACTION_AUDIO_CALL";
54    static final String ACTION_RTT_CALL =
55            "com.android.server.telecom.testapps.ACTION_RTT_CALL";
56
57    /** {@inheritDoc} */
58    @Override
59    public void onReceive(Context context, Intent intent) {
60        String action = intent.getAction();
61        if (ACTION_CALL_SERVICE_EXIT.equals(action)) {
62            CallServiceNotifier.getInstance().cancelNotifications(context);
63        } else if (ACTION_REGISTER_PHONE_ACCOUNT.equals(action)) {
64            CallServiceNotifier.getInstance().registerPhoneAccount(context);
65        } else if (ACTION_SHOW_ALL_PHONE_ACCOUNTS.equals(action)) {
66            CallServiceNotifier.getInstance().showAllPhoneAccounts(context);
67        } else if (ACTION_ONE_WAY_VIDEO_CALL.equals(action)) {
68            sendIncomingCallIntent(context, null, VideoProfile.STATE_RX_ENABLED);
69        } else if (ACTION_TWO_WAY_VIDEO_CALL.equals(action)) {
70            sendIncomingCallIntent(context, null, VideoProfile.STATE_BIDIRECTIONAL);
71        } else if (ACTION_RTT_CALL.equals(action)) {
72            sendIncomingRttCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
73        } else if (ACTION_AUDIO_CALL.equals(action)) {
74            sendIncomingCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
75        }
76    }
77
78    /**
79     * Creates and sends the intent to add an incoming call through Telecom.
80     *
81     * @param context The current context.
82     * @param videoState The video state requested for the incoming call.
83     */
84    public static void sendIncomingCallIntent(Context context, Uri handle, int videoState) {
85        PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
86                new ComponentName(context, TestConnectionService.class),
87                CallServiceNotifier.SIM_SUBSCRIPTION_ID);
88
89        // For the purposes of testing, indicate whether the incoming call is a video call by
90        // stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
91        Bundle extras = new Bundle();
92        extras.putInt(TestConnectionService.EXTRA_START_VIDEO_STATE, videoState);
93        if (handle != null) {
94            extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
95        }
96
97        TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
98    }
99
100    public static void sendIncomingRttCallIntent(Context context, Uri handle, int videoState) {
101        PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
102                new ComponentName(context, TestConnectionService.class),
103                CallServiceNotifier.SIM_SUBSCRIPTION_ID);
104
105        // For the purposes of testing, indicate whether the incoming call is a video call by
106        // stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
107        Bundle extras = new Bundle();
108        extras.putInt(TestConnectionService.EXTRA_START_VIDEO_STATE, videoState);
109        if (handle != null) {
110            extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
111        }
112        extras.putBoolean(TelecomManager.EXTRA_START_CALL_WITH_RTT, true);
113
114        TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
115    }
116
117    public static void addNewUnknownCall(Context context, Uri handle, Bundle extras) {
118        Log.i(TAG, "Adding new unknown call with handle " + handle);
119        PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
120                new ComponentName(context, TestConnectionService.class),
121                CallServiceNotifier.SIM_SUBSCRIPTION_ID);
122
123        if (extras == null) {
124            extras = new Bundle();
125        }
126
127        if (handle != null) {
128            extras.putParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE, handle);
129            extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
130        }
131
132        TelecomManager.from(context).addNewUnknownCall(phoneAccount, extras);
133    }
134
135    public static void hangupCalls(Context context) {
136        Log.i(TAG, "Hanging up all calls");
137        LocalBroadcastManager.getInstance(context).sendBroadcast(
138                new Intent(TestCallActivity.ACTION_HANGUP_CALLS));
139    }
140
141    public static void sendUpgradeRequest(Context context, Uri data) {
142        Log.i(TAG, "Sending upgrade request of type: " + data);
143        final Intent intent = new Intent(TestCallActivity.ACTION_SEND_UPGRADE_REQUEST);
144        intent.setData(data);
145        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
146    }
147
148    public static void remoteRttUpgrade(Context context) {
149        final Intent intent = new Intent(TestCallActivity.ACTION_REMOTE_RTT_UPGRADE);
150        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
151    }
152}
153