CinemaControlService.java 12.5 KB
/*
 * Copyright 2016 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.xgimi.gimicinema.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;
import com.gimi.common.cinema.model.MessageEvent;
import com.gimi.common.cinema.utils.LogUtils;
import com.gimi.common.cinema.utils.Utils;
import com.qnbar.smc.model.Light;
import com.qnbar.smc.model.Lights;
import com.qnbar.smc.service.SmartControlService;
import com.qnbar.smc.service.TelinkLightService;
import com.telink.bluetooth.light.ConnectionStatus;
import com.telink.bluetooth.light.DeviceInfo;
import com.xgimi.gimicinema.ICinemaControl;
import com.xgimi.gimicinema.application.FangTangApplication;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class CinemaControlService extends Service {
    private static final int LIGHT_MIN_VALUE = 5;
    private ArrayList<String> movies = new ArrayList<>();
    private long duration = 0;
    private long currentPosition = 0;
    private int currentState = 0;
    private String currentPath = null;
    //    private static final int SAVE_BEAT = 500;
    private static final int SAVE_BEAT = 5;//default 50
    private int saveBeat = SAVE_BEAT;

    private final ICinemaControl.Stub cinemaControl = new ICinemaControl.Stub() {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void setMovieDuration(long duration) throws RemoteException {
//            Log.d("aidl", "setMovieDuration: " + duration);
            CinemaControlService.this.duration = duration;
        }

        @Override
        public long getMovieDuration() throws RemoteException {
            return duration;
        }

        @Override
        public void setCurrentMoviePosition(long duration) throws RemoteException {
//            Log.d("aidl", "setCurrentMoviePosition: " + duration);
            currentPosition = duration;
            if (saveBeat-- == 0) {
                //TODO 保存当前进度,需要时从保存位置开始播放,取消播放器播放记录功能
                // 播放一段时间确保状态为关闭
//                saveBeat = SAVE_BEAT;
                try {
                    if (!fadeOuting && Lights.getInstance().getByMeshAddress(
                            ((FangTangApplication) getApplication()).getConnectDevice().meshAddress).status
                            == ConnectionStatus.ON) {
                        fadeOut();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public long getCurrentMoviePosition() throws RemoteException {
            return currentPosition;
        }

        @Override
        public int getCurrentStatus() throws RemoteException {
            return currentState;
        }

        @Override
        public void setCurrentStatus(int state) throws RemoteException {
            Log.d("aidl", "setCurrentStatus: " + state);
            if (TelinkLightService.Instance() == null) {
                return;
            }
            if (state != currentState) {
                currentState = state;
                Log.d("aidl", "state change," + currentPath + "," + duration + "," + currentPosition + ",state:" + state);
                Log.d("aidl", "state change cinemaSMC not null");
//                if (true) {
//                    return;
//                }
//                MessageEvent msgEvent = new MessageEvent();
                switch (state) {
                    case 0://空闲
                        fadeIn();
                        showQrCodeRightNow();
//                        msgEvent.setEventId(0x19910);
//                        msgEvent.setMessage("open_switch");
//                        EventBus.getDefault().post(msgEvent);
                        break;
                    case 1://播放
                        fadeOut();
//                        msgEvent.setEventId(0x19910);
//                        msgEvent.setMessage("close_switch");
//                        EventBus.getDefault().post(msgEvent);
                        break;
                    case 2://暂停
//                        fadeIn();
//                        msgEvent.setEventId(0x19910);
//                        msgEvent.setMessage("open_switch");
//                        EventBus.getDefault().post(msgEvent);
                        break;
                }
            }
        }

        @Override
        public void addPlayPath(String path) throws RemoteException {
            movies.add(path);//strict contract
//            MessageEvent msgEvent = new MessageEvent();
//            msgEvent.setEventId(0x19910);
//            msgEvent.setMessage("open_switch");
//            EventBus.getDefault().post(msgEvent);
        }

        @Override
        public void setCurrentPlayPath(String path) throws RemoteException {
//            Log.d("aidl", "setCurrentPlayPath: " + path);
            CinemaControlService.this.currentPath = path;
            FangTangApplication application = (FangTangApplication) getApplication();
            application.setCurrentPlayUrl(path);
        }

        @Override
        public String getCurrentPath() throws RemoteException {
            return CinemaControlService.this.currentPath;
        }

        @Override
        public List<String> getPlayList() throws RemoteException {
            return movies;
        }

        @Override
        public void setPlayList(List<String> movies) throws RemoteException {
            CinemaControlService.this.movies.clear();
            if (movies == null || movies.size() == 0) {
                return;
            }
            CinemaControlService.this.movies.addAll(movies);
        }

        public void openLight() throws RemoteException {
            fadeIn();
        }

        public void closeLight() throws RemoteException {
            fadeOut();
        }
    };

    public CinemaControlService() {
    }

    Runnable doClose = new Runnable() {
        @Override
        public void run() {
            close();
        }
    };
    private int value;
    private Handler handler = new Handler();
    private Runnable closeRunnable = new Runnable() {
        @Override
        public void run() {
            setLightValue(value);
            if (value-- == maxValue) {
                handler.postDelayed(this, 4 * 1000);
            } else {
                if (value == 0) {
                    //how to remove
                    handler.postDelayed(doClose, 3 * 1000);
                } else {
                    handler.postDelayed(this, 80);
                }
            }
        }
    };

    private Runnable openRunnable = new Runnable() {
        @Override
        public void run() {
            if (value++ == LIGHT_MIN_VALUE) {
                open();
                handler.postDelayed(this, 2 * 1000);
            } else {
                setLightValue(value);
                if (value != maxValue) {
                    handler.postDelayed(this, 200);
                }
            }
        }
    };

    private FangTangApplication mApplication;
    private Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        mApplication = (FangTangApplication) getApplication();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return cinemaControl;
    }

    private void open() {
        byte opcode = (byte) 0xD0;
        int address = 0xFFFF;
        byte[] params = new byte[]{0x01, 0x00, 0x00};
        if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
            Log.d("aidl", "open all success");
        } else {
            Log.d("aidl", "open all failure:" + (TelinkLightService.Instance() != null));
        }
    }

    private void close() {
        byte opcode = (byte) 0xD0;
        int address = 0xFFFF;
        byte[] params = new byte[]{0x00, 0x00, 0x00};
        if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
            Log.d("aidl", "close all success");
        } else {
            Log.d("aidl", "close all failure:" + (TelinkLightService.Instance() != null));
        }
        fadeOuting = false;
    }

    private boolean fadeOuting = false;
    private int maxValue = 50;

    public void fadeIn() {
        DeviceInfo connectDevice = mApplication.getConnectDevice();
        if (connectDevice == null) {
            Log.d("aidl", "connect device is null");
            return;
        }
        Light byMeshAddress = Lights.getInstance().getByMeshAddress(connectDevice.meshAddress);
        if (byMeshAddress == null) {
            Toast.makeText(context, "若智能灯光是出厂设置,请重新配置", Toast.LENGTH_SHORT).show();
            return;
        }
        maxValue = Utils.getInt(context, "brightness", 50);
        if (byMeshAddress.status == ConnectionStatus.ON) {
            fadeOuting = false;
            handler.removeCallbacks(doClose);
            handler.removeCallbacks(closeRunnable);
            open();
            setLightValue(maxValue);
            return;
        }
//        int status = mApplication.getConnectDevice().status;
//        Log.d("status_charge", "fadeIn: " + status);
        value = LIGHT_MIN_VALUE;
//        if (byMeshAddress.brightness > value) {
//            value = byMeshAddress.brightness;
//        }
        handler.post(openRunnable);
    }

    public void fadeOut() {
        fadeOuting = true;
        DeviceInfo connectDevice = mApplication.getConnectDevice();
        if (connectDevice == null) {
            fadeOuting = false;
            Log.d("aidl", "connect device is null");
            return;
        }
        Light byMeshAddress = Lights.getInstance().getByMeshAddress(connectDevice.meshAddress);
        if (byMeshAddress == null) {
            Toast.makeText(context, "若智能灯光是出厂设置,请重新配置", Toast.LENGTH_SHORT).show();
            fadeOuting = false;
            return;
        }
        if (byMeshAddress.status == ConnectionStatus.OFF) {
            handler.removeCallbacks(openRunnable);
            fadeOuting = false;
            setLightValue(LIGHT_MIN_VALUE);
            close();
            return;
        }
        maxValue = Utils.getInt(context, "brightness", 50);

//        int status = connectDevice.status;
//        Log.d("status_charge", "fadeOut: " + status);
        value = maxValue;
        handler.post(closeRunnable);
    }

    private void setLightValue(int value) {
        int addr = 0xFFFF;//addr of all
        //change group brightness or color temperature
//            addr = 0x8003;
        byte opcode;
        byte[] params;
        opcode = (byte) 0xD2;
        params = new byte[]{(byte) value};
        if (TelinkLightService.Instance() != null && TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, true)) {
            Log.d("aidl", "set value success:" + value);
        } else {
            Log.d("aidl", "set value failure:" + (TelinkLightService.Instance() != null));
        }

//        opcode = (byte) 0xE2;
//        params = new byte[]{0x05, (byte) value};
//        TelinkLightService.Instance().sendCommandNoResponse(opcode, addr, params, true);
    }

    private void showQrCodeRightNow() {
        if (duration - currentPosition < 1000 * 60) {
            MessageEvent messageEvent = new MessageEvent();
            messageEvent.setEventId(SmartControlService.ORDER_PLAY_COMPLETE);
            messageEvent.setMessage("电影播放完成,记录完成订单信息");
            EventBus.getDefault().post(messageEvent);
            LogUtils.i("room-info", "stop media completed:" + currentPath);
        } else {
            LogUtils.i("room-info", "stop media but not completed:" + currentPath);
        }
    }
}