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

Commit 888b516a authored by thiruram's avatar thiruram
Browse files

Fixes NPE when creating folder with null suggestedFolderNames.

Bug: 149993849

Change-Id: Ia2758b76aef1812dab225c8a95e7141fcdeefab4
parent 91f97cb2
Loading
Loading
Loading
Loading
+41 −26
Original line number Original line Diff line number Diff line
@@ -33,6 +33,7 @@ import static com.android.launcher3.userevent.LauncherLogProto.Target.FromFolder


import static java.util.Arrays.asList;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Arrays.stream;
import static java.util.Optional.ofNullable;


import android.animation.Animator;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorListenerAdapter;
@@ -322,12 +323,11 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo
        post(() -> {
        post(() -> {
            if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) {
            if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) {
                if (isEmpty(mFolderName.getText())) {
                if (isEmpty(mFolderName.getText())) {
                    FolderNameInfo[] nameInfos =
                    ofNullable(mInfo)
                            (FolderNameInfo[]) mInfo.suggestedFolderNames.getParcelableArrayExtra(
                            .map(info -> info.suggestedFolderNames)
                                    FolderInfo.EXTRA_FOLDER_SUGGESTIONS);
                            .map(folderNames -> (FolderNameInfo[]) folderNames
                    if (nameInfos != null) {
                                    .getParcelableArrayExtra(FolderInfo.EXTRA_FOLDER_SUGGESTIONS))
                        showLabelSuggestion(nameInfos, false);
                            .ifPresent(nameInfos -> showLabelSuggestion(nameInfos, false));
                    }
                }
                }
            }
            }
            mFolderName.setHint("");
            mFolderName.setHint("");
@@ -1647,26 +1647,8 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo
                checkNotNull(mFolderName.getText().toString(),
                checkNotNull(mFolderName.getText().toString(),
                        "Expected valid folder label, but found null");
                        "Expected valid folder label, but found null");


        Optional<String[]> suggestedLabels = Optional.ofNullable(
        Optional<String[]> suggestedLabels = getSuggestedLabels();
                (FolderNameInfo[]) mInfo.suggestedFolderNames
        int accepted_suggestion_index = getAcceptedSuggestionIndex();
                        .getParcelableArrayExtra(FolderInfo.EXTRA_FOLDER_SUGGESTIONS))
                .map(folderNameInfoArray ->
                        stream(folderNameInfoArray)
                                .filter(Objects::nonNull)
                                .map(FolderNameInfo::getLabel)
                                .map(CharSequence::toString)
                                .toArray(String[]::new));


        int accepted_suggestion_index = suggestedLabels
                .map(folderNameInfoArray ->
                        IntStream.range(0, folderNameInfoArray.length)
                                .filter(index -> newLabel.equalsIgnoreCase(
                                        folderNameInfoArray[index]))
                                .findFirst()
                                .orElse(-1)
                ).orElse(-1);

        boolean hasValidPrimary = suggestedLabels
        boolean hasValidPrimary = suggestedLabels
                .map(labels -> labels.length > 0 && !isEmpty(labels[0]))
                .map(labels -> labels.length > 0 && !isEmpty(labels[0]))
                .orElse(false);
                .orElse(false);
@@ -1695,6 +1677,39 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo
                        : Target.ToFolderLabelState.valueOf("TO_CUSTOM" + suggestionsSuffix);
                        : Target.ToFolderLabelState.valueOf("TO_CUSTOM" + suggestionsSuffix);
    }
    }


    private Optional<String[]> getSuggestedLabels() {
        return ofNullable(mInfo)
            .map(info -> info.suggestedFolderNames)
            .map(
                folderNames ->
                    (FolderNameInfo[])
                        folderNames.getParcelableArrayExtra(FolderInfo.EXTRA_FOLDER_SUGGESTIONS))
            .map(
                folderNameInfoArray ->
                    stream(folderNameInfoArray)
                        .filter(Objects::nonNull)
                        .map(FolderNameInfo::getLabel)
                        .filter(Objects::nonNull)
                        .map(CharSequence::toString)
                        .toArray(String[]::new));
    }

    private int getAcceptedSuggestionIndex() {
        String newLabel =
                checkNotNull(mFolderName.getText().toString(),
                        "Expected valid folder label, but found null");

        return getSuggestedLabels()
                .map(suggestionsArray ->
                        IntStream.range(0, suggestionsArray.length)
                                .filter(index -> newLabel.equalsIgnoreCase(
                                        suggestionsArray[index]))
                                .findFirst()
                                .orElse(-1)
                ).orElse(-1);

    }



    private Target.Builder newEditTextTargetBuilder() {
    private Target.Builder newEditTextTargetBuilder() {
        return Target.newBuilder().setType(Target.Type.ITEM).setItemType(ItemType.EDITTEXT);
        return Target.newBuilder().setType(Target.Type.ITEM).setItemType(ItemType.EDITTEXT);