summaryrefslogtreecommitdiff
path: root/testapps/src/com/android/server/telecom/testapps/SelfManagedCallListAdapter.java
blob: 75ceb62613e13d767075324d6008220d31173dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.server.telecom.testapps;

import android.telecom.CallAudioState;
import android.telecom.Connection;
import android.telecom.DisconnectCause;
import android.telecom.PhoneAccountHandle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.android.server.telecom.testapps.R;

import java.util.List;

public class SelfManagedCallListAdapter extends BaseAdapter {

    private static final String TAG = "SelfMgCallListAd";
    /**
     * Listener used to handle tap of the "disconnect" button for a connection.
     */
    private View.OnClickListener mDisconnectListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setConnectionDisconnected(DisconnectCause.LOCAL);
            SelfManagedCallList.getInstance().removeConnection(connection);
        }
    };

    /**
     * Listener used to handle tap of the "active" button for a connection.
     */
    private View.OnClickListener mActiveListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setConnectionActive();
            notifyDataSetChanged();
        }
    };

    /**
     * Listener used to handle tap of the "missed" button for a connection.
     */
    private View.OnClickListener mMissedListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setConnectionDisconnected(DisconnectCause.MISSED);
            SelfManagedCallList.getInstance().removeConnection(connection);
        }
    };

    private View.OnClickListener mHeldListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setConnectionHeld();
            notifyDataSetChanged();
        }
    };

    private View.OnClickListener mSpeakerListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
            notifyDataSetChanged();
        }
    };

    private View.OnClickListener mEarpieceListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setAudioRoute(CallAudioState.ROUTE_EARPIECE);
            notifyDataSetChanged();
        }
    };

    private View.OnClickListener mBluetoothListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            connection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
            notifyDataSetChanged();
        }
    };

    private View.OnClickListener mHoldableListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            View parent = (View) v.getParent().getParent();
            SelfManagedConnection connection = (SelfManagedConnection) parent.getTag();
            int capabilities = connection.getConnectionCapabilities();
            if ((capabilities & Connection.CAPABILITY_HOLD) == Connection.CAPABILITY_HOLD) {
                capabilities &= ~(Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
            } else {
                capabilities |= (Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
            }
            connection.setConnectionCapabilities(capabilities);
            notifyDataSetChanged();
        }
    };

    private final LayoutInflater mLayoutInflater;

    private List<SelfManagedConnection> mConnections;

    public SelfManagedCallListAdapter(LayoutInflater layoutInflater,
                                      List<SelfManagedConnection> connections) {

        mLayoutInflater = layoutInflater;
        mConnections = connections;
    }

    @Override
    public int getCount() {
        return mConnections.size();
    }

    @Override
    public Object getItem(int position) {
        return mConnections.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View result = convertView == null
                ? mLayoutInflater.inflate(R.layout.self_managed_call_list_item, parent, false)
                : convertView;
        SelfManagedConnection connection = mConnections.get(position);
        PhoneAccountHandle phoneAccountHandle = connection.getExtras().getParcelable(
                SelfManagedConnection.EXTRA_PHONE_ACCOUNT_HANDLE);
        if (phoneAccountHandle.getId().equals(SelfManagedCallList.SELF_MANAGED_ACCOUNT_1)) {
            result.setBackgroundColor(result.getContext().getColor(R.color.test_call_a_color));
        } else {
            result.setBackgroundColor(result.getContext().getColor(R.color.test_call_b_color));
        }

        CallAudioState audioState = connection.getCallAudioState();
        String audioRoute = "?";
        if (audioState != null) {
            switch (audioState.getRoute()) {
                case CallAudioState.ROUTE_BLUETOOTH:
                    audioRoute = "BT";
                    break;
                case CallAudioState.ROUTE_SPEAKER:
                    audioRoute = "\uD83D\uDD0A";
                    break;
                case CallAudioState.ROUTE_EARPIECE:
                    audioRoute = "\uD83D\uDC42";
                    break;
                case CallAudioState.ROUTE_WIRED_HEADSET:
                    audioRoute = "\uD83D\uDD0C";
                    break;
                default:
                    audioRoute = "?";
                    break;
            }
        }
        String callType;
        if (connection.isIncomingCall()) {
            if (connection.isIncomingCallUiShowing()) {
                callType = "Incoming(our ux) ";
            } else {
                callType = "Incoming(sys ux) ";
            }
        } else {
            callType = "Outgoing";
        }
        setInfoForRow(result, phoneAccountHandle.getId(), connection.getAddress().toString(),
                android.telecom.Connection.stateToString(connection.getState()), audioRoute,
                callType, connection.getState() == android.telecom.Connection.STATE_RINGING, 
                connection.isHoldable());
        result.setTag(connection);
        return result;
    }

    public void updateConnections() {
        Log.i(TAG, "updateConnections "+ mConnections.size());

        notifyDataSetChanged();
    }

    private void setInfoForRow(View view, String accountName, String number,
                               String status, String audioRoute, String callType,
            boolean isRinging, boolean isHoldable) {

        TextView numberTextView = (TextView) view.findViewById(R.id.phoneNumber);
        TextView statusTextView = (TextView) view.findViewById(R.id.callState);
        View activeButton = view.findViewById(R.id.setActiveButton);
        activeButton.setOnClickListener(mActiveListener);
        View disconnectButton = view.findViewById(R.id.disconnectButton);
        disconnectButton.setOnClickListener(mDisconnectListener);
        View setHeldButton = view.findViewById(R.id.setHeldButton);
        setHeldButton.setOnClickListener(mHeldListener);
        View speakerButton = view.findViewById(R.id.speakerButton);
        speakerButton.setOnClickListener(mSpeakerListener);
        View earpieceButton = view.findViewById(R.id.earpieceButton);
        earpieceButton.setOnClickListener(mEarpieceListener);
        View bluetoothButton = view.findViewById(R.id.bluetoothButton);
        bluetoothButton.setOnClickListener(mBluetoothListener);
        View missedButton = view.findViewById(R.id.missedButton);
        missedButton.setOnClickListener(mMissedListener);
        missedButton.setVisibility(isRinging ? View.VISIBLE : View.GONE);
        setHeldButton.setVisibility(!isRinging ? View.VISIBLE : View.GONE);
        disconnectButton.setVisibility(!isRinging ? View.VISIBLE : View.GONE);
        CheckBox holdableCheckbox = view.findViewById(R.id.holdable);
        holdableCheckbox.setOnClickListener(mHoldableListener);
        holdableCheckbox.setChecked(isHoldable);
        numberTextView.setText(accountName + " - " + number + " (" + audioRoute + ")");
        statusTextView.setText(callType + " - Status: " + status);
    }
}