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 
17 package com.android.server.telecom.testapps;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.support.v4.content.LocalBroadcastManager;
26 import android.telecom.PhoneAccountHandle;
27 import android.telecom.TelecomManager;
28 import android.telecom.VideoProfile;
29 import android.util.Log;
30 
31 /**
32  * This class receives the notification callback intents used to update call states for
33  * {@link TestConnectionService}.
34  */
35 public 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 
55     /** {@inheritDoc} */
56     @Override
onReceive(Context context, Intent intent)57     public void onReceive(Context context, Intent intent) {
58         String action = intent.getAction();
59         if (ACTION_CALL_SERVICE_EXIT.equals(action)) {
60             CallServiceNotifier.getInstance().cancelNotifications(context);
61         } else if (ACTION_REGISTER_PHONE_ACCOUNT.equals(action)) {
62             CallServiceNotifier.getInstance().registerPhoneAccount(context);
63         } else if (ACTION_SHOW_ALL_PHONE_ACCOUNTS.equals(action)) {
64             CallServiceNotifier.getInstance().showAllPhoneAccounts(context);
65         } else if (ACTION_ONE_WAY_VIDEO_CALL.equals(action)) {
66             sendIncomingCallIntent(context, null, VideoProfile.STATE_RX_ENABLED);
67         } else if (ACTION_TWO_WAY_VIDEO_CALL.equals(action)) {
68             sendIncomingCallIntent(context, null, VideoProfile.STATE_BIDIRECTIONAL);
69         } else if (ACTION_AUDIO_CALL.equals(action)) {
70             sendIncomingCallIntent(context, null, VideoProfile.STATE_AUDIO_ONLY);
71         }
72     }
73 
74     /**
75      * Creates and sends the intent to add an incoming call through Telecom.
76      *
77      * @param context The current context.
78      * @param videoState The video state requested for the incoming call.
79      */
sendIncomingCallIntent(Context context, Uri handle, int videoState)80     public static void sendIncomingCallIntent(Context context, Uri handle, int videoState) {
81         PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
82                 new ComponentName(context, TestConnectionService.class),
83                 CallServiceNotifier.SIM_SUBSCRIPTION_ID);
84 
85         // For the purposes of testing, indicate whether the incoming call is a video call by
86         // stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
87         Bundle extras = new Bundle();
88         extras.putInt(TestConnectionService.EXTRA_START_VIDEO_STATE, videoState);
89         if (handle != null) {
90             extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
91         }
92 
93         TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
94     }
95 
addNewUnknownCall(Context context, Uri handle, Bundle extras)96     public static void addNewUnknownCall(Context context, Uri handle, Bundle extras) {
97         Log.i(TAG, "Adding new unknown call with handle " + handle);
98         PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
99                 new ComponentName(context, TestConnectionService.class),
100                 CallServiceNotifier.SIM_SUBSCRIPTION_ID);
101 
102         if (extras == null) {
103             extras = new Bundle();
104         }
105 
106         if (handle != null) {
107             extras.putParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE, handle);
108             extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
109         }
110 
111         TelecomManager.from(context).addNewUnknownCall(phoneAccount, extras);
112     }
113 
hangupCalls(Context context)114     public static void hangupCalls(Context context) {
115         Log.i(TAG, "Hanging up all calls");
116         LocalBroadcastManager.getInstance(context).sendBroadcast(
117                 new Intent(TestCallActivity.ACTION_HANGUP_CALLS));
118     }
119 
sendUpgradeRequest(Context context, Uri data)120     public static void sendUpgradeRequest(Context context, Uri data) {
121         Log.i(TAG, "Sending upgrade request of type: " + data);
122         final Intent intent = new Intent(TestCallActivity.ACTION_SEND_UPGRADE_REQUEST);
123         intent.setData(data);
124         LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
125     }
126 }
127