Fix #841 "Link" functionality in text menu causes error

This commit is contained in:
Stefan Niedermann 2020-05-26 12:59:08 +02:00
parent 85021909f3
commit 81f71ca574
2 changed files with 27 additions and 11 deletions

View file

@ -3,6 +3,7 @@ package it.niedermann.owncloud.notes.util;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@ -11,7 +12,6 @@ import androidx.annotation.Nullable;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Objects;
import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.R;
@ -44,15 +44,30 @@ public class ClipboardUtil {
} }
public static String getClipboardURLorNull(Context context) { public static String getClipboardURLorNull(Context context) {
String clipboardURL = null; final ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
ClipData clipboardData = Objects.requireNonNull(((ClipboardManager) Objects.requireNonNull(context.getSystemService(CLIPBOARD_SERVICE))).getPrimaryClip()); if (clipboardManager == null) {
if (clipboardData.getItemCount() > 0) { return null;
try {
clipboardURL = new URL(clipboardData.getItemAt(0).getText().toString()).toString();
} catch (MalformedURLException e) {
Log.d(TAG, "Clipboard does not contain a valid URL: " + clipboardData.getItemAt(0).getText().toString());
}
} }
return clipboardURL; final ClipData clipboardData = clipboardManager.getPrimaryClip();
if (clipboardData == null) {
return null;
}
if (clipboardData.getItemCount() < 1) {
return null;
}
final ClipData.Item clipItem = clipboardData.getItemAt(0);
if (clipItem == null) {
return null;
}
CharSequence clipText = clipItem.getText();
if (TextUtils.isEmpty(clipText)) {
return null;
}
try {
return new URL(clipText.toString()).toString();
} catch (MalformedURLException e) {
Log.d(TAG, "Clipboard does not contain a valid URL: " + clipText);
}
return null;
} }
} }

View file

@ -1 +1,2 @@
- Note list widget should create a note in it's respective category (#817) - Note list widget should create a note in it's respective category (#817)
- "Link" functionality in text menu causes error (#841)