summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Nikita Dubrovsky <dubrovsky@google.com> 2020-12-01 16:40:56 -0800
committer Nikita Dubrovsky <dubrovsky@google.com> 2020-12-02 16:25:43 -0800
commit1cea0934cc42f2ae5fcda6152793773b1aeaeb21 (patch)
treee922a074e9f33974f2fdb1181e18a27cbfad8989
parent9e442109b75bec63b8685e3005991bff4612ee94 (diff)
Update ContentInfo.partition to return a Pair instead of a Map
Bug: 174125311 Test: atest CtsViewTestCases:ContentInfoTest Change-Id: I91908fe3b23fca8e530b2b107c57ba80761f67ac
-rw-r--r--core/api/current.txt2
-rw-r--r--core/java/android/view/ContentInfo.java59
-rw-r--r--core/java/android/view/OnReceiveContentListener.java12
-rw-r--r--packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java16
4 files changed, 45 insertions, 44 deletions
diff --git a/core/api/current.txt b/core/api/current.txt
index b61bc2fde0e9..67a5b1c42cf5 100644
--- a/core/api/current.txt
+++ b/core/api/current.txt
@@ -50868,7 +50868,7 @@ package android.view {
method public int getFlags();
method @Nullable public android.net.Uri getLinkUri();
method public int getSource();
- method @NonNull public java.util.Map<java.lang.Boolean,android.view.ContentInfo> partition(@NonNull java.util.function.Predicate<android.content.ClipData.Item>);
+ method @NonNull public android.util.Pair<android.view.ContentInfo,android.view.ContentInfo> partition(@NonNull java.util.function.Predicate<android.content.ClipData.Item>);
field public static final int FLAG_CONVERT_TO_PLAIN_TEXT = 1; // 0x1
field public static final int SOURCE_APP = 0; // 0x0
field public static final int SOURCE_AUTOFILL = 4; // 0x4
diff --git a/core/java/android/view/ContentInfo.java b/core/java/android/view/ContentInfo.java
index b58937beed55..fcf699faafe1 100644
--- a/core/java/android/view/ContentInfo.java
+++ b/core/java/android/view/ContentInfo.java
@@ -20,16 +20,16 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ClipData;
+import android.content.ClipDescription;
import android.net.Uri;
import android.os.Bundle;
-import android.util.ArrayMap;
+import android.util.Pair;
import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
-import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
@@ -208,50 +208,51 @@ public final class ContentInfo {
}
/**
- * Partitions the content based on the given predicate.
+ * Partitions this content based on the given predicate.
*
- * <p>Similar to a
- * {@link java.util.stream.Collectors#partitioningBy(Predicate) partitioning collector},
- * this function classifies the content and organizes it into a map, grouping the items that
- * matched vs didn't match the predicate.
+ * <p>This function classifies the content and organizes it into a pair, grouping the items
+ * that matched vs didn't match the predicate.
*
* <p>Except for the {@link ClipData} items, the returned objects will contain all the same
- * metadata as the original.
+ * metadata as this {@link ContentInfo}.
*
* @param itemPredicate The predicate to test each {@link ClipData.Item} to determine which
- * partition to place it into.
- * @return A map containing the partitioned content. The map will contain a single entry if
- * all items were classified into the same partition (all matched or all didn't match the
- * predicate) or two entries (if there's at least one item that matched the predicate and at
- * least one item that didn't match the predicate).
+ * partition to place it into.
+ * @return A pair containing the partitioned content. The pair's first object will have the
+ * content that matched the predicate, or null if none of the items matched. The pair's
+ * second object will have the content that didn't match the predicate, or null if all of
+ * the items matched.
*/
@NonNull
- public Map<Boolean, ContentInfo> partition(@NonNull Predicate<ClipData.Item> itemPredicate) {
+ public Pair<ContentInfo, ContentInfo> partition(
+ @NonNull Predicate<ClipData.Item> itemPredicate) {
if (mClip.getItemCount() == 1) {
- Map<Boolean, ContentInfo> result = new ArrayMap<>(1);
- result.put(itemPredicate.test(mClip.getItemAt(0)), this);
- return result;
+ boolean matched = itemPredicate.test(mClip.getItemAt(0));
+ return Pair.create(matched ? this : null, matched ? null : this);
}
- ArrayList<ClipData.Item> accepted = new ArrayList<>();
- ArrayList<ClipData.Item> remaining = new ArrayList<>();
+ ArrayList<ClipData.Item> acceptedItems = new ArrayList<>();
+ ArrayList<ClipData.Item> remainingItems = new ArrayList<>();
for (int i = 0; i < mClip.getItemCount(); i++) {
ClipData.Item item = mClip.getItemAt(i);
if (itemPredicate.test(item)) {
- accepted.add(item);
+ acceptedItems.add(item);
} else {
- remaining.add(item);
+ remainingItems.add(item);
}
}
- Map<Boolean, ContentInfo> result = new ArrayMap<>(2);
- if (!accepted.isEmpty()) {
- ClipData acceptedClip = new ClipData(mClip.getDescription(), accepted);
- result.put(true, new Builder(this).setClip(acceptedClip).build());
+ if (acceptedItems.isEmpty()) {
+ return Pair.create(null, this);
}
- if (!remaining.isEmpty()) {
- ClipData remainingClip = new ClipData(mClip.getDescription(), remaining);
- result.put(false, new Builder(this).setClip(remainingClip).build());
+ if (remainingItems.isEmpty()) {
+ return Pair.create(this, null);
}
- return result;
+ ContentInfo accepted = new Builder(this)
+ .setClip(new ClipData(new ClipDescription(mClip.getDescription()), acceptedItems))
+ .build();
+ ContentInfo remaining = new Builder(this)
+ .setClip(new ClipData(new ClipDescription(mClip.getDescription()), remainingItems))
+ .build();
+ return Pair.create(accepted, remaining);
}
/**
diff --git a/core/java/android/view/OnReceiveContentListener.java b/core/java/android/view/OnReceiveContentListener.java
index b551fa80b5cd..419f964ccd25 100644
--- a/core/java/android/view/OnReceiveContentListener.java
+++ b/core/java/android/view/OnReceiveContentListener.java
@@ -35,12 +35,12 @@ import android.annotation.Nullable;
*
* &#64;Override
* public ContentInfo onReceiveContent(View view, ContentInfo payload) {
- * Map&lt;Boolean, ContentInfo&gt; split =
+ * Pair&lt;ContentInfo, ContentInfo&gt; split =
* payload.partition(item -&gt; item.getUri() != null);
- * ContentInfo uriItems = split.get(true);
- * ContentInfo remainingItems = split.get(false);
- * if (uriItems != null) {
- * ClipData clip = uriItems.getClip();
+ * ContentInfo uriContent = split.first;
+ * ContentInfo remaining = split.second;
+ * if (uriContent != null) {
+ * ClipData clip = uriContent.getClip();
* for (int i = 0; i < clip.getItemCount(); i++) {
* Uri uri = clip.getItemAt(i).getUri();
* // ... app-specific logic to handle the URI ...
@@ -48,7 +48,7 @@ import android.annotation.Nullable;
* }
* // Return anything that we didn't handle ourselves. This preserves the default platform
* // behavior for text and anything else for which we are not implementing custom handling.
- * return remainingItems;
+ * return remaining;
* }
* }
*
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
index adbc85b8e2e5..46ddc3b809a7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
@@ -42,6 +42,7 @@ import android.text.SpannedString;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
+import android.util.Pair;
import android.view.ContentInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@@ -75,7 +76,6 @@ import com.android.systemui.statusbar.notification.stack.StackStateAnimator;
import com.android.systemui.statusbar.phone.LightBarController;
import java.util.HashMap;
-import java.util.Map;
import java.util.function.Consumer;
/**
@@ -593,16 +593,16 @@ public class RemoteInputView extends LinearLayout implements View.OnClickListene
@Nullable
public ContentInfo onReceiveContent(@NonNull View view,
@NonNull ContentInfo payload) {
- Map<Boolean, ContentInfo> split = payload.partition(
+ Pair<ContentInfo, ContentInfo> split = payload.partition(
item -> item.getUri() != null);
- ContentInfo uriItems = split.get(true);
- ContentInfo remainingItems = split.get(false);
- if (uriItems != null) {
- ClipData clip = uriItems.getClip();
+ ContentInfo uriContent = split.first;
+ ContentInfo remaining = split.second;
+ if (uriContent != null) {
+ ClipData clip = uriContent.getClip();
ClipDescription description = clip.getDescription();
if (clip.getItemCount() > 1
|| description.getMimeTypeCount() < 1
- || remainingItems != null) {
+ || remaining != null) {
// TODO(b/172363500): Update to loop over all the items
return payload;
}
@@ -612,7 +612,7 @@ public class RemoteInputView extends LinearLayout implements View.OnClickListene
.prepareRemoteInputFromData(mimeType, contentUri);
mRemoteInputView.sendRemoteInput(dataIntent);
}
- return remainingItems;
+ return remaining;
}
});
}