VideoManager.java 15.6 KB
package com.sw.laryngoscope.camera;

import android.content.Context;
import android.os.HandlerThread;

import com.sw.laryngoscope.db.AccountInfoDB;
import com.sw.laryngoscope.db.RecordInfoDB;
import com.sw.laryngoscope.manager.AccountManager;
import com.sw.laryngoscope.manager.InfoManager;
import com.sw.laryngoscope.manager.SpManager;
import com.sw.laryngoscope.utils.Logger;
import com.sw.laryngoscope.utils.Storage;
import com.sw.laryngoscope.utils.Thumbnail;

import org.litepal.LitePal;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Iterator;

public class VideoManager implements Runnable {

    public static final long MAX_STORAGE_THRESHOLD_BYTES = 50 * 1024 * 1024 * 1024l;
    public static final long FREE_STORAGE_THRESHOLD_BYTES = 600 * 1024 * 1024l;

    public long totalFileSpace = 0L;

    private InfoManager mInfoManager;

    private Thread mCheckThread = null;
    private boolean isExitThread = false;

    Context mContext;

    SimpleDateFormat ymdSdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat ymSdf = new SimpleDateFormat("yyyy-MM");
    SimpleDateFormat ySdf = new SimpleDateFormat("yyyy");


    public VideoManager(Context context) {
        mContext = context;

        mInfoManager = InfoManager.getInstance();

        initFileList();
    }

    public void startThread(Context context) {

        if ( mCheckThread == null ) {
            mCheckThread = new Thread(this);
            mCheckThread.start();
        }

        HandlerThread ht = new HandlerThread("video Handler Thread");
        ht.start();

    }

    public void stopThread() {
        isExitThread = true;
    }

