拷贝文件

代码块
package com.oushang.factorymode.config.mvp.view;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.oushang.factorymode.common.BaseFragment;
import com.oushang.factorymode.config.R;
import com.oushang.factorymode.config.utils.NaviFileUtils;

import java.io.File;

/**
 * Created by Android Studio.
 * User: li.yuan34
 * Date: 2020/5/27
 * Time: 10:19
 */
public class ConfigNaviFragment extends BaseFragment implements OnClickListener {
    private static final String TAG = "NaviFac_Fragment";
    public static final String ACTIVATION_CODE_FROM_FILE = "/storage/usbotg/AutoNaviData/ReLoginCode.csv";
    public static final String ACTIVATION_CODE_TO_FOLDER = "/oem/AutoNaviData";
    public static final String ACTIVATION_CODE_FILE_NAME = "ReLoginCode.csv";



    private static final String OFFLINE_DATA_FROM_FOLDER_PATH = "/storage/usbotg/AutoNaviData/chn";
    private static final String OFFLINE_DATA_TO_FOLDER_PATH = "/navi/AutoNaviData/chn";

    private static final String FROM_MD5_FOLDER_PATH = "/storage/usbotg/AutoNaviData/md5";
    private static final String FROM_COPY_MD5_FOLDER_PATH = "/navi/AutoNaviData/md5";
    private static final String TO_MD5_FOLDER_PATH = "/navi/md5";
    private static final String OFFLINE_DATA_MD5_FILE_NAME = "chnmd5.txt";

    private static final String OFFLINE_DATA_FROM_MD5_FILE_PATH = FROM_MD5_FOLDER_PATH + "/" + OFFLINE_DATA_MD5_FILE_NAME;
    private static final String OFFLINE_DATA_FROM_COPY_MD5_FILE_PATH = FROM_COPY_MD5_FOLDER_PATH + "/" + OFFLINE_DATA_MD5_FILE_NAME;
    private static final String OFFLINE_DATA_TO_MD5_FILE_PATH = TO_MD5_FOLDER_PATH + "/" + OFFLINE_DATA_MD5_FILE_NAME;

    private static final int ACTION_DEFAULT = -100;
    private static final int ACTION_COPY_ACTIVATION_CODE = 0;
    private static final int ACTION_COPY_OFFLINE_DATA = 1;
    private static final int ACTION_MD5_OFFLINE_DATA = 2;
    private static final int ACTION_DELETE_OFFLINE_DATA = 3;
    private static final int ACTION_CREATE_OFFLINE_DATA_MD5 = 4;
    private static final int ACTION_COPE_OFFLINE_DATA_MD5 = 5;

    private static final int CMD_RESULT_DEFAULT = -100;

    private static final int MUIL_CLICK_DURATION = 1000;
    long mLastClickTime;
    int mClickCount;
    boolean mIsVisiable;
    Button mImportBtn;
    Button mCopyButton;
    Button mMD5Button;
    Button mDeleteButton;
    Button mCreateMD5Button;
    Button mCopyMD5Button;
    TextView mTextView;


    Handler mHandler;
    Handler mWorkHandler;
    HandlerThread mWorkThread;

    private static final int WORK_MSG_ACTION = 0;

    private static final int MAIN_MSG_RESULT = 0;

