Merge "Override MIME type to match MediaStore behavior." into rvc-dev am: 38a6333ff6
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11962282 Change-Id: I2c67138e54e1fb6c5149317c960a2727aef64e98
This commit is contained in:
@@ -25,6 +25,7 @@ import android.annotation.SystemApi;
|
|||||||
import android.annotation.SystemService;
|
import android.annotation.SystemService;
|
||||||
import android.annotation.TestApi;
|
import android.annotation.TestApi;
|
||||||
import android.compat.annotation.UnsupportedAppUsage;
|
import android.compat.annotation.UnsupportedAppUsage;
|
||||||
|
import android.content.ClipDescription;
|
||||||
import android.content.ContentProviderClient;
|
import android.content.ContentProviderClient;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
@@ -50,11 +51,13 @@ import android.provider.Settings.SettingNotFoundException;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.LongSparseArray;
|
import android.util.LongSparseArray;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
import android.webkit.MimeTypeMap;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The download manager is a system service that handles long-running HTTP downloads. Clients may
|
* The download manager is a system service that handles long-running HTTP downloads. Clients may
|
||||||
@@ -1554,6 +1557,7 @@ public class DownloadManager {
|
|||||||
values.put(Downloads.Impl.COLUMN_DESTINATION,
|
values.put(Downloads.Impl.COLUMN_DESTINATION,
|
||||||
Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
|
Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD);
|
||||||
values.put(Downloads.Impl._DATA, path);
|
values.put(Downloads.Impl._DATA, path);
|
||||||
|
values.put(Downloads.Impl.COLUMN_MIME_TYPE, resolveMimeType(new File(path)));
|
||||||
values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
|
values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS);
|
||||||
values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
|
values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length);
|
||||||
values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
|
values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED,
|
||||||
@@ -1569,6 +1573,58 @@ public class DownloadManager {
|
|||||||
return Long.parseLong(downloadUri.getLastPathSegment());
|
return Long.parseLong(downloadUri.getLastPathSegment());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shamelessly borrowed from
|
||||||
|
* {@code packages/providers/MediaProvider/src/com/android/providers/media/util/MimeUtils.java}
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
private static @NonNull String resolveMimeType(@NonNull File file) {
|
||||||
|
final String extension = extractFileExtension(file.getPath());
|
||||||
|
if (extension == null) return ClipDescription.MIMETYPE_UNKNOWN;
|
||||||
|
|
||||||
|
final String mimeType = MimeTypeMap.getSingleton()
|
||||||
|
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
|
||||||
|
if (mimeType == null) return ClipDescription.MIMETYPE_UNKNOWN;
|
||||||
|
|
||||||
|
return mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shamelessly borrowed from
|
||||||
|
* {@code packages/providers/MediaProvider/src/com/android/providers/media/util/FileUtils.java}
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
private static @Nullable String extractDisplayName(@Nullable String data) {
|
||||||
|
if (data == null) return null;
|
||||||
|
if (data.indexOf('/') == -1) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
if (data.endsWith("/")) {
|
||||||
|
data = data.substring(0, data.length() - 1);
|
||||||
|
}
|
||||||
|
return data.substring(data.lastIndexOf('/') + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shamelessly borrowed from
|
||||||
|
* {@code packages/providers/MediaProvider/src/com/android/providers/media/util/FileUtils.java}
|
||||||
|
*
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
private static @Nullable String extractFileExtension(@Nullable String data) {
|
||||||
|
if (data == null) return null;
|
||||||
|
data = extractDisplayName(data);
|
||||||
|
|
||||||
|
final int lastDot = data.lastIndexOf('.');
|
||||||
|
if (lastDot == -1) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return data.substring(lastDot + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
|
private static final String NON_DOWNLOADMANAGER_DOWNLOAD =
|
||||||
"non-dwnldmngr-download-dont-retry2download";
|
"non-dwnldmngr-download-dont-retry2download";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user