Android Open Source - miner Screen Devices






From Project

Back to project page miner.

License

The source code is released under:

Apache License

If you think the Android project miner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kolomiyets.miner.screen;
// w w w.j a va  2  s. co  m
import java.util.ArrayList;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kolomiyets.miner.Miner;
import com.kolomiyets.miner.MinerActivity;
import com.kolomiyets.miner.R;
import com.kolomiyets.miner.bt.EConnectMethod;
import com.kolomiyets.miner.bt.IRequestBtResult;
import com.kolomiyets.miner.bt.notification.BtListener;
import com.kolomiyets.miner.bt.notification.BtNotificationDiscovery;
import com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice;
import com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState;
import com.kolomiyets.miner.bt.notification.BtNotificationPower;
import com.kolomiyets.miner.screen.ScreenManager.ScreenType;

public class ScreenDevices extends ScreenBase implements IRequestBtResult {
  
  String TAG = ScreenDevices.class.getSimpleName();
  
  ArrayList<BluetoothDevice> devices;
  ProgressBar pgs;
  Button rescan;
  ListView list;
  DevicesAdapter adapter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    devices = new ArrayList<BluetoothDevice>();
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.screen_devices, null);
    pgs = (ProgressBar)root.findViewById(R.id.pgs_devices_search);
    rescan = (Button)root.findViewById(R.id.btn_devices_rescan);
    list = (ListView)root.findViewById(R.id.list_devices_servers);
    
    rescan.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View arg0) {
        devices.clear();
        adapter.notifyDataSetChanged();
        list.invalidate();
        BluetoothAdapter.getDefaultAdapter().startDiscovery();
      }
    });
    
    adapter = new DevicesAdapter(devices);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle args = new Bundle();
        args.putString(
            ScreenManager.ARG_CONNECT_METHOD, 
            EConnectMethod.SLAVE.toString());
        args.putParcelable(
            ScreenManager.ARG_DEVICE, 
            devices.get(position));
        
        activity.getScreenManager().goTo(
            ScreenType.CONNECTING, args);
      }
    });
    
    return root;
  }
  
  @Override
  public void onStart() {
    super.onStart();
    invokeDiscovery();
  }
  
  @Override
  public void onStop() {
    ((Miner)getActivity().getApplicationContext())
    .getNotificationManager().unregisterListener(listener);
    
    super.onStop();
  }
  
  @Override
  public void onRequestEnableResult(int resultCode) {
    if(resultCode == Activity.RESULT_OK){
      invokeDiscovery();
    } else {
      activity.getScreenManager().goTo(ScreenType.START);
    }
  }
  
  @Override
  public void onRequestVisibilityResult(int resultCode) {}
  
  private void invokeDiscovery(){
    if(Miner.D) Log.d(TAG, "invoking BlueTooth connection...");
    if(!activity.getConnectionManager().isBlueToothEnabled()){
      powerListener.register(activity);
      activity.getConnectionManager()
      .requestBlueToothEnable(MinerActivity.REQUEST_ENABLE_BT);
    } else {
      ((Miner)getActivity().getApplicationContext())
      .getNotificationManager().registerListener(listener);
      
      BluetoothAdapter.getDefaultAdapter().startDiscovery();
    }
  }
  
  BtListener<BtNotificationPower> powerListener = 
    new BtListener<BtNotificationPower>(BtNotificationPower.class) {
    
    @Override
    public void processNotification(BtNotificationPower notification) {
      if(notification.state == BluetoothAdapter.STATE_ON){
        unregister(activity);
        invokeDiscovery();
      }
    }
  };
  
  BtListener<BtNotificationDiscovery> listener = 
    new BtListener<BtNotificationDiscovery>(BtNotificationDiscovery.class) {
    
    @Override
    public void processNotification(BtNotificationDiscovery notification) {
      if(notification instanceof BtNotificationDiscoveryDevice){
        for(BluetoothDevice d: devices){
          if(((BtNotificationDiscoveryDevice)notification).
              device.getAddress().equals(d.getAddress())) return;
        }
        
        devices.add(((BtNotificationDiscoveryDevice)notification).device);
        adapter.notifyDataSetChanged();
        list.invalidate();
      } else if(notification instanceof BtNotificationDiscoveryState){
        toggleControlsVisibility(((BtNotificationDiscoveryState)notification).state);
      }
    }
  };
  
  private void toggleControlsVisibility(boolean isDescovering){
    if(isDescovering){
      pgs.setVisibility(View.VISIBLE);
      rescan.setVisibility(View.INVISIBLE);
    } else {
      pgs.setVisibility(View.INVISIBLE);
      rescan.setVisibility(View.VISIBLE);
    }
  }
  
  private class DevicesAdapter extends ArrayAdapter<BluetoothDevice> {
    public DevicesAdapter(ArrayList<BluetoothDevice> devices) {
      super(getActivity(), R.layout.devices_list_item, R.id.lbl_title, devices);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView==null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.devices_list_item, null);
      }
      
      BluetoothDevice device = getItem(position);
      TextView lblTitle = (TextView)convertView.findViewById(R.id.lbl_title);
      TextView lblDetails = (TextView)convertView.findViewById(R.id.lbl_detail);
      
      lblTitle.setText(device.getName());
      lblDetails.setText(device.getAddress());
      
      return convertView;
    }
  }
}




