1 /* 2 * Copyright (C) 2013 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.bluetooth; 18 19 import com.android.cts.verifier.PassFailButtons; 20 import com.android.cts.verifier.R; 21 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.Bundle; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.view.ViewGroup; 31 import android.widget.BaseAdapter; 32 import android.widget.Button; 33 import android.widget.ListView; 34 import android.widget.TextView; 35 36 import java.util.ArrayList; 37 import java.util.Map; 38 import java.util.HashMap; 39 40 public class BleScannerHardwareScanFilterActivity extends PassFailButtons.Activity { 41 42 private static final String TAG = "BleScannerHardwareScanFilter"; 43 44 private ListView mScanResultListView; 45 private MapAdapter mAdapter; 46 47 @Override onCreate(Bundle savedInstanceState)48 public void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.ble_scanner_hardware_scan_filter); 51 setPassFailButtonClickListeners(); 52 setInfoResources(R.string.ble_scanner_scan_filter_name, 53 R.string.ble_scanner_scan_filter_info, -1); 54 55 mScanResultListView = (ListView)findViewById(R.id.ble_scan_result_list); 56 mAdapter = new MapAdapter(); 57 mScanResultListView.setAdapter(mAdapter); 58 59 ((Button) findViewById(R.id.ble_scan_with_filter)) 60 .setOnClickListener(new OnClickListener() { 61 @Override 62 public void onClick(View v) { 63 Intent intent = new Intent(BleScannerHardwareScanFilterActivity.this, 64 BleScannerService.class); 65 intent.putExtra(BleScannerService.EXTRA_COMMAND, 66 BleScannerService.COMMAND_SCAN_WITH_FILTER); 67 startService(intent); 68 } 69 }); 70 71 ((Button) findViewById(R.id.ble_scan_without_filter)) 72 .setOnClickListener(new OnClickListener() { 73 @Override 74 public void onClick(View v) { 75 Intent intent = new Intent(BleScannerHardwareScanFilterActivity.this, 76 BleScannerService.class); 77 intent.putExtra(BleScannerService.EXTRA_COMMAND, 78 BleScannerService.COMMAND_SCAN_WITHOUT_FILTER); 79 startService(intent); 80 } 81 }); 82 } 83 84 @Override onResume()85 public void onResume() { 86 super.onResume(); 87 88 IntentFilter filter = new IntentFilter(); 89 filter.addAction(BleScannerService.BLE_SCAN_RESULT); 90 registerReceiver(onBroadcast, filter); 91 } 92 93 94 private BroadcastReceiver onBroadcast = new BroadcastReceiver() { 95 @Override 96 public void onReceive(Context context, Intent intent) { 97 switch (intent.getAction()) { 98 case BleScannerService.BLE_SCAN_RESULT: 99 String uuid = intent.getStringExtra(BleScannerService.EXTRA_UUID); 100 String data = intent.getStringExtra(BleScannerService.EXTRA_DATA); 101 if (data != null) { 102 mAdapter.addItem(uuid + " : " + data); 103 } 104 break; 105 } 106 } 107 }; 108 109 @Override onPause()110 public void onPause() { 111 super.onPause(); 112 unregisterReceiver(onBroadcast); 113 } 114 stop()115 private void stop() { 116 stopService(new Intent(this, BleScannerService.class)); 117 } 118 119 @Override onDestroy()120 public void onDestroy() { 121 super.onDestroy(); 122 stop(); 123 } 124 125 public class MapAdapter extends BaseAdapter { 126 private Map<String, Integer> mData; 127 private ArrayList<String> mKeys; MapAdapter()128 public MapAdapter() { 129 mData = new HashMap<>(); 130 mKeys = new ArrayList<>(); 131 } 132 133 @Override getCount()134 public int getCount() { 135 return mData.size(); 136 } 137 138 @Override getItem(int position)139 public Object getItem(int position) { 140 return mData.get(mKeys.get(position)); 141 } 142 143 @Override getItemId(int arg0)144 public long getItemId(int arg0) { 145 return arg0; 146 } 147 addItem(String key)148 public void addItem(String key) { 149 if (!mData.containsKey(key)) { 150 mKeys.add(key); 151 mData.put(key, new Integer(1)); 152 } else { 153 mData.put(key, mData.get(key) + 1); 154 } 155 this.notifyDataSetChanged(); 156 } 157 158 @Override getView(int pos, View view, ViewGroup parent)159 public View getView(int pos, View view, ViewGroup parent) { 160 if (view == null) { 161 view = LayoutInflater.from(parent.getContext()) 162 .inflate(android.R.layout.simple_list_item_2, parent, false); 163 } 164 String key = mKeys.get(pos); 165 String value = getItem(pos).toString(); 166 TextView text1 = (TextView) view.findViewById(android.R.id.text1); 167 TextView text2 = (TextView) view.findViewById(android.R.id.text2); 168 text1.setText(key); 169 text2.setText(value); 170 return view; 171 } 172 } 173 } 174