summaryrefslogtreecommitdiffstats
path: root/bttestapp/src/org/codeaurora/bluetooth/bttestapp/HfpTestActivity.java
blob: aaf13691f264dd609f02bcbde0fcd46b6c91a1cc (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *        * Redistributions of source code must retain the above copyright
 *            notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *            notice, this list of conditions and the following disclaimer in the
 *            documentation and/or other materials provided with the distribution.
 *        * Neither the name of The Linux Foundation nor
 *            the names of its contributors may be used to endorse or promote
 *            products derived from this software without specific prior written
 *            permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.    IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package org.codeaurora.bluetooth.bttestapp;

import android.app.ActionBar;
import android.app.DialogFragment;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothHeadsetClientCall;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import org.codeaurora.bluetooth.bttestapp.CallHistoryDialogFragment.CallHistoryDialogListener;
import org.codeaurora.bluetooth.bttestapp.R;
import org.codeaurora.bluetooth.bttestapp.util.Logger;
import org.codeaurora.bluetooth.bttestapp.util.MonkeyEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Hashtable;

public class HfpTestActivity extends MonkeyActivity implements IBluetoothConnectionObserver,
        CallHistoryDialogListener {

    private final String TAG = "HfpTestActivity";

    private DialpadFragment mDialpadFragment = null;

    private IndicatorsFragment mIndicatorsFragment = null;

    private CallsListFragment mCallsListFragment = null;

    private final ArrayList<String> mCallHistory = new ArrayList<String>();

    private ActionBar mActionBar = null;

    private Hashtable<Integer, BluetoothHeadsetClientCall> mCalls;

   // this should be visible for fragments
    BluetoothHeadsetClient mBluetoothHeadsetClient;
    ProfileService mProfileService = null;
    BluetoothDevice mDevice;

    // this should be done in a more elegant way //
    public boolean mFeatVtag;
    public boolean mFeatVoiceRecognition;
    public boolean mFeatThreeWayCalling;
    public boolean mFeatEnhancedCallControl;

    public boolean mFeatReject;
    public boolean mFeatMerge;
    public boolean mFeatMergeDetach;
    public boolean mFeatReleaseAndAccept;
    public boolean mFeatAcceptHeldOrWaiting;
    public boolean mFeatReleaseHeldOrWaiting;

    private final BroadcastReceiver mHfpClientReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = (BluetoothDevice)
                    intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (action.equals(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED)) {

                int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
                int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                onReceiveActionConnectionStateChanged(device, prevState, state, intent.getExtras());
                mCallsListFragment.onConnStateChanged(state, prevState);
                mIndicatorsFragment.onConnStateChanged(state, prevState);

                if (state == BluetoothProfile.STATE_CONNECTED) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable(){
                        @Override
                        public void run(){
                                if (mBluetoothHeadsetClient == null || mDevice == null) {
                                    Logger.v(TAG, "Profile is disconnected");
                                    return;
                                }
                                for (BluetoothHeadsetClientCall call :
                                        mBluetoothHeadsetClient.getCurrentCalls(mDevice)) {
                                    Logger.v(TAG, "Updating call controls");
                                    mCallsListFragment.onCallChanged(call);
                                }
                            }
                    }, 500);
                }

                // Send MonkeyEvent
                new MonkeyEvent("hfp-connection-state-changed", true)
                        .addReplyParam("state", state)
                        .addReplyParam("prevState", prevState)
                        .send();
            } else if (action.equals(BluetoothHeadsetClient.ACTION_AG_EVENT)) {

                mIndicatorsFragment.onAgEvent(intent.getExtras());

            } else if (action.equals(BluetoothHeadsetClient.ACTION_CALL_CHANGED)) {

                BluetoothHeadsetClientCall call = (BluetoothHeadsetClientCall) intent.
                        getParcelableExtra(BluetoothHeadsetClient.EXTRA_CALL);
                onCallChanged(call);
                mCallsListFragment.onCallChanged(call);

                // Send MonkeyEvent
                new MonkeyEvent("hfp-call-changed", true)
                        .addExtReply(callToJson(call))
                        .send();
            } else if (action.equals(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED)) {

                int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);
                int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
                onReceiveAudioStateChange(device, prevState, state);
                mIndicatorsFragment.onAudioStateChanged(state, prevState);
                if (state == BluetoothHeadsetClient.STATE_AUDIO_CONNECTED) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable(){
                        @Override
                        public void run(){
                                if (mBluetoothHeadsetClient == null || mDevice == null) {
                                    Logger.v(TAG, "Profile is disconnected");
                                    return;
                                }
                                for (BluetoothHeadsetClientCall call :
                                        mBluetoothHeadsetClient.getCurrentCalls(mDevice)) {
                                    Logger.v(TAG, "Updating call controls");
                                    mCallsListFragment.onCallChanged(call);
                                }
                            }
                    }, 500);
                }

                // Send MonkeyEvent
                new MonkeyEvent("hfp-audio-state-changed", true)
                        .addReplyParam("state", state)
                        .addReplyParam("prevState", prevState)
                        .send();
            } else if(action.equals(BluetoothHeadsetClient.ACTION_RESULT)) {
                int result = intent.getIntExtra(
                        BluetoothHeadsetClient.EXTRA_RESULT_CODE, -1);
                int cme = intent.getIntExtra(
                        BluetoothHeadsetClient.EXTRA_CME_CODE, -1);

                onReceiveResult(result, cme);

                // Send MonkeyEvent
                new MonkeyEvent("hfp-receive-result", true)
                        .addReplyParam("result", result)
                        .addReplyParam("cme", cme)
                        .send();
            } else if (action.equals(BluetoothHeadsetClient.ACTION_LAST_VTAG)) {

                String number = intent.getStringExtra(BluetoothHeadsetClient.EXTRA_NUMBER);
                Toast.makeText(HfpTestActivity.this, "Phone number received: " + number,
                        Toast.LENGTH_LONG).show();
                // Send MonkeyEvent
                new MonkeyEvent("hfp-receive-phonenumber", true)
                        .addReplyParam("number", number)
                        .send();
            }
        }

        private void onCallChanged(BluetoothHeadsetClientCall call) {
            int state = call.getState();
            String number = call.getNumber().trim();

            new MonkeyEvent("hfp-call-changed", true).addExtReply(callToJson(call)).send();

            if (number.isEmpty()) {
                return;
            }

            // we add any "new" call to list and put in at the beginning
            if (state == BluetoothHeadsetClientCall.CALL_STATE_ALERTING ||
                    state == BluetoothHeadsetClientCall.CALL_STATE_DIALING ||
                    state == BluetoothHeadsetClientCall.CALL_STATE_INCOMING ||
                    state == BluetoothHeadsetClientCall.CALL_STATE_WAITING) {
                mCallHistory.remove(number);
                mCallHistory.add(0, number);

                if (mCallHistory.size() > 20) {
                    mCallHistory.remove(20);
                }
            }
        }

        private void onReceiveResult(int result, int cme) {
            Logger.v(TAG, "onReceiveResult (result: " + result + " cme: " +
                    cme);

            Toast.makeText(HfpTestActivity.this,
                    resultCodeToString(result, cme),
                    Toast.LENGTH_SHORT).show();
        }

        private synchronized void onReceiveAudioStateChange(BluetoothDevice device,
                int prevState, int state) {
            Logger.v(TAG, "onReceiveAudioStateChange (" + prevState +
                    " -> " + state + ")");

            invalidateOptionsMenu();
        }

        private void onReceiveActionConnectionStateChanged(BluetoothDevice device,
                int prevState, int state, Bundle features) {
            Logger.v(TAG, "onReceiveActionConnectionStateChanged: " +
                    device.getAddress() + " (" +
                    String.valueOf(prevState) + " -> " +
                    String.valueOf(state) + ")");

            switch (state) {
                case BluetoothProfile.STATE_CONNECTED:
                    updateAgFeatures(features);
                    Toast.makeText(HfpTestActivity.this, R.string.msg_connection_success,
                            Toast.LENGTH_SHORT).show();

                    new MonkeyEvent("hfp-connected", true).send();

                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    new MonkeyEvent("hfp-disconnected", true).send();

                    Toast.makeText(HfpTestActivity.this, R.string.msg_disconnection_success,
                            Toast.LENGTH_SHORT).show();

                    break;
            }

            invalidateOptionsMenu();
        }
    };

    // HFP Service Connection.
    private final ServiceConnection mHfpServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Logger.v(TAG, "onServiceConnected()");
            mProfileService = ((ProfileService.LocalBinder) service).getService();
            mBluetoothHeadsetClient = mProfileService.getHfpClient();

            int connState = mBluetoothHeadsetClient.getConnectionState(mDevice);

            if (connState == BluetoothProfile.STATE_CONNECTED) {
                // trigger refresh of calls list
                for (BluetoothHeadsetClientCall call : mBluetoothHeadsetClient
                        .getCurrentCalls(mDevice)) {
                    mCallsListFragment.onCallChanged(call);
                }

                // get supported AG features
                updateAgFeatures(mBluetoothHeadsetClient.getCurrentAgFeatures(mDevice));
            }

            // trigger refresh of indicators
            mIndicatorsFragment.onAudioStateChanged(
                    mBluetoothHeadsetClient.getAudioState(mDevice), 0);
            mIndicatorsFragment.onConnStateChanged(connState, 0);
            mIndicatorsFragment.onAgEvent(mBluetoothHeadsetClient.getCurrentAgEvents(mDevice));

            invalidateOptionsMenu();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Logger.v(TAG, "onServiceDisconnected()");
            mProfileService = null;
            mBluetoothHeadsetClient = null;

            invalidateOptionsMenu();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Logger.v(TAG, "onCreate()");

        ActivityHelper.initialize(this, R.layout.activity_hfp_test);
        BluetoothConnectionReceiver.registerObserver(this);
        mCalls = new Hashtable<Integer, BluetoothHeadsetClientCall>();

        // bind to app service
        Intent intent = new Intent(this, ProfileService.class);
        bindService(intent, mHfpServiceConnection, BIND_AUTO_CREATE);

        prepareActionBar();

        mIndicatorsFragment = (IndicatorsFragment) getFragmentManager().findFragmentById(
                R.id.indicators);
        mCallsListFragment = (CallsListFragment) getFragmentManager().findFragmentById(
                R.id.calls_list);
        mDialpadFragment = (DialpadFragment) getFragmentManager().findFragmentById(R.id.dialpad);

        setVolumeControlStream(AudioManager.STREAM_BLUETOOTH_SCO);
    }

    @Override
    protected void onDestroy() {
        Logger.v(TAG, "onDestroy");

        super.onDestroy();

        unbindService(mHfpServiceConnection);
        BluetoothConnectionReceiver.removeObserver(this);
    }

    @Override
    protected void onResume() {
        Logger.v(TAG, "onResume");

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothHeadsetClient.ACTION_AG_EVENT);
        filter.addAction(BluetoothHeadsetClient.ACTION_CALL_CHANGED);
        filter.addAction(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED);
        filter.addAction(BluetoothHeadsetClient.ACTION_RESULT);
        filter.addAction(BluetoothHeadsetClient.ACTION_LAST_VTAG);
        registerReceiver(mHfpClientReceiver, filter);
        if (mBluetoothHeadsetClient != null) {
            for (BluetoothHeadsetClientCall call : mCalls.values()) {
                call.setState(BluetoothHeadsetClientCall.CALL_STATE_TERMINATED);
                mCallsListFragment.onCallChanged(call);
                new MonkeyEvent("hfp-call-changed", true)
                        .addExtReply(callToJson(call))
                        .send();
            }
            for (BluetoothHeadsetClientCall call :
                    mBluetoothHeadsetClient.getCurrentCalls(mDevice)) {
                Logger.v(TAG, "Updating call controls");
                mCallsListFragment.onCallChanged(call);
                new MonkeyEvent("hfp-call-changed", true)
                        .addExtReply(callToJson(call))
                        .send();
            }
        } else {
            Logger.v(TAG,"mBluetoothHeadsetClient is null");
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        Logger.v(TAG, "onPause");

        unregisterReceiver(mHfpClientReceiver);
        if (mBluetoothHeadsetClient != null) {
            mCalls.clear();
            Integer id = 1;
            // save all calls status
            if (!mBluetoothHeadsetClient.getCurrentCalls(mDevice).isEmpty()) {
                for (BluetoothHeadsetClientCall call :
                    mBluetoothHeadsetClient.getCurrentCalls(mDevice)) {
                    mCalls.put(id, call);
                    id++;
                }
            }
        } else {
            Logger.v(TAG,"mBluetoothHeadsetClient is null");
        }
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mActionBarMenu = menu;

        getMenuInflater().inflate(R.menu.menu_hfp_test, menu);
        menu.findItem(R.id.menu_request_phone_num).setEnabled(mFeatVtag);

        return true;
    }

    @Override
    public void onDeviceChanged(BluetoothDevice device) {
        Logger.v(TAG, "onDeviceChanged()");

        mDevice = device;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_request_phone_num:
                mBluetoothHeadsetClient.getLastVoiceTagNumber(mDevice);
                return true;

            case R.id.menu_call_history:
                new CallHistoryDialogFragment(mCallHistory).show(getFragmentManager(),
                        "call-history");
                return true;
        }

        return false;
    }

    @Override
    public void onDeviceDisconected() {
        Logger.v(TAG, "onDeviceDisconected");

        invalidateOptionsMenu();
    }

    @Override
    public void onCallHistoryDialogPositive(DialogFragment dialog, String number) {
        mDialpadFragment.setNumber(number);
    }

    private void prepareActionBar() {
        Logger.v(TAG, "prepareActionBar()");

        mActionBar = getActionBar();
        if (mActionBar != null) {
            mActionBar.setTitle(R.string.title_hfp_test);
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        }
    }

    private String resultCodeToString(int result, int cme) {
        Logger.v(TAG, "resultCodeToString()");

        switch (result) {
            case BluetoothHeadsetClient.ACTION_RESULT_OK:
                return getString(R.string.hfptest_result_ok_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR:
                return getString(R.string.hfptest_result_error_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_NO_CARRIER:
                return getString(R.string.hfptest_result_no_carrier_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_BUSY:
                return getString(R.string.hfptest_result_busy_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_NO_ANSWER:
                return getString(R.string.hfptest_result_no_answer_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_DELAYED:
                return getString(R.string.hfptest_result_delayed_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_BLACKLISTED:
                return getString(R.string.hfptest_result_blacklisted_text);
            case BluetoothHeadsetClient.ACTION_RESULT_ERROR_CME:
                return getString(R.string.hfptest_result_cme_text) + " (" + cme + ")";
        }

        return getString(R.string.hfptest_call_state_unknown_text);
    }

    @Override
    protected void onMonkeyQuery(String op, Bundle params) {
        if ("getIndicators".equals(op)) {
            Bundle bundle = mBluetoothHeadsetClient.getCurrentAgEvents(mDevice);

            MonkeyEvent me = new MonkeyEvent("hfp-ag-event", true);

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION)) {
                me.addReplyParam("vr", bundle.getInt(BluetoothHeadsetClient.EXTRA_VOICE_RECOGNITION));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_IN_BAND_RING)) {
                me.addReplyParam("in-band", bundle.getInt(BluetoothHeadsetClient.EXTRA_IN_BAND_RING));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_NETWORK_STATUS)) {
                me.addReplyParam("network-status",
                        bundle.getInt(BluetoothHeadsetClient.EXTRA_NETWORK_STATUS));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_NETWORK_ROAMING)) {
                me.addReplyParam("network-roaming",
                        bundle.getInt(BluetoothHeadsetClient.EXTRA_NETWORK_ROAMING));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH)) {
                me.addReplyParam("signal-strength",
                        bundle.getInt(BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_BATTERY_LEVEL)) {
                me.addReplyParam("battery-level",
                        bundle.getInt(BluetoothHeadsetClient.EXTRA_BATTERY_LEVEL));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_OPERATOR_NAME)) {
                me.addReplyParam("operator-name",
                        bundle.getString(BluetoothHeadsetClient.EXTRA_OPERATOR_NAME));
            }

            if (bundle.containsKey(BluetoothHeadsetClient.EXTRA_SUBSCRIBER_INFO)) {
                me.addReplyParam("subscriber-info",
                        bundle.getString(BluetoothHeadsetClient.EXTRA_SUBSCRIBER_INFO));
            }

            me.send();
        }
    }

    static String callToJson(BluetoothHeadsetClientCall call) {
        JSONObject json = new JSONObject();

        try {
            json.put("id", call.getId());
            json.put("number", call.getNumber());
            json.put("state", callStateToString(call.getState()));
            json.put("outgoing", call.isOutgoing());
            json.put("multiparty", call.isMultiParty());
        } catch (JSONException e) {
            // do nothing
        }

        return json.toString();
    }

    static String callStateToString(int state) {
        switch (state) {
            case BluetoothHeadsetClientCall.CALL_STATE_ACTIVE:
                return "active";
            case BluetoothHeadsetClientCall.CALL_STATE_HELD:
                return "held";
            case BluetoothHeadsetClientCall.CALL_STATE_DIALING:
                return "dialing";
            case BluetoothHeadsetClientCall.CALL_STATE_ALERTING:
                return "alerting";
            case BluetoothHeadsetClientCall.CALL_STATE_INCOMING:
                return "incoming";
            case BluetoothHeadsetClientCall.CALL_STATE_WAITING:
                return "waiting";
            case BluetoothHeadsetClientCall.CALL_STATE_HELD_BY_RESPONSE_AND_HOLD:
                return "held_by_rnh";
            case BluetoothHeadsetClientCall.CALL_STATE_TERMINATED:
                return "terminated";
        }

        return "unknown";
    }

    private void updateAgFeatures(Bundle b) {
        // only supported AG features are being sent
        mFeatVtag = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT);
        mFeatVoiceRecognition = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_VOICE_RECOGNITION);
        mFeatThreeWayCalling = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_3WAY_CALLING);
        mFeatEnhancedCallControl = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_ECC);
        mFeatReject = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_REJECT_CALL);
        mFeatMerge = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_MERGE);
        mFeatMergeDetach = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_MERGE_AND_DETACH);
        mFeatReleaseAndAccept = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT);
        mFeatAcceptHeldOrWaiting = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL);
        mFeatReleaseHeldOrWaiting = b.containsKey(BluetoothHeadsetClient.EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL);
    }
}