Android Open Source - WifiHotspotBTEnabler Main Activity






From Project

Back to project page WifiHotspotBTEnabler.

License

The source code is released under:

GNU General Public License

If you think the Android project WifiHotspotBTEnabler 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 ca.diogosoares.android.HotspotBTEnabler;
/*from  w ww  .j a v  a 2  s.c om*/
import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import ca.diogosoares.android.R;
import ca.diogosoares.android.utilities.HotspotUtilities;

//Toast.makeText(getApplicationContext(), "TEXT", Toast.LENGTH_LONG).show();

/***
 * 
 * @author Diogo Soares, 02/2014
 *
 */
public class MainActivity extends Activity implements HotspotBTEnablerListener {
  private HotspotBTEnablerManagerFragment mHotspotBTEnablerManagerFragment;
  private HotspotBTEnablerManager mHotspotBTEnablerManager = null;
  private boolean mUpdatingButtonState = false;
  private boolean mFillingDevicesSpinner = false;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_main);
    
    FragmentManager fm = getFragmentManager();
    mHotspotBTEnablerManagerFragment = (HotspotBTEnablerManagerFragment) fm.findFragmentByTag("manager");
    
    if (mHotspotBTEnablerManagerFragment == null) {
      // Create the new manager
      mHotspotBTEnablerManager = new HotspotBTEnablerManager(this.getApplicationContext());
      
      // Commit the manager into the fragment manager
      mHotspotBTEnablerManagerFragment = new HotspotBTEnablerManagerFragment();
      fm.beginTransaction().add(mHotspotBTEnablerManagerFragment, "manager").commit();
      mHotspotBTEnablerManagerFragment.setData(mHotspotBTEnablerManager);

    } else {
      mHotspotBTEnablerManager = mHotspotBTEnablerManagerFragment.getData();
    }
    
    mHotspotBTEnablerManager.registerAsListener(this);
    
    final Switch wifiHotspotSwitch = (Switch) findViewById(R.id.wifiHotspotSwitch);
    final Switch autoModeSwitch = (Switch) findViewById(R.id.autoModeSwitch);
    final Spinner selectDeviceSpinner = (Spinner) findViewById(R.id.selectDeviceSpinner);
    final ScrollView logScrollView = (ScrollView) findViewById(R.id.logScrollView);
    
    fillDevicesSpinner();
    
    updateState();
    updateLog();
    updateButtonState();
    
    //////////////////////////////
    //     Widget Listeners     //
    //////////////////////////////
    
    wifiHotspotSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!mUpdatingButtonState)
          mHotspotBTEnablerManager.setEnableWifiHotspot(isChecked);
      }
    });
    
    autoModeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!mUpdatingButtonState)
          mHotspotBTEnablerManager.setEnableAutoMode(isChecked);
      }
    });
    
    selectDeviceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

      @Override
      public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        if (!mFillingDevicesSpinner) {
          BTDevice btDevice = mHotspotBTEnablerManager.getBTDevices().get(pos);
          mHotspotBTEnablerManager.setSelectedDevice(btDevice);
        }
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        
      }
    });
    
    logScrollView.post(new Runnable() {
      
      @Override
      public void run() {
        logScrollView.fullScroll(View.FOCUS_DOWN);
      }
    });
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    mHotspotBTEnablerManager.removeAsListener(this);
    
        // store the data in the fragment
        mHotspotBTEnablerManagerFragment.setData(mHotspotBTEnablerManager);
  }
  
  private synchronized void updateButtonState() {
    mUpdatingButtonState = true;
    
    final Switch wifiHotspotSwitch = (Switch) findViewById(R.id.wifiHotspotSwitch);
    final Switch autoModeSwitch = (Switch) findViewById(R.id.autoModeSwitch);
    
    wifiHotspotSwitch.setChecked(mHotspotBTEnablerManager.getWifiHotspotState() == HotspotUtilities.WIFI_AP_STATE_ENABLED);
    wifiHotspotSwitch.setEnabled(!autoModeSwitch.isChecked());
    
    autoModeSwitch.setChecked(mHotspotBTEnablerManager.getEnableAutoMode());
  
    mUpdatingButtonState = false;
  }
  
  private synchronized void updateState() {
    TextView stateTextView = (TextView) findViewById(R.id.stateTXT);

    stateTextView.setText(mHotspotBTEnablerManager.getFullState());
    Log.i("MainActivity::UpdateInformations", (String)stateTextView.getText());
  }
  
  private synchronized void updateLog() {
    TextView logTextView = (TextView) findViewById(R.id.logTXT);          
    
    logTextView.setText(mHotspotBTEnablerManager.getLogs());
    Log.i("MainActivity::UpdateInformations", (String)logTextView.getText());
  }
  
  private void fillDevicesSpinner() {
    mFillingDevicesSpinner = true;
    
    final Spinner selectDeviceSpinner = (Spinner) findViewById(R.id.selectDeviceSpinner);
    
    ArrayList<String> BTDeviceNames = new ArrayList<String>();
    for (BTDevice bd:mHotspotBTEnablerManager.getBTDevices())
      BTDeviceNames.add(bd.name);
    
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, BTDeviceNames);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    selectDeviceSpinner.setAdapter(dataAdapter);
    
    selectDeviceSpinner.setSelection(BTDeviceNames.indexOf(mHotspotBTEnablerManager.getSelectedDeviceName()));
    
    mFillingDevicesSpinner = false;
  }

  @Override
  public void onHotspotStateChanged() {
    updateButtonState();
    updateState();
  }
  
  @Override
  public void onBluetoothBondChanged() {
    fillDevicesSpinner();
  }

  @Override
  public void onLogsChanged() {
    updateLog();
    
    // play notification sound
    try {
          Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          Ringtone r = RingtoneManager.getRingtone(this, notification);
          r.play();
      } catch (Exception e) {
        Log.e("MainActivity::onLogsChanged", e.getCause().getMessage());
      }
  }
}




Java Source Code List

ca.diogosoares.android.HotspotBTEnabler.BTDevice.java
ca.diogosoares.android.HotspotBTEnabler.Config.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerListener.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerManagerFragment.java
ca.diogosoares.android.HotspotBTEnabler.HotspotBTEnablerManager.java
ca.diogosoares.android.HotspotBTEnabler.MainActivity.java
ca.diogosoares.android.listeners.BluetoothUtilitiesListener.java
ca.diogosoares.android.listeners.HotspotUtilitiesListener.java
ca.diogosoares.android.listeners.WifiUtilitiesListener.java
ca.diogosoares.android.utilities.BluetoothUtilities.java
ca.diogosoares.android.utilities.HotspotUtilities.java
ca.diogosoares.android.utilities.WifiUtilities.java