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 import static android.app.admin.DevicePolicyResources.Strings.Core.RESOLVER_WORK_PAUSED_TITLE;
20 
21 import static java.util.Objects.requireNonNull;
22 
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.os.UserHandle;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 
30 import com.android.intentresolver.ProfileAvailability;
31 import com.android.intentresolver.ProfileHelper;
32 import com.android.intentresolver.R;
33 import com.android.intentresolver.ResolverListAdapter;
34 import com.android.intentresolver.profiles.OnSwitchOnWorkSelectedListener;
35 import com.android.intentresolver.shared.model.Profile;
36 
37 /**
38  * Chooser/ResolverActivity empty state provider that returns empty state which is shown when
39  * work profile is paused and we need to show a button to enable it.
40  */
41 public class WorkProfilePausedEmptyStateProvider implements EmptyStateProvider {
42 
43     private final ProfileHelper mProfileHelper;
44     private final ProfileAvailability mProfileAvailability;
45     private final String mMetricsCategory;
46     private final OnSwitchOnWorkSelectedListener mOnSwitchOnWorkSelectedListener;
47     private final Context mContext;
48 
WorkProfilePausedEmptyStateProvider(@onNull Context context, ProfileHelper profileHelper, ProfileAvailability profileAvailability, @Nullable OnSwitchOnWorkSelectedListener onSwitchOnWorkSelectedListener, @NonNull String metricsCategory)49     public WorkProfilePausedEmptyStateProvider(@NonNull Context context,
50             ProfileHelper profileHelper,
51             ProfileAvailability profileAvailability,
52             @Nullable OnSwitchOnWorkSelectedListener onSwitchOnWorkSelectedListener,
53             @NonNull String metricsCategory) {
54         mContext = context;
55         mProfileHelper = profileHelper;
56         mProfileAvailability = profileAvailability;
57         mMetricsCategory = metricsCategory;
58         mOnSwitchOnWorkSelectedListener = onSwitchOnWorkSelectedListener;
59     }
60 
61     @Nullable
62     @Override
getEmptyState(ResolverListAdapter resolverListAdapter)63     public EmptyState getEmptyState(ResolverListAdapter resolverListAdapter) {
64         UserHandle userHandle = resolverListAdapter.getUserHandle();
65         if (!mProfileHelper.getWorkProfilePresent()) {
66             return null;
67         }
68         Profile workProfile = requireNonNull(mProfileHelper.getWorkProfile());
69 
70         // Policy: only show the "Work profile paused" state when:
71         // * provided list adapter is from the work profile
72         // * the list adapter is not empty
73         // * work profile quiet mode is _enabled_ (unavailable)
74 
75         if (!userHandle.equals(workProfile.getPrimary().getHandle())
76                 || resolverListAdapter.getCount() == 0
77                 || mProfileAvailability.isAvailable(workProfile)) {
78             return null;
79         }
80 
81         String title = mContext.getSystemService(DevicePolicyManager.class)
82                 .getResources().getString(RESOLVER_WORK_PAUSED_TITLE,
83                     () -> mContext.getString(R.string.resolver_turn_on_work_apps));
84 
85         return new WorkProfileOffEmptyState(title, /* EmptyState.ClickListener */ (tab) -> {
86             tab.showSpinner();
87             if (mOnSwitchOnWorkSelectedListener != null) {
88                 mOnSwitchOnWorkSelectedListener.onSwitchOnWorkSelected();
89             }
90             mProfileAvailability.requestQuietModeState(workProfile, false);
91         }, mMetricsCategory);
92     }
93 
94 }
95