From 483cb7023c9cbb536978d487e4d9a4efb27a32d7 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Sat, 24 Dec 2022 05:47:41 +0800 Subject: Allow importing theme as any text (#1275) * Allow importing theme as any text Browsers copy content as text/html and it caused theme import to fail * Coerce clipboard data to text Clipboard can have mime type of text even when it doesn't actually contain text. For example it can contain uri to text content provider. Using `coerceToText` automatically handles all cases and returns best text representation of content. --- .../activities/CustomThemeListingActivity.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java index c18fd0fc..3aeb102a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java @@ -8,6 +8,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; +import android.text.TextUtils; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; @@ -269,16 +270,20 @@ public class CustomThemeListingActivity extends BaseActivity implements if (!clipboard.hasPrimaryClip()) { Snackbar.make(coordinatorLayout, R.string.no_data_in_clipboard, Snackbar.LENGTH_SHORT).show(); } else if (clipboard.getPrimaryClipDescription() != null && - !clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) { - // since the clipboard has data but it is not plain text + !clipboard.getPrimaryClipDescription().hasMimeType("text/*")) { + // since the clipboard has data but it is not text Snackbar.make(coordinatorLayout, R.string.no_data_in_clipboard, Snackbar.LENGTH_SHORT).show(); } else if (clipboard.getPrimaryClip() != null) { ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); - String json = item.getText().toString(); - try { - CustomTheme customTheme = new Gson().fromJson(json, CustomTheme.class); - checkDuplicateAndImportTheme(customTheme, true); - } catch (JsonSyntaxException e) { + String json = item.coerceToText(this.getApplicationContext()).toString(); + if (!TextUtils.isEmpty(json)) { + try { + CustomTheme customTheme = new Gson().fromJson(json, CustomTheme.class); + checkDuplicateAndImportTheme(customTheme, true); + } catch (JsonSyntaxException e) { + Snackbar.make(coordinatorLayout, R.string.parse_theme_failed, Snackbar.LENGTH_SHORT).show(); + } + } else { Snackbar.make(coordinatorLayout, R.string.parse_theme_failed, Snackbar.LENGTH_SHORT).show(); } } -- cgit v1.2.3