Android Open Source - lifx-sdk-android L F X S D K Light Edit Label Activity






From Project

Back to project page lifx-sdk-android.

License

The source code is released under:

MIT License

If you think the Android project lifx-sdk-android 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

//
//  LFXSDKLightEditLabelActivity.java
//  LIFX//  w  ww .  j  a  va 2  s  .c om
//
//  Created by Jarrod Boyes on 24/03/14.
//  Copyright (c) 2014 LIFX Labs. All rights reserved.
//

package com.example.lifx_sdk_samples;

import java.util.ArrayList;

import lifx.java.android.client.LFXClient;
import lifx.java.android.entities.LFXHSBKColor;
import lifx.java.android.entities.LFXTypes.LFXFuzzyPowerState;
import lifx.java.android.entities.LFXTypes.LFXPowerState;
import lifx.java.android.light.LFXLight;
import lifx.java.android.light.LFXLight.LFXLightListener;
import lifx.java.android.light.LFXLightCollection;
import lifx.java.android.light.LFXLightCollection.LFXLightCollectionListener;
import lifx.java.android.light.LFXTaggedLightCollection;
import lifx.java.android.network_context.LFXNetworkContext;
import lifx.java.android.network_context.LFXNetworkContext.LFXNetworkContextListener;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class LFXSDKLightEditLabelActivity extends Activity implements LFXLightListener, LFXLightCollectionListener, LFXNetworkContextListener
{
  private LFXNetworkContext networkContext;
  private LFXSDKLightListAdapter lightListAdapter;
  private LFXSDKTaggedLightCollectionListAdapter groupListAdapter;
  
  @Override
  protected void onCreate( Bundle savedInstanceState)
  {
    super.onCreate( savedInstanceState);
    setContentView( R.layout.lifx_list_sample_layout);
    
    networkContext = LFXClient.getSharedInstance( getApplicationContext()).getLocalNetworkContext();
    networkContext.addNetworkContextListener( this);
    networkContext.getAllLightsCollection().addLightCollectionListener( this);
    
    ListView lightListView = (ListView) findViewById( R.id.light_list_view);
    lightListAdapter = new LFXSDKLightListAdapter( this);
    lightListView.setAdapter( lightListAdapter);
    
    ListView groupListView = (ListView) findViewById( R.id.group_list_view);
    groupListAdapter = new LFXSDKTaggedLightCollectionListAdapter( this);
    groupListView.setAdapter( groupListAdapter);
    
    updateStateFromLIFX();
    
    lightListView.setOnItemClickListener( new OnItemClickListener()
    {
      @Override
      public void onItemClick( AdapterView<?> arg0, View arg1, int position, long arg3)
      {
        LFXLight light = (LFXLight) lightListAdapter.getItem( position);
        showLabelDialogForLight( light);
      }
    });
    
    groupListView.setOnItemClickListener( new OnItemClickListener()
    {
      @Override
      public void onItemClick( AdapterView<?> arg0, View arg1, int position, long arg3)
      {
        LFXTaggedLightCollection lightCollection = (LFXTaggedLightCollection) groupListAdapter.getItem( position);    
        showLabelDialogForLightCollection( lightCollection);
      }
    });
  }

  public void showLabelDialogForLight( LFXLight light)
  {
    final LFXLight finalLight = light;
    
    AlertDialog.Builder alert = new AlertDialog.Builder( this);                 
    alert.setTitle("Rename Light Too:");               

     // Set an EditText view to get user input   
     final EditText input = new EditText( this); 
     alert.setView(input);

        alert.setPositiveButton( "Ok", new DialogInterface.OnClickListener() 
        {  
          public void onClick( DialogInterface dialog, int whichButton) 
          {  
              String value = input.getText().toString();
              finalLight.setLabel( value);
              return;                  
          }  
        });  

        alert.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                return;   
            }
        });
        
       alert.show();
  }
  
  public void showLabelDialogForLightCollection( LFXTaggedLightCollection lightCollection)
  {
    final LFXTaggedLightCollection finalLightCollection = lightCollection;
    
    AlertDialog.Builder alert = new AlertDialog.Builder( this);                 
    alert.setTitle("Rename Group Too:");               

     // Set an EditText view to get user input   
     final EditText input = new EditText( this); 
     alert.setView(input);
  
        alert.setPositiveButton( "Ok", new DialogInterface.OnClickListener() 
        {  
          public void onClick( DialogInterface dialog, int whichButton) 
          {  
              String value = input.getText().toString();
              finalLightCollection.setLabel( value);
              return;                  
          }  
        });  
  
        alert.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which) 
            {
                return;   
            }
        });
        
       alert.show();
  }
  
  @Override
  public boolean onCreateOptionsMenu( Menu menu)
  {
    getMenuInflater().inflate( R.menu.sample, menu);
    return true;
  }
  
  @Override
  protected void onPause()
  {
    networkContext.removeNetworkContextListener( this);
    super.onPause();
  }
  
  private void updateStateFromLIFX()
  {  
    ArrayList<LFXLight> allLights = networkContext.getAllLightsCollection().getLights();
    lightListAdapter.updateWithLights( allLights);
    
    ArrayList<LFXTaggedLightCollection> lightCollections = networkContext.getTaggedLightCollections();
    groupListAdapter.updateWithLightCollections( lightCollections);
  }

  @Override
  public void networkContextDidConnect( LFXNetworkContext networkContext)
  {
    Toast toast = Toast.makeText( this, "CONNECTED", Toast.LENGTH_SHORT);
    toast.show();
  }

  @Override
  public void networkContextDidDisconnect( LFXNetworkContext networkContext)
  {
    Toast toast = Toast.makeText( this, "DISCONNECTED", Toast.LENGTH_SHORT);
    toast.show();
  }

  @Override
  public void networkContextDidAddTaggedLightCollection( LFXNetworkContext networkContext, LFXTaggedLightCollection collection) 
  {
    updateStateFromLIFX();
  }

  @Override
  public void networkContextDidRemoveTaggedLightCollection( LFXNetworkContext networkContext, LFXTaggedLightCollection collection) 
  {
    updateStateFromLIFX();
  }

  @Override
  public void lightCollectionDidAddLight( LFXLightCollection lightCollection, LFXLight light)
  {
    updateStateFromLIFX();
    light.addLightListener( this);
  }

  @Override
  public void lightCollectionDidRemoveLight( LFXLightCollection lightCollection, LFXLight light)
  {
    updateStateFromLIFX();
    light.removeLightListener( this);
  }

  @Override
  public void lightCollectionDidChangeLabel( LFXLightCollection lightCollection, String label) {}

  @Override
  public void lightCollectionDidChangeColor( LFXLightCollection lightCollection, LFXHSBKColor color) {}

  @Override
  public void lightCollectionDidChangeFuzzyPowerState( LFXLightCollection lightCollection, LFXFuzzyPowerState fuzzyPowerState) {}

  @Override
  public void lightDidChangeLabel( LFXLight light, String label)
  {
    updateStateFromLIFX();
  }

  @Override
  public void lightDidChangeColor( LFXLight light, LFXHSBKColor color)
  {
    updateStateFromLIFX();
  }

  @Override
  public void lightDidChangePowerState( LFXLight light, LFXPowerState powerState)
  {
    updateStateFromLIFX();
  }
}




