Add support for backing up wallpapers.

This commit is contained in:
Greyson Parrelli
2024-09-20 12:24:57 -04:00
committed by GitHub
parent e14078d2ec
commit a7bdfb6d76
30 changed files with 907 additions and 410 deletions

View File

@@ -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
}
}

View File

@@ -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;
}
}
}

View 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
}
}
}