    public synchronized void addVideoToDb(String title, String filename, String startTimestamp, String endTimestamp,
            String filepath, String procedureName, String hourTime, String thumbnailname,
            String thumbnailpath, long duration, int type, int cameraType, int width, int height) {
        try {
            Logger.d("===addVideoToDb==filename=" + filename +
                    " filepath " + filepath + " thumbnailpath " + thumbnailpath +
                    " type " + type);
            /*if ( !isNeedUpload ) {
                File tmpFile = new File(path + "/" + filename);
                if ( tmpFile.exists() ) {
                    tmpFile.delete();
                }
                return ;
            }*/

            duration = Thumbnail.getVideoDur(filepath + "/" + filename);
            Logger.d("===addVideoToDb==filename=" + filename +
                    " duration " + duration );
            SpManager.set_value_record_totaltime(SpManager.get_value_record_totaltime() + duration);

            AccountInfoDB user = AccountManager.getInstance().getLoginAcc();
            RecordInfoDB tmp = new RecordInfoDB(title, filename, startTimestamp, endTimestamp,
                    filepath, procedureName, hourTime, thumbnailname,
                    thumbnailpath, duration, type, cameraType,
                    ymSdf.format(Long.parseLong(endTimestamp)),
                    ymdSdf.format(Long.parseLong(endTimestamp)),
                    ySdf.format(Long.parseLong(endTimestamp)),
                    user.getName(), false, width, height, user.getId());

            mInfoManager.recDblist.add(tmp);
            tmp.save();
            File tmpFile = new File(filepath + "/" + filename);
            totalFileSpace += tmpFile.length();

            checkSpaceSize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void addPicToDb(String title, String filename, String startTimestamp, String endTimestamp,
            String filepath, String procedureName, String hourTime, String thumbnailname,
            String thumbnailpath, long duration, int type, int cameraType, int width, int height) {
        try {
            Logger.d("===addPicToDb==filename=" + filename +
                    " filepath " + filepath + " thumbnailpath " + thumbnailpath +
                    " type " + type);
            /*if ( !isNeedUpload ) {
                File tmpFile = new File(path + "/" + filename);
                if ( tmpFile.exists() ) {
                    tmpFile.delete();
                }
                return ;
            }*/

            AccountInfoDB user = AccountManager.getInstance().getLoginAcc();
            RecordInfoDB tmp = new RecordInfoDB(title, filename, startTimestamp, endTimestamp,
                    filepath, procedureName, hourTime, thumbnailname,
                    thumbnailpath, duration, type, cameraType,
                    ymSdf.format(Long.parseLong(endTimestamp)),
                    ymdSdf.format(Long.parseLong(endTimestamp)),
                    ySdf.format(Long.parseLong(endTimestamp)),
                    user.getName(), false, width, height, user.getId());
            mInfoManager.recDblist.add(tmp);
            tmp.save();
            File tmpFile = new File(filepath + "/" + filename);
            totalFileSpace += tmpFile.length();

            checkSpaceSize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized RecordInfoDB addPdfToDb(String title, String filename, String startTimestamp, String endTimestamp,
                                        String filepath, String procedureName, String hourTime, String thumbnailname,
                                        String thumbnailpath, long duration, int type, int cameraType, int width, int height) {
        try {
            Logger.d("===addPdfToDb==filename=" + filename +
                    " filepath " + filepath + " thumbnailpath " + thumbnailpath +
                    " type " + type);
            AccountInfoDB user = AccountManager.getInstance().getLoginAcc();
            RecordInfoDB tmp = new RecordInfoDB(title, filename, startTimestamp, endTimestamp,
                    filepath, procedureName, hourTime, thumbnailname,
                    thumbnailpath, duration, type, cameraType,
                    ymSdf.format(Long.parseLong(endTimestamp)),
                    ymdSdf.format(Long.parseLong(endTimestamp)),
                    ySdf.format(Long.parseLong(endTimestamp)),
                    user.getName(), true, width, height, user.getId());
            mInfoManager.recDblist.add(tmp);
            tmp.save();
            File tmpFile = new File(filepath + "/" + filename);
            totalFileSpace += tmpFile.length();

            checkSpaceSize();
            return tmp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public synchronized RecordInfoDB addConcatToDb(String title, String filename, String startTimestamp, String endTimestamp,
                                                String filepath, String procedureName, String hourTime, String thumbnailname,
                                                String thumbnailpath, long duration, int type, int cameraType, int width, int height) {
        try {
            Logger.d("===addPdfToDb==filename=" + filename +
                    " filepath " + filepath + " thumbnailpath " + thumbnailpath +
                    " type " + type);
            //duration = Thumbnail.getVideoDur(filepath + "/" + filename);

            AccountInfoDB user = AccountManager.getInstance().getLoginAcc();
            RecordInfoDB tmp = new RecordInfoDB(title, filename, startTimestamp, endTimestamp,
                    filepath, procedureName, hourTime, thumbnailname,
                    thumbnailpath, duration, type, cameraType,
                    ymSdf.format(Long.parseLong(endTimestamp)),
                    ymdSdf.format(Long.parseLong(endTimestamp)),
                    ySdf.format(Long.parseLong(endTimestamp)),
                    user.getName(), true, width, height, user.getId());
            mInfoManager.recDblist.add(tmp);
            tmp.save();
            File tmpFile = new File(filepath + "/" + filename);
            totalFileSpace += tmpFile.length();

            checkSpaceSize();
            return tmp;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void run() {
        while ( !isExitThread ) {
            try {

                checkSpaceSize();

                Thread.sleep(10 * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void deleteFile(String name) {
        try {
            //long availSpace = MAX_STORAGE_THRESHOLD_BYTES - totalFileSpace;
            long availSpace = Storage.getAvailableSpace();
            Logger.d("===deleteFile==01=availSpace " + availSpace + " size " + mInfoManager.recDblist.size()
                    + " MAX_STORAGE_THRESHOLD_BYTES " + MAX_STORAGE_THRESHOLD_BYTES
                    + " FREE_STORAGE_THRESHOLD_BYTES " + FREE_STORAGE_THRESHOLD_BYTES + " totalFileSpace " + totalFileSpace);
            Iterator <RecordInfoDB> iterator = mInfoManager.recDblist.iterator();
            while ( iterator.hasNext() ) {
                RecordInfoDB deleteInfo = iterator.next();
                if ( deleteInfo.getFilename().equals(name) ) {
                    File cbFile = new File(deleteInfo.getFilepath() + "/" + deleteInfo.getFilename());
                    if ( cbFile.exists() ) {
                        cbFile.delete();
                        totalFileSpace -= cbFile.length();
                        iterator.remove();//使用迭代器的删除方法删除
                        deleteInfo.delete();
                    }
                    Logger.d("===deleteFile==1=" + cbFile.length() +
                            " " + cbFile.getAbsolutePath() + " availSpace " + availSpace);
                    availSpace = Storage.getAvailableSpace();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void checkSpaceSize() {
        try {
            //long availSpace = MAX_STORAGE_THRESHOLD_BYTES - totalFileSpace;
            mInfoManager.recDblist = LitePal.findAll(RecordInfoDB.class);
            long availSpace = Storage.getAvailableSpace();
            Logger.d("===checkSpaceSize==0=availSpace " + availSpace + " size " + mInfoManager.recDblist.size()
                    + " MAX_STORAGE_THRESHOLD_BYTES " + MAX_STORAGE_THRESHOLD_BYTES
                    + " FREE_STORAGE_THRESHOLD_BYTES " + FREE_STORAGE_THRESHOLD_BYTES + " totalFileSpace " + totalFileSpace);

            /*if ( availSpace < 0 || (availSpace > 0 && availSpace < FREE_STORAGE_THRESHOLD_BYTES) ) {
                Iterator <RecordInfoDB> iterator = mInfoManager.recDblist.iterator();
                while ( iterator.hasNext() ) {
                    RecordInfoDB deleteInfo = iterator.next();
                    if ( !deleteInfo.isNeedUpload() ) {
                        File cbFile = new File(deleteInfo.getFilepath() + "/" + deleteInfo.getFilename());
                        totalFileSpace -= cbFile.length();
                        //availSpace = MAX_STORAGE_THRESHOLD_BYTES - totalFileSpace;
                        availSpace = Storage.getAvailableSpace();
                        Logger.d("===checkSpaceSize==11=" + cbFile.length() +
                                " " + cbFile.getAbsolutePath() + " availSpace " + availSpace);
                        if ( cbFile.exists() ) {
                            cbFile.delete();
                        }
                        iterator.remove();//使用迭代器的删除方法删除
                        //LitePal.delete(RecordInfoDB.class, 0);
                        deleteInfo.delete();
                        if ( availSpace > FREE_STORAGE_THRESHOLD_BYTES ) {
                            break ;
                        }
                    }
                }
            }*/
            if ( availSpace <= 0 || (availSpace > 0 && availSpace < FREE_STORAGE_THRESHOLD_BYTES) ) {
                Iterator <RecordInfoDB> iterator = mInfoManager.recDblist.iterator();
                while ( iterator.hasNext() ) {
                    RecordInfoDB deleteInfo = iterator.next();

                    File cbFile = new File(deleteInfo.getFilepath() + "/" + deleteInfo.getFilename());
                    totalFileSpace -= cbFile.length();
                    //availSpace = MAX_STORAGE_THRESHOLD_BYTES - totalFileSpace;
                    availSpace = Storage.getAvailableSpace();
                    Logger.d("===checkSpaceSize==2=" + cbFile.length() +
                            " " + cbFile.getAbsolutePath() + " availSpace " + availSpace);

                    if ( cbFile.exists() ) {
                        cbFile.delete();
                    }

                    iterator.remove();//使用迭代器的删除方法删除
                    //LitePal.delete(RecordInfoDB.class, 0);
                    deleteInfo.delete();
                    if ( availSpace > FREE_STORAGE_THRESHOLD_BYTES ) {
                        break ;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initFileList() {/*
        File cbFile1 = new File("/storage/emulated/0/DCIM/OCamera/CB_20210109_094240.mp4");
        Logutil.d("===initFileList==0=" + cbFile1.length());*/
        mInfoManager.recDblist = LitePal.findAll(RecordInfoDB.class);

        Iterator <RecordInfoDB> iterator = mInfoManager.recDblist.iterator();
        while ( iterator.hasNext() ) {
            RecordInfoDB clearInfo = iterator.next();
            File cbFile = new File(clearInfo.getFilepath() + "/" + clearInfo.getFilename());
            Logger.d("===initFileList==0=" + cbFile.exists() + " cbFile1.length() " + cbFile.length());
            if ( !cbFile.exists() ) {
                iterator.remove();
                clearInfo.delete();
            }
        }
        Logger.d("===initFileList==3=" + mInfoManager.recDblist.size());

        String initVideoData = "";
        for ( RecordInfoDB tmp : mInfoManager.recDblist  ) {
            initVideoData += tmp.toString();
        }
        Logger.d("===initVideoData===" + initVideoData );

        File dir = new File(Storage.VIDEO_DIR);
        dir.mkdirs();
        File[] files = dir.listFiles();
        if ( files != null ) {
            for( int i = 0; i < files.length; i++ ) {
                //Logutil.d("===videoDblist===" + files[i].getAbsolutePath() + " " + files[i].getName() );
                String name = files[i].getName();
                if ( initVideoData.contains(name.substring(0, name.indexOf("."))) ) {
                    /*if ( files[i].getName().contains(InitParam.prefixBack) ) {
                        filelistCB.add(files[i].getAbsolutePath());
                    } else if ( files[i].getName().contains(InitParam.prefixFront) ) {
                        filelistCF.add(files[i].getAbsolutePath());
                    } else if ( files[i].getName().contains(InitParam.prefixUart) ) {
                        filelistUart.add(files[i].getAbsolutePath());
                    }*/
                    totalFileSpace += files[i].length();
                    Logger.d("===initFileList==010=" + files[i].length());
                } else {
                    files[i].delete();
                    Logger.d("===initFileList==01=" + files[i].getName());
                }
            }
        }
        Logger.d("===initFileList==totalFileSpace=" + totalFileSpace);
    }

}