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