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; 18 19 import static android.app.admin.DevicePolicyResources.Strings.Core.RESOLVER_WORK_PAUSED_TITLE; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.app.admin.DevicePolicyEventLogger; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 import android.os.UserHandle; 27 import android.stats.devicepolicy.nano.DevicePolicyEnums; 28 29 import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyState; 30 import com.android.intentresolver.AbstractMultiProfilePagerAdapter.EmptyStateProvider; 31 import com.android.intentresolver.AbstractMultiProfilePagerAdapter.OnSwitchOnWorkSelectedListener; 32 33 /** 34 * Chooser/ResolverActivity empty state provider that returns empty state which is shown when 35 * work profile is paused and we need to show a button to enable it. 36 */ 37 public class WorkProfilePausedEmptyStateProvider implements EmptyStateProvider { 38 39 private final UserHandle mWorkProfileUserHandle; 40 private final WorkProfileAvailabilityManager mWorkProfileAvailability; 41 private final String mMetricsCategory; 42 private final OnSwitchOnWorkSelectedListener mOnSwitchOnWorkSelectedListener; 43 private final Context mContext; 44 WorkProfilePausedEmptyStateProvider(@onNull Context context, @Nullable UserHandle workProfileUserHandle, @NonNull WorkProfileAvailabilityManager workProfileAvailability, @Nullable OnSwitchOnWorkSelectedListener onSwitchOnWorkSelectedListener, @NonNull String metricsCategory)45 public WorkProfilePausedEmptyStateProvider(@NonNull Context context, 46 @Nullable UserHandle workProfileUserHandle, 47 @NonNull WorkProfileAvailabilityManager workProfileAvailability, 48 @Nullable OnSwitchOnWorkSelectedListener onSwitchOnWorkSelectedListener, 49 @NonNull String metricsCategory) { 50 mContext = context; 51 mWorkProfileUserHandle = workProfileUserHandle; 52 mWorkProfileAvailability = workProfileAvailability; 53 mMetricsCategory = metricsCategory; 54 mOnSwitchOnWorkSelectedListener = onSwitchOnWorkSelectedListener; 55 } 56 57 @Nullable 58 @Override getEmptyState(ResolverListAdapter resolverListAdapter)59 public EmptyState getEmptyState(ResolverListAdapter resolverListAdapter) { 60 if (!resolverListAdapter.getUserHandle().equals(mWorkProfileUserHandle) 61 || !mWorkProfileAvailability.isQuietModeEnabled() 62 || resolverListAdapter.getCount() == 0) { 63 return null; 64 } 65 66 final String title = mContext.getSystemService(DevicePolicyManager.class) 67 .getResources().getString(RESOLVER_WORK_PAUSED_TITLE, 68 () -> mContext.getString(R.string.resolver_turn_on_work_apps)); 69 70 return new WorkProfileOffEmptyState(title, (tab) -> { 71 tab.showSpinner(); 72 if (mOnSwitchOnWorkSelectedListener != null) { 73 mOnSwitchOnWorkSelectedListener.onSwitchOnWorkSelected(); 74 } 75 mWorkProfileAvailability.requestQuietModeEnabled(false); 76 }, mMetricsCategory); 77 } 78 79 public static class WorkProfileOffEmptyState implements EmptyState { 80 81 private final String mTitle; 82 private final ClickListener mOnClick; 83 private final String mMetricsCategory; 84 WorkProfileOffEmptyState(String title, @NonNull ClickListener onClick, @NonNull String metricsCategory)85 public WorkProfileOffEmptyState(String title, @NonNull ClickListener onClick, 86 @NonNull String metricsCategory) { 87 mTitle = title; 88 mOnClick = onClick; 89 mMetricsCategory = metricsCategory; 90 } 91 92 @Nullable 93 @Override getTitle()94 public String getTitle() { 95 return mTitle; 96 } 97 98 @Nullable 99 @Override getButtonClickListener()100 public ClickListener getButtonClickListener() { 101 return mOnClick; 102 } 103 104 @Override onEmptyStateShown()105 public void onEmptyStateShown() { 106 DevicePolicyEventLogger 107 .createEvent(DevicePolicyEnums.RESOLVER_EMPTY_STATE_WORK_APPS_DISABLED) 108 .setStrings(mMetricsCategory) 109 .write(); 110 } 111 } 112 } 113