/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.ase.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.ase.AseAnalytics;
import com.google.ase.Constants;
import com.google.ase.R;
import com.google.ase.bluetooth.BluetoothHelper.DeviceListener;

public class BluetoothDeviceList extends ListActivity {

  private static class DeviceInfo {
    public final String mmName;
    public final String mmAddress;

    public DeviceInfo(String name, String address) {
      mmName = name;
      mmAddress = address;
    }
  }

  private final DeviceListAdapter mDeviceListAdapter = new DeviceListAdapter();

  private class DeviceListAdapter extends BaseAdapter implements DeviceListener {
    List<DeviceInfo> mmDeviceList;

    public DeviceListAdapter() {
      mmDeviceList = new ArrayList<DeviceInfo>();
    }

    public void addDevice(String name, String address) {
      mmDeviceList.add(new DeviceInfo(name, address));
      notifyDataSetChanged();
    }

    @Override
    public int getCount() {
      return mmDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
      return mmDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
      return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
      final DeviceInfo device = mmDeviceList.get(position);

      final TextView textView = new TextView(BluetoothDeviceList.this);
      textView.setText(device.mmName + " (" + device.mmAddress + ")");

      return textView;
    }

    @Override
    public void addBondedDevice(String name, String address) {
      addDevice(name, address);
    }

    @Override
    public void scanDone() {
    }
  }

  private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
      DeviceInfo device = (DeviceInfo) mDeviceListAdapter.getItem(position);
      final Intent result = new Intent();
      result.putExtra(Constants.EXTRA_DEVICE_ADDRESS, device.mmAddress);
      setResult(RESULT_OK, result);
      finish();
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CustomizeWindow
        .requestCustomTitle(this, "Bluetooth Devices", R.layout.bluetooth_device_manager);
    setResult(RESULT_CANCELED);
    setListAdapter(mDeviceListAdapter);
    getListView().setOnItemClickListener(mOnItemClickListener);
    AseAnalytics.trackActivity(this);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
  }
}

@@ -765,16 +765,48 @@
 Bundle;%0A
+import android.util.TypedValue;%0A
 import a
@@ -1525,16 +1525,862 @@
 ter();%0A%0A
+  @Override%0A  protected void onCreate(Bundle savedInstanceState) %7B%0A    super.onCreate(savedInstanceState);%0A    CustomizeWindow%0A        .requestCustomTitle(this, %22Bluetooth Devices%22, R.layout.bluetooth_device_manager);%0A    setResult(RESULT_CANCELED);%0A    setListAdapter(mDeviceListAdapter);%0A    getListView().setOnItemClickListener(mOnItemClickListener);%0A    AseAnalytics.trackActivity(this);%0A  %7D%0A%0A  private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() %7B%0A    @Override%0A    public void onItemClick(AdapterView%3C?%3E adapterView, View view, int position, long id) %7B%0A      DeviceInfo device = (DeviceInfo) mDeviceListAdapter.getItem(position);%0A      final Intent result = new Intent();%0A      result.putExtra(Constants.EXTRA_DEVICE_ADDRESS, device.mmAddress);%0A      setResult(RESULT_OK, result);%0A      finish();%0A    %7D%0A  %7D;%0A%0A
   privat
@@ -3054,25 +3054,32 @@
 ition, View 
-v
+convertV
 iew, ViewGro
@@ -3154,17 +3154,16 @@
 ition);%0A
-%0A
       fi
@@ -3175,21 +3175,17 @@
 extView 
-textV
+v
 iew = ne
@@ -3228,21 +3228,109 @@
 ;%0A      
-textV
+view.setPadding(2, 2, 2, 2);%0A      view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);%0A      v
 iew.setT
@@ -3381,17 +3381,16 @@
 + %22)%22);%0A
-%0A
       re
@@ -3398,13 +3398,9 @@
 urn 
-textV
+v
 iew;
@@ -3579,924 +3579,7 @@
 %0A  %7D
-%0A%0A  private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() %7B%0A    @Override%0A    public void onItemClick(AdapterView%3C?%3E adapterView, View view, int position, long id) %7B%0A      DeviceInfo device = (DeviceInfo) mDeviceListAdapter.getItem(position);%0A      final Intent result = new Intent();%0A      result.putExtra(Constants.EXTRA_DEVICE_ADDRESS, device.mmAddress);%0A      setResult(RESULT_OK, result);%0A      finish();%0A    %7D%0A  %7D;%0A%0A  @Override%0A  protected void onCreate(Bundle savedInstanceState) %7B%0A    super.onCreate(savedInstanceState);%0A    CustomizeWindow%0A        .requestCustomTitle(this, %22Bluetooth Devices%22, R.layout.bluetooth_device_manager);%0A    setResult(RESULT_CANCELED);%0A    setListAdapter(mDeviceListAdapter);%0A    getListView().setOnItemClickListener(mOnItemClickListener);%0A    AseAnalytics.trackActivity(this);%0A  %7D%0A%0A  @Override%0A  protected void onDestroy() %7B%0A    super.onDestroy();%0A  %7D
 %0A%7D%0A

Clean up Bluetooth device list a bit. Make the text bigger and easier to tap.