Java Source Code List

com.example.lifx_sdk_samples.LFXSDKLightEditLabelActivity.java
com.example.lifx_sdk_samples.LFXSDKLightListAdapter.java
com.example.lifx_sdk_samples.LFXSDKLightPowerActivity.java
com.example.lifx_sdk_samples.LFXSDKLightRandomColorActivity.java
com.example.lifx_sdk_samples.LFXSDKSamplesActivity.java
com.example.lifx_sdk_samples.LFXSDKTaggedLightCollectionListAdapter.java
lifx.java.android.client.LFXClient.java
lifx.java.android.constant.LFXSDKConstants.java
lifx.java.android.entities.LFXHSBKColor.java
lifx.java.android.entities.LFXLightTarget.java
lifx.java.android.entities.LFXTypes.java
lifx.java.android.entities.internal.LFXBinaryPath.java
lifx.java.android.entities.internal.LFXBinaryTargetID.java
lifx.java.android.entities.internal.LFXBinaryTypes.java
lifx.java.android.entities.internal.LFXDeviceMapping.java
lifx.java.android.entities.internal.LFXGatewayDescriptor.java
lifx.java.android.entities.internal.LFXMessageObservationDescriptor.java
lifx.java.android.entities.internal.LFXMessage.java
lifx.java.android.entities.internal.LFXSiteID.java
lifx.java.android.entities.internal.LFXTagMapping.java
lifx.java.android.entities.internal.LFXTarget.java
lifx.java.android.entities.internal.structle.LxProtocolDevice.java
lifx.java.android.entities.internal.structle.LxProtocolLight.java
lifx.java.android.entities.internal.structle.LxProtocolSensor.java
lifx.java.android.entities.internal.structle.LxProtocolWan.java
lifx.java.android.entities.internal.structle.LxProtocolWifi.java
lifx.java.android.entities.internal.structle.LxProtocol.java
lifx.java.android.entities.internal.structle.Lx.java
lifx.java.android.entities.internal.structle.StructleTypes.java
lifx.java.android.internal.LFXWiFiObserver.java
lifx.java.android.light.LFXLightCollection.java
lifx.java.android.light.LFXLight.java
lifx.java.android.light.LFXTaggedLightCollection.java
lifx.java.android.light.internal.LFXAllLightsCollection.java
lifx.java.android.network_context.LFXNetworkContext.java
lifx.java.android.network_context.internal.routing_table.LFXRoutingTable.java
lifx.java.android.network_context.internal.transport_manager.LFXTransportManager.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketGeneric.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketTCP.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketUDP.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXTCPGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXUDPGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.lan.LFXLANTransportManager.java
lifx.java.android.network_context.internal.transport_manager.lan.gateway_discovery.LFXGatewayDiscoveryController.java
lifx.java.android.network_context.internal.transport_manager.lan.gateway_discovery.LFXGatewayDiscoveryTableEntry.java
lifx.java.android.util.LFXByteUtils.java
lifx.java.android.util.LFXLog.java
lifx.java.android.util.LFXNetworkUtils.java
lifx.java.android.util.LFXTimerUtils.java