• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.model.data;
17 
18 import static com.android.launcher3.LauncherSettings.Favorites.EXTENDED_CONTAINERS;
19 
20 import android.app.PendingIntent;
21 import android.content.Intent;
22 import android.graphics.drawable.Icon;
23 import android.os.Process;
24 import android.os.UserHandle;
25 
26 import androidx.annotation.Nullable;
27 
28 import com.android.launcher3.logger.LauncherAtom.ItemInfo;
29 import com.android.launcher3.logger.LauncherAtom.SearchActionItem;
30 
31 /**
32  * Represents a SearchAction with in launcher
33  */
34 public class SearchActionItemInfo extends ItemInfoWithIcon {
35 
36     public static final int FLAG_SHOULD_START = 1 << 1;
37     public static final int FLAG_SHOULD_START_FOR_RESULT = FLAG_SHOULD_START | 1 << 2;
38     public static final int FLAG_BADGE_WITH_PACKAGE = 1 << 3;
39     public static final int FLAG_PRIMARY_ICON_FROM_TITLE = 1 << 4;
40     public static final int FLAG_BADGE_WITH_COMPONENT_NAME = 1 << 5;
41 
42     private final String mFallbackPackageName;
43     private int mFlags = 0;
44     private final Icon mIcon;
45 
46     // If true title does not contain any personal info and eligible for logging.
47     private final boolean mIsPersonalTitle;
48     private Intent mIntent;
49 
50     private PendingIntent mPendingIntent;
51 
SearchActionItemInfo(Icon icon, String packageName, UserHandle user, CharSequence title, boolean isPersonalTitle)52     public SearchActionItemInfo(Icon icon, String packageName, UserHandle user,
53             CharSequence title, boolean isPersonalTitle) {
54         mIsPersonalTitle = isPersonalTitle;
55         this.user = user == null ? Process.myUserHandle() : user;
56         this.title = title;
57         this.container = EXTENDED_CONTAINERS;
58         mFallbackPackageName = packageName;
59         mIcon = icon;
60     }
61 
SearchActionItemInfo(SearchActionItemInfo info)62     public SearchActionItemInfo(SearchActionItemInfo info) {
63         super(info);
64         mIcon = info.mIcon;
65         mFallbackPackageName = info.mFallbackPackageName;
66         mFlags = info.mFlags;
67         title = info.title;
68         this.container = EXTENDED_CONTAINERS;
69         this.mIsPersonalTitle = info.mIsPersonalTitle;
70     }
71 
72     /**
73      * Returns if multiple flags are all available.
74      */
hasFlags(int flags)75     public boolean hasFlags(int flags) {
76         return (mFlags & flags) != 0;
77     }
78 
setFlags(int flags)79     public void setFlags(int flags) {
80         mFlags |= flags ;
81     }
82 
83     @Override
getIntent()84     public Intent getIntent() {
85         return mIntent;
86     }
87 
88     /**
89      * Setter for mIntent with assertion for null value mPendingIntent
90      */
setIntent(Intent intent)91     public void setIntent(Intent intent) {
92         if (mPendingIntent != null && intent != null) {
93             throw new RuntimeException(
94                     "SearchActionItemInfo can only have either an Intent or a PendingIntent");
95         }
96         mIntent = intent;
97     }
98 
getPendingIntent()99     public PendingIntent getPendingIntent() {
100         return mPendingIntent;
101     }
102 
103     /**
104      * Setter of mPendingIntent with assertion for null value mIntent
105      */
setPendingIntent(PendingIntent pendingIntent)106     public void setPendingIntent(PendingIntent pendingIntent) {
107         if (mIntent != null && pendingIntent != null) {
108             throw new RuntimeException(
109                     "SearchActionItemInfo can only have either an Intent or a PendingIntent");
110         }
111         mPendingIntent = pendingIntent;
112     }
113 
114     @Nullable
getIcon()115     public Icon getIcon() {
116         return mIcon;
117     }
118 
119     @Override
clone()120     public ItemInfoWithIcon clone() {
121         return new SearchActionItemInfo(this);
122     }
123 
124     @Override
buildProto(FolderInfo fInfo)125     public ItemInfo buildProto(FolderInfo fInfo) {
126         SearchActionItem.Builder itemBuilder = SearchActionItem.newBuilder()
127                 .setPackageName(mFallbackPackageName);
128 
129         if (!mIsPersonalTitle) {
130             itemBuilder.setTitle(title.toString());
131         }
132         return getDefaultItemInfoBuilder()
133                 .setSearchActionItem(itemBuilder)
134                 .setContainerInfo(getContainerInfo())
135                 .build();
136     }
137 }
138