Android Open Source - lifx-wear-test Lights Fragment






From Project

Back to project page lifx-wear-test.

License

The source code is released under:

Apache License

If you think the Android project lifx-wear-test 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 de.dp.lifxtest.app;
/*from  w w w.  j a v a2 s.  c om*/
/**
 * Created by dennispriess on 04/06/14.
 */

import com.gcx.lifxtest.app.R;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import lifx.java.android.client.LFXClient;
import lifx.java.android.entities.LFXTypes;
import lifx.java.android.network_context.LFXNetworkContext;

/**
 * A placeholder fragment containing a simple view.
 */
public class LightsFragment extends Fragment implements View.OnClickListener {

    private Button mLightButton, mSetCountdownButton;

    private EditText mCountdownEditText;

    private TextView mTimeLeftText;

    private boolean isTimerFinished = true;

    private LFXNetworkContext mLocalNetworkContext;

    private CountDownTimer mCountDownTimer;

    public LightsFragment() {
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            // turn all lights off
            case R.id.button:
                createNotification();
                if (mLocalNetworkContext.getAllLightsCollection().getFuzzyPowerState()
                        == LFXTypes.LFXFuzzyPowerState.OFF) {
                    mLocalNetworkContext.getAllLightsCollection()
                            .setPowerState(LFXTypes.LFXPowerState.ON);

                } else {
                    mLocalNetworkContext.getAllLightsCollection()
                            .setPowerState(LFXTypes.LFXPowerState.OFF);
                }
                break;

            case R.id.countdown_button:

                int minutes = Integer.parseInt(mCountdownEditText.getEditableText().toString());

                int minutesInMillis = minutes * 1000 * 60;

                if (isTimerFinished) {
                    startCountdown(minutesInMillis);
                } else {
                    mCountDownTimer.cancel();
                    startCountdown(minutesInMillis);
                }

                break;
        }
    }

    private void startCountdown(final int millisMinutes) {
        mCountDownTimer = new CountDownTimer(millisMinutes, 1000) {

            public void onTick(long millisUntilFinished) {
                isTimerFinished = false;
                mTimeLeftText
                        .setText("seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                isTimerFinished = true;
                mTimeLeftText.setText("done!");
                mLocalNetworkContext.getAllLightsCollection()
                        .setPowerState(LFXTypes.LFXPowerState.OFF);
            }
        }.start();
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLocalNetworkContext = LFXClient.getSharedInstance(getActivity())
                .getLocalNetworkContext();
        mLocalNetworkContext.connect();

        if (getArguments() != null) {
            getArguments().getBoolean("lifx", false);
            Toast.makeText(getActivity(), "bundle contains boolean", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_master, container, false);
        mLightButton = (Button) rootView.findViewById(R.id.button);
        mCountdownEditText = (EditText) rootView.findViewById(R.id.countdown_setter);
        mSetCountdownButton = (Button) rootView.findViewById(R.id.countdown_button);
        mTimeLeftText = (TextView) rootView.findViewById(R.id.countdown_left);

        mSetCountdownButton.setOnClickListener(this);
        mLightButton.setOnClickListener(this);
        return rootView;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void createNotification() {

        // prepare intent which is triggered if the
        // notification button is selected

        Intent intent = new Intent(getActivity(), MasterActivity.class);
        intent.putExtra("lifx", true);
        PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

        NotificationManager notificationManager =
                (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(getActivity())
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Control your lifx")
                        .setContentText("turn light on / off")
                        .setAutoCancel(false)
                        .addAction(R.drawable.ic_launcher,
                                "On / Off", pIntent);

        notificationManager.notify(0, notificationBuilder.build());


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mLocalNetworkContext.disconnect();
    }
}




Java Source Code List

de.dp.lifxtest.app.LightsFragment.java
de.dp.lifxtest.app.MasterActivity.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