DeviceSettingActivity.java 5.28 KB
/*
 * Copyright 2017 wugian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qnbar.smc;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;

import com.qnbar.smc.model.Light;
import com.qnbar.smc.model.Lights;
import com.qnbar.smc.service.TelinkLightService;
import com.xgimi.gimicinema.R;


public class DeviceSettingActivity extends Activity implements OnClickListener {

    private Handler handler = new Handler();

    private SeekBar brightnessBar;
    private SeekBar temperatureBar;
    private Button remove;
    private View delete;
    private SeekBar.OnSeekBarChangeListener barChangeListener = new SeekBar.OnSeekBarChangeListener() {

        private long preTime;
        private int delayTime = 100;

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            this.onValueChange(seekBar, seekBar.getProgress(), true);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            this.preTime = System.currentTimeMillis();
            this.onValueChange(seekBar, seekBar.getProgress(), true);
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {

           /* if (progress % 5 != 0)
                return;

            long currentTime = System.currentTimeMillis();

            if ((currentTime - this.preTime) >= this.delayTime) {
                this.preTime = currentTime;*/
            this.onValueChange(seekBar, progress, false);
            //}
        }

        private void onValueChange(View view, int progress, boolean immediate) {

            int addr = meshAddress;
            byte opcode;
            byte[] params;

            if (view == brightnessBar) {
                opcode = (byte) 0xD2;
                params = new byte[]{(byte) progress};

                TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, immediate);

            } else if (view == temperatureBar) {

                opcode = (byte) 0xE2;
                params = new byte[]{0x05, (byte) progress};

                TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, immediate);
            }
        }
    };
    private int meshAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.activity_device_setting);

        this.meshAddress = this.getIntent().getIntExtra("meshAddress", 0);

        Light light = Lights.getInstance().getByMeshAddress(meshAddress);

//        if (light != null) {
//            TextView txtTitle = (TextView) this
//                    .findViewById(R.id.txt_header_title);
//            txtTitle.setText(TextUtils.isEmpty(light.getLabel2()) ? "" : light.getLabel2());
//        }

        this.brightnessBar = (SeekBar) findViewById(R.id.sb_brightness);
        this.temperatureBar = (SeekBar) findViewById(R.id.sb_temperature);

        this.brightnessBar.setOnSeekBarChangeListener(barChangeListener);
        this.temperatureBar.setOnSeekBarChangeListener(barChangeListener);


        this.remove = (Button) findViewById(R.id.btn_remove);
        this.remove.setOnClickListener(this);

        this.delete = findViewById(R.id.btn_delete);
        this.delete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

    }

    int brightness;
    int colorTemp;

    public void changeTemperature(View view) {
        colorTemp = 0;
        handler.post(cupRunnable);
    }

    Runnable cupRunnable = new Runnable() {
        @Override
        public void run() {
            if (colorTemp == 100) {
                handler.removeCallbacks(this);
                return;
            }
            temperatureBar.setProgress(colorTemp++);
            handler.postDelayed(this, 200);
        }
    };

    private void brightnessUp() {
        brightness = 0;
        handler.post(bupRunnable);
    }

    Runnable bupRunnable = new Runnable() {
        @Override
        public void run() {
            if (brightness == 100) {
                handler.removeCallbacks(this);
                return;
            }
            brightnessBar.setProgress(brightness++);
            handler.postDelayed(this, 200);
        }
    };

    private void brightnessDown() {

    }

    public void changeBrightness(View view) {
        brightnessUp();
    }
}