Skip to content

Commit

Permalink
Changes for MtpStorageManager
Browse files Browse the repository at this point in the history
Remove sendObject* from MtpService and callers.
Mediaprovider is no longer responsible for MTP events.

Remove storageId from Mediaprovider. New Mediaprovider
tables will no longer have this column. Mediaprovider
is no longer responsible for keeping storage information.

Clean up the code in MtpService to be less redundant.

Bug: 63143623
Test: Use MTP with emulated sdcard, try mounting and ejecting the card
during the session.
Change-Id: I2a5f178edcfab87c20c913bfbee38c105e9a6eb2
  • Loading branch information
Jerry Zhang committed Dec 15, 2017
1 parent 8030ddd commit ddf4110
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 274 deletions.
2 changes: 0 additions & 2 deletions src/com/android/providers/media/IMtpService.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ package com.android.providers.media;

interface IMtpService
{
void sendObjectAdded(int objectHandle);
void sendObjectRemoved(int objectHandle);
}
99 changes: 5 additions & 94 deletions src/com/android/providers/media/MediaProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
Expand Down Expand Up @@ -286,8 +285,6 @@ public void onReceive(Context context, Intent intent) {
context.sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_STARTED, uri));

// don't send objectRemoved events - MTP be sending StorageRemoved anyway
mDisableMtpObjectCallbacks = true;
Log.d(TAG, "deleting all entries for storage " + storage);
SQLiteDatabase db = database.getWritableDatabase();
// First clear the file path to disable the _DELETE_FILE database hook.
Expand All @@ -296,7 +293,7 @@ public void onReceive(Context context, Intent intent) {
ContentValues values = new ContentValues();
values.putNull(Files.FileColumns.DATA);
String where = FileColumns.STORAGE_ID + "=?";
String[] whereArgs = new String[] { Integer.toString(storage.getStorageId()) };
String[] whereArgs = new String[] { };
database.mNumUpdates++;
db.beginTransaction();
try {
Expand Down Expand Up @@ -324,17 +321,13 @@ public void onReceive(Context context, Intent intent) {
} finally {
context.sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED, uri));
mDisableMtpObjectCallbacks = false;
}
}
}
}
}
};

// set to disable sending events when the operation originates from MTP
private boolean mDisableMtpObjectCallbacks;

private final SQLiteDatabase.CustomFunction mObjectRemovedCallback =
new SQLiteDatabase.CustomFunction() {
@Override
Expand All @@ -345,18 +338,6 @@ public void callback(String[] args) {
// TODO: include the path in the callback and only remove the affected
// entry from the cache
mDirectoryCache.clear();
// do nothing if the operation originated from MTP
if (mDisableMtpObjectCallbacks) return;

Log.d(TAG, "object removed " + args[0]);
IMtpService mtpService = mMtpService;
if (mtpService != null) {
try {
sendObjectRemoved(Integer.parseInt(args[0]));
} catch (NumberFormatException e) {
Log.e(TAG, "NumberFormatException in mObjectRemovedCallback", e);
}
}
}
};

