代码路径:frameworks\base\core\java\com\mediatek\storage\StorageManagerEx.java
public static String getDefaultPath() {
String path = "";
boolean deviceTablet = false;
boolean supportMultiUsers = false;
String user_first = SystemProperties.get("persist.sys.path_sd");
if(user_first.equals("1")){
String externalStoragePath = getExternalStoragePath();
if(!externalStoragePath.equals("")){
SystemProperties.set(PROP_SD_DEFAULT_PATH,externalStoragePath);
SystemProperties.set("persist.sys.path_sd","0");
}
}
try {
path = SystemProperties.get(PROP_SD_DEFAULT_PATH);
//Log.i(TAG, "get path from system property, path=" + path);
} catch (IllegalArgumentException e) {
Log.e(TAG, "IllegalArgumentException when get default path:" + e);
}// Property will be empty when first boot, should set to default
// For OTA upgrade, path is invalid, need update default path
if (path.equals("")
|| path.equals(STORAGE_PATH_SD1_ICS) || path.equals(STORAGE_PATH_SD1)
|| path.equals(STORAGE_PATH_SD2_ICS) || path.equals(STORAGE_PATH_SD2)) {
//Log.i(TAG, "DefaultPath invalid! " + "path = " + path + ", set to default.");
try {
IMountService mountService =
IMountService.Stub.asInterface(ServiceManager.getService("mount"));
if (mountService == null) {
Log.e(TAG, "mount service is not initialized!");
return "";
}
int userId = UserHandle.myUserId();
VolumeInfo[] volumeInfos = mountService.getVolumes(0);
for (int i = 0; i < volumeInfos.length; ++i) {
VolumeInfo vol = volumeInfos[i];
if (vol.isVisibleForWrite(userId) && vol.isPrimary()) {
path = vol.getPathForUser(userId).getAbsolutePath();
//Log.i(TAG, "Find primary and visible volumeInfo, "
//+ "path=" + path + ", volumeInfo:" + vol);
break;
}
}
setDefaultPath(path);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException when set default path:" + e);
}
}
Log.i(TAG, "current system default path = " + path);
return path;
}
/**
* Returns external SD card path.
* SD card might have multi partitions
* will return first partition path
* @hide
* @internal
*/
public static String getExternalStoragePath() {
String path = "";
try {
IMountService mountService =
IMountService.Stub.asInterface(ServiceManager.getService("mount"));
if (mountService == null) {
Log.e(TAG, "mount service is not initialized!");
return "";
}
int userId = UserHandle.myUserId();
boolean isEMMCProject = SystemProperties.get("ro.mtk_emmc_support").equals("1");
VolumeInfo[] volumeInfos = mountService.getVolumes(0);
for (int i = 0; i < volumeInfos.length; ++i) {
VolumeInfo vol = volumeInfos[i];
String diskID = vol.getDiskId();
//Log.d(TAG, "getExternalStoragePath, diskID=" + diskID);
if (diskID != null) {
// portable sd card
if (vol.isVisibleForWrite(userId)
&& vol.getState() == VolumeInfo.STATE_MOUNTED) {
if (isEMMCProject) {
// sd card disk id is "179,128" or "179,xxx", but not "179,0"
if (diskID.startsWith("disk:179") && !diskID.endsWith(",0")) {
path = vol.getPathForUser(userId).getAbsolutePath();
break;
}
} else {
// sd card disk id is "179,0"
if (diskID.equals("disk:179,0")) {
path = vol.getPathForUser(userId).getAbsolutePath();
break;
}
}
}
} else {
// sd card is adopted and migrate data
if (vol.getType() == VolumeInfo.TYPE_EMULATED
&& vol.getState() == VolumeInfo.STATE_MOUNTED) {
String emulatedPath = vol.getPathForUser(userId).getAbsolutePath();
File internalPathFile = vol.getInternalPath();
if (internalPathFile != null
&& !internalPathFile.getAbsolutePath().equals("/data/media")) {
path = emulatedPath;
break;
} else {
Log.d(TAG, "getExternalStoragePath, igore path=" + emulatedPath);
}
}
}
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException when getExternalStoragePath:" + e);
}
Log.d(TAG, "getExternalStoragePath path=" + path);
return path ;
}