Browse Source

centralize mime type definitions

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 2 years ago
parent
commit
444ff05cf5

+ 3 - 1
app/src/main/java/com/nextcloud/talk/activities/TakePhotoActivity.java

@@ -67,6 +67,8 @@ import androidx.core.content.ContextCompat;
 import androidx.exifinterface.media.ExifInterface;
 import androidx.lifecycle.ViewModelProvider;
 
+import static com.nextcloud.talk.utils.Mimetype.IMAGE_JPEG;
+
 public class TakePhotoActivity extends AppCompatActivity {
 
     private static final String TAG = TakePhotoActivity.class.getSimpleName();
@@ -175,7 +177,7 @@ public class TakePhotoActivity extends AppCompatActivity {
                 });
                 binding.send.setOnClickListener((v) -> {
                     Uri uri = (Uri) binding.photoPreview.getTag();
-                    setResult(RESULT_OK, new Intent().setDataAndType(uri, "image/jpeg"));
+                    setResult(RESULT_OK, new Intent().setDataAndType(uri, IMAGE_JPEG));
                     binding.photoPreview.setTag(null);
                     finish();
                 });

+ 2 - 1
app/src/main/java/com/nextcloud/talk/components/filebrowser/models/BrowserFile.kt

@@ -36,6 +36,7 @@ import com.nextcloud.talk.components.filebrowser.models.properties.NCPreview
 import com.nextcloud.talk.components.filebrowser.models.properties.OCFavorite
 import com.nextcloud.talk.components.filebrowser.models.properties.OCId
 import com.nextcloud.talk.components.filebrowser.models.properties.OCSize
+import com.nextcloud.talk.utils.Mimetype.Companion.FOLDER
 import kotlinx.android.parcel.Parcelize
 import java.io.File
 
@@ -73,7 +74,7 @@ data class BrowserFile(
                 browserFile.isAllowedToReShare = true
             }
             if (TextUtils.isEmpty(browserFile.mimeType) && !browserFile.isFile) {
-                browserFile.mimeType = "inode/directory"
+                browserFile.mimeType = FOLDER
             }
 
             return browserFile

+ 2 - 1
app/src/main/java/com/nextcloud/talk/components/filebrowser/webdav/ReadFolderListingOperation.kt

@@ -44,6 +44,7 @@ import com.nextcloud.talk.dagger.modules.RestModule.MagicAuthenticator
 import com.nextcloud.talk.models.database.UserEntity
 import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem
 import com.nextcloud.talk.utils.ApiUtils
+import com.nextcloud.talk.utils.Mimetype.Companion.FOLDER
 import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
 import okhttp3.OkHttpClient
 import java.io.File
@@ -130,7 +131,7 @@ class ReadFolderListingOperation(okHttpClient: OkHttpClient, currentUser: UserEn
             remoteFileBrowserItem.isAllowedToReShare = true
         }
         if (TextUtils.isEmpty(remoteFileBrowserItem.mimeType) && !remoteFileBrowserItem.isFile) {
-            remoteFileBrowserItem.mimeType = "inode/directory"
+            remoteFileBrowserItem.mimeType = FOLDER
         }
 
         return remoteFileBrowserItem

+ 2 - 1
app/src/main/java/com/nextcloud/talk/controllers/ProfileController.kt

@@ -71,6 +71,7 @@ import com.nextcloud.talk.ui.dialog.ScopeDialog
 import com.nextcloud.talk.utils.ApiUtils
 import com.nextcloud.talk.utils.DisplayUtils
 import com.nextcloud.talk.utils.FileUtils
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_PREFIX
 import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_MIME_TYPE_FILTER
 import com.nextcloud.talk.utils.database.user.UserUtils
 import io.reactivex.Observer
