diff options
author | 2019-02-19 14:43:04 -0700 | |
---|---|---|
committer | 2019-02-20 18:17:51 -0700 | |
commit | 6396c47c8b3c7a99a729c7bc75190b154b4d8029 (patch) | |
tree | 65537698383ad9fc0e77f0fd2aebee7c25dc2f5b | |
parent | 2c0b485dd056da2d41841bb58488942ea413739d (diff) |
Flesh out CR.getTypeDrawable() with more details.
Providing icons for MIME types is generally useful, but we also
need to provide a label and content description. This interface
can be extended internally to surface details about special MIME
types, such as "image/vnd.google.panorama360+jpg".
Bug: 122887179, 120791890
Test: atest android.content.cts.ContentResolverTest
Change-Id: If62ee1a628feb909cd7b30ecf6e61f6845112d82
-rw-r--r-- | res/values/mimes.xml | 69 | ||||
-rw-r--r-- | src/com/android/documentsui/FileTypeMap.java | 135 | ||||
-rw-r--r-- | src/com/android/documentsui/IconUtils.java | 3 | ||||
-rw-r--r-- | tests/unit/com/android/documentsui/FileTypeMapTest.java | 124 |
4 files changed, 7 insertions, 324 deletions
diff --git a/res/values/mimes.xml b/res/values/mimes.xml deleted file mode 100644 index 05597418c..000000000 --- a/res/values/mimes.xml +++ /dev/null @@ -1,69 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- - ~ Copyright (C) 2017 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. - --> - -<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> - <!-- Generic file type with an extention shown in type column in list view. [CHAR LIMIT=30] --> - <string name="generic_extention_file_type"><xliff:g id="extension" example="APE">%1$s</xliff:g> file</string> - <!-- Generic file type without an extention shown in type column in list view. [CHAR LIMIT=30] --> - <string name="generic_file_type">File</string> - <!-- Generic image file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="image_file_type">Image</string> - <!-- Image file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="image_extension_file_type"><xliff:g id="fileType" example="JPG">%1$s</xliff:g> image</string> - <!-- Generic audio file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="audio_file_type">Audio</string> - <!-- Audio file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="audio_extension_file_type"><xliff:g id="fileType" example="MP3">%1$s</xliff:g> audio</string> - <!-- Generic video file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="video_file_type">Video</string> - <!-- Video file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="video_extension_file_type"><xliff:g id="fileType" example="AVI">%1$s</xliff:g> video</string> - <!-- Archive file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="archive_file_type"><xliff:g id="fileType" example="Zip">%1$s</xliff:g> archive</string> - <!-- Android application file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="apk_file_type">Android application</string> - <!-- Plain text file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="txt_file_type">Plain text</string> - <!-- HTML file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="html_file_type">HTML document</string> - <!-- PDF file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="pdf_file_type">PDF document</string> - <!-- Word document file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="word_file_type">Word document</string> - <!-- PowerPoint presentation file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="ppt_file_type">PowerPoint presentation</string> - <!-- Excel spreadsheet file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="excel_file_type">Excel spreadsheet</string> - <!-- Google document file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gdoc_file_type">Google document</string> - <!-- Google spreadsheet file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gsheet_file_type">Google spreadsheet</string> - <!-- Google presentation file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gslides_file_type">Google presentation</string> - <!-- Google drawing file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gdraw_file_type">Google drawing</string> - <!-- Google table file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gtable_file_type">Google table</string> - <!-- Google form file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gform_file_type">Google form</string> - <!-- Google map file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gmap_file_type">Google map</string> - <!-- Google site file type shown in type column in list view. [CHAR LIMIT=30] --> - <string name="gsite_file_type">Google site</string> - <!-- Generic directory type shown when viewing a folder in the properties view --> - <string name="directory_type">Folder</string> -</resources> diff --git a/src/com/android/documentsui/FileTypeMap.java b/src/com/android/documentsui/FileTypeMap.java index 02012848a..fbad7646e 100644 --- a/src/com/android/documentsui/FileTypeMap.java +++ b/src/com/android/documentsui/FileTypeMap.java @@ -16,149 +16,24 @@ package com.android.documentsui; -import androidx.annotation.StringRes; +import android.content.ContentResolver; import android.content.Context; -import android.content.res.Resources; -import android.text.TextUtils; -import android.util.Log; -import android.util.SparseArray; -import android.webkit.MimeTypeMap; import com.android.documentsui.base.Lookup; -import com.android.documentsui.base.MimeTypes; - -import java.util.HashMap; /** * A map from mime type to user friendly type string. */ public class FileTypeMap implements Lookup<String, String> { - - private static final String TAG = "FileTypeMap"; - - private final Resources mRes; - - private final SparseArray<Integer> mMediaTypeStringMap = new SparseArray<>(); - - private final HashMap<String, Integer> mFileTypeMap = new HashMap<>(); - private final HashMap<String, String> mArchiveTypeMap = new HashMap<>(); - private final HashMap<String, Integer> mSpecialMediaMimeType = new HashMap<>(); + private final ContentResolver mResolver; FileTypeMap(Context context) { - mRes = context.getResources(); - - // Mapping from generic media type string to extension media type string - mMediaTypeStringMap.put(R.string.video_file_type, R.string.video_extension_file_type); - mMediaTypeStringMap.put(R.string.audio_file_type, R.string.audio_extension_file_type); - mMediaTypeStringMap.put(R.string.image_file_type, R.string.image_extension_file_type); - - // Common file types - mFileTypeMap.put(MimeTypes.APK_TYPE, R.string.apk_file_type); - mFileTypeMap.put("text/plain", R.string.txt_file_type); - mFileTypeMap.put("text/html", R.string.html_file_type); - mFileTypeMap.put("application/xhtml+xml", R.string.html_file_type); - mFileTypeMap.put("application/pdf", R.string.pdf_file_type); - - // MS file types - mFileTypeMap.put("application/msword", R.string.word_file_type); - mFileTypeMap.put( - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - R.string.word_file_type); - mFileTypeMap.put("application/vnd.ms-powerpoint", R.string.ppt_file_type); - mFileTypeMap.put( - "application/vnd.openxmlformats-officedocument.presentationml.presentation", - R.string.ppt_file_type); - mFileTypeMap.put("application/vnd.ms-excel", R.string.excel_file_type); - mFileTypeMap.put( - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - R.string.excel_file_type); - - // Google doc types - mFileTypeMap.put("application/vnd.google-apps.document", R.string.gdoc_file_type); - mFileTypeMap.put("application/vnd.google-apps.spreadsheet", R.string.gsheet_file_type); - mFileTypeMap.put("application/vnd.google-apps.presentation", R.string.gslides_file_type); - mFileTypeMap.put("application/vnd.google-apps.drawing", R.string.gdraw_file_type); - mFileTypeMap.put("application/vnd.google-apps.fusiontable", R.string.gtable_file_type); - mFileTypeMap.put("application/vnd.google-apps.form", R.string.gform_file_type); - mFileTypeMap.put("application/vnd.google-apps.map", R.string.gmap_file_type); - mFileTypeMap.put("application/vnd.google-apps.sites", R.string.gsite_file_type); - - // Directory type - mFileTypeMap.put("vnd.android.document/directory", R.string.directory_type); - - // Archive types - mArchiveTypeMap.put("application/rar", "RAR"); - mArchiveTypeMap.put("application/zip", "Zip"); - mArchiveTypeMap.put("application/x-tar", "Tar"); - mArchiveTypeMap.put("application/gzip", "Gzip"); - mArchiveTypeMap.put("application/x-7z-compressed", "7z"); - mArchiveTypeMap.put("application/x-rar-compressed", "RAR"); - - // Special media mime types - mSpecialMediaMimeType.put("application/ogg", R.string.audio_file_type); - mSpecialMediaMimeType.put("application/x-flac", R.string.audio_file_type); + mResolver = context.getContentResolver(); } @Override public String lookup(String mimeType) { - if (mFileTypeMap.containsKey(mimeType)) { - return getPredefinedFileTypeString(mimeType); - } - - if (mArchiveTypeMap.containsKey(mimeType)) { - return buildArchiveTypeString(mimeType); - } - - if (mSpecialMediaMimeType.containsKey(mimeType)) { - int genericType = mSpecialMediaMimeType.get(mimeType); - return getFileTypeString(mimeType, mMediaTypeStringMap.get(genericType), genericType); - } - - final String[] type = MimeTypes.splitMimeType(mimeType); - if (type == null) { - Log.w(TAG, "Unexpected mime type " + mimeType); - return getGenericFileTypeString(); - } - - switch (type[0]) { - case MimeTypes.IMAGE_PREFIX: - return getFileTypeString( - mimeType, R.string.image_extension_file_type, R.string.image_file_type); - case MimeTypes.AUDIO_PREFIX: - return getFileTypeString( - mimeType, R.string.audio_extension_file_type, R.string.audio_file_type); - case MimeTypes.VIDEO_PREFIX: - return getFileTypeString( - mimeType, R.string.video_extension_file_type, R.string.video_file_type); - default: - return getFileTypeString( - mimeType, R.string.generic_extention_file_type, R.string.generic_file_type); - } - } - - private String buildArchiveTypeString(String mimeType) { - final String archiveType = mArchiveTypeMap.get(mimeType); - - assert(!TextUtils.isEmpty(archiveType)); - - final String format = mRes.getString(R.string.archive_file_type); - return String.format(format, archiveType); - } - - private String getPredefinedFileTypeString(String mimeType) { - return mRes.getString(mFileTypeMap.get(mimeType)); - } - - private String getFileTypeString( - String mimeType, @StringRes int formatStringId, @StringRes int defaultStringId) { - final String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); - - return TextUtils.isEmpty(extension) - ? mRes.getString(defaultStringId) - : String.format(mRes.getString(formatStringId), extension.toUpperCase()); - } - - private String getGenericFileTypeString() { - return mRes.getString(R.string.generic_file_type); + if (mimeType == null) return null; + return String.valueOf(mResolver.getTypeInfo(mimeType).getLabel()); } } diff --git a/src/com/android/documentsui/IconUtils.java b/src/com/android/documentsui/IconUtils.java index 4a67375b5..46d032124 100644 --- a/src/com/android/documentsui/IconUtils.java +++ b/src/com/android/documentsui/IconUtils.java @@ -52,7 +52,8 @@ public class IconUtils { * @return drawable of mime type files from system default */ public static Drawable loadMimeIcon(Context context, String mimeType) { - return context.getContentResolver().getTypeDrawable(mimeType); + if (mimeType == null) return null; + return context.getContentResolver().getTypeInfo(mimeType).getIcon().loadDrawable(context); } public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) { diff --git a/tests/unit/com/android/documentsui/FileTypeMapTest.java b/tests/unit/com/android/documentsui/FileTypeMapTest.java deleted file mode 100644 index 1d71431b2..000000000 --- a/tests/unit/com/android/documentsui/FileTypeMapTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (C) 2017 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.documentsui; - -import static junit.framework.Assert.assertEquals; - -import android.content.Context; -import android.content.res.Resources; - -import androidx.annotation.StringRes; -import androidx.test.InstrumentationRegistry; -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import libcore.net.MimeUtils; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class FileTypeMapTest { - - private Resources mRes; - private FileTypeMap mMap; - - @Before - public void setUp() { - Context context = InstrumentationRegistry.getTargetContext(); - mRes = context.getResources(); - mMap = new FileTypeMap(context); - } - - @Test - public void testPlainTextType() { - String expected = mRes.getString(R.string.txt_file_type); - assertEquals(expected, mMap.lookup("text/plain")); - } - - @Test - public void testPortableDocumentFormatType() { - String expected = mRes.getString(R.string.pdf_file_type); - assertEquals(expected, mMap.lookup("application/pdf")); - } - - @Test - public void testMsWordType() { - String expected = mRes.getString(R.string.word_file_type); - assertEquals(expected, mMap.lookup("application/msword")); - } - - @Test - public void testGoogleDocType() { - String expected = mRes.getString(R.string.gdoc_file_type); - assertEquals(expected, mMap.lookup("application/vnd.google-apps.document")); - } - - @Test - public void testZipType() { - final String mime = "application/zip"; - String expected = getExtensionTypeFromExtension(R.string.archive_file_type, "Zip"); - assertEquals(expected, mMap.lookup(mime)); - } - - @Test - public void testMp3Type() { - final String mime = "audio/mpeg"; - String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime); - assertEquals(expected, mMap.lookup(mime)); - } - - @Test - public void testMkvType() { - final String mime = "video/avi"; - String expected = getExtensionTypeFromMime(R.string.video_extension_file_type, mime); - assertEquals(expected, mMap.lookup(mime)); - } - - @Test - public void testJpgType() { - final String mime = "image/jpeg"; - String expected = getExtensionTypeFromMime(R.string.image_extension_file_type, mime); - assertEquals(expected, mMap.lookup(mime)); - } - - @Test - public void testOggType() { - final String mime = "application/ogg"; - String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime); - assertEquals(expected, mMap.lookup("application/ogg")); - } - - @Test - public void testFlacType() { - final String mime = "application/x-flac"; - String expected = getExtensionTypeFromMime(R.string.audio_extension_file_type, mime); - assertEquals(expected, mMap.lookup(mime)); - } - - private String getExtensionTypeFromMime(@StringRes int formatStringId, String mime) { - final String extension = MimeUtils.guessExtensionFromMimeType(mime).toUpperCase(); - return getExtensionTypeFromExtension(formatStringId, extension); - } - - private String getExtensionTypeFromExtension(@StringRes int formatStringId, String extension) { - final String format = mRes.getString(formatStringId); - return String.format(format, extension); - } -} |