DualFirstTabFragment.java 源代码


package activity.live.dualfragment;

import activity.live.DualLiveViewActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import base.HiFragment;
import bean.MyCamera;
import butterknife.ButterKnife;
import com.heytap.mcssdk.a.a;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.callback.ICameraPlayStateCallback;
import com.hichip.campro.R;
import com.hichip.control.HiCamera;
import com.xiaomi.mipush.sdk.Constants;
import common.HiDataValue;
import java.io.File;
import org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylePropertyConstants;
import utils.HiLogcatUtil;
import utils.HiTools;
import utils.MyToast;
import utils.ToastUtil;

public class DualFirstTabFragment extends HiFragment implements View.OnClickListener, ICameraIOSessionCallback, ICameraPlayStateCallback, View.OnTouchListener {
    public static final int MY_PERMISSION_REQUEST_CODE = 10022;
    public ImageView iv_duijiang;
    public ImageView iv_listen;
    public ImageView iv_mvfull;
    public ImageView iv_record;
    public ImageView iv_snapshot;
    RelativeLayout ll_root;
    LinearLayout ll_square;
    private DualLiveViewActivity mAct;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            int i = message.what;
            if (i == -2147483647) {
                DualFirstTabFragment.this.handMsgPlayState(message);
            } else {
                if (i != 2457) {
                    return;
                }
                DualFirstTabFragment.this.endRecording();
                MyToast.showToast(DualFirstTabFragment.this.getActivity(), DualFirstTabFragment.this.getString(R.string.failed_recording));
            }
        }
    };
    private int mHeight;
    private MyCamera mMyCamera;
    private long oldClickTime;
    Toast toast;

    @Override
    public void callbackPlayUTC(HiCamera hiCamera, int i) {
    }

    @Override
    public void callbackPlayUTC2Dual(HiCamera hiCamera, int i, int i2, int i3) {
    }

    @Override
    public void callbackPlayUTCDual(HiCamera hiCamera, int i, int i2) {
    }

    @Override
    public void callbackRecordState(HiCamera hiCamera, int i, int i2, int i3, String str) {
    }

    @Override
    public void callbackRecordStateDual(HiCamera hiCamera, int i, int i2, int i3, int i4, String str) {
    }

    @Override
    public void callbackStateDual(HiCamera hiCamera, int i, int i2, int i3, int i4) {
    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_duallive_first_tab, (ViewGroup) null);
        ButterKnife.bind(this, inflate);
        initViewAndData();
        setListeners();
        return inflate;
    }

    private void setListeners() {
        this.iv_listen.setOnClickListener(this);
        this.iv_record.setOnClickListener(this);
        this.iv_snapshot.setOnClickListener(this);
        this.iv_mvfull.setOnClickListener(this);
        this.iv_mvfull.setClickable(false);
        this.iv_duijiang.setOnTouchListener(this);
    }

    private void initViewAndData() {
        this.mAct = (DualLiveViewActivity) getActivity();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        this.mAct.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int i = displayMetrics.widthPixels;
    }

    public void initHeight(int i) {
        this.mHeight = i;
    }

    public void setmMyCamera(MyCamera myCamera) {
        this.mMyCamera = myCamera;
        myCamera.registerIOSessionListener(this);
        this.mMyCamera.registerPlayStateListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (this.mAct.timer != null) {
            this.mAct.timer.cancel();
            this.mAct.timer = null;
        }
        if (this.mAct.timerTask != null) {
            this.mAct.timerTask.cancel();
            this.mAct.timerTask = null;
        }
        MyCamera myCamera = this.mMyCamera;
        if (myCamera != null) {
            myCamera.unregisterPlayStateListener(this);
            this.mMyCamera.unregisterIOSessionListener(this);
        }
        Handler handler = this.mHandler;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (this.mMyCamera == null || getResources().getConfiguration().orientation != 1) {
            return;
        }
        this.mMyCamera.registerPlayStateListener(this);
        this.mMyCamera.registerIOSessionListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_duijiang:
                handTalkingNew();
                return;
            case R.id.iv_full:
                DualLiveViewActivity dualLiveViewActivity = this.mAct;
                if (dualLiveViewActivity != null) {
                    dualLiveViewActivity.clickfull();
                    return;
                }
                return;
            case R.id.iv_listen:
                clickListen();
                return;
            case R.id.iv_record:
                if (HiTools.HiPermission(getActivity(), getActivity(), 1, 10022)) {
                    this.mAct.mIsOnpause = true;
                    return;
                } else {
                    clickRecording();
                    return;
                }
            case R.id.iv_snapshot:
                if (HiTools.HiPermission(getActivity(), getActivity(), 1, 10022)) {
                    this.mAct.mIsOnpause = true;
                    return;
                } else {
                    handSnapshot();
                    return;
                }
            default:
                return;
        }
    }

    private void handTalkingNew() {
        DualLiveViewActivity dualLiveViewActivity = this.mAct;
        if (dualLiveViewActivity == null) {
            return;
        }
        if (dualLiveViewActivity.mRecordingState == 2) {
            if (this.mAct.isListening) {
                this.mMyCamera.PausePlayAudio();
                this.iv_listen.setSelected(false);
                this.mAct.iv_landscape_listen.setSelected(false);
                this.mAct.isListening = !r0.isListening;
                this.mAct.mIsListenTrans = true;
            }
        } else if (this.mAct.isListening) {
            this.mMyCamera.stopListening();
            this.mAct.mVoiceIsTran = false;
            this.iv_listen.setSelected(false);
            this.mAct.iv_landscape_listen.setSelected(false);
            this.mAct.isListening = !r0.isListening;
            this.mAct.mIsListenTrans = true;
        }
        if (this.mAct.isTalking) {
            this.mMyCamera.stopTalk();
            if (this.mAct.mIsListenTrans && !this.mAct.isListening) {
                if (this.mAct.mVoiceIsTran) {
                    this.mMyCamera.ResumePlayAudio();
                } else {
                    this.mMyCamera.startListening();
                    this.mAct.mVoiceIsTran = true;
                }
                this.mAct.iv_landscape_listen.setSelected(true);
                this.iv_listen.setSelected(true);
                this.mAct.isListening = true;
            }
        } else {
            this.mMyCamera.startTalk();
        }
        this.mAct.isTalking = !r0.isTalking;
        this.iv_duijiang.setSelected(this.mAct.isTalking);
        this.mAct.iv_landscape_talk.setSelected(this.mAct.isTalking);
    }

    private void handSnapshot() {
        if (this.mAct == null) {
            return;
        }
        if (HiTools.mDualvalue == 0 || HiTools.mDualvalue == 1) {
            this.mAct.clickSnapshot();
        }
        if (HiTools.mDualvalue == 0 || HiTools.mDualvalue == 2) {
            this.mAct.clickSnap2shot();
        }
    }

    private void clickListen() {
        DualLiveViewActivity dualLiveViewActivity = this.mAct;
        if (dualLiveViewActivity == null) {
            return;
        }
        if (dualLiveViewActivity.mRecordingState == 2) {
            if (this.mAct.mVoiceIsTran) {
                this.mMyCamera.PausePlayAudio();
                this.mAct.mIsListenTrans = false;
                if (this.mAct.isListening) {
                    this.mMyCamera.PausePlayAudio();
                    this.mAct.mIsListenTrans = false;
                } else {
                    this.mMyCamera.ResumePlayAudio();
                }
            } else {
                this.mMyCamera.stopRecording();
                this.mMyCamera.startListening();
                this.mAct.mVoiceIsTran = true;
                this.mAct.TimerRecording();
                this.mAct.mStartRecordTime = 0L;
            }
            if (this.mAct.isListening) {
                this.mAct.iv_landscape_listen.setSelected(false);
                this.iv_listen.setSelected(false);
            } else {
                this.mAct.iv_landscape_listen.setSelected(true);
                this.iv_listen.setSelected(true);
            }
        } else if (this.mAct.isListening) {
            this.mAct.iv_landscape_listen.setSelected(false);
            this.iv_listen.setSelected(false);
            this.mMyCamera.stopListening();
            this.mAct.mIsListenTrans = false;
            this.mAct.mVoiceIsTran = false;
        } else {
            this.mAct.iv_landscape_listen.setSelected(true);
            this.iv_listen.setSelected(true);
            this.mMyCamera.startListening();
            this.mAct.mVoiceIsTran = true;
        }
        if (this.mAct.isTalking) {
            this.iv_duijiang.setSelected(false);
            this.mAct.iv_landscape_talk.setSelected(false);
            this.mMyCamera.stopTalk();
            this.mAct.isTalking = !r0.isTalking;
        }
        this.mAct.isListening = !r0.isListening;
    }

    private void clickRecording() {
        DualLiveViewActivity dualLiveViewActivity = this.mAct;
        if (dualLiveViewActivity == null) {
            return;
        }
        if (dualLiveViewActivity.mRecordingState == 0) {
            this.mAct.mRecordingState = 2;
            this.mAct.TimerRecording();
            this.iv_record.setSelected(true);
            this.mAct.iv_landscape_record.setSelected(true);
            this.mAct.mStartRecordTime = 0L;
            this.mAct.ll_recording.setVisibility(0);
            this.mAct.iv_recording.setVisibility(0);
            this.mAct.ll_2recording.setVisibility(0);
            this.mAct.iv_2recording.setVisibility(0);
            this.mAct.mRecHandler.sendEmptyMessage(110);
            return;
        }
        if (this.mAct.mRecordingState == 2) {
            endRecording();
        }
    }

    public void endRecording() {
        DualLiveViewActivity dualLiveViewActivity = this.mAct;
        if (dualLiveViewActivity == null) {
            return;
        }
        dualLiveViewActivity.mRecordingState = 0;
        if (this.mAct.mVoiceIsTran && !this.iv_listen.isSelected()) {
            this.mMyCamera.stopListening();
            this.mAct.mVoiceIsTran = false;
        }
        this.mMyCamera.stopRecording();
        if (this.mAct.timer != null) {
            this.mAct.timer.cancel();
            this.mAct.timer = null;
        }
        if (this.mAct.timerTask != null) {
            this.mAct.timerTask.cancel();
            this.mAct.timerTask = null;
        }
        this.iv_record.setSelected(false);
        this.mAct.iv_landscape_record.setSelected(false);
        this.mAct.ll_recording.setVisibility(4);
        this.mAct.iv_recording.setVisibility(4);
        this.mAct.ll_2recording.setVisibility(4);
        this.mAct.iv_2recording.setVisibility(4);
        this.mAct.mRecHandler.removeCallbacksAndMessages(null);
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(getString(R.string.tips_no_permission));
        builder.setPositiveButton(getString(R.string.setting), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                HiTools.toSelfSetting(DualFirstTabFragment.this.getActivity());
            }
        });
        builder.setNegativeButton(getString(R.string.cancel), (DialogInterface.OnClickListener) null);
        builder.show();
    }

    public void handMsgPlayState(Message message) {
        int i = message.getData().getInt(a.k);
        if (i == 3) {
            grabRecordSnapshot();
            return;
        }
        if ((i == 4 || i == 5) && !TextUtils.isEmpty(this.mAct.recordFile)) {
            File file = new File(this.mAct.recordFile);
            if (file.length() <= 1024 && file.isFile() && file.exists()) {
                file.delete();
            }
        }
    }

    private void grabRecordSnapshot() {
        MyCamera myCamera = this.mMyCamera;
        Bitmap snapshot_EXT = myCamera != null ? myCamera.getSnapshot_EXT(1, 640, 352) : null;
        if (snapshot_EXT != null) {
            saveRecordSnapshot(snapshot_EXT);
        }
    }

    private void saveRecordSnapshot(final Bitmap bitmap) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            public void onPostExecute(Void r1) {
            }

            @Override
            public Void doInBackground(Void... voidArr) {
                if (!Environment.getExternalStorageState().equals("mounted")) {
                    return null;
                }
                File file = new File(HiDataValue.getPathCameraLocalRecordSnapShot(DualFirstTabFragment.this.mMyCamera.getUid(), DualFirstTabFragment.this.mAct));
                if (!file.exists()) {
                    file.mkdirs();
                }
                if (TextUtils.isEmpty(DualFirstTabFragment.this.mAct.recordFile)) {
                    return null;
                }
                String[] split = new File(DualFirstTabFragment.this.mAct.recordFile).getName().split("\\.");
                HiTools.saveBitmap(bitmap, file.getAbsolutePath() + "/" + split[0]);
                return null;
            }
        }.execute(new Void[0]);
    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {
        if (this.mMyCamera != hiCamera) {
            return;
        }
        Message obtainMessage = this.mHandler.obtainMessage();
        obtainMessage.what = HiDataValue.HANDLE_MESSAGE_SESSION_STATE;
        obtainMessage.arg1 = i;
        obtainMessage.obj = hiCamera;
        this.mHandler.sendMessage(obtainMessage);
    }

    @Override
    public void receiveIOCtrlData(HiCamera hiCamera, int i, byte[] bArr, int i2) {
        if (hiCamera != this.mMyCamera) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putByteArray("data", bArr);
        Message obtainMessage = this.mHandler.obtainMessage();
        obtainMessage.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        obtainMessage.obj = hiCamera;
        obtainMessage.arg1 = i;
        obtainMessage.arg2 = i2;
        obtainMessage.setData(bundle);
        this.mHandler.sendMessage(obtainMessage);
    }

    @Override
    public void callbackState(HiCamera hiCamera, int i, int i2, int i3) {
        if (this.mMyCamera != hiCamera) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putInt(a.k, i);
        bundle.putInt(CSSStylePropertyConstants.WIDTH, i2);
        bundle.putInt(CSSStylePropertyConstants.HEIGHT, i3);
        Message obtainMessage = this.mHandler.obtainMessage();
        obtainMessage.what = HiDataValue.HANDLE_MESSAGE_PLAY_STATE;
        obtainMessage.setData(bundle);
        this.mHandler.sendMessage(obtainMessage);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (view.getId() == R.id.iv_duijiang) {
            if (!this.iv_duijiang.isClickable()) {
                return true;
            }
            int action = motionEvent.getAction();
            if (action != 0) {
                if (action == 1) {
                    HiTools.Hi_CloseOrientation(getActivity());
                    this.mAct.viewPager.setCanScroll(true);
                    if (System.currentTimeMillis() - this.oldClickTime < 500) {
                        ToastUtil.showCenter(getContext(), R.string.tips_press_intercom, 1000);
                    }
                    if (this.mAct.mRecordingState == 2) {
                        if (this.mAct.isListening) {
                            this.mMyCamera.PausePlayAudio();
                            this.iv_listen.setSelected(false);
                            this.mAct.iv_landscape_listen.setSelected(false);
                            this.mAct.isListening = !r7.isListening;
                            this.mAct.mIsListenTrans = true;
                        }
                    } else if (this.mAct.isListening) {
                        this.mMyCamera.stopListening();
                        this.mAct.mVoiceIsTran = false;
                        this.iv_listen.setSelected(false);
                        this.mAct.iv_landscape_listen.setSelected(false);
                        this.mAct.isListening = !r7.isListening;
                        this.mAct.mIsListenTrans = true;
                    }
                    this.mMyCamera.stopTalk();
                    if (this.mAct.mIsListenTrans && !this.mAct.isListening) {
                        if (this.mAct.mVoiceIsTran) {
                            this.mMyCamera.ResumePlayAudio();
                        } else {
                            this.mMyCamera.startListening();
                            this.mAct.mVoiceIsTran = true;
                        }
                        this.mAct.iv_landscape_listen.setSelected(true);
                        this.iv_listen.setSelected(true);
                        this.mAct.isListening = true;
                    }
                    this.mAct.isTalking = false;
                    this.iv_duijiang.setSelected(false);
                    this.mAct.iv_landscape_talk.setSelected(this.mAct.isTalking);
                    setViewClickable(true);
                }
            } else {
                if (HiTools.HiPermission(getActivity(), getActivity(), 2, 10022)) {
                    this.mAct.mIsOnpause = true;
                    return true;
                }
                HiTools.Hi_OpenOrientation(getActivity());
                this.mAct.viewPager.setCanScroll(false);
                if (System.currentTimeMillis() - this.oldClickTime >= 1000) {
                    this.oldClickTime = System.currentTimeMillis();
                    if (this.mAct.mRecordingState == 2) {
                        if (this.mAct.isListening) {
                            this.mMyCamera.PausePlayAudio();
                            this.iv_listen.setSelected(false);
                            this.mAct.iv_landscape_listen.setSelected(false);
                            this.mAct.isListening = !r7.isListening;
                            this.mAct.mIsListenTrans = true;
                        }
                    } else if (this.mAct.isListening) {
                        this.mMyCamera.stopListening();
                        this.mAct.mVoiceIsTran = false;
                        this.iv_listen.setSelected(false);
                        this.mAct.iv_landscape_listen.setSelected(false);
                        this.mAct.isListening = !r7.isListening;
                        this.mAct.mIsListenTrans = true;
                    }
                    this.mMyCamera.startTalk();
                    this.mAct.isTalking = true;
                    this.iv_duijiang.setSelected(true);
                    this.mAct.iv_landscape_talk.setSelected(this.mAct.isTalking);
                    setViewClickable(false);
                }
            }
        }
        return false;
    }

    private void setViewClickable(boolean z) {
        this.iv_listen.setClickable(z);
        this.iv_record.setClickable(z);
        this.iv_snapshot.setClickable(z);
    }

    @Override
    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        boolean z = true;
        HiLogcatUtil.e("" + i);
        int length = strArr.length;
        for (int i2 = 0; i2 < length; i2++) {
            HiLogcatUtil.e(strArr.length + Constants.COLON_SEPARATOR + strArr[i2]);
        }
        for (int i3 : iArr) {
            HiLogcatUtil.e(iArr.length + Constants.COLON_SEPARATOR + i3);
        }
        if (i == 10022) {
            int length2 = iArr.length;
            int i4 = 0;
            while (true) {
                if (i4 >= length2) {
                    break;
                }
                if (iArr[i4] != 0) {
                    z = false;
                    break;
                }
                i4++;
            }
            if (z) {
                return;
            }
            HiTools.Hi_GoToSetting(strArr, getActivity(), getActivity());
        }
    }
}