1/*
2 * Copyright (C) 2014 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.Context;
20import android.content.Intent;
21import android.telecom.Call;
22import android.telecom.CallAudioState;
23import android.telecom.InCallService;
24import android.telecom.Phone;
25import android.util.Log;
26
27import java.lang.Override;
28import java.lang.String;
29
30/**
31 * Test In-Call service implementation.  Logs incoming events.  Mainly used to test binding to
32 * multiple {@link InCallService} implementations.
33 */
34public class TestInCallServiceImpl extends InCallService {
35    private static final String TAG = "TestInCallServiceImpl";
36    public static TestInCallServiceImpl sInstance;
37
38    private Phone mPhone;
39
40    private Phone.Listener mPhoneListener = new Phone.Listener() {
41        @Override
42        public void onCallAdded(Phone phone, Call call) {
43            Log.i(TAG, "onCallAdded: " + call.toString());
44            TestCallList callList = TestCallList.getInstance();
45            callList.addCall(call);
46
47            if (callList.size() == 1) {
48                startInCallUI();
49            }
50        }
51
52        @Override
53        public void onCallRemoved(Phone phone, Call call) {
54            Log.i(TAG, "onCallRemoved: "+call.toString());
55            TestCallList.getInstance().removeCall(call);
56        }
57    };
58
59    @Override
60    public void onPhoneCreated(Phone phone) {
61        Log.i(TAG, "onPhoneCreated");
62        mPhone = phone;
63        mPhone.addListener(mPhoneListener);
64        TestCallList.getInstance().clearCalls();
65    }
66
67    @Override
68    public boolean onUnbind(Intent intent) {
69        Log.i(TAG, "onPhoneDestroyed");
70        mPhone.removeListener(mPhoneListener);
71        mPhone = null;
72        TestCallList.getInstance().clearCalls();
73        sInstance = null;
74        return super.onUnbind(intent);
75    }
76
77    @Override
78    public void onCallAudioStateChanged(CallAudioState cas) {
79        if (TestInCallUI.sInstance != null) {
80            TestInCallUI.sInstance.updateCallAudioState(cas);
81        }
82    }
83
84    private void startInCallUI() {
85        sInstance = this;
86        Intent intent = new Intent(Intent.ACTION_MAIN);
87        intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
88        intent.setClass(this, TestInCallUI.class);
89        startActivity(intent);
90    }
91}
92