实现接口的其他文件我就不一一赘述了,直接上重点
1,在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl定义接口
package android.app.customized;
interface ICustomizedService{
void allowTFcard(boolean iscontrol);
boolean isTFcardAllowed();
}
2,在frameworks/base/core/java/android/app/customized/CustomizedManager.java中定义
package android.app.customized;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.RemoteException;
import android.provider.Settings;
import java.io.IOException;
import android.os.ServiceManager;
import android.os.IBinder;
import java.util.List;
import android.app.ActivityManager;
import android.graphics.Bitmap;
public class CustomizedManager{
private static final String TAG="CustomizedManager";
private static final boolean DBG=true;
private static ICustomizedService mService;
private final Context mContext;
public CustomizedManager(Context context){
mContext = context;
mService = ICustomizedService.Stub.asInterface(
ServiceManager.getService("customized"));
}
private static ICustomizedService getService(){
if (mService != null) {
return mService;
}
IBinder b = ServiceManager.getService("customized");
mService = ICustomizedService.Stub.asInterface(b);
return mService;
}
public void allowTFcard(boolean iscontrol){
ICustomizedService service = getService();
try {
service.allowTFcard(iscontrol);
} catch (RemoteException e) {}
}
public boolean isTFcardAllowed(){
ICustomizedService service = getService();
try {
return service.isTFcardAllowed();
} catch (RemoteException e) {}
return false;
}
}
3, 在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;
}
}
}