Home | History | Annotate | Download | only in testapps
      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.telecom.CallAudioState;
     20 import android.telecom.Connection;
     21 import android.telecom.DisconnectCause;
     22 import android.telecom.PhoneAccountHandle;
     23 import android.util.Log;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.BaseAdapter;
     28 import android.widget.CheckBox;
     29 import android.widget.TextView;
     30 
     31 import com.android.server.telecom.testapps.R;
     32 
     33 import java.util.List;
     34 
     35 public class SelfManagedCallListAdapter extends BaseAdapter {
     36 
     37     private static final String TAG = "SelfMgCallListAd";
     38     /**
     39      * Listener used to handle tap of the "disconnect" button for a connection.
     40      */
     41     private View.OnClickListener mDisconnectListener = new View.OnClickListener() {
     42         @Override
     43         public void onClick(View v) {
     44             View parent = (View) v.getParent().getParent();
     45             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
     46             connection.setConnectionDisconnected(DisconnectCause.LOCAL);
     47             SelfManagedCallList.getInstance().removeConnection(connection);
     48         }
     49     };
     50 
     51     /**
     52      * Listener used to handle tap of the "active" button for a connection.
     53      */
     54     private View.OnClickListener mActiveListener = new View.OnClickListener() {
     55         @Override
     56         public void onClick(View v) {
     57             View parent = (View) v.getParent().getParent();
     58             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
     59             connection.setConnectionActive();
     60             notifyDataSetChanged();
     61         }
     62     };
     63 
     64     /**
     65      * Listener used to handle tap of the "missed" button for a connection.
     66      */
     67     private View.OnClickListener mMissedListener = new View.OnClickListener() {
     68         @Override
     69         public void onClick(View v) {
     70             View parent = (View) v.getParent().getParent();
     71             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
     72             connection.setConnectionDisconnected(DisconnectCause.MISSED);
     73             SelfManagedCallList.getInstance().removeConnection(connection);
     74         }
     75     };
     76 
     77     private View.OnClickListener mHeldListener = new View.OnClickListener() {
     78         @Override
     79         public void onClick(View v) {
     80             View parent = (View) v.getParent().getParent();
     81             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
     82             connection.setConnectionHeld();
     83             notifyDataSetChanged();
     84         }
     85     };
     86 
     87     private View.OnClickListener mSpeakerListener = new View.OnClickListener() {
     88         @Override
     89         public void onClick(View v) {
     90             View parent = (View) v.getParent().getParent();
     91             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
     92             connection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
     93             notifyDataSetChanged();
     94         }
     95     };
     96 
     97     private View.OnClickListener mEarpieceListener = new View.OnClickListener() {
     98         @Override
     99         public void onClick(View v) {
    100             View parent = (View) v.getParent().getParent();
    101             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
    102             connection.setAudioRoute(CallAudioState.ROUTE_EARPIECE);
    103             notifyDataSetChanged();
    104         }
    105     };
    106 
    107     private View.OnClickListener mHoldableListener = new View.OnClickListener() {
    108         @Override
    109         public void onClick (View v) {
    110             View parent = (View) v.getParent().getParent();
    111             SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
    112             int capabilities = connection.getConnectionCapabilities();
    113             if ((capabilities & Connection.CAPABILITY_HOLD) == Connection.CAPABILITY_HOLD) {
    114                 capabilities &= ~(Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
    115             } else {
    116                 capabilities |= (Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
    117             }
    118             connection.setConnectionCapabilities(capabilities);
    119             notifyDataSetChanged();
    120         }
    121     };
    122 
    123     private final LayoutInflater mLayoutInflater;
    124 
    125     private List<SelfManagedConnection> mConnections;
    126 
    127     public SelfManagedCallListAdapter(LayoutInflater layoutInflater,
    128                                       List<SelfManagedConnection> connections) {
    129 
    130         mLayoutInflater = layoutInflater;
    131         mConnections = connections;
    132     }
    133 
    134     @Override
    135     public int getCount() {
    136         return mConnections.size();
    137     }
    138 
    139     @Override
    140     public Object getItem(int position) {
    141         return mConnections.get(position);
    142     }
    143 
    144     @Override
    145     public long getItemId(int position) {
    146         return position;
    147     }
    148 
    149     @Override
    150     public View getView(int position, View convertView, ViewGroup parent) {
    151         View result = convertView == null
    152                 ? mLayoutInflater.inflate(R.layout.self_managed_call_list_item, parent, false)
    153                 : convertView;
    154         SelfManagedConnection connection = mConnections.get(position);
    155         PhoneAccountHandle phoneAccountHandle = connection.getExtras().getParcelable(
    156                 SelfManagedConnection.EXTRA_PHONE_ACCOUNT_HANDLE);
    157         if (phoneAccountHandle.getId().equals(SelfManagedCallList.SELF_MANAGED_ACCOUNT_1)) {
    158             result.setBackgroundColor(result.getContext().getColor(R.color.test_call_a_color));
    159         } else {
    160             result.setBackgroundColor(result.getContext().getColor(R.color.test_call_b_color));
    161         }
    162 
    163         CallAudioState audioState = connection.getCallAudioState();
    164         String audioRoute = "?";
    165         if (audioState != null) {
    166             switch (audioState.getRoute()) {
    167                 case CallAudioState.ROUTE_BLUETOOTH:
    168                     audioRoute = "BT";
    169                     break;
    170                 case CallAudioState.ROUTE_SPEAKER:
    171                     audioRoute = "\uD83D\uDD0A";
    172                     break;
    173                 case CallAudioState.ROUTE_EARPIECE:
    174                     audioRoute = "\uD83D\uDC42";
    175                     break;
    176                 case CallAudioState.ROUTE_WIRED_HEADSET:
    177                     audioRoute = "\uD83D\uDD0C";
    178                     break;
    179                 default:
    180                     audioRoute = "?";
    181                     break;
    182             }
    183         }
    184         String callType;
    185         if (connection.isIncomingCall()) {
    186             if (connection.isIncomingCallUiShowing()) {
    187                 callType = "Incoming(our ux) ";
    188             } else {
    189                 callType = "Incoming(sys ux) ";
    190             }
    191         } else {
    192             callType = "Outgoing";
    193         }
    194         setInfoForRow(result, phoneAccountHandle.getId(), connection.getAddress().toString(),
    195                 android.telecom.Connection.stateToString(connection.getState()), audioRoute,
    196                 callType, connection.getState() == android.telecom.Connection.STATE_RINGING,
    197                 connection.isHoldable());
    198         result.setTag(connection);
    199         return result;
    200     }
    201 
    202     public void updateConnections() {
    203         Log.i(TAG, "updateConnections "+ mConnections.size());
    204 
    205         notifyDataSetChanged();
    206     }
    207 
    208     private void setInfoForRow(View view, String accountName, String number,
    209                                String status, String audioRoute, String callType,
    210             boolean isRinging, boolean isHoldable) {
    211 
    212         TextView numberTextView = (TextView) view.findViewById(R.id.phoneNumber);
    213         TextView statusTextView = (TextView) view.findViewById(R.id.callState);
    214         View activeButton = view.findViewById(R.id.setActiveButton);
    215         activeButton.setOnClickListener(mActiveListener);
    216         View disconnectButton = view.findViewById(R.id.disconnectButton);
    217         disconnectButton.setOnClickListener(mDisconnectListener);
    218         View setHeldButton = view.findViewById(R.id.setHeldButton);
    219         setHeldButton.setOnClickListener(mHeldListener);
    220         View speakerButton = view.findViewById(R.id.speakerButton);
    221         speakerButton.setOnClickListener(mSpeakerListener);
    222         View earpieceButton = view.findViewById(R.id.earpieceButton);
    223         earpieceButton.setOnClickListener(mEarpieceListener);
    224         View missedButton = view.findViewById(R.id.missedButton);
    225         missedButton.setOnClickListener(mMissedListener);
    226         missedButton.setVisibility(isRinging ? View.VISIBLE : View.GONE);
    227         setHeldButton.setVisibility(!isRinging ? View.VISIBLE : View.GONE);
    228         disconnectButton.setVisibility(!isRinging ? View.VISIBLE : View.GONE);
    229         CheckBox holdableCheckbox = view.findViewById(R.id.holdable);
    230         holdableCheckbox.setOnClickListener(mHoldableListener);
    231         holdableCheckbox.setChecked(isHoldable);
    232         numberTextView.setText(accountName + " - " + number + " (" + audioRoute + ")");
    233         statusTextView.setText(callType + " - Status: " + status);
    234     }
    235 }
    236