From a4c563c8cc9075f922541028b1d33e2ad5c1f186 Mon Sep 17 00:00:00 2001 From: Samuel Fufa Date: Tue, 14 Apr 2020 09:22:31 -0700 Subject: Allow ComponentKey creation from its string representation Bug: 152582306 Test: Manual Change-Id: I9b11e3a85501239170f902d67d9e7b51d9f53e84 (cherry picked from commit 4e5080abf1c5c7793a8a55f27df2cdb8e642417f) --- .../com/android/launcher3/util/ComponentKey.java | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/iconloaderlib/src/com/android/launcher3/util/ComponentKey.java b/iconloaderlib/src/com/android/launcher3/util/ComponentKey.java index 34bed94..7145103 100644 --- a/iconloaderlib/src/com/android/launcher3/util/ComponentKey.java +++ b/iconloaderlib/src/com/android/launcher3/util/ComponentKey.java @@ -19,6 +19,9 @@ package com.android.launcher3.util; import android.content.ComponentName; import android.os.UserHandle; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import java.util.Arrays; public class ComponentKey { @@ -54,6 +57,28 @@ public class ComponentKey { */ @Override public String toString() { - return componentName.flattenToString() + "#" + user; + return componentName.flattenToString() + "#" + user.hashCode(); + } + + /** + * Parses and returns ComponentKey objected from string representation + * Returns null if string is not properly formatted + */ + @Nullable + public static ComponentKey fromString(@NonNull String str) { + int sep = str.indexOf('#'); + if (sep < 0 || (sep + 1) >= str.length()) { + return null; + } + ComponentName componentName = ComponentName.unflattenFromString(str.substring(0, sep)); + if (componentName == null) { + return null; + } + try { + return new ComponentKey(componentName, + UserHandle.getUserHandleForUid(Integer.parseInt(str.substring(sep + 1)))); + } catch (NumberFormatException ex) { + return null; + } } } \ No newline at end of file -- cgit v1.2.3