最近遇到客户一个比较苛刻的需求,要求动态改变外置T卡的读写权限,然后接下来的几天就各种尝试,各种失败到了放弃的前0.0001秒时突然灵光一现,终于在周五快下班的时候搞出来了,可以回家过个安心周末了!记录一下整个开发过程,留作以后备忘。有了以下代码,以后想动态改变任意区域的读写权限将变得相当容易!
try{
execCommand(new String[]{"/system/bin/sh", "-c",
"echo "+ (isAllowSDWriteable?0:1)+" > /sys/module/tpd_setting/parameters/sd_writeable_solution"});
}catch(Exception e){
android.util.Log.d("xujiademo","isAllowSDWriteable=="+e);
}
doMount(getMountService());
private IMountService mMountService;
private void doMount(final IMountService mountService) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
if (mountService != null) {
Xlog.d(TAG, "Settings mountVolume : " + "/storage/sdcard1");
mountService.mountVolume("/storage/sdcard1"); /*modified by sunjinbiao on 20150123 for external sdcard read and write permission*/
} else {
Xlog.e(TAG, "Mount service is null, can't mount");
}
} catch (RemoteException e) {
// Not much can be done
}
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private synchronized IMountService getMountService() {
if (mMountService == null) {
IBinder service = ServiceManager.getService("mount");
if (service != null) {
mMountService = IMountService.Stub.asInterface(service);
} else {
android.util.Log.e(TAG, "Can't get mount service");
}
}
return mMountService;
}
在volume.cpp的int Volume::mountVol() 函数适当位置加入如下几句:
else if (getState() != Volume::State_Idle) {
//Begin:modified by sunjinbiao for external sdcard read and write permission
mountPath = "/mnt/media_rw/sdcard1";
if(!strcmp(mountPath, getMountpoint())){
int i=read_nvram();
if(i==2609){
if (Fat::doMount("/dev/block/vold/179:97", "/mnt/media_rw/sdcard1", true, true, false,
AID_MEDIA_RW, AID_MEDIA_RW, 0702, true)) {
SLOGE("/storage/sdcard1 failed to mount \n");
return -1;
}
}
else
{
if (Fat::doMount("/dev/block/vold/179:97", "/mnt/media_rw/sdcard1", false, true, false,
AID_MEDIA_RW, AID_MEDIA_RW, 0702, true)) {
SLOGE("/storage/sdcard1 failed to mount \n");
return -1;
}
}
}
else
{
errno = EBUSY;
if (getState() == Volume::State_Pending) {
mRetryMount = true;
}
return -1;
}
//End:modified by sunjinbiao for external sdcard read and write permission
}
const char sd_writeable_solution[] = "/sys/module/tpd_setting/parameters/sd_writeable_solution";
int read_nvram()
{
int fd = -1;
int status = 0;
fd = open(sd_writeable_solution,O_RDONLY, 0);
if (fd < 0) {
ALOGE("[Posix_connect Debug]open file fail: %s",sd_writeable_solution);
}
read(fd, &status, sizeof(int));
ALOGE("[Posix_connect Debug]STATUS: %d",status);
//printf("bk trace: read_logo_status status=%d \n", status);
close(fd);
return status;
}