Debian Codegraph @

core/java/com/android/internal/app/NoAppsAvailableEmptyStateProvider.java

raw symbols SCIP
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.internal.app;
18
19 import static android.app.admin.DevicePolicyResources.Strings.Core.RESOLVER_NO_PERSONAL_APPS;
20 import static android.app.admin.DevicePolicyResources.Strings.Core.RESOLVER_NO_WORK_APPS;
21
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.app.admin.DevicePolicyEventLogger;
25 import android.app.admin.DevicePolicyManager;
26 import android.content.Context;
27 import android.content.pm.ResolveInfo;
28 import android.os.UserHandle;
29 import android.stats.devicepolicy.nano.DevicePolicyEnums;
30
31 import com.android.internal.app.AbstractMultiProfilePagerAdapter.EmptyState;
32 import com.android.internal.app.AbstractMultiProfilePagerAdapter.EmptyStateProvider;
33 import com.android.internal.app.AbstractMultiProfilePagerAdapter.MyUserIdProvider;
34 import com.android.internal.R;
35
36 import java.util.List;
37
38 /**
39 * Chooser/ResolverActivity empty state provider that returns empty state which is shown when
40 * there are no apps available.
41 */
42 public class NoAppsAvailableEmptyStateProvider implements EmptyStateProvider {
43
44 @NonNull
45 private final Context mContext;
46 @Nullable
47 private final UserHandle mWorkProfileUserHandle;
48 @Nullable
49 private final UserHandle mPersonalProfileUserHandle;
50 @NonNull
51 private final String mMetricsCategory;
52 @NonNull
53 private final MyUserIdProvider mMyUserIdProvider;
54
55 public NoAppsAvailableEmptyStateProvider(Context context, UserHandle workProfileUserHandle,
56 UserHandle personalProfileUserHandle, String metricsCategory,
57 MyUserIdProvider myUserIdProvider) {
58 mContext = context;
59 mWorkProfileUserHandle = workProfileUserHandle;
60 mPersonalProfileUserHandle = personalProfileUserHandle;
61 mMetricsCategory = metricsCategory;
62 mMyUserIdProvider = myUserIdProvider;
63 }
64
65 @Nullable
66 @Override
67 @SuppressWarnings("ReferenceEquality")
68 public EmptyState getEmptyState(ResolverListAdapter resolverListAdapter) {
69 UserHandle listUserHandle = resolverListAdapter.getUserHandle();
70
71 if (mWorkProfileUserHandle != null
72 && (mMyUserIdProvider.getMyUserId() == listUserHandle.getIdentifier()
73 || !hasAppsInOtherProfile(resolverListAdapter))) {
74
75 String title;
76 if (listUserHandle == mPersonalProfileUserHandle) {
77 title = mContext.getSystemService(
78 DevicePolicyManager.class).getResources().getString(
79 RESOLVER_NO_PERSONAL_APPS,
80 () -> mContext.getString(R.string.resolver_no_personal_apps_available));
81 } else {
82 title = mContext.getSystemService(
83 DevicePolicyManager.class).getResources().getString(
84 RESOLVER_NO_WORK_APPS,
85 () -> mContext.getString(R.string.resolver_no_work_apps_available));
86 }
87
88 return new NoAppsAvailableEmptyState(
89 title, mMetricsCategory,
90 /* isPersonalProfile= */ listUserHandle == mPersonalProfileUserHandle
91 );
92 } else if (mWorkProfileUserHandle == null) {
93 // Return default empty state without tracking
94 return new DefaultEmptyState();
95 }
96
97 return null;
98 }
99
100 private boolean hasAppsInOtherProfile(ResolverListAdapter adapter) {
101 if (mWorkProfileUserHandle == null) {
102 return false;
103 }
104 List<ResolverActivity.ResolvedComponentInfo> resolversForIntent =
105 adapter.getResolversForUser(UserHandle.of(mMyUserIdProvider.getMyUserId()));
106 for (ResolverActivity.ResolvedComponentInfo info : resolversForIntent) {
107 ResolveInfo resolveInfo = info.getResolveInfoAt(0);
108 if (resolveInfo.targetUserId != UserHandle.USER_CURRENT) {
109 return true;
110 }
111 }
112 return false;
113 }
114
115 public static class DefaultEmptyState implements EmptyState {
116 @Override
117 public boolean useDefaultEmptyView() {
118 return true;
119 }
120 }
121
122 public static class NoAppsAvailableEmptyState implements EmptyState {
123
124 @NonNull
125 private String mTitle;
126
127 @NonNull
128 private String mMetricsCategory;
129
130 private boolean mIsPersonalProfile;
131
132 public NoAppsAvailableEmptyState(String title, String metricsCategory,
133 boolean isPersonalProfile) {
134 mTitle = title;
135 mMetricsCategory = metricsCategory;
136 mIsPersonalProfile = isPersonalProfile;
137 }
138
139 @Nullable
140 @Override
141 public String getTitle() {
142 return mTitle;
143 }
144
145 @Override
146 public void onEmptyStateShown() {
147 DevicePolicyEventLogger.createEvent(
148 DevicePolicyEnums.RESOLVER_EMPTY_STATE_NO_APPS_RESOLVED)
149 .setStrings(mMetricsCategory)
150 .setBoolean(/*isPersonalProfile*/ mIsPersonalProfile)
151 .write();
152 }
153 }
154 }