Android Open Source - lamp Lamps Activity






From Project

Back to project page lamp.

License

The source code is released under:

GNU General Public License

If you think the Android project lamp 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

/**    Copyright (C) 2013 Marek Sebera <marek@msebera.cz>
 *//  ww w. ja  v  a 2 s .co  m
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * */
package cz.tomsuch.lampicka.activities;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cz.tomsuch.lampicka.R;
import cz.tomsuch.lampicka.adapters.BluetoothDevicesAdapter;
import cz.tomsuch.lampicka.util.Preferences;

/**
 * Allows user to select BluetoothDevice, to be communicated with
 * 
 * @author Marek Sebera <marek@msebera.cz>
 * */
public final class LampsActivity extends Activity {

  private BluetoothAdapter btAdapter;
  private BluetoothDevicesAdapter listAdapter;
  private ListView listView;
  private Handler handler = new Handler();
  private static final int RQ_BT_ENABLE = 1, RQ_LAMP_CONTROL = 2;
  private boolean runDiscovery = false;

  /**
   * Listener for ListView/Adapter item click
   * */
  private OnItemClickListener onListItemClickListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
      BluetoothDevice bd = listAdapter.getDeviceByPosition(arg2);
      if (bd != null) {
        btAdapter.cancelDiscovery();
        Preferences.getInstance().saveBluetoothDevice(bd);
        startActivityForResult(new Intent(LampsActivity.this,
            LampActivity.class), RQ_LAMP_CONTROL);
      }
    }
  };

  /**
   * BroadcastListener for discovered bluetooth devices
   * */
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent
            .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (device != null)
          listAdapter.addDevice(device);
      }
    }
  };

  /**
   * Runs loop, every 10 seconds is bluetooth device discovery
   * */
  private void discoveryLoop() {
    if (runDiscovery) {
      btAdapter.cancelDiscovery();
      btAdapter.startDiscovery();
      handler.postDelayed(new Runnable() {

        @Override
        public void run() {
          discoveryLoop();
        }
      }, 10000);
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case RQ_BT_ENABLE:
      if (requestCode != Activity.RESULT_OK)
        scanBluetoothDevices();
      else
        finish();
      break;
    case RQ_LAMP_CONTROL:
      if(resultCode == RESULT_OK)
        finish();
      break;
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Preferences.getInstance().getLastBluetoothDevice().isValid()) {
      startActivityForResult(new Intent(this, LampActivity.class),
          RQ_LAMP_CONTROL);
    }
    setTitle(R.string.choose_bluetooth_device);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.main_listview);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    listAdapter = new BluetoothDevicesAdapter();
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(onListItemClickListener);
  }

  @Override
  protected void onResume() {
    super.onResume();
    try {
      runDiscovery = true;
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      registerReceiver(mReceiver, filter);
      scanBluetoothDevices();
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }

  @Override
  protected void onStop() {
    super.onStop();
    try {
      unregisterReceiver(mReceiver);
    } catch (Throwable t) {
      t.printStackTrace();
    }
    runDiscovery = false;
  }

  /**
   * Requires user to enable bluetooth adapter, and if is enabled, runs
   * discoveryLoop()
   * */
  private void scanBluetoothDevices() {
    if (!btAdapter.isEnabled()) {
      Intent enableBtIntent = new Intent(
          BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableBtIntent, RQ_BT_ENABLE);
    } else {
      discoveryLoop();
    }
  }
}




Java Source Code List

cz.tomsuch.lampicka.AppController.java
cz.tomsuch.lampicka.abstracts.CustomSeekBar.java
cz.tomsuch.lampicka.activities.LampActivity.java
cz.tomsuch.lampicka.activities.LampsActivity.java
cz.tomsuch.lampicka.adapters.BluetoothDevicesAdapter.java
cz.tomsuch.lampicka.enums.BluetoothLampBacklightMode.java
cz.tomsuch.lampicka.enums.BluetoothLampColorSetMode.java
cz.tomsuch.lampicka.enums.BluetoothLampCommand.java
cz.tomsuch.lampicka.enums.BluetoothLampEffect.java
cz.tomsuch.lampicka.impl.DefaultBluetoothLamp.java
cz.tomsuch.lampicka.interfaces.BluetoothInputLineListener.java
cz.tomsuch.lampicka.interfaces.BluetoothLampCommandListener.java
cz.tomsuch.lampicka.interfaces.BluetoothLamp.java
cz.tomsuch.lampicka.interfaces.OnColorChangedListener.java
cz.tomsuch.lampicka.interfaces.PalleteDialogColorListener.java
cz.tomsuch.lampicka.pallete.ColorChooserDialog.java
cz.tomsuch.lampicka.pallete.PalleteDialog.java
cz.tomsuch.lampicka.util.BluetoothDeviceComparator.java
cz.tomsuch.lampicka.util.BluetoothDeviceWrapper.java
cz.tomsuch.lampicka.util.BluetoothSocketListener.java
cz.tomsuch.lampicka.util.ColorPickerView.java
cz.tomsuch.lampicka.util.FixedBluetoothSocket.java
cz.tomsuch.lampicka.util.HorizontalSeekBar.java
cz.tomsuch.lampicka.util.Preferences.java
cz.tomsuch.lampicka.util.VerticalSeekBar.java