mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 01:40:07 +01:00
Add support for backing up wallpapers.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.util
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException
|
||||
import com.squareup.wire.ProtoAdapter
|
||||
|
||||
/**
|
||||
* Performs the common pattern of attempting to decode a serialized proto and returning null if it fails to decode.
|
||||
*/
|
||||
fun <E> ProtoAdapter<E>.decodeOrNull(serialized: ByteArray): E? {
|
||||
return try {
|
||||
this.decode(serialized)
|
||||
} catch (e: InvalidProtocolBufferException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public final class UriUtil {
|
||||
|
||||
/**
|
||||
* Ensures that an external URI is valid and doesn't contain any references to internal files or
|
||||
* any other trickiness.
|
||||
*/
|
||||
public static boolean isValidExternalUri(@NonNull Context context, @NonNull Uri uri) {
|
||||
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
|
||||
try {
|
||||
File file = new File(uri.getPath());
|
||||
|
||||
return file.getCanonicalPath().equals(file.getPath()) &&
|
||||
!file.getCanonicalPath().startsWith("/data") &&
|
||||
!file.getCanonicalPath().contains(context.getPackageName());
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
app/src/main/java/org/thoughtcrime/securesms/util/UriUtil.kt
Normal file
47
app/src/main/java/org/thoughtcrime/securesms/util/UriUtil.kt
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.util
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
||||
object UriUtil {
|
||||
|
||||
/**
|
||||
* Ensures that an external URI is valid and doesn't contain any references to internal files or
|
||||
* any other trickiness.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun isValidExternalUri(context: Context, uri: Uri): Boolean {
|
||||
if (ContentResolver.SCHEME_FILE == uri.scheme) {
|
||||
try {
|
||||
val file = File(uri.path)
|
||||
|
||||
return file.canonicalPath == file.path &&
|
||||
!file.canonicalPath.startsWith("/data") &&
|
||||
!file.canonicalPath.contains(context.packageName)
|
||||
} catch (e: IOException) {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string to a URI if it's valid, otherwise null.
|
||||
*/
|
||||
fun parseOrNull(uri: String): Uri? {
|
||||
return try {
|
||||
Uri.parse(uri)
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user