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.telecom.CallState;
26import android.telecom.PhoneAccountHandle;
27import android.telecom.TelecomManager;
28import android.util.Log;
29
30/**
31 * This class receives the notification callback intents used to update call states for
32 * {@link TestConnectionService}.
33 */
34public class CallNotificationReceiver extends BroadcastReceiver {
35
36    static final String TAG = CallNotificationReceiver.class.getSimpleName();
37    /**
38     * Exit intent action is sent when the user clicks the "exit" action of the
39     * TestConnectionService notification. Used to cancel (remove) the notification.
40     */
41    static final String ACTION_CALL_SERVICE_EXIT =
42            "com.android.server.telecom.testapps.ACTION_CALL_SERVICE_EXIT";
43    static final String ACTION_REGISTER_PHONE_ACCOUNT =
44            "com.android.server.telecom.testapps.ACTION_REGISTER_PHONE_ACCOUNT";
45    static final String ACTION_SHOW_ALL_PHONE_ACCOUNTS =
46            "com.android.server.telecom.testapps.ACTION_SHOW_ALL_PHONE_ACCOUNTS";
47    static final String ACTION_VIDEO_CALL =
48            "com.android.server.telecom.testapps.ACTION_VIDEO_CALL";
49    static final String ACTION_AUDIO_CALL =
50            "com.android.server.telecom.testapps.ACTION_AUDIO_CALL";
51
52    /** {@inheritDoc} */
53    @Override
54    public void onReceive(Context context, Intent intent) {
55        String action = intent.getAction();
56        if (ACTION_CALL_SERVICE_EXIT.equals(action)) {
57            CallServiceNotifier.getInstance().cancelNotifications(context);
58        } else if (ACTION_REGISTER_PHONE_ACCOUNT.equals(action)) {
59            CallServiceNotifier.getInstance().registerPhoneAccount(context);
60        } else if (ACTION_SHOW_ALL_PHONE_ACCOUNTS.equals(action)) {
61            CallServiceNotifier.getInstance().showAllPhoneAccounts(context);
62        } else if (ACTION_VIDEO_CALL.equals(action)) {
63            sendIncomingCallIntent(context, null, true);
64        } else if (ACTION_AUDIO_CALL.equals(action)) {
65            sendIncomingCallIntent(context, null, false);
66        }
67    }
68
69    /**
70     * Creates and sends the intent to add an incoming call through Telecom.
71     *
72     * @param context The current context.
73     * @param isVideoCall {@code True} if this is a video call.
74     */
75    public static void sendIncomingCallIntent(Context context, Uri handle, boolean isVideoCall) {
76        PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
77                new ComponentName(context, TestConnectionService.class),
78                CallServiceNotifier.SIM_SUBSCRIPTION_ID);
79
80        // For the purposes of testing, indicate whether the incoming call is a video call by
81        // stashing an indicator in the EXTRA_INCOMING_CALL_EXTRAS.
82        Bundle extras = new Bundle();
83        extras.putBoolean(TestConnectionService.EXTRA_IS_VIDEO_CALL, isVideoCall);
84        if (handle != null) {
85            extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
86        }
87
88        TelecomManager.from(context).addNewIncomingCall(phoneAccount, extras);
89    }
90
91    public static void addNewUnknownCall(Context context, Uri handle, Bundle extras) {
92        Log.i(TAG, "Adding new unknown call with handle " + handle);
93        PhoneAccountHandle phoneAccount = new PhoneAccountHandle(
94                new ComponentName(context, TestConnectionService.class),
95                CallServiceNotifier.SIM_SUBSCRIPTION_ID);
96
97        if (extras == null) {
98            extras = new Bundle();
99        }
100
101        if (handle != null) {
102            extras.putParcelable(TelecomManager.EXTRA_UNKNOWN_CALL_HANDLE, handle);
103            extras.putParcelable(TestConnectionService.EXTRA_HANDLE, handle);
104        }
105
106        TelecomManager.from(context).addNewUnknownCall(phoneAccount, extras);
107    }
108}
109