Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e160633e authored by Samuel Fufa's avatar Samuel Fufa Committed by Automerger Merge Worker
Browse files

Allow ComponentKey creation from its string representation am: a4c563c8 am:...

Allow ComponentKey creation from its string representation am: a4c563c8 am: 75f2f677 am: 15d6904d

Change-Id: Ia6b439b5b2bb75c442cc6f25de21627800fb821d
parents 55a5eb74 15d6904d
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -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