TF卡控制接口

 实现接口的其他文件我就不一一赘述了,直接上重点

在frameworks/base/services/core/java/com/android/server/customized/CustomizedService.java中实现allowTFcard和isTFcardAllowed接口

   /**
     *  28 add interface allowTFcard(boolean allow){}
     */
    public void allowTFcard(boolean iscontrol){
	long jh = Binder.clearCallingIdentity();
        StorageManager mStorageManager = StorageManager.from(mContext);
        final List<VolumeInfo> vols = mStorageManager.getVolumes();
        VolumeInfo sdcardVolume = null ;

        for(VolumeInfo vol : vols){
            if(vol.getDisk() != null && vol.getDisk().isSd()) sdcardVolume = vol;

            //如果需要获取usbotg的路径,则改为
            // if(vol.getDisk() != null &&vol.getDisk().isUsb()) usbotgVolume = vol;
        }
        if(sdcardVolume!= null) {
            String sdcardPath = sdcardVolume.getPath()==null ? null :sdcardVolume.getPath().toString();
            String sdcardState = sdcardVolume.getEnvironmentForState(sdcardVolume.getState());
            //外置SD卡已挂载,且路径有效
            // sdcardPath 外置SD卡的路径

            System.out.println("T iscontrol=="+iscontrol);
            System.out.println("T sdcardPath=="+sdcardPath);
            System.out.println("T sdcardState=="+sdcardState);

            System.out.println("sdcardVolume  not  null");
	    Settings.System.putInt(mContext.getContentResolver(), "isForbidTFcard",iscontrol ? 1 : 0);
            if(iscontrol){
		System.out.println("mount.......................");
                if(sdcardState.equals(Environment.MEDIA_UNMOUNTED) && sdcardPath != null){
                    mStorageManager.mount(sdcardVolume.getId());
                }
            }else{
		System.out.println("unmount.......................");
                if(sdcardState.equals(Environment.MEDIA_MOUNTED) && sdcardPath != null){
                    mStorageManager.unmount(sdcardVolume.getId());
                }
            }


        }else{
                System.out.println("sdcardVolume ==null");
        }
        Binder.restoreCallingIdentity(jh);
    }

    /**
     *  29 add interface isTFcardAllowed(){}
     */
    public boolean isTFcardAllowed(){
	if(Settings.System.getInt(mContext.getContentResolver(),"isForbidTFcard",0)==1) {
                return true;
        } else {
                return false;
        }

    }


  

还需要在frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java中修改部分功能

   public UnmountTask(Context context, VolumeInfo volume) {
            mStorageManager = context.getSystemService(StorageManager.class);
            mVolumeId = volume.getId();
        }

        @Override
        protected Exception doInBackground(Void... params) {
            try {
                mStorageManager.unmount(mVolumeId);
                return null;
            } catch (Exception e) {
                return e;
            }
        }
    }

    private Notification onVolumeMounted(VolumeInfo vol) {   //在onVolumeMounted中对sdcard开机是否加载进行判断
        if(vol.getDisk() != null && vol.getDisk().isSd()){
            System.out.println("isSd==================");
            String sdcardPath = vol.getPath()==null ? null :vol.getPath().toString();
            String sdcardState = vol.getEnvironmentForState(vol.getState());
            if(sdcardState.equals(Environment.MEDIA_MOUNTED) && sdcardPath != null){
                if(0 == Settings.System.getInt(mContext.getContentResolver(), "isForbidTFcard",0)){
                    System.out.println("onVolumeMounted unmount...........................");
                    new UnmountTask(mContext, vol).execute();
                    return null;
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/lancelots/article/details/82150491