1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.folder; 17 18 import android.text.TextUtils; 19 20 import androidx.annotation.NonNull; 21 22 import java.util.Arrays; 23 import java.util.Objects; 24 25 /** 26 * Information about a label suggestions of a Folder. 27 */ 28 29 public class FolderNameInfos { 30 public static final int SUCCESS = 1; 31 public static final int HAS_PRIMARY = 1 << 1; 32 public static final int HAS_SUGGESTIONS = 1 << 2; 33 public static final int ERROR_NO_PROVIDER = 1 << 3; 34 public static final int ERROR_APP_LOOKUP_FAILED = 1 << 4; 35 public static final int ERROR_ALL_APP_LOOKUP_FAILED = 1 << 5; 36 public static final int ERROR_NO_LABELS_GENERATED = 1 << 6; 37 public static final int ERROR_LABEL_LOOKUP_FAILED = 1 << 7; 38 public static final int ERROR_ALL_LABEL_LOOKUP_FAILED = 1 << 8; 39 public static final int ERROR_NO_PACKAGES = 1 << 9; 40 41 private int mStatus; 42 private final CharSequence[] mLabels; 43 private final Float[] mScores; 44 FolderNameInfos()45 public FolderNameInfos() { 46 mStatus = 0; 47 mLabels = new CharSequence[FolderNameProvider.SUGGEST_MAX]; 48 mScores = new Float[FolderNameProvider.SUGGEST_MAX]; 49 } 50 51 /** 52 * set the status of FolderNameInfos. 53 */ setStatus(int statusBit)54 public void setStatus(int statusBit) { 55 mStatus = mStatus | statusBit; 56 } 57 58 /** 59 * returns status of FolderNameInfos generations. 60 */ status()61 public int status() { 62 return mStatus; 63 } 64 65 /** 66 * return true if the first suggestion is a Primary suggestion. 67 */ hasPrimary()68 public boolean hasPrimary() { 69 return (mStatus & HAS_PRIMARY) > 0 && (mLabels[0] != null); 70 } 71 72 /** 73 * return true if there is at least one valid suggestion. 74 */ hasSuggestions()75 public boolean hasSuggestions() { 76 for (CharSequence l : mLabels) { 77 if (l != null && !TextUtils.isEmpty(l)) return true; 78 } 79 return false; 80 } 81 82 /** 83 * assign label and score in the specified index. 84 */ setLabel(int index, CharSequence label, Float score)85 public void setLabel(int index, CharSequence label, Float score) { 86 if (index < mLabels.length) { 87 mLabels[index] = label; 88 mScores[index] = score; 89 } 90 } 91 92 /** 93 * returns true if the label is found in label suggestions/ 94 */ contains(CharSequence label)95 public boolean contains(CharSequence label) { 96 return Arrays.stream(mLabels) 97 .filter(Objects::nonNull) 98 .anyMatch(l -> l.toString().equalsIgnoreCase(label.toString())); 99 } 100 101 getLabels()102 public CharSequence[] getLabels() { 103 return mLabels; 104 } 105 getScores()106 public Float[] getScores() { 107 return mScores; 108 } 109 110 @Override 111 @NonNull toString()112 public String toString() { 113 return String.format("status=%s, labels=%s", Integer.toBinaryString(mStatus), 114 Arrays.toString(mLabels)); 115 } 116 } 117 118