@@ -482,7 +483,7 @@ class ProfileController : NewBaseController(R.layout.controller_profile) {
 
     private fun showBrowserScreen() {
         val bundle = Bundle()
-        bundle.putString(KEY_MIME_TYPE_FILTER, "image/")
+        bundle.putString(KEY_MIME_TYPE_FILTER, IMAGE_PREFIX)
 
         val avatarIntent = Intent(activity, RemoteFileBrowserActivity::class.java)
         avatarIntent.putExtras(bundle)

+ 2 - 1
app/src/main/java/com/nextcloud/talk/remotefilebrowser/adapters/RemoteFileBrowserItemsListViewHolder.kt

@@ -37,6 +37,7 @@ import com.nextcloud.talk.utils.ApiUtils
 import com.nextcloud.talk.utils.DateUtils.getLocalDateTimeStringFromTimestamp
 import com.nextcloud.talk.utils.DisplayUtils
 import com.nextcloud.talk.utils.DrawableUtils.getDrawableResourceIdForMimeType
+import com.nextcloud.talk.utils.Mimetype.Companion.FOLDER
 
 @AutoInjector(NextcloudTalkApplication::class)
 class RemoteFileBrowserItemsListViewHolder(
@@ -143,7 +144,7 @@ class RemoteFileBrowserItemsListViewHolder(
     }
 
     private fun calculateClickability(item: RemoteFileBrowserItem, selectableItem: Boolean) {
-        clickable = selectableItem || "inode/directory" == item.mimeType
+        clickable = selectableItem || FOLDER == item.mimeType
     }
 
     companion object {

+ 2 - 2
app/src/main/java/com/nextcloud/talk/remotefilebrowser/viewmodels/RemoteFileBrowserItemsViewModel.kt

@@ -29,6 +29,7 @@ import androidx.lifecycle.ViewModel
 import com.nextcloud.talk.remotefilebrowser.model.RemoteFileBrowserItem
 import com.nextcloud.talk.remotefilebrowser.repositories.RemoteFileBrowserItemsRepository
 import com.nextcloud.talk.utils.FileSortOrder
+import com.nextcloud.talk.utils.Mimetype.Companion.FOLDER
 import com.nextcloud.talk.utils.preferences.AppPreferences
 import io.reactivex.Observer
 import io.reactivex.android.schedulers.AndroidSchedulers
@@ -171,7 +172,7 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
     }
 
     fun onItemClicked(remoteFileBrowserItem: RemoteFileBrowserItem) {
-        if (remoteFileBrowserItem.mimeType == MIME_DIRECTORY) {
+        if (remoteFileBrowserItem.mimeType == FOLDER) {
             changePath(remoteFileBrowserItem.path!!)
         } else {
             toggleBrowserItemSelection(remoteFileBrowserItem.path!!)
@@ -225,6 +226,5 @@ class RemoteFileBrowserItemsViewModel @Inject constructor(
     companion object {
         private val TAG = RemoteFileBrowserItemsViewModel::class.simpleName
         private const val ROOT_PATH = "/"
-        private const val MIME_DIRECTORY = "inode/directory"
     }
 }

+ 11 - 6
app/src/main/java/com/nextcloud/talk/utils/DrawableUtils.kt

@@ -21,6 +21,11 @@
 package com.nextcloud.talk.utils
 
 import com.nextcloud.talk.R
+import com.nextcloud.talk.utils.Mimetype.Companion.AUDIO_PREFIX
+import com.nextcloud.talk.utils.Mimetype.Companion.FOLDER
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_PREFIX
+import com.nextcloud.talk.utils.Mimetype.Companion.TEXT_PREFIX
+import com.nextcloud.talk.utils.Mimetype.Companion.VIDEO_PREFIX
 import java.util.HashMap
 
 object DrawableUtils {
@@ -140,7 +145,7 @@ object DrawableUtils {
         drawableMap["web"] = R.drawable.ic_mimetype_text_code
         drawableMap["application/internet-shortcut"] = R.drawable.ic_mimetype_link
 
-        drawableMap["inode/directory"] = R.drawable.ic_mimetype_folder
+        drawableMap[FOLDER] = R.drawable.ic_mimetype_folder
         drawableMap["unknown"] = R.drawable.ic_mimetype_file
         drawableMap["application/pdf"] = R.drawable.ic_mimetype_application_pdf
 
@@ -149,7 +154,7 @@ object DrawableUtils {
         }
 
         if ("DIR" == localMimetype) {
-            localMimetype = "inode/directory"
+            localMimetype = FOLDER
             return drawableMap[localMimetype]!!
         }
 
@@ -157,19 +162,19 @@ object DrawableUtils {
             return drawableMap[localMimetype]!!
         }
 
-        if (localMimetype.startsWith("image/")) {
+        if (localMimetype.startsWith(IMAGE_PREFIX)) {
             return R.drawable.ic_mimetype_image
         }
 
-        if (localMimetype.startsWith("video/")) {
+        if (localMimetype.startsWith(VIDEO_PREFIX)) {
             return R.drawable.ic_mimetype_video
         }
 
-        if (localMimetype.startsWith("text/")) {
+        if (localMimetype.startsWith(TEXT_PREFIX)) {
             return R.drawable.ic_mimetype_text
         }
 
-        return if (localMimetype.startsWith("audio")) {
+        return if (localMimetype.startsWith(AUDIO_PREFIX)) {
             R.drawable.ic_mimetype_audio
         } else drawableMap["unknown"]!!
     }

+ 11 - 7
app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt

@@ -47,6 +47,10 @@ import com.nextcloud.talk.models.database.CapabilitiesUtil
 import com.nextcloud.talk.models.database.UserEntity
 import com.nextcloud.talk.models.json.chat.ChatMessage
 import com.nextcloud.talk.utils.AccountUtils.canWeOpenFilesApp
+import com.nextcloud.talk.utils.Mimetype.Companion.AUDIO_PREFIX
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_GIF
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_JPEG
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_PNG
 import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ACCOUNT
 import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FILE_ID
 import java.io.File
@@ -150,9 +154,9 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
                 "video/quicktime",
                 "video/ogg"
                 -> openMediaView(filename, mimetype)
-                "image/png",
-                "image/jpeg",
-                "image/gif"
+                IMAGE_PNG,
+                IMAGE_JPEG,
+                IMAGE_GIF
                 -> openImageView(filename, mimetype)
                 "text/markdown",
                 "text/plain"
@@ -249,8 +253,8 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
 
     fun isSupportedForInternalViewer(mimetype: String?): Boolean {
         return when (mimetype) {
-            "image/png", "image/jpeg",
-            "image/gif", "audio/mpeg",
+            IMAGE_PNG, IMAGE_JPEG,
+            IMAGE_GIF, "audio/mpeg",
             "audio/wav", "audio/ogg",
             "video/mp4", "video/quicktime",
             "video/ogg", "text/markdown",
@@ -260,7 +264,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
     }
 
     private fun isGif(mimetype: String): Boolean {
-        return "image/gif" == mimetype
+        return IMAGE_GIF == mimetype
     }
 
     private fun isMarkdown(mimetype: String): Boolean {
@@ -268,7 +272,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
     }
 
     private fun isAudioOnly(mimetype: String): Boolean {
-        return mimetype.startsWith("audio")
+        return mimetype.startsWith(AUDIO_PREFIX)
     }
 
     @SuppressLint("LongLogTag")

+ 4 - 1
app/src/main/java/com/nextcloud/talk/utils/ImageEmojiEditText.kt

@@ -29,6 +29,9 @@ import android.view.inputmethod.InputConnection
 import androidx.core.view.inputmethod.EditorInfoCompat
 import androidx.core.view.inputmethod.InputConnectionCompat
 import androidx.emoji.widget.EmojiEditText
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_GIF
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_JPEG
+import com.nextcloud.talk.utils.Mimetype.Companion.IMAGE_PNG
 
 /*
 Subclass of EmojiEditText with support for image keyboards - primarily for GIF handling. ;-)
@@ -48,7 +51,7 @@ class ImageEmojiEditText : EmojiEditText {
 
         val ic: InputConnection = super.onCreateInputConnection(editorInfo)
 
-        EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/gif", "image/jpeg", "image/png"))
+        EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf(IMAGE_GIF, IMAGE_JPEG, IMAGE_PNG))
 
         val callback =
             InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, _ ->

+ 36 - 0
app/src/main/java/com/nextcloud/talk/utils/Mimetype.kt

@@ -0,0 +1,36 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Andy Scherzinger
+ * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.utils
+
+class Mimetype {
+    companion object {
+        const val IMAGE_PREFIX = "image/"
+        const val VIDEO_PREFIX = "video/"
+        const val TEXT_PREFIX = "text/"
+        const val AUDIO_PREFIX = "audio"
+
+        const val FOLDER = "inode/directory"
+
+        const val IMAGE_PNG = "image/png"
+        const val IMAGE_JPEG = "image/jpeg"
+        const val IMAGE_GIF = "image/gif"
+    }
+}