SRE/02-Easy5/E5/sources/androidx/appcompat/widget/AppCompatReceiveContentHelper.java
2025-03-31 16:33:42 +02:00

92 lines
3.8 KiB
Java

package androidx.appcompat.widget;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.widget.TextView;
import androidx.core.view.ContentInfoCompat;
import androidx.core.view.ViewCompat;
/* loaded from: classes.dex */
final class AppCompatReceiveContentHelper {
private static final String LOG_TAG = "ReceiveContent";
private AppCompatReceiveContentHelper() {
}
static boolean maybeHandleMenuActionViaPerformReceiveContent(TextView textView, int i) {
if (Build.VERSION.SDK_INT >= 31 || ViewCompat.getOnReceiveContentMimeTypes(textView) == null || !(i == 16908322 || i == 16908337)) {
return false;
}
ClipboardManager clipboardManager = (ClipboardManager) textView.getContext().getSystemService("clipboard");
ClipData primaryClip = clipboardManager == null ? null : clipboardManager.getPrimaryClip();
if (primaryClip != null && primaryClip.getItemCount() > 0) {
ViewCompat.performReceiveContent(textView, new ContentInfoCompat.Builder(primaryClip, 1).setFlags(i != 16908322 ? 1 : 0).build());
}
return true;
}
static boolean maybeHandleDragEventViaPerformReceiveContent(View view, DragEvent dragEvent) {
if (Build.VERSION.SDK_INT < 31 && Build.VERSION.SDK_INT >= 24 && dragEvent.getLocalState() == null && ViewCompat.getOnReceiveContentMimeTypes(view) != null) {
Activity tryGetActivity = tryGetActivity(view);
if (tryGetActivity == null) {
Log.i(LOG_TAG, "Can't handle drop: no activity: view=" + view);
return false;
}
if (dragEvent.getAction() == 1) {
return !(view instanceof TextView);
}
if (dragEvent.getAction() == 3) {
if (view instanceof TextView) {
return OnDropApi24Impl.onDropForTextView(dragEvent, (TextView) view, tryGetActivity);
}
return OnDropApi24Impl.onDropForView(dragEvent, view, tryGetActivity);
}
}
return false;
}
private static final class OnDropApi24Impl {
private OnDropApi24Impl() {
}
static boolean onDropForTextView(DragEvent dragEvent, TextView textView, Activity activity) {
activity.requestDragAndDropPermissions(dragEvent);
int offsetForPosition = textView.getOffsetForPosition(dragEvent.getX(), dragEvent.getY());
textView.beginBatchEdit();
try {
Selection.setSelection((Spannable) textView.getText(), offsetForPosition);
ViewCompat.performReceiveContent(textView, new ContentInfoCompat.Builder(dragEvent.getClipData(), 3).build());
textView.endBatchEdit();
return true;
} catch (Throwable th) {
textView.endBatchEdit();
throw th;
}
}
static boolean onDropForView(DragEvent dragEvent, View view, Activity activity) {
activity.requestDragAndDropPermissions(dragEvent);
ViewCompat.performReceiveContent(view, new ContentInfoCompat.Builder(dragEvent.getClipData(), 3).build());
return true;
}
}
static Activity tryGetActivity(View view) {
for (Context context = view.getContext(); context instanceof ContextWrapper; context = ((ContextWrapper) context).getBaseContext()) {
if (context instanceof Activity) {
return (Activity) context;
}
}
return null;
}
}