android 中浅谈业务与操作分离的思想

作者: 追梦小乐 | 来源:发表于2017-12-11 15:04 被阅读13次

    笔者公司有做收集指纹的业务,用的是设备中心提供的一些so库和jar包,因为有很多地方有用到,为了不导致复制粘贴,就封装了一下代码,也想借此机会,来简单谈谈业务与操作代码分离的思想。

    1、先定义借口类FingerprintOperationCallback ,主要是操作中供给核心业务调用的方法
    public interface FingerprintOperationCallback {
        
    
        /**
         * 申请权限失败的回调
         * 
         */
        void onApplypermissionsFail();
        
        /**
         * 成功打开指纹模块的回调
         *
         * status = 1 成功, -1为失败
         */
    //  void onOpenStatus(int status);
    
        /**
         * 打开指纹模块失敗的回调
         *
         */
        void onFailOpenFP();
    
        /**
         * 采集指纹的回调
         * 
         */
        void onReadResult(Bitmap bitmap);
    }
    
    2、操作封装类FingerprintUtil,主要是封装了第三方的一些基本用法,比如:打开指纹设备、申请权限、开始采集等等
    public class FingerprintUtil {
        
    
        private static final String TAG = FingerprintUtil.class.getSimpleName();
        private static final int FLAG_APPLY_PERMISSIONS_SUCCESS  = 100;
        private static final int FLAG_APPLY_PERMISSIONS_FAIL  = 101;
        private static final int FLAG_OPEN_FP_SUCCESS  = 102;
        private static final int FLAG_OPEN_FP_FAIL = 103;
        private static final int FLAG_READ_FP_RESULT_SUCCESS = 104;
        
        private static volatile FingerprintUtil instance;
        
        private Bitmap mFingerprintImage;
        
        private boolean isOpen = false;
        
        private ExecutorService mExecutorService;
        private Handler mHandler;
        
    
        private FingerprintOperationCallback mOperationCallback;
    
        private FP mFp;
        private Context mContext;
    
        private boolean isReadSuccess;
    
        private int openStatus;
    
        private Object mLockObject = new Object( );
    
        public static FingerprintUtil getInstance( Context context) {
            if ( instance == null ) {
                synchronized ( FingerprintUtil.class ) {
                    if ( instance == null ) {
                        instance = new FingerprintUtil(context);
                    }
                }
            }
            return instance;
        }
        
        public FingerprintUtil( ) {
    
        }
    
        public FingerprintUtil(Context context) {
            this.mContext = context;
        }
        
        private void init( ) {
            LogUtil.d(TAG,"init====================");
            if ( mFp == null ) {
                mFp = new FP(mContext);
            }
            if ( mExecutorService == null ) {
                mExecutorService = Executors.newSingleThreadExecutor( );
            }
            if ( mHandler == null ) {
                mHandler = new Handler( mCallback );
            }
            registerUSBpermisson();
        }
    
    
        /**
         * 申请权限的广播
         */
        private final BroadcastReceiver mUsbReceiverPermission = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (FP.ACTION_USB_PERMISSION.equals(action)) {
                    synchronized (this) {
                        UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        //用户允许权限申请true
                        if (usbDevice != null && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            //用户同意 ;
                            //申请权限成功的回调
                            Message msg;
                            msg = mHandler.obtainMessage( FLAG_APPLY_PERMISSIONS_SUCCESS );
                            mHandler.sendMessage( msg );
                            LogUtil.d(TAG,"用户同意打开指纹模块");
                        } else {
                            //用户拒绝
                            //申请权限失败的回调
                            Message msg;
                            msg = mHandler.obtainMessage( FLAG_APPLY_PERMISSIONS_FAIL );
                            mHandler.sendMessage( msg );
                            Log.e("jp", "permission denied for device!!!");
                            ToastUtils.showLongMessage(mContext, R.string.err_no_permission);
                        }
                    }
                } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
    
                }
            }
        };
    
        /*注册USB权限*/
        public void registerUSBpermisson() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(FP.ACTION_USB_PERMISSION);
            filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);//拔出USB
            mContext.registerReceiver(mUsbReceiverPermission, filter);
        }
    
        public void unregisterReceiver() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(FP.ACTION_USB_PERMISSION);
            mContext.unregisterReceiver(mUsbReceiverPermission);
            isReadSuccess = true;
            LogUtil.d(TAG,"unregisterReceiver================");
        }
    
        /**
         * 申请权限
         */
        public void applypermissions(){
            mExecutorService.execute(mApplypermissions);
        }
    
        /**
         * 申请权限
         */
        private Runnable mApplypermissions = new Runnable() {
            @Override
            public void run() {
                registerUSBpermisson();
            }
        };
    
        /**
         * 权限注销
         */
        private Runnable mUnregisterReceiver = new Runnable() {
            @Override
            public void run() {
                unregisterReceiver();
            }
        };
        
    
        /**
         * 初始化设备
         */
        public void initFP( ) {
            init( );
        }
    
        /**
         * 打开指纹模块
         */
        public void openFP( ) {
            mExecutorService.execute(mOpenFP);
        }
    
        /**
         * 打开指纹模块
         */
        private Runnable mOpenFP = new Runnable() {
            @Override
            public void run() {
                if (mFp.findDevice()) {
                    if (!mFp.isPermissed()) {
                        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("FP_Request_USB_PERMISSION"), 0);
                        mFp.requestPermission(mPermissionIntent);
                        return;
                    }
                    openStatus = mFp.open();
                    LogUtil.d(TAG,"openFp====openStatus===="+ openStatus);
                    if (openStatus == 0){
                        Message msg;
                        msg = mHandler.obtainMessage( FLAG_OPEN_FP_SUCCESS );
                        mHandler.sendMessage( msg );
                    }else if (openStatus == -1){
                        Message msg;
                        msg = mHandler.obtainMessage( FLAG_OPEN_FP_FAIL );
                        mHandler.sendMessage( msg );
                    }
                }
            }
        };
    
    
        public void readFPResult(){
            mExecutorService.execute(mReadFPResult);
        }
    
        /**
         * 指纹采集
         */
        private Runnable mReadFPResult = new Runnable() {
            @Override
            public void run() {
                isReadSuccess = false;
                while (!isReadSuccess) {
                    LogUtil.d(TAG,"等待指纹输入======");
                    mFingerprintImage = mFp.read();
                    if (mFingerprintImage != null) {
                        isReadSuccess = true;
                        Message msg;
                        msg = mHandler.obtainMessage( FLAG_READ_FP_RESULT_SUCCESS );
                        mHandler.sendMessage( msg );
                    }
                }
            }
        };
    
    
    
        private Callback mCallback = new Callback( ) {
    
            @Override
            public boolean handleMessage( Message msg ) {
                // TODO Auto-generated method stub
                switch ( msg.what ) {
                    case FLAG_APPLY_PERMISSIONS_SUCCESS :
                        openFP();
                        break;
                    case FLAG_APPLY_PERMISSIONS_FAIL :
                        if ( mOperationCallback != null ) {
                            mOperationCallback.onApplypermissionsFail();
                        }
                        break;
                    case FLAG_OPEN_FP_SUCCESS :
                        readFPResult();
                        break;
                    case FLAG_OPEN_FP_FAIL :
                        if ( mOperationCallback != null ) {
                            mOperationCallback.onFailOpenFP();
                        }
                        break;
                    case FLAG_READ_FP_RESULT_SUCCESS :
                        if ( mOperationCallback != null ) {
                            mOperationCallback.onReadResult(mFingerprintImage);
                        }
                        break;
                    default :
                        break;
                }
                return false;
            }
        };
    
    
        public void setFingerprintOperationCallback( FingerprintOperationCallback operationCallback ) {
            this.mOperationCallback = operationCallback;
        }
    
        public void release( ) {
            synchronized ( mLockObject ) {
                mOperationCallback = null;
                if ( mHandler != null ) {
                    mHandler.removeCallbacksAndMessages( null );
                }
                if ( mFingerprintImage != null && !mFingerprintImage.isRecycled( ) ) {
                    mFingerprintImage.recycle( );
                    mFingerprintImage = null;
                }
                unregisterReceiver();
            }
        }
        
    }
    
    3、操作封装类在自己核心业务中使用
      FingerprintUtil.getInstance(getActivity()).setFingerprintOperationCallback(mOperationCallback);
      FingerprintUtil.getInstance(getActivity()).initFP();
      FingerprintUtil.getInstance(getActivity()).openFP();
    
      @Override
        public void onDestroy() {
            super.onDestroy();
            FingerprintUtil.getInstance(getActivity()).release();
        }
    
        FingerprintOperationCallback mOperationCallback = new FingerprintOperationCallback() {
            @Override
            public void onApplypermissionsFail() {
                LogUtil.d(TAG,"onApplypermissionsFail==========");
                mHandler.sendEmptyMessage(3);
            }
    
            @Override
            public void onFailOpenFP() {
                LogUtil.d(TAG,"onFailOpenFP==========");
                mHandler.sendEmptyMessage(1);
            }
    
            @Override
            public void onReadResult(Bitmap bitmap) {
                LogUtil.d(TAG,"onReadResult==========");
                Message message = Message.obtain();
                message.obj = bitmap;
                message.what = 2;
                mHandler.sendMessage(message);
            }
        };
    

    相关文章

      网友评论

        本文标题:android 中浅谈业务与操作分离的思想

        本文链接:https://www.haomeiwen.com/subject/xpmiixtx.html