1 /*
2  * Copyright (C) 2022 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 
17 package com.android.intentresolver.emptystate;
18 
19 
20 import static com.android.intentresolver.shared.model.Profile.Type.PERSONAL;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import android.os.UserHandle;
25 
26 import androidx.annotation.NonNull;
27 
28 import com.android.intentresolver.ProfileAvailability;
29 import com.android.intentresolver.ProfileHelper;
30 import com.android.intentresolver.ResolverListAdapter;
31 import com.android.intentresolver.shared.model.Profile;
32 import com.android.intentresolver.ui.ProfilePagerResources;
33 
34 /**
35  * Chooser/ResolverActivity empty state provider that returns empty state which is shown when
36  * there are no apps available.
37  */
38 public class NoAppsAvailableEmptyStateProvider implements EmptyStateProvider {
39 
40     @NonNull private final String mMetricsCategory;
41     private final ProfilePagerResources mProfilePagerResources;
42     private final ProfileHelper mProfileHelper;
43     private final ProfileAvailability mProfileAvailability;
44 
NoAppsAvailableEmptyStateProvider( ProfileHelper profileHelper, ProfileAvailability profileAvailability, @NonNull String metricsCategory, ProfilePagerResources profilePagerResources)45     public NoAppsAvailableEmptyStateProvider(
46             ProfileHelper profileHelper,
47             ProfileAvailability profileAvailability,
48             @NonNull String metricsCategory,
49             ProfilePagerResources profilePagerResources) {
50         mProfileHelper = profileHelper;
51         mProfileAvailability = profileAvailability;
52         mMetricsCategory = metricsCategory;
53         mProfilePagerResources = profilePagerResources;
54     }
55 
56     @NonNull
57     @Override
getEmptyState(ResolverListAdapter resolverListAdapter)58     public EmptyState getEmptyState(ResolverListAdapter resolverListAdapter) {
59         UserHandle listUserHandle = resolverListAdapter.getUserHandle();
60         if (mProfileAvailability.visibleProfileCount() == 1) {
61             return new DefaultEmptyState();
62         } else {
63             Profile.Type profileType =
64                     requireNonNull(mProfileHelper.findProfileType(listUserHandle));
65             String title = mProfilePagerResources.noAppsMessage(profileType);
66             return new NoAppsAvailableEmptyState(
67                     title,
68                     mMetricsCategory,
69                     /* isPersonalProfile= */ profileType == PERSONAL
70             );
71         }
72     }
73 }
74