• 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 
17 package com.android.providers.media.photopicker.ui;
18 
19 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Drawables.Style.OUTLINE;
20 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Drawables.WORK_PROFILE_ICON;
21 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Strings.BLOCKED_BY_ADMIN_TITLE;
22 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Strings.BLOCKED_FROM_PERSONAL_MESSAGE;
23 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Strings.BLOCKED_FROM_WORK_MESSAGE;
24 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Strings.WORK_PROFILE_PAUSED_MESSAGE;
25 import static com.android.providers.media.photopicker.ui.DevicePolicyResources.Strings.WORK_PROFILE_PAUSED_TITLE;
26 
27 import android.app.Dialog;
28 import android.app.admin.DevicePolicyManager;
29 import android.graphics.drawable.Drawable;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.util.Log;
33 
34 import androidx.annotation.RequiresApi;
35 import androidx.fragment.app.DialogFragment;
36 import androidx.fragment.app.Fragment;
37 import androidx.fragment.app.FragmentManager;
38 import androidx.fragment.app.FragmentTransaction;
39 import androidx.lifecycle.ViewModelProvider;
40 
41 import com.android.modules.utils.build.SdkLevel;
42 import com.android.providers.media.R;
43 import com.android.providers.media.photopicker.data.UserIdManager;
44 import com.android.providers.media.photopicker.viewmodel.PickerViewModel;
45 
46 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
47 
48 public class ProfileDialogFragment extends DialogFragment {
49 
50     private static final String TAG = "ProfileDialog";
51 
52     @Override
onCreateDialog(Bundle savedInstanceState)53     public Dialog onCreateDialog(Bundle savedInstanceState) {
54         final PickerViewModel pickerViewModel = new ViewModelProvider(requireActivity()).get(
55                 PickerViewModel.class);
56         final UserIdManager userIdManager = pickerViewModel.getUserIdManager();
57         final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity());
58         if (userIdManager.isBlockedByAdmin()) {
59             setBlockedByAdminParams(userIdManager.isManagedUserSelected(), builder);
60         } else if (userIdManager.isWorkProfileOff()) {
61             setWorkProfileOffParams(builder);
62         } else {
63             Log.e(TAG, "Unknown error for profile dialog");
64             return null;
65         }
66         return builder.create();
67     }
68 
setBlockedByAdminParams( boolean isManagedUserSelected, MaterialAlertDialogBuilder builder)69     private void setBlockedByAdminParams(
70             boolean isManagedUserSelected, MaterialAlertDialogBuilder builder) {
71         String title;
72         String message;
73         if (SdkLevel.isAtLeastT()) {
74             title = getUpdatedEnterpriseString(
75                     BLOCKED_BY_ADMIN_TITLE, R.string.picker_profile_admin_title);
76             message = isManagedUserSelected
77                     ? getUpdatedEnterpriseString(
78                             BLOCKED_FROM_WORK_MESSAGE, R.string.picker_profile_admin_msg_from_work)
79                     : getUpdatedEnterpriseString(
80                             BLOCKED_FROM_PERSONAL_MESSAGE,
81                             R.string.picker_profile_admin_msg_from_personal);
82         } else {
83             title = getString(R.string.picker_profile_admin_title);
84             message = isManagedUserSelected
85                     ? getString(R.string.picker_profile_admin_msg_from_work)
86                     : getString(R.string.picker_profile_admin_msg_from_personal);
87         }
88         builder.setIcon(R.drawable.ic_lock);
89         builder.setTitle(title);
90         builder.setMessage(message);
91         builder.setPositiveButton(android.R.string.ok, null);
92     }
93 
setWorkProfileOffParams(MaterialAlertDialogBuilder builder)94     private void setWorkProfileOffParams(MaterialAlertDialogBuilder builder) {
95         Drawable icon;
96         String title;
97         String message;
98         if (SdkLevel.isAtLeastT()) {
99             icon = getUpdatedWorkProfileIcon();
100             title = getUpdatedEnterpriseString(
101                     WORK_PROFILE_PAUSED_TITLE, R.string.picker_profile_work_paused_title);
102             message = getUpdatedEnterpriseString(
103                     WORK_PROFILE_PAUSED_MESSAGE, R.string.picker_profile_work_paused_msg);
104         } else {
105             icon = getContext().getDrawable(R.drawable.ic_work_outline);
106             title = getContext().getString(R.string.picker_profile_work_paused_title);
107             message = getContext().getString(R.string.picker_profile_work_paused_msg);
108         }
109         builder.setIcon(icon);
110         builder.setTitle(title);
111         builder.setMessage(message);
112         // TODO(b/197199728): Add listener to turn on apps. This maybe a bit tricky because
113         // after turning on Work profile, work profile MediaProvider may not be available
114         // immediately.
115         builder.setPositiveButton(android.R.string.ok, null);
116     }
117 
118     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
getUpdatedEnterpriseString(String updatableStringId, int defaultStringId)119     private String getUpdatedEnterpriseString(String updatableStringId, int defaultStringId) {
120         final DevicePolicyManager dpm = getContext().getSystemService(DevicePolicyManager.class);
121         return dpm.getResources().getString(updatableStringId, () -> getString(defaultStringId));
122     }
123 
124     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
getUpdatedWorkProfileIcon()125     private Drawable getUpdatedWorkProfileIcon() {
126         final DevicePolicyManager dpm = getContext().getSystemService(DevicePolicyManager.class);
127         return dpm.getResources().getDrawable(WORK_PROFILE_ICON, OUTLINE, () ->
128                 getContext().getDrawable(R.drawable.ic_work_outline));
129     }
130 
show(FragmentManager fm)131     public static void show(FragmentManager fm) {
132         FragmentTransaction ft = fm.beginTransaction();
133         Fragment f = new ProfileDialogFragment();
134         ft.add(f, TAG);
135         ft.commitAllowingStateLoss();
136     }
137 }
138