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.cts.verifier.companion;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.companion.AssociationRequest;
21 import android.companion.BluetoothDeviceFilter;
22 import android.companion.CompanionDeviceManager;
23 import android.content.Intent;
24 import android.content.IntentSender;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.widget.Toast;
28 
29 import com.android.cts.verifier.PassFailButtons;
30 import com.android.cts.verifier.R;
31 
32 import java.util.List;
33 
34 
35 /**
36  * Test that checks that the {@link CompanionDeviceManager} API is functional
37  */
38 public class CompanionDeviceTestActivity extends PassFailButtons.Activity {
39 
40     private static final String LOG_TAG = "CompanionDeviceTestActi";
41     private static final int REQUEST_CODE_CHOOSER = 0;
42 
43     private CompanionDeviceManager mCompanionDeviceManager;
44     private List<String> mInitialAssociations;
45 
46     @Override
onCreate(Bundle savedInstanceState)47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49 
50         setContentView(R.layout.companion_test_main);
51         setPassFailButtonClickListeners();
52 
53         mCompanionDeviceManager = getSystemService(CompanionDeviceManager.class);
54 
55         getPassButton().setEnabled(false);
56 
57         findViewById(R.id.button).setOnClickListener(v -> test());
58     }
59 
test()60     private void test() {
61         mInitialAssociations = mCompanionDeviceManager.getAssociations();
62 
63         AssociationRequest request = new AssociationRequest.Builder()
64                 .addDeviceFilter(new BluetoothDeviceFilter.Builder().build())
65                 .build();
66         CompanionDeviceManager.Callback callback = new CompanionDeviceManager.Callback() {
67             @Override
68             public void onDeviceFound(IntentSender chooserLauncher) {
69                 try {
70                     startIntentSenderForResult(chooserLauncher,
71                             REQUEST_CODE_CHOOSER, null, 0, 0, 0);
72                 } catch (IntentSender.SendIntentException e) {
73                     fail(e);
74                 }
75             }
76 
77             @Override
78             public void onFailure(CharSequence error) {
79                 fail(error);
80             }
81         };
82         mCompanionDeviceManager.associate(request, callback, null);
83     }
84 
fail(Throwable reason)85     private void fail(Throwable reason) {
86         Log.e(LOG_TAG, "Test failed", reason);
87         fail(reason.getMessage());
88     }
89 
fail(CharSequence reason)90     private void fail(CharSequence reason) {
91         Toast.makeText(this, reason, Toast.LENGTH_LONG).show();
92         Log.e(LOG_TAG, reason.toString());
93         setTestResultAndFinish(false);
94     }
95 
96     @Override
onActivityResult(int requestCode, int resultCode, Intent data)97     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
98         if (requestCode == REQUEST_CODE_CHOOSER) {
99 
100             if (resultCode != RESULT_OK) fail("Activity result code " + resultCode);
101 
102             List<String> newAssociations = mCompanionDeviceManager.getAssociations();
103             if (!newAssociations.containsAll(mInitialAssociations)) {
104                 fail("New associations " + newAssociations
105                         + " lack some of the original items from "
106                         + mInitialAssociations);
107             }
108             if (newAssociations.size() != mInitialAssociations.size() + 1) {
109                 fail("New associations " + newAssociations + " are not 1 item larger from initial "
110                         + mInitialAssociations);
111             }
112 
113             BluetoothDevice associatedDevice
114                     = data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
115             String deviceAddress = associatedDevice.getAddress();
116             if (!newAssociations.contains(deviceAddress)) {
117                 fail("Selected device is not present among new associations " + newAssociations);
118             }
119 
120             mCompanionDeviceManager.disassociate(associatedDevice.getAddress());
121             List<String> associations = mCompanionDeviceManager.getAssociations();
122             if (associations.contains(deviceAddress)) {
123                 fail("Disassociating device " + deviceAddress
124                         + " did not remove it from associations list"
125                         + associations);
126             }
127 
128             if (!isFinishing()) {
129                 getPassButton().setEnabled(true);
130             }
131 
132         } else super.onActivityResult(requestCode, resultCode, data);
133     }
134 }
135