Home | History | Annotate | Download | only in activity
      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.googlecode.android_scripting.activity;
     18 
     19 import android.app.ListActivity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.util.TypedValue;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.BaseAdapter;
     26 import android.widget.TextView;
     27 
     28 import com.googlecode.android_scripting.Constants;
     29 import com.googlecode.android_scripting.R;
     30 import com.googlecode.android_scripting.facade.bluetooth.BluetoothDiscoveryHelper;
     31 import com.googlecode.android_scripting.facade.bluetooth.BluetoothDiscoveryHelper.BluetoothDiscoveryListener;
     32 
     33 import java.util.ArrayList;
     34 import java.util.List;
     35 
     36 public class BluetoothDeviceList extends ListActivity {
     37 
     38   private static class DeviceInfo {
     39     public final String mmName;
     40     public final String mmAddress;
     41 
     42     public DeviceInfo(String name, String address) {
     43       mmName = name;
     44       mmAddress = address;
     45     }
     46   }
     47 
     48   private final DeviceListAdapter mAdapter = new DeviceListAdapter();
     49   private final BluetoothDiscoveryHelper mBluetoothHelper =
     50       new BluetoothDiscoveryHelper(this, mAdapter);
     51 
     52   @Override
     53   protected void onCreate(Bundle savedInstanceState) {
     54     super.onCreate(savedInstanceState);
     55     CustomizeWindow.requestCustomTitle(this, "Bluetooth Devices", R.layout.bluetooth_device_list);
     56     setListAdapter(mAdapter);
     57   }
     58 
     59   @Override
     60   protected void onStart() {
     61     super.onStart();
     62     CustomizeWindow.toggleProgressBarVisibility(this, true);
     63     mBluetoothHelper.startDiscovery();
     64   }
     65 
     66   @Override
     67   protected void onStop() {
     68     super.onStop();
     69     mBluetoothHelper.cancel();
     70   }
     71 
     72   @Override
     73   protected void onListItemClick(android.widget.ListView l, View v, int position, long id) {
     74     DeviceInfo device = (DeviceInfo) mAdapter.getItem(position);
     75     final Intent result = new Intent();
     76     result.putExtra(Constants.EXTRA_DEVICE_ADDRESS, device.mmAddress);
     77     setResult(RESULT_OK, result);
     78     finish();
     79   };
     80 
     81   private class DeviceListAdapter extends BaseAdapter implements BluetoothDiscoveryListener {
     82     List<DeviceInfo> mmDeviceList;
     83 
     84     public DeviceListAdapter() {
     85       mmDeviceList = new ArrayList<DeviceInfo>();
     86     }
     87 
     88     @Override
     89     public void addDevice(String name, String address) {
     90       mmDeviceList.add(new DeviceInfo(name, address));
     91       notifyDataSetChanged();
     92     }
     93 
     94     @Override
     95     public int getCount() {
     96       return mmDeviceList.size();
     97     }
     98 
     99     @Override
    100     public Object getItem(int position) {
    101       return mmDeviceList.get(position);
    102     }
    103 
    104     @Override
    105     public long getItemId(int position) {
    106       return position;
    107     }
    108 
    109     @Override
    110     public View getView(int position, View convertView, ViewGroup viewGroup) {
    111       final DeviceInfo device = mmDeviceList.get(position);
    112       final TextView view = new TextView(BluetoothDeviceList.this);
    113       view.setPadding(2, 2, 2, 2);
    114       view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    115       view.setText(device.mmName + " (" + device.mmAddress + ")");
    116       return view;
    117     }
    118 
    119     @Override
    120     public void addBondedDevice(String name, String address) {
    121       addDevice(name, address);
    122     }
    123 
    124     @Override
    125     public void scanDone() {
    126       CustomizeWindow.toggleProgressBarVisibility(BluetoothDeviceList.this, false);
    127     }
    128   }
    129 }
    130