Fix clipboard issues
This commit is contained in:
8 files changed
+121
-85
No files matched your search
@@ -54,6 +54,7 @@ import com.hippo.network.StatusCodeException;
|
||||
import com.hippo.text.Html;
|
||||
import com.hippo.unifile.UniFile;
|
||||
import com.hippo.util.BitmapUtils;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
import com.hippo.util.ExceptionUtils;
|
||||
import com.hippo.util.IoThreadPoolExecutor;
|
||||
import com.hippo.util.ReadableTime;
|
||||
@@ -300,6 +301,7 @@ public class EhApplication extends RecordingApplication {
|
||||
|
||||
super.onCreate();
|
||||
|
||||
ClipboardUtil.initialize(this);
|
||||
GetText.initialize(this);
|
||||
StatusCodeException.initialize(this);
|
||||
Settings.initialize(this);
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.hippo.ehviewer.preference;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Toast;
|
||||
@@ -30,6 +28,7 @@ import com.hippo.ehviewer.client.EhCookieStore;
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
import com.hippo.preference.MessagePreference;
|
||||
import com.hippo.text.Html;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -98,8 +97,7 @@ public class IdentityCookiePreference extends MessagePreference {
|
||||
super.onPrepareDialogBuilder(builder);
|
||||
if (message != null) {
|
||||
builder.setPositiveButton(R.string.settings_eh_identity_cookies_copy, (dialog, which) -> {
|
||||
ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
cmb.setPrimaryClip(ClipData.newPlainText(null, message));
|
||||
ClipboardUtil.addTextToClipboard(message);
|
||||
Toast.makeText(getContext(), R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
|
||||
|
||||
IdentityCookiePreference.this.onClick(dialog, which);
|
||||
|
||||
@@ -16,9 +16,6 @@
|
||||
|
||||
package com.hippo.ehviewer.ui;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
@@ -83,6 +80,7 @@ import com.hippo.scene.SceneFragment;
|
||||
import com.hippo.scene.StageActivity;
|
||||
import com.hippo.unifile.UniFile;
|
||||
import com.hippo.util.BitmapUtils;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
import com.hippo.widget.LoadImageView;
|
||||
import com.hippo.yorozuya.IOUtils;
|
||||
import com.hippo.yorozuya.SimpleHandler;
|
||||
@@ -410,17 +408,6 @@ public final class MainActivity extends StageActivity
|
||||
return topClass == null || SolidScene.class.isAssignableFrom(topClass);
|
||||
}
|
||||
|
||||
private String getTextFromClipboard() {
|
||||
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipboard != null) {
|
||||
ClipData clip = clipboard.getPrimaryClip();
|
||||
if (clip != null && clip.getItemCount() > 0 && clip.getItemAt(0).getText() != null) {
|
||||
return clip.getItemAt(0).getText().toString();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Announcer createAnnouncerFromClipboardUrl(String url) {
|
||||
GalleryDetailUrlParser.Result result1 = GalleryDetailUrlParser.parse(url, false);
|
||||
@@ -446,7 +433,7 @@ public final class MainActivity extends StageActivity
|
||||
}
|
||||
|
||||
private void checkClipboardUrlInternal() {
|
||||
String text = getTextFromClipboard();
|
||||
String text = ClipboardUtil.getTextFromClipboard();
|
||||
int hashCode = text != null ? text.hashCode() : 0;
|
||||
|
||||
if (text != null && hashCode != 0 && Settings.getClipboardTextHashCode() != hashCode) {
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.hippo.ehviewer.ui.scene;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Paint;
|
||||
@@ -40,6 +38,8 @@ import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.client.EhCookieStore;
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
import com.hippo.ehviewer.client.EhUtils;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
import com.hippo.util.ExceptionUtils;
|
||||
import com.hippo.yorozuya.AssertUtils;
|
||||
import com.hippo.yorozuya.ViewUtils;
|
||||
|
||||
@@ -67,8 +67,6 @@ public class CookieSignInScene extends SolidScene implements EditText.OnEditorAc
|
||||
private View mOk;
|
||||
@Nullable
|
||||
private TextView mFromClipboard;
|
||||
@Nullable
|
||||
private ClipboardManager clipboardManager;
|
||||
|
||||
// false for error
|
||||
private static boolean checkIpbMemberId(String id) {
|
||||
@@ -159,7 +157,6 @@ public class CookieSignInScene extends SolidScene implements EditText.OnEditorAc
|
||||
@Override
|
||||
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
clipboardManager = (ClipboardManager) requireActivity().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
showSoftInput(mIpbMemberId);
|
||||
}
|
||||
|
||||
@@ -256,65 +253,60 @@ public class CookieSignInScene extends SolidScene implements EditText.OnEditorAc
|
||||
|
||||
private void fillCookiesFromClipboard() {
|
||||
hideSoftInput();
|
||||
String text = ClipboardUtil.getTextFromClipboard();
|
||||
if (text == null) {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String pasteData;
|
||||
if (!clipboardManager.hasPrimaryClip()/* || !clipboardManager.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)*/) {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
String[] kvs;
|
||||
if (text.contains(";")) {
|
||||
kvs = text.split(";");
|
||||
} else if (text.contains("\n")) {
|
||||
kvs = text.split("\n");
|
||||
} else {
|
||||
ClipData.Item item = clipboardManager.getPrimaryClip().getItemAt(0);
|
||||
pasteData = String.valueOf(item.coerceToText(requireContext()));
|
||||
if (!TextUtils.isEmpty(pasteData)) {
|
||||
String[] kvs;
|
||||
if (pasteData.contains(";")) {
|
||||
kvs = pasteData.split(";");
|
||||
} else if (pasteData.contains("\n")) {
|
||||
kvs = pasteData.split("\n");
|
||||
} else {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
return;
|
||||
}
|
||||
if (kvs.length < 3) {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
return;
|
||||
}
|
||||
for (String s : kvs) {
|
||||
String[] kv;
|
||||
if (s.contains("=")) {
|
||||
kv = s.split("=");
|
||||
} else if (s.contains(":")) {
|
||||
kv = s.split(":");
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if (kv.length != 2) {
|
||||
continue;
|
||||
}
|
||||
switch (kv[0].trim().toLowerCase()) {
|
||||
case "ipb_member_id":
|
||||
if (mIpbMemberId != null) {
|
||||
mIpbMemberId.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
case "ipb_pass_hash":
|
||||
if (mIpbPassHash != null) {
|
||||
mIpbPassHash.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
case "igneous":
|
||||
if (mIgneous != null) {
|
||||
mIgneous.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
enter();
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
return;
|
||||
}
|
||||
if (kvs.length < 3) {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
return;
|
||||
}
|
||||
for (String s : kvs) {
|
||||
String[] kv;
|
||||
if (s.contains("=")) {
|
||||
kv = s.split("=");
|
||||
} else if (s.contains(":")) {
|
||||
kv = s.split(":");
|
||||
} else {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
continue;
|
||||
}
|
||||
if (kv.length != 2) {
|
||||
continue;
|
||||
}
|
||||
switch (kv[0].trim().toLowerCase()) {
|
||||
case "ipb_member_id":
|
||||
if (mIpbMemberId != null) {
|
||||
mIpbMemberId.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
case "ipb_pass_hash":
|
||||
if (mIpbPassHash != null) {
|
||||
mIpbPassHash.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
case "igneous":
|
||||
if (mIgneous != null) {
|
||||
mIgneous.setText(kv[1].trim());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
enter();
|
||||
} catch (Exception e) {
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
ExceptionUtils.throwIfFatal(e);
|
||||
e.printStackTrace();
|
||||
showTip(R.string.from_clipboard_error, LENGTH_SHORT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,8 +18,6 @@ package com.hippo.ehviewer.ui.scene;
|
||||
|
||||
import android.animation.Animator;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Typeface;
|
||||
@@ -71,6 +69,7 @@ import com.hippo.refreshlayout.RefreshLayout;
|
||||
import com.hippo.scene.SceneFragment;
|
||||
import com.hippo.text.Html;
|
||||
import com.hippo.text.URLImageGetter;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
import com.hippo.util.DrawableManager;
|
||||
import com.hippo.util.ExceptionUtils;
|
||||
import com.hippo.util.ReadableTime;
|
||||
@@ -391,8 +390,7 @@ public final class GalleryCommentsScene extends ToolbarScene
|
||||
}
|
||||
int id = menuId.get(which);
|
||||
if (id == R.id.copy) {
|
||||
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
cmb.setPrimaryClip(ClipData.newPlainText(null, comment.comment));
|
||||
ClipboardUtil.addTextToClipboard(comment.comment);
|
||||
showTip(R.string.copied_to_clipboard, LENGTH_SHORT);
|
||||
} else if (id == R.id.vote_up) {
|
||||
voteComment(comment.id, 1);
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.hippo.ehviewer.ui.scene;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -39,6 +37,7 @@ import com.hippo.ehviewer.UrlOpener;
|
||||
import com.hippo.ehviewer.client.EhUrl;
|
||||
import com.hippo.ehviewer.client.EhUtils;
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail;
|
||||
import com.hippo.util.ClipboardUtil;
|
||||
import com.hippo.yorozuya.AssertUtils;
|
||||
import com.hippo.yorozuya.LayoutUtils;
|
||||
import com.hippo.yorozuya.ViewUtils;
|
||||
@@ -200,8 +199,7 @@ public final class GalleryInfoScene extends ToolbarScene {
|
||||
if (position == INDEX_PARENT) {
|
||||
UrlOpener.openUrl(context, mValues.get(position), true);
|
||||
} else {
|
||||
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
cmb.setPrimaryClip(ClipData.newPlainText(null, mValues.get(position)));
|
||||
ClipboardUtil.addTextToClipboard(mValues.get(position));
|
||||
|
||||
if (position == INDEX_URL) {
|
||||
// Save it to avoid detect the gallery
|
||||
|
||||
@@ -19,7 +19,6 @@ package com.hippo.ehviewer.ui.scene;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.hippo.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ClipboardUtil {
|
||||
private static final String TAG = "ClipboardUtil";
|
||||
@Nullable
|
||||
private static ClipboardManager clipboardManager;
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context sContext;
|
||||
|
||||
public static void initialize(Context context) {
|
||||
sContext = context;
|
||||
clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipboardManager == null) {
|
||||
Log.e(TAG, "This device has no clipboard!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void addTextToClipboard(String text) {
|
||||
if (clipboardManager != null) {
|
||||
clipboardManager.setPrimaryClip(ClipData.newPlainText(null,text));
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTextFromClipboard() {
|
||||
if (clipboardManager != null) {
|
||||
try {
|
||||
if (!clipboardManager.hasPrimaryClip()) {
|
||||
return null;
|
||||
}
|
||||
ClipData primaryClip = clipboardManager.getPrimaryClip();
|
||||
if (primaryClip == null) {
|
||||
return null;
|
||||
}
|
||||
ClipData.Item item = primaryClip.getItemAt(0);
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
String string = String.valueOf(item.coerceToText(sContext));
|
||||
if (!TextUtils.isEmpty(string)) {
|
||||
return string;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user