From 22dd86e0556bf60f46bb92a4d90aef8c2d55da58 Mon Sep 17 00:00:00 2001 From: San Mehat Date: Tue, 12 Jan 2010 12:21:18 -0800 Subject: [PATCH] MountService: Refactor to use NativeDaemonConnector and clean-up Signed-off-by: San Mehat --- core/java/android/os/IMountService.aidl | 6 +- .../app/ExternalMediaFormatActivity.java | 2 +- .../com/android/server/MountListener.java | 377 ------------------ .../java/com/android/server/MountService.java | 202 ++++++++-- 4 files changed, 173 insertions(+), 414 deletions(-) delete mode 100644 services/java/com/android/server/MountListener.java diff --git a/core/java/android/os/IMountService.aidl b/core/java/android/os/IMountService.aidl index 1e79030e46f8f..e73569adc97ee 100644 --- a/core/java/android/os/IMountService.aidl +++ b/core/java/android/os/IMountService.aidl @@ -42,17 +42,17 @@ interface IMountService /** * Mount external storage at given mount point. */ - void mountMedia(String mountPoint); + void mountVolume(String mountPoint); /** * Safely unmount external storage at given mount point. */ - void unmountMedia(String mountPoint); + void unmountVolume(String mountPoint); /** * Format external storage given a mount point. */ - void formatMedia(String mountPoint); + void formatVolume(String mountPoint); /** * Returns true if media notification sounds are enabled. diff --git a/core/java/com/android/internal/app/ExternalMediaFormatActivity.java b/core/java/com/android/internal/app/ExternalMediaFormatActivity.java index 000f6c49ab499..2b07ae67c5666 100644 --- a/core/java/com/android/internal/app/ExternalMediaFormatActivity.java +++ b/core/java/com/android/internal/app/ExternalMediaFormatActivity.java @@ -102,7 +102,7 @@ public class ExternalMediaFormatActivity extends AlertActivity implements Dialog .getService("mount")); if (mountService != null) { try { - mountService.formatMedia(Environment.getExternalStorageDirectory().toString()); + mountService.formatVolume(Environment.getExternalStorageDirectory().toString()); } catch (RemoteException e) { } } diff --git a/services/java/com/android/server/MountListener.java b/services/java/com/android/server/MountListener.java deleted file mode 100644 index 9443ff84921fa..0000000000000 --- a/services/java/com/android/server/MountListener.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.server; - -import android.net.LocalSocketAddress; -import android.net.LocalSocket; -import android.os.Environment; -import android.os.SystemClock; -import android.os.SystemProperties; -import android.util.Config; -import android.util.Log; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.Socket; -import java.lang.IllegalStateException; - -import java.util.List; -import java.util.ArrayList; -import java.util.ListIterator; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -/** - * Vold Connection class - */ -final class MountListener implements Runnable { - private static final String TAG = "MountListener"; - private static final String VOLD_SOCKET = "vold"; - private static final int RESPONSE_QUEUE_SIZE = 10; - - private MountService mService; - private BlockingQueue mResponseQueue; - private OutputStream mOutputStream; - - class ResponseCode { - public static final int ActionInitiated = 100; - public static final int VolumeListResult = 110; - public static final int AsecListResult = 111; - - public static final int CommandOkay = 200; - public static final int ShareAvailabilityResult = 210; - public static final int AsecPathResult = 211; - - public static final int UnsolicitedInformational = 600; - public static final int VolumeStateChange = 605; - public static final int VolumeMountFailedBlank = 610; - public static final int VolumeMountFailedDamaged = 611; - public static final int VolumeMountFailedNoMedia = 612; - public static final int ShareAvailabilityChange = 620; - public static final int VolumeDiskInserted = 630; - public static final int VolumeDiskRemoved = 631; - public static final int VolumeBadRemoval = 632; - } - - MountListener(MountService service) { - mService = service; - mResponseQueue = new LinkedBlockingQueue(RESPONSE_QUEUE_SIZE); - } - - public void run() { - // Vold does not run in the simulator, so fake out a mounted event to trigger the Media Scanner - if ("simulator".equals(SystemProperties.get("ro.product.device"))) { - mService.notifyMediaMounted(Environment.getExternalStorageDirectory().getPath(), false); - return; - } - - try { - while (true) { - listenToSocket(); - } - } catch (Throwable t) { - Log.e(TAG, "Fatal error " + t + " in MountListener thread!"); - } - } - - private void listenToSocket() { - LocalSocket socket = null; - - try { - socket = new LocalSocket(); - LocalSocketAddress address = new LocalSocketAddress(VOLD_SOCKET, - LocalSocketAddress.Namespace.RESERVED); - - socket.connect(address); - mService.onVoldConnected(); - - InputStream inputStream = socket.getInputStream(); - mOutputStream = socket.getOutputStream(); - - byte[] buffer = new byte[4096]; - - while (true) { - int count = inputStream.read(buffer); - if (count < 0) break; - - int start = 0; - for (int i = 0; i < count; i++) { - if (buffer[i] == 0) { - String event = new String(buffer, start, i - start); -// Log.d(TAG, "Got packet {" + event + "}"); - - String[] tokens = event.split(" "); - try { - int code = Integer.parseInt(tokens[0]); - - if (code >= ResponseCode.UnsolicitedInformational) { - try { - handleUnsolicitedEvent(code, event, tokens); - } catch (Exception ex) { - Log.e(TAG, String.format( - "Error handling '%s'", event), ex); - } - } else { - try { - mResponseQueue.put(event); - } catch (InterruptedException ex) { - Log.e(TAG, "InterruptedException"); - } - } - } catch (NumberFormatException nfe) { - Log.w(TAG, - "Unknown msg from Vold '" + event + "'"); - } - start = i + 1; - } - } - } - } catch (IOException ex) { - Log.e(TAG, "IOException in listenToSocket"); - } - - synchronized (this) { - if (mOutputStream != null) { - try { - mOutputStream.close(); - } catch (IOException e) { - Log.w(TAG, "IOException closing output stream"); - } - - mOutputStream = null; - } - } - - try { - if (socket != null) { - socket.close(); - } - } catch (IOException ex) { - Log.w(TAG, "IOException closing socket"); - } - - Log.e(TAG, "Failed to connect to Vold", new IllegalStateException()); - SystemClock.sleep(5000); - } - - private void handleUnsolicitedEvent(int code, String raw, - String[] cooked) throws IllegalStateException { -// Log.d(TAG, "unsolicited {" + raw + "}"); - if (code == ResponseCode.VolumeStateChange) { - // FMT: NNN Volume