1 /* 2 * Copyright (c) 2016, 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 package com.android.car.stream.telecom; 17 18 import android.app.PendingIntent; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.graphics.Bitmap; 22 import android.graphics.drawable.VectorDrawable; 23 import android.telecom.Call; 24 import com.android.car.stream.BitmapUtils; 25 import com.android.car.stream.CurrentCallExtension; 26 import com.android.car.stream.R; 27 import com.android.car.stream.StreamCard; 28 import com.android.car.stream.StreamConstants; 29 30 /** 31 * A converter that creates a {@link StreamCard} for the current call events. 32 */ 33 public class CurrentCallConverter { 34 private static final int MUTE_BUTTON_REQUEST_CODE = 12; 35 private static final int CALL_BUTTON_REQUEST_CODE = 13; 36 37 private PendingIntent mMuteAction; 38 private PendingIntent mUnMuteAction; 39 private PendingIntent mAcceptCallAction; 40 private PendingIntent mHangupCallAction; 41 CurrentCallConverter(Context context)42 public CurrentCallConverter(Context context) { 43 mMuteAction = getCurrentCallAction(context, 44 TelecomConstants.ACTION_MUTE, MUTE_BUTTON_REQUEST_CODE); 45 mUnMuteAction = getCurrentCallAction(context, 46 TelecomConstants.ACTION_MUTE, MUTE_BUTTON_REQUEST_CODE); 47 48 mAcceptCallAction = getCurrentCallAction(context, 49 TelecomConstants.ACTION_ACCEPT_CALL, CALL_BUTTON_REQUEST_CODE); 50 mHangupCallAction = getCurrentCallAction(context, 51 TelecomConstants.ACTION_HANG_UP_CALL, CALL_BUTTON_REQUEST_CODE); 52 } 53 getCurrentCallAction(Context context, String action, int requestcode)54 private PendingIntent getCurrentCallAction(Context context, 55 String action, int requestcode) { 56 Intent intent = new Intent(TelecomConstants.INTENT_ACTION_STREAM_CALL_CONTROL); 57 intent.setPackage(context.getPackageName()); 58 intent.putExtra(TelecomConstants.EXTRA_STREAM_CALL_ACTION, action); 59 PendingIntent pendingIntent = 60 PendingIntent.getBroadcast( 61 context, 62 requestcode, 63 intent, 64 PendingIntent.FLAG_CANCEL_CURRENT 65 ); 66 return pendingIntent; 67 } 68 convert(Call call, Context context, boolean isMuted, long callStartTime, String dialerPackage)69 public StreamCard convert(Call call, Context context, boolean isMuted, 70 long callStartTime, String dialerPackage) { 71 long timeStamp = System.currentTimeMillis() - call.getDetails().getConnectTimeMillis(); 72 int callState = call.getState(); 73 String number = TelecomUtils.getNumber(call); 74 String displayName = TelecomUtils.getDisplayName(context, call); 75 long digits = Long.valueOf(number.replaceAll("[^0-9]", "")); 76 77 PendingIntent dialerPendingIntent = 78 PendingIntent.getActivity( 79 context, 80 0, 81 context.getPackageManager().getLaunchIntentForPackage(dialerPackage), 82 PendingIntent.FLAG_UPDATE_CURRENT 83 ); 84 85 StreamCard.Builder builder = new StreamCard.Builder(StreamConstants.CARD_TYPE_CURRENT_CALL, 86 digits /* id */, timeStamp); 87 builder.setPrimaryText(displayName); 88 builder.setSecondaryText(getCallState(context, callState)); 89 90 Bitmap phoneIcon = BitmapUtils.getBitmap( 91 (VectorDrawable) context.getDrawable(R.drawable.ic_phone)); 92 builder.setPrimaryIcon(phoneIcon); 93 builder.setSecondaryIcon(TelecomUtils.createStreamCardSecondaryIcon(context, number)); 94 builder.setClickAction(dialerPendingIntent); 95 builder.setCardExtension(createCurrentCallExtension(context, callStartTime, displayName, 96 callState, isMuted, number)); 97 return builder.build(); 98 } 99 createCurrentCallExtension(Context context, long callStartTime, String displayName, int callState, boolean isMuted, String number)100 private CurrentCallExtension createCurrentCallExtension(Context context, long callStartTime, 101 String displayName, int callState, boolean isMuted, String number) { 102 103 Bitmap contactPhoto = TelecomUtils 104 .getContactPhotoFromNumber(context.getContentResolver(), number); 105 CurrentCallExtension extension 106 = new CurrentCallExtension(callStartTime, displayName, callState, isMuted, 107 contactPhoto, mMuteAction, mUnMuteAction, mAcceptCallAction, mHangupCallAction); 108 return extension; 109 } 110 getCallState(Context context, int state)111 private String getCallState(Context context, int state) { 112 switch (state) { 113 case Call.STATE_ACTIVE: 114 return context.getString(R.string.ongoing_call); 115 case Call.STATE_DIALING: 116 return context.getString(R.string.dialing_call); 117 case Call.STATE_DISCONNECTING: 118 return context.getString(R.string.disconnecting_call); 119 case Call.STATE_RINGING: 120 return context.getString(R.string.notification_incoming_call); 121 default: 122 return context.getString(R.string.unknown); 123 } 124 } 125 126 } 127