Expand Down Expand Up @@ -822,7 +803,7 @@ private static void createLatestSchema(SQLiteDatabase db, boolean internal) {
+ "is_alarm INTEGER,is_notification INTEGER,is_podcast INTEGER,album_artist TEXT,"
+ "duration INTEGER,bookmark INTEGER,artist TEXT,album TEXT,resolution TEXT,"
+ "tags TEXT,category TEXT,language TEXT,mini_thumb_data TEXT,name TEXT,"
+ "media_type INTEGER,old_id INTEGER,storage_id INTEGER,is_drm INTEGER,"
+ "media_type INTEGER,old_id INTEGER,is_drm INTEGER,"
+ "width INTEGER, height INTEGER, title_resource_uri TEXT)");
db.execSQL("CREATE TABLE log (time DATETIME, message TEXT)");
if (!internal) {
Expand Down Expand Up @@ -1809,32 +1790,6 @@ private ContentValues ensureFile(boolean internal, ContentValues initialValues,
return values;
}

private void sendObjectAdded(long objectHandle) {
synchronized (mMtpServiceConnection) {
if (mMtpService != null) {
try {
mMtpService.sendObjectAdded((int)objectHandle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in sendObjectAdded", e);
mMtpService = null;
}
}
}
}

private void sendObjectRemoved(long objectHandle) {
synchronized (mMtpServiceConnection) {
if (mMtpService != null) {
try {
mMtpService.sendObjectRemoved((int)objectHandle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in sendObjectRemoved", e);
mMtpService = null;
}
}
}
}

@Override
public int bulkInsert(Uri uri, ContentValues values[]) {
int match = URI_MATCHER.match(uri);
Expand Down Expand Up @@ -1878,13 +1833,6 @@ public int bulkInsert(Uri uri, ContentValues values[]) {
}
}

// Notify MTP (outside of successful transaction)
if (uri != null) {
if (uri.toString().startsWith("content://media/external/")) {
notifyMtp(notifyRowIds);
}
}

getContext().getContentResolver().notifyChange(uri, null);
return numInserted;
}
Expand All @@ -1895,11 +1843,6 @@ public Uri insert(Uri uri, ContentValues initialValues) {

ArrayList<Long> notifyRowIds = new ArrayList<Long>();
Uri newUri = insertInternal(uri, match, initialValues, notifyRowIds);
if (uri != null) {
if (uri.toString().startsWith("content://media/external/")) {
notifyMtp(notifyRowIds);
}
}

// do not signal notification for MTP objects.
// we will signal instead after file transfer is successful.
Expand All @@ -1921,13 +1864,6 @@ public Uri insert(Uri uri, ContentValues initialValues) {
return newUri;
}

private void notifyMtp(ArrayList<Long> rowIds) {
int size = rowIds.size();
for (int i = 0; i < size; i++) {
sendObjectAdded(rowIds.get(i).longValue());
}
}

private int playlistBulkInsert(SQLiteDatabase db, Uri uri, ContentValues values[]) {
DatabaseUtils.InsertHelper helper =
new DatabaseUtils.InsertHelper(db, "audio_playlists_map");
Expand Down Expand Up @@ -1971,14 +1907,12 @@ private long insertDirectory(DatabaseHelper helper, SQLiteDatabase db, String pa
values.put(FileColumns.FORMAT, MtpConstants.FORMAT_ASSOCIATION);
values.put(FileColumns.DATA, path);
values.put(FileColumns.PARENT, getParent(helper, db, path));
values.put(FileColumns.STORAGE_ID, getStorageId(path));
File file = new File(path);
if (file.exists()) {
values.put(FileColumns.DATE_MODIFIED, file.lastModified() / 1000);
}
helper.mNumInserts++;
long rowId = db.insert("files", FileColumns.DATE_MODIFIED, values);
sendObjectAdded(rowId);
return rowId;
}

Expand Down Expand Up @@ -2027,17 +1961,6 @@ private long getParent(DatabaseHelper helper, SQLiteDatabase db, String path) {
}
}

private int getStorageId(String path) {
final StorageManager storage = getContext().getSystemService(StorageManager.class);
final StorageVolume vol = storage.getStorageVolume(new File(path));
if (vol != null) {
return vol.getStorageId();
} else {
Log.w(TAG, "Missing volume for " + path + "; assuming invalid");
return StorageVolume.STORAGE_ID_INVALID;
}
}

/**
* Localizable titles conform to this URI pattern:
* Scheme: {@link ContentResolver.SCHEME_ANDROID_RESOURCE}
Expand Down Expand Up @@ -2337,11 +2260,6 @@ private long insertFile(DatabaseHelper helper, Uri uri, ContentValues initialVal
values.put(FileColumns.PARENT, parentId);
}
}
Integer storage = values.getAsInteger(FileColumns.STORAGE_ID);
if (storage == null) {
int storageId = getStorageId(path);
values.put(FileColumns.STORAGE_ID, storageId);
}

helper.mNumInserts++;
rowId = db.insert("files", FileColumns.DATE_MODIFIED, values);
Expand Down Expand Up @@ -3330,14 +3248,8 @@ public int delete(Uri uri, String userWhere, String[] whereArgs) {
switch (match) {
case MTP_OBJECTS:
case MTP_OBJECTS_ID:
try {
// don't send objectRemoved event since this originated from MTP
mDisableMtpObjectCallbacks = true;
database.mNumDeletes++;
count = db.delete("files", tableAndWhere.where, whereArgs);
} finally {
mDisableMtpObjectCallbacks = false;
}
database.mNumDeletes++;
count = db.delete("files", tableAndWhere.where, whereArgs);
break;
case AUDIO_GENRES_ID_MEMBERS:
database.mNumDeletes++;
Expand Down Expand Up @@ -3430,8 +3342,7 @@ public int update(Uri uri, ContentValues initialValues, String userWhere,
// Is a rename operation
&& ((initialValues.size() == 1 && initialValues.containsKey(FileColumns.DATA))
// Is a move operation
|| (initialValues.size() == 3 && initialValues.containsKey(FileColumns.DATA)
&& initialValues.containsKey(FileColumns.STORAGE_ID)
|| (initialValues.size() == 2 && initialValues.containsKey(FileColumns.DATA)
&& initialValues.containsKey(FileColumns.PARENT)))) {
String oldPath = null;
String newPath = initialValues.getAsString(MediaStore.MediaColumns.DATA);
Expand Down
2 changes: 0 additions & 2 deletions src/com/android/providers/media/MtpReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.android.providers.media;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;
import android.mtp.MtpServer;

Expand Down
Loading

0 comments on commit ddf4110

Please sign in to comment.