Эх сурвалжийг харах

Check ClassCastException edge, nullability of bundle or intent

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 жил өмнө
parent
commit
573e9d6580

+ 35 - 12
app/src/main/java/com/nextcloud/utils/extensions/BundleExtensions.kt

@@ -24,22 +24,45 @@ package com.nextcloud.utils.extensions
 import android.os.Build
 import android.os.Bundle
 import android.os.Parcelable
+import com.owncloud.android.lib.common.utils.Log_OC
+import org.apache.commons.logging.Log
 import java.io.Serializable
 
-fun <T : Serializable?> Bundle.getSerializableArgument(key: String, type: Class<T>): T? {
-    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
-        this.getSerializable(key, type)
-    } else {
-        @Suppress("UNCHECKED_CAST", "DEPRECATION")
-        this.getSerializable(key) as T
+private const val tag = "BundleExtension"
+
+fun <T : Serializable?> Bundle?.getSerializableArgument(key: String, type: Class<T>): T? {
+    if (this == null) {
+        return null
+    }
+
+    return try {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            this.getSerializable(key, type)
+        } else {
+            @Suppress("UNCHECKED_CAST", "DEPRECATION")
+            this.getSerializable(key) as T
+        }
+    } catch (e: ClassCastException) {
+        Log_OC.e(tag, e.localizedMessage)
+        null
     }
 }
 
-fun <T : Parcelable?> Bundle.getParcelableArgument(key: String, type: Class<T>): T? {
-    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
-        this.getParcelable(key, type)
-    } else {
-        @Suppress("DEPRECATION")
-        this.getParcelable(key)
+fun <T : Parcelable?> Bundle?.getParcelableArgument(key: String, type: Class<T>): T? {
+    if (this == null) {
+        return null
+    }
+
+    return try {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            this.getParcelable(key, type)
+        } else {
+            @Suppress("DEPRECATION")
+            this.getParcelable(key)
+        }
+    } catch (e: ClassCastException) {
+        Log_OC.e(tag, e.localizedMessage)
+        e.printStackTrace()
+        null
     }
 }

+ 33 - 12
app/src/main/java/com/nextcloud/utils/extensions/IntentExtensions.kt

@@ -24,22 +24,43 @@ package com.nextcloud.utils.extensions
 import android.content.Intent
 import android.os.Build
 import android.os.Parcelable
+import com.owncloud.android.lib.common.utils.Log_OC
 import java.io.Serializable
 
-fun <T : Serializable?> Intent.getSerializableArgument(key: String, type: Class<T>): T? {
-    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
-        this.getSerializableExtra(key, type)
-    } else {
-        @Suppress("UNCHECKED_CAST", "DEPRECATION")
-        this.getSerializableExtra(key) as T
+private const val tag = "IntentExtension"
+
+fun <T : Serializable?> Intent?.getSerializableArgument(key: String, type: Class<T>): T? {
+    if (this == null) {
+        return null
+    }
+
+    return try {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            this.getSerializableExtra(key, type)
+        } else {
+            @Suppress("UNCHECKED_CAST", "DEPRECATION")
+            this.getSerializableExtra(key) as T
+        }
+    } catch (e: ClassCastException) {
+        Log_OC.e(tag, e.localizedMessage)
+        null
     }
 }
 
-fun <T : Parcelable?> Intent.getParcelableArgument(key: String, type: Class<T>): T? {
-    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
-        this.getParcelableExtra(key, type)
-    } else {
-        @Suppress("DEPRECATION")
-        this.getParcelableExtra(key)
+fun <T : Parcelable?> Intent?.getParcelableArgument(key: String, type: Class<T>): T? {
+    if (this == null) {
+        return null
+    }
+
+    return try {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            this.getParcelableExtra(key, type)
+        } else {
+            @Suppress("DEPRECATION")
+            this.getParcelableExtra(key)
+        }
+    } catch (e: ClassCastException) {
+        Log_OC.e(tag, e.localizedMessage)
+        null
     }
 }