7.0是可以同步的,因为在挂载成功后会给MediaScannerReceiver发个更新媒体库的广播
如下
frameworks/base/services/core/java/com/android/server/MountService.java
break;
}
case H_VOLUME_BROADCAST: {
final StorageVolume userVol = (StorageVolume) msg.obj;
final String envState = userVol.getState();
Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to "
+ userVol.getOwner());
final String action = VolumeInfo.getBroadcastForEnvironment(envState);
if (action != null) {
final Intent intent = new Intent(action,
Uri.fromFile(userVol.getPathFile()));
intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
mContext.sendBroadcastAsUser(intent, userVol.getOwner());
}
break;
}
但是,android8.0就没有MountService.java这个文件,发广播的地方是在
frameworks/base/services/core/java/com/android/server/StorageManagerService.java
case H_VOLUME_BROADCAST: {
final StorageVolume userVol = (StorageVolume) msg.obj;
final String envState = userVol.getState();
Slog.d(TAG, "Volume " + userVol.getId() + " broadcasting " + envState + " to "
+ userVol.getOwner());
final String action = VolumeInfo.getBroadcastForEnvironment(envState);
if (action != null) {
final Intent intent = new Intent(action,
Uri.fromFile(userVol.getPathFile()));
intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, userVol);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
| Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
mContext.sendBroadcastAsUser(intent, userVol.getOwner());
}
break;
}
然后上面没有走到的原因是:
(vol.isVisibleForRead(userId)为false
if (!Objects.equals(oldStateEnv, newStateEnv)) {
// Kick state changed event towards all started users. Any users
// started after this point will trigger additional
// user-specific broadcasts.
for (int userId : mSystemUnlockedUsers) {
Slog.w(TAG, "vol.isVisibleForRead(userId)-----"+vol.isVisibleForRead(userId));
if (vol.isVisibleForRead(userId)) {
final StorageVolume userVol = vol.buildStorageVolume(mContext, userId, false);
mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();
Slog.w(TAG, "mHandler.obtainMessage(H_VOLUME_BROADCAST, userVol).sendToTarget();");
mCallbacks.notifyStorageStateChanged(userVol.getPath(), oldStateEnv,
newStateEnv);
}
}
}
然后影响
(vol.isVisibleForRead(userId)为false
是因为mountFlags为0
public boolean isVisible() {
Slog.w(TAG, "isVisible-----"+mountFlags);
return (mountFlags & MOUNT_FLAG_VISIBLE) != 0;
}
private static final String TAG = "StorageManagerService";
public boolean isVisibleForRead(int userId) {
if (type == TYPE_PUBLIC) {
Slog.w(TAG, "isVisibleForRead11111111");
if (isPrimary() && mountUserId != userId) {
Slog.w(TAG, "isVisibleForRead1111_1111");
// Primary physical is only visible to single user
return false;
} else {
Slog.w(TAG, "isVisibleForRead1111_2222");
return isVisible();
}
} else if (type == TYPE_EMULATED) {
Slog.w(TAG, "isVisibleForRead222222");
return isVisible();
} else {
Slog.w(TAG, "isVisibleForRead33333");
return false;
}
}
然后mountFlags具体赋值的地方是
system/vold/CommandListener.cpp
int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
vol->setMountFlags(mountFlags);
vol->setMountUserId(mountUserId);
int res = vol->mount();
if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
vm->setPrimary(vol);
}
system/vold/CommandListener.cpp
int mountFlags = (argc > 3) ? atoi(argv[3]) : 0;
userid_t mountUserId = (argc > 4) ? atoi(argv[4]) : -1;
vol->setMountFlags(mountFlags);
vol->setMountUserId(mountUserId);
int res = vol->mount();
if (mountFlags & android::vold::VolumeBase::MountFlags::kPrimary) {
vm->setPrimary(vol);
}