1 /* 2 * Copyright (C) 2017 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.app.Notification; 20 import android.app.NotificationManager; 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.graphics.drawable.Icon; 25 import android.media.MediaPlayer; 26 import android.telecom.CallAudioState; 27 import android.telecom.Connection; 28 import android.telecom.ConnectionService; 29 import android.telecom.DisconnectCause; 30 import android.telecom.VideoProfile; 31 32 import com.android.server.telecom.testapps.R; 33 34 /** 35 * Sample self-managed {@link Connection} for a self-managed {@link ConnectionService}. 36 * <p> 37 * See {@link android.telecom} for more information on self-managed {@link ConnectionService}s. 38 */ 39 public class SelfManagedConnection extends Connection { 40 public static class Listener { onConnectionStateChanged(SelfManagedConnection connection)41 public void onConnectionStateChanged(SelfManagedConnection connection) {} onConnectionRemoved(SelfManagedConnection connection)42 public void onConnectionRemoved(SelfManagedConnection connection) {} 43 } 44 45 public static final String EXTRA_PHONE_ACCOUNT_HANDLE = 46 "com.android.server.telecom.testapps.extra.PHONE_ACCOUNT_HANDLE"; 47 public static final String CALL_NOTIFICATION = "com.android.server.telecom.testapps.CALL"; 48 49 private static int sNextCallId = 1; 50 51 private final int mCallId; 52 private final Context mContext; 53 private final SelfManagedCallList mCallList; 54 private final MediaPlayer mMediaPlayer; 55 private final boolean mIsIncomingCall; 56 private boolean mIsIncomingCallUiShowing; 57 private Listener mListener; 58 SelfManagedConnection(SelfManagedCallList callList, Context context, boolean isIncoming)59 SelfManagedConnection(SelfManagedCallList callList, Context context, boolean isIncoming) { 60 mCallList = callList; 61 mMediaPlayer = createMediaPlayer(context); 62 mIsIncomingCall = isIncoming; 63 mContext = context; 64 mCallId = sNextCallId++; 65 } 66 setListener(Listener listener)67 public void setListener(Listener listener) { 68 mListener = listener; 69 } 70 71 /** 72 * Handles updates to the audio state of the connection. 73 * @param state The new connection audio state. 74 */ 75 @Override onCallAudioStateChanged(CallAudioState state)76 public void onCallAudioStateChanged(CallAudioState state) { 77 mCallList.notifyCallModified(); 78 } 79 80 @Override onShowIncomingCallUi()81 public void onShowIncomingCallUi() { 82 // Create the fullscreen intent used to show the fullscreen incoming call UX. 83 Intent intent = new Intent(Intent.ACTION_MAIN, null); 84 intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK); 85 intent.setClass(mContext, IncomingSelfManagedCallActivity.class); 86 intent.putExtra(IncomingSelfManagedCallActivity.EXTRA_CALL_ID, mCallId); 87 PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 1, intent, 0); 88 89 // Build the notification as an ongoing high priority item. 90 final Notification.Builder builder = new Notification.Builder(mContext); 91 builder.setOngoing(true); 92 builder.setPriority(Notification.PRIORITY_HIGH); 93 94 // Set up the main intent to send the user to the incoming call screen. 95 builder.setContentIntent(pendingIntent); 96 builder.setFullScreenIntent(pendingIntent, true); 97 98 // Setup notification content. 99 builder.setSmallIcon(R.drawable.ic_android_black_24dp); 100 builder.setContentTitle("Incoming call..."); 101 builder.setContentText("Incoming test call from " + getAddress()); 102 103 // Setup answer and reject call button 104 final Intent answerIntent = new Intent( 105 SelfManagedCallNotificationReceiver.ACTION_ANSWER_CALL, null, mContext, 106 SelfManagedCallNotificationReceiver.class); 107 answerIntent.putExtra(IncomingSelfManagedCallActivity.EXTRA_CALL_ID, mCallId); 108 final Intent rejectIntent = new Intent( 109 SelfManagedCallNotificationReceiver.ACTION_REJECT_CALL, null, mContext, 110 SelfManagedCallNotificationReceiver.class); 111 rejectIntent.putExtra(IncomingSelfManagedCallActivity.EXTRA_CALL_ID, mCallId); 112 113 builder.addAction( 114 new Notification.Action.Builder( 115 Icon.createWithResource(mContext, R.drawable.ic_android_black_24dp), 116 "Answer", 117 PendingIntent.getBroadcast(mContext, 0, answerIntent, 118 PendingIntent.FLAG_UPDATE_CURRENT)) 119 .build()); 120 builder.addAction( 121 new Notification.Action.Builder( 122 Icon.createWithResource(mContext, R.drawable.ic_android_black_24dp), 123 "Reject", 124 PendingIntent.getBroadcast(mContext, 0, rejectIntent, 125 PendingIntent.FLAG_UPDATE_CURRENT)) 126 .build()); 127 128 NotificationManager notificationManager = mContext.getSystemService( 129 NotificationManager.class); 130 notificationManager.notify(CALL_NOTIFICATION, mCallId, builder.build()); 131 } 132 133 @Override onHold()134 public void onHold() { 135 setOnHold(); 136 } 137 138 @Override onUnhold()139 public void onUnhold() { 140 setActive(); 141 } 142 143 @Override onAnswer(int videoState)144 public void onAnswer(int videoState) { 145 setConnectionActive(); 146 } 147 148 @Override onAnswer()149 public void onAnswer() { 150 onAnswer(VideoProfile.STATE_AUDIO_ONLY); 151 } 152 153 @Override onReject()154 public void onReject() { 155 setConnectionDisconnected(DisconnectCause.REJECTED); 156 } 157 158 @Override onDisconnect()159 public void onDisconnect() { 160 setConnectionDisconnected(DisconnectCause.LOCAL); 161 } 162 setConnectionActive()163 public void setConnectionActive() { 164 mMediaPlayer.start(); 165 setActive(); 166 if (mListener != null ) { 167 mListener.onConnectionStateChanged(this); 168 } 169 } 170 setConnectionHeld()171 public void setConnectionHeld() { 172 mMediaPlayer.pause(); 173 setOnHold(); 174 if (mListener != null ) { 175 mListener.onConnectionStateChanged(this); 176 } 177 } 178 setConnectionDisconnected(int cause)179 public void setConnectionDisconnected(int cause) { 180 mMediaPlayer.stop(); 181 setDisconnected(new DisconnectCause(cause)); 182 destroy(); 183 if (mListener != null ) { 184 mListener.onConnectionRemoved(this); 185 } 186 } 187 setIsIncomingCallUiShowing(boolean showing)188 public void setIsIncomingCallUiShowing(boolean showing) { 189 mIsIncomingCallUiShowing = showing; 190 } 191 isIncomingCallUiShowing()192 public boolean isIncomingCallUiShowing() { 193 return mIsIncomingCallUiShowing; 194 } 195 isIncomingCall()196 public boolean isIncomingCall() { 197 return mIsIncomingCall; 198 } 199 getCallId()200 public int getCallId() { 201 return mCallId; 202 } 203 createMediaPlayer(Context context)204 private MediaPlayer createMediaPlayer(Context context) { 205 int audioToPlay = (Math.random() > 0.5f) ? R.raw.sample_audio : R.raw.sample_audio2; 206 MediaPlayer mediaPlayer = MediaPlayer.create(context, audioToPlay); 207 mediaPlayer.setLooping(true); 208 return mediaPlayer; 209 } 210 } 211