Java Source Code List

com.kolomiyets.miner.MinerActivity.java
com.kolomiyets.miner.Miner.java
com.kolomiyets.miner.bt.BtStateMonitor.java
com.kolomiyets.miner.bt.ConnectionBase.java
com.kolomiyets.miner.bt.ConnectionManager.java
com.kolomiyets.miner.bt.ConnectionMaster.java
com.kolomiyets.miner.bt.ConnectionProcessor.java
com.kolomiyets.miner.bt.ConnectionSlave.java
com.kolomiyets.miner.bt.EConnectMethod.java
com.kolomiyets.miner.bt.IBtEnableCallback.java
com.kolomiyets.miner.bt.IRequestBtResult.java
com.kolomiyets.miner.bt.notification.BtListener.java
com.kolomiyets.miner.bt.notification.BtNotificationCmd.java
com.kolomiyets.miner.bt.notification.BtNotificationConnect.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscovery.java
com.kolomiyets.miner.bt.notification.BtNotificationHandshake.java
com.kolomiyets.miner.bt.notification.BtNotificationPower.java
com.kolomiyets.miner.bt.notification.BtNotificationState.java
com.kolomiyets.miner.bt.notification.BtNotification.java
com.kolomiyets.miner.bt.notification.EConnectionSate.java
com.kolomiyets.miner.bt.notification.NotificationManager.java
com.kolomiyets.miner.bt.protocol.CmdBase.java
com.kolomiyets.miner.bt.protocol.CmdFactory.java
com.kolomiyets.miner.bt.protocol.CmdFieldState.java
com.kolomiyets.miner.bt.protocol.CmdGameState.java
com.kolomiyets.miner.bt.protocol.CmdGameTeam.java
com.kolomiyets.miner.bt.protocol.CmdHandshake.java
com.kolomiyets.miner.bt.protocol.CmdPing.java
com.kolomiyets.miner.bt.protocol.CmdResponse.java
com.kolomiyets.miner.bt.protocol.ECmdName.java
com.kolomiyets.miner.bt.protocol.EGameState.java
com.kolomiyets.miner.dialog.DialogProvider.java
com.kolomiyets.miner.dialog.EDialogType.java
com.kolomiyets.miner.dialog.IDialogResult.java
com.kolomiyets.miner.screen.PlayGroundBase.java
com.kolomiyets.miner.screen.PlayGroundMiner.java
com.kolomiyets.miner.screen.PlayGroundSapper.java
com.kolomiyets.miner.screen.ScreenBase.java
com.kolomiyets.miner.screen.ScreenConnectingGame.java
com.kolomiyets.miner.screen.ScreenCreateGame.java
com.kolomiyets.miner.screen.ScreenDevices.java
com.kolomiyets.miner.screen.ScreenManager.java
com.kolomiyets.miner.screen.ScreenSelectPlayer.java
com.kolomiyets.miner.view.GridCell.java