    Context mContext;
    String mText;
    public static boolean mIsProcessRuning = false;
    static int mAction = ACTION_DEFAULT;
    static int mResult = CMD_RESULT_DEFAULT;
    static int mTextColor = Color.GREEN;
    static ConfigNaviFragment mFragment;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView mAction=" + mAction + " mResult=" + mResult + " mIsProcessRuning=" + mIsProcessRuning + " this="+this);
        mView = inflater.inflate(R.layout.config_navi_import_fragment, container, false);
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.d(TAG,"main handleMessage msg.arg1=" + msg.arg1 + " msg.arg2=" + msg.arg2 + "mIsProcessRuning=" + mIsProcessRuning);
                switch (msg.what){
                    case MAIN_MSG_RESULT:
                        if (mFragment != null){
                            mFragment.updateTextView(msg.arg1, msg.arg2);
                        }
                        break;
                    default:
                        break;
                }
            }
        };
        mWorkThread = new HandlerThread("workThread");
        mWorkThread.start();
        mWorkHandler = new WorkHandler(mWorkThread.getLooper());
        mContext = getActivity();
        mFragment = this;
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        mFragment = null;
    }

    @Override
    protected void initView() {
        Log.d(TAG, "initView");
        mImportBtn = mView.findViewById(R.id.config_import_navi_btn);
        mCopyButton = mView.findViewById(R.id.copy_button);
        mMD5Button = mView.findViewById(R.id.md5_button);
        mDeleteButton = mView.findViewById(R.id.delete_button);
        mCreateMD5Button = mView.findViewById(R.id.md5_create_button);
        mCopyMD5Button = mView.findViewById(R.id.md5_copy_button);
        mTextView = mView.findViewById(R.id.copy_text);
        updateTextView(mAction, mResult);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void setAllListener() {
        mImportBtn.setOnClickListener(this);
        mCopyButton.setOnClickListener(this);
        mMD5Button.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);
        mCreateMD5Button.setOnClickListener(this);
        mCopyMD5Button.setOnClickListener(this);
        mTextView.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.copy_text){//显示操作状态,连续点击10次可显示或隐藏删除、MD5生成、导入和强行停止选项
            textViewClick();
            return;
        }

        if (mIsProcessRuning){
            Toast.makeText(mContext,getString(R.string.navigation_toast),Toast.LENGTH_SHORT).show();
            return;
        }

        int action = ACTION_DEFAULT;
        if (view.getId() == R.id.config_import_navi_btn){//导入导航激活码
            action = ACTION_COPY_ACTIVATION_CODE;
        }else if (view.getId() == R.id.copy_button){//从U盘导入离线数据和MD5文件
            action = ACTION_COPY_OFFLINE_DATA;
        }else if (view.getId() == R.id.md5_button){//对离线数据进行MD5校验,即对离线数据生成MD5文件,并与导入的MD5文件进行比对(如果没有导入则实时对U盘离线数据生成MD5文件)
            action = ACTION_MD5_OFFLINE_DATA;
        }else if (view.getId() == R.id.delete_button){//删除离线数据和MD5文件
            action = ACTION_DELETE_OFFLINE_DATA;
        }else if (view.getId() == R.id.md5_create_button){//对U盘离线数据生成MD5文件至U盘
            action = ACTION_CREATE_OFFLINE_DATA_MD5;
        }else if (view.getId() == R.id.md5_copy_button){//从U盘导入MD5文件
            action = ACTION_COPE_OFFLINE_DATA_MD5;
        }
        mIsProcessRuning = true;
        mAction = action;
        updateTextView(action);
        Message message = Message.obtain();
        message.what = WORK_MSG_ACTION;
        message.arg1 = mAction;
        mWorkHandler.sendMessage(message);
    }

    private String getDoingString(int action){
        String string = "";
        if (action == ACTION_COPY_ACTIVATION_CODE){
            string = getString(R.string.navigation_activation_code_drive);
        }else if (action == ACTION_COPY_OFFLINE_DATA){
            string = getString(R.string.navigation_offline_data_drive);
        }else if (action == ACTION_MD5_OFFLINE_DATA){
            string = getString(R.string.navigation_checkout_offline_data_drive);
        }else if (action == ACTION_DELETE_OFFLINE_DATA){
            string = getString(R.string.navigation_delete_offline_data_drive);
        }else if (action == ACTION_CREATE_OFFLINE_DATA_MD5){
            string = getString(R.string.navigation_generate_data_file);
        }else if (action == ACTION_COPE_OFFLINE_DATA_MD5){
            string = getString(R.string.navigation_md5_copy);
        }
        return string;
    }

    public void updateTextView(int action) {
        mTextView.setText(getDoingString(action));
        mTextColor = Color.RED;
        mTextView.setTextColor(mTextColor);
    }

    public void updateTextView(int action, int result) {
        Log.d(TAG,"updateTextView "+ this);
        if (result == NaviFileUtils.CMD_RESULT_SUCCESS){
            mTextColor = Color.GREEN;
        }else {
            mTextColor = Color.RED;
        }
        if (mFragment != null && !mFragment.isAdded()){
            Log.d(TAG,"fragment is not added");
            return;
        }
        if (mIsProcessRuning){//处理中
            Log.d(TAG,"isProcessRuning");
            updateTextView(action);
            return;
        }

        if (mResult == CMD_RESULT_DEFAULT){//第一次进入
            return;
        }

        //真正返回结果
        String actionText = "";
        switch (action) {
            case ACTION_COPY_ACTIVATION_CODE:
                actionText = getString(R.string.navigation_activation_code_copy);
                break;
            case ACTION_COPY_OFFLINE_DATA:
                actionText = getString(R.string.navigation_offline_data_copy);
                break;
            case ACTION_MD5_OFFLINE_DATA:
                actionText = getString(R.string.navigation_md5_Validity);
                break;
            case ACTION_DELETE_OFFLINE_DATA:
                actionText = getString(R.string.navigation_delete);
                break;
            case ACTION_CREATE_OFFLINE_DATA_MD5:
                actionText = getString(R.string.navigation_md5_generate);
                break;
            case ACTION_COPE_OFFLINE_DATA_MD5:
                actionText = getString(R.string.navigation_copy);
                break;
        }
        String resultText;
        switch (result) {
            case NaviFileUtils.CMD_RESULT_SUCCESS:
                resultText = getString(R.string.navigation_succeed);
                break;
            case NaviFileUtils.CMD_RESULT_FROM_FILE_NONEXIST:
                resultText = getString(R.string.navigation_Failed_error) + result + getString(R.string.navigation_file_does_not_exist);
                break;
            case NaviFileUtils.CMD_RESULT_TO_FILE_NONEXIST:
                resultText = getString(R.string.navigation_Failed_error) + result + getString(R.string.navigation_The_target_file_does_not_exist);
                break;
            case NaviFileUtils.CMD_RESULT_FILE_NONSAME:
                resultText = getString(R.string.navigation_Failed_error) + result + getString(R.string.navigation_File_inconsistency);
                break;
            case NaviFileUtils.CMD_NO_CONNECT_USB:
                resultText = getString(R.string.navigation_Failed_error) + result + getString(R.string.navigation_Confirm_U_disk_connection_status);
                break;
            default:
                resultText = getString(R.string.navigation_Failed_error) + result;
                break;
        }
        mText = actionText + resultText;
        mTextView.setText(mText);
        mTextView.setTextColor(mTextColor);
    }

    private void textViewClick(){
        long newClickTime = SystemClock.uptimeMillis();
        long duration = newClickTime - mLastClickTime;
        if (duration > 0 && duration < MUIL_CLICK_DURATION) {
            mClickCount++;
            if (mClickCount == 10) {
                mCreateMD5Button.setVisibility(mIsVisiable ? View.VISIBLE : View.GONE);
                mCopyMD5Button.setVisibility(mIsVisiable ? View.VISIBLE : View.GONE);
                mDeleteButton.setVisibility(mIsVisiable ? View.VISIBLE : View.GONE);
                mClickCount = 0;
                mIsVisiable = !mIsVisiable;
            }
        } else {
            mClickCount = 0;
        }
        mLastClickTime = SystemClock.uptimeMillis();
    }

    class WorkHandler extends Handler{
        public WorkHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case WORK_MSG_ACTION:
                    int result = NaviFileUtils.CMD_RESULT_UNKNOWN;
                    switch (msg.arg1){
                        case ACTION_COPY_ACTIVATION_CODE:
                            result = NaviFileUtils.copyFile(ACTIVATION_CODE_FROM_FILE, ACTIVATION_CODE_TO_FOLDER, ACTIVATION_CODE_FILE_NAME);
                            break;
                        case ACTION_COPY_OFFLINE_DATA:
                            if ((OFFLINE_DATA_FROM_FOLDER_PATH.contains(NaviFileUtils.USB_PATH) || OFFLINE_DATA_TO_FOLDER_PATH.contains(NaviFileUtils.USB_PATH))
                                    && !new File(NaviFileUtils.USB_PATH).exists()) {
                                result = NaviFileUtils.CMD_NO_CONNECT_USB;
                                Log.e(TAG, "no connect usb");
                            }else {
                                //先删除之前拷贝的和生成的
                                NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_TO_MD5_FILE_PATH);
                                NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_FROM_COPY_MD5_FILE_PATH);
                                NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_TO_FOLDER_PATH);
                                //然后先拷贝MD5文件,再拷贝离线数据
                                NaviFileUtils.copyFile(OFFLINE_DATA_FROM_MD5_FILE_PATH, FROM_COPY_MD5_FOLDER_PATH, OFFLINE_DATA_MD5_FILE_NAME);
                                result = NaviFileUtils.copyFolder(OFFLINE_DATA_FROM_FOLDER_PATH, OFFLINE_DATA_TO_FOLDER_PATH);
                            }
                            break;
                        case ACTION_MD5_OFFLINE_DATA:
                            result = NaviFileUtils.compareMD5(OFFLINE_DATA_FROM_FOLDER_PATH, OFFLINE_DATA_TO_FOLDER_PATH, FROM_COPY_MD5_FOLDER_PATH,
                                    TO_MD5_FOLDER_PATH, OFFLINE_DATA_MD5_FILE_NAME);
                            break;
                        case ACTION_DELETE_OFFLINE_DATA:
                            //删除之前拷贝的和生成的
                            NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_TO_MD5_FILE_PATH);
                            NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_FROM_COPY_MD5_FILE_PATH);
                            result = NaviFileUtils.deleteFolderOrFile(OFFLINE_DATA_TO_FOLDER_PATH);
                            break;
                        case ACTION_CREATE_OFFLINE_DATA_MD5:
                            result = NaviFileUtils.createMD5File(OFFLINE_DATA_FROM_FOLDER_PATH, FROM_MD5_FOLDER_PATH, OFFLINE_DATA_MD5_FILE_NAME);
                            break;
                        case ACTION_COPE_OFFLINE_DATA_MD5:
                            result = NaviFileUtils.copyFile(OFFLINE_DATA_FROM_MD5_FILE_PATH, FROM_COPY_MD5_FOLDER_PATH, OFFLINE_DATA_MD5_FILE_NAME);
                            break;
                        default:
                            break;
                    }
                    mResult = result;
                    mIsProcessRuning = false;
                    Message message = Message.obtain();
                    message.what = MAIN_MSG_RESULT;
                    message.arg1 = msg.arg1;
                    message.arg2 = mResult;
                    mHandler.sendMessage(message);
                    Log.d(TAG, "work handleMessage mAction=" + mAction + " mResult=" + mResult + " mIsProcessRuning=" + mIsProcessRuning);
                    break;
                default:
                    break;

            }
        }
    }
}

你可能感兴趣的:(拷贝文件)