am 234b0b03: Merge "Fix, MountService now only sends one onShutDownComplete"

* commit '234b0b037ee1f8dc50c846279216a8e4d779ff56':
  Fix, MountService now only sends one onShutDownComplete
This commit is contained in:
Jeff Sharkey
2014-02-18 09:12:57 -08:00
committed by Android Git Automerger

View File

@ -91,6 +91,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -384,18 +385,37 @@ class MountService extends IMountService.Stub
} }
class ShutdownCallBack extends UnmountCallBack { class ShutdownCallBack extends UnmountCallBack {
IMountShutdownObserver observer; MountShutdownLatch mMountShutdownLatch;
ShutdownCallBack(String path, IMountShutdownObserver observer) { ShutdownCallBack(String path, final MountShutdownLatch mountShutdownLatch) {
super(path, true, false); super(path, true, false);
this.observer = observer; mMountShutdownLatch = mountShutdownLatch;
} }
@Override @Override
void handleFinished() { void handleFinished() {
int ret = doUnmountVolume(path, true, removeEncryption); int ret = doUnmountVolume(path, true, removeEncryption);
if (observer != null) { Slog.i(TAG, "Unmount completed: " + path + ", result code: " + ret);
mMountShutdownLatch.countDown();
}
}
static class MountShutdownLatch {
private IMountShutdownObserver mObserver;
private AtomicInteger mCount;
MountShutdownLatch(final IMountShutdownObserver observer, int count) {
mObserver = observer;
mCount = new AtomicInteger(count);
}
void countDown() {
boolean sendShutdown = false;
if (mCount.decrementAndGet() == 0) {
sendShutdown = true;
}
if (sendShutdown && mObserver != null) {
try { try {
observer.onShutDownComplete(ret); mObserver.onShutDownComplete(StorageResultCode.OperationSucceeded);
} catch (RemoteException e) { } catch (RemoteException e) {
Slog.w(TAG, "RemoteException when shutting down"); Slog.w(TAG, "RemoteException when shutting down");
} }
@ -1426,6 +1446,10 @@ class MountService extends IMountService.Stub
Slog.i(TAG, "Shutting down"); Slog.i(TAG, "Shutting down");
synchronized (mVolumesLock) { synchronized (mVolumesLock) {
// Get all volumes to be unmounted.
MountShutdownLatch mountShutdownLatch = new MountShutdownLatch(observer,
mVolumeStates.size());
for (String path : mVolumeStates.keySet()) { for (String path : mVolumeStates.keySet()) {
String state = mVolumeStates.get(path); String state = mVolumeStates.get(path);
@ -1461,19 +1485,16 @@ class MountService extends IMountService.Stub
if (state.equals(Environment.MEDIA_MOUNTED)) { if (state.equals(Environment.MEDIA_MOUNTED)) {
// Post a unmount message. // Post a unmount message.
ShutdownCallBack ucb = new ShutdownCallBack(path, observer); ShutdownCallBack ucb = new ShutdownCallBack(path, mountShutdownLatch);
mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb)); mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
} else if (observer != null) { } else if (observer != null) {
/* /*
* Observer is waiting for onShutDownComplete when we are done. * Count down, since nothing will be done. The observer will be
* Since nothing will be done send notification directly so shutdown * notified when we are done so shutdown sequence can continue.
* sequence can continue.
*/ */
try { mountShutdownLatch.countDown();
observer.onShutDownComplete(StorageResultCode.OperationSucceeded); Slog.i(TAG, "Unmount completed: " + path +
} catch (RemoteException e) { ", result code: " + StorageResultCode.OperationSucceeded);
Slog.w(TAG, "RemoteException when shutting down");
}
} }
} }
} }