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