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.launcher3.model;
18 
19 import android.annotation.SuppressLint;
20 import android.app.admin.DevicePolicyManager;
21 import android.app.admin.ManagedSubscriptionsPolicy;
22 import android.content.Context;
23 import android.os.Build;
24 
25 import androidx.annotation.RequiresApi;
26 
27 import com.android.launcher3.R;
28 import com.android.launcher3.Utilities;
29 
30 import java.util.function.Supplier;
31 
32 /**
33  *
34  * Cache for the device policy strings used in Launcher.
35  */
36 public class StringCache {
37 
38     private static final String PREFIX = "Launcher.";
39 
40     /**
41      * Work folder name.
42      */
43     public static final String WORK_FOLDER_NAME = PREFIX + "WORK_FOLDER_NAME";
44 
45     /**
46      * User on-boarding title for work profile apps.
47      */
48     private static final String WORK_PROFILE_EDU = PREFIX + "WORK_PROFILE_EDU";
49 
50     /**
51      * Action label to finish work profile edu.
52      */
53     private static final String WORK_PROFILE_EDU_ACCEPT = PREFIX + "WORK_PROFILE_EDU_ACCEPT";
54 
55     /**
56      * Title shown when user opens work apps tab while work profile is paused.
57      */
58     private static final String WORK_PROFILE_PAUSED_TITLE =
59             PREFIX + "WORK_PROFILE_PAUSED_TITLE";
60 
61     /**
62      * Description shown when user opens work apps tab while work profile is paused.
63      */
64     private static final String WORK_PROFILE_PAUSED_DESCRIPTION =
65             PREFIX + "WORK_PROFILE_PAUSED_DESCRIPTION";
66 
67     /**
68      * Shown on the button to pause work profile.
69      */
70     private static final String WORK_PROFILE_PAUSE_BUTTON =
71             PREFIX + "WORK_PROFILE_PAUSE_BUTTON";
72 
73     /**
74      * Shown on the button to enable work profile.
75      */
76     private static final String WORK_PROFILE_ENABLE_BUTTON =
77             PREFIX + "WORK_PROFILE_ENABLE_BUTTON";
78 
79     /**
80      * Label on launcher tab to indicate work apps.
81      */
82     private static final String ALL_APPS_WORK_TAB = PREFIX + "ALL_APPS_WORK_TAB";
83 
84     /**
85      * Label on launcher tab to indicate personal apps.
86      */
87     private static final String ALL_APPS_PERSONAL_TAB = PREFIX + "ALL_APPS_PERSONAL_TAB";
88 
89     /**
90      * Accessibility description for launcher tab to indicate work apps.
91      */
92     private static final String ALL_APPS_WORK_TAB_ACCESSIBILITY =
93             PREFIX + "ALL_APPS_WORK_TAB_ACCESSIBILITY";
94 
95     /**
96      * Accessibility description for launcher tab to indicate personal apps.
97      */
98     private static final String ALL_APPS_PERSONAL_TAB_ACCESSIBILITY =
99             PREFIX + "ALL_APPS_PERSONAL_TAB_ACCESSIBILITY";
100 
101     /**
102      * Label on widget tab to indicate work app widgets.
103      */
104     private static final String WIDGETS_WORK_TAB = PREFIX + "WIDGETS_WORK_TAB";
105 
106     /**
107      * Label on widget tab to indicate personal app widgets.
108      */
109     private static final String WIDGETS_PERSONAL_TAB = PREFIX + "WIDGETS_PERSONAL_TAB";
110 
111     /**
112      * Message shown when a feature is disabled by the admin (e.g. changing wallpaper).
113      */
114     private static final String DISABLED_BY_ADMIN_MESSAGE =
115             PREFIX + "DISABLED_BY_ADMIN_MESSAGE";
116 
117     /**
118      * User on-boarding title for work profile apps.
119      */
120     public String workProfileEdu;
121 
122     /**
123      * Action label to finish work profile edu.
124      */
125     public String workProfileEduAccept;
126 
127     /**
128      * Title shown when user opens work apps tab while work profile is paused.
129      */
130     public String workProfilePausedTitle;
131 
132     /**
133      * Description shown when user opens work apps tab while work profile is paused.
134      */
135     public String workProfilePausedDescription;
136 
137     /**
138      * Shown on the button to pause work profile.
139      */
140     public String workProfilePauseButton;
141 
142     /**
143      * Shown on the button to enable work profile.
144      */
145     public String workProfileEnableButton;
146 
147     /**
148      * Label on launcher tab to indicate work apps.
149      */
150     public String allAppsWorkTab;
151 
152     /**
153      * Label on launcher tab to indicate personal apps.
154      */
155     public String allAppsPersonalTab;
156 
157     /**
158      * Accessibility description for launcher tab to indicate work apps.
159      */
160     public String allAppsWorkTabAccessibility;
161 
162     /**
163      * Accessibility description for launcher tab to indicate personal apps.
164      */
165     public String allAppsPersonalTabAccessibility;
166 
167     /**
168      * Work folder name.
169      */
170     public String workFolderName;
171 
172     /**
173      * Label on widget tab to indicate work app widgets.
174      */
175     public String widgetsWorkTab;
176 
177     /**
178      * Label on widget tab to indicate personal app widgets.
179      */
180     public String widgetsPersonalTab;
181 
182     /**
183      * Message shown when a feature is disabled by the admin (e.g. changing wallpaper).
184      */
185     public String disabledByAdminMessage;
186 
187     /**
188      * Sets the default values for the strings.
189      */
loadStrings(Context context)190     public void loadStrings(Context context) {
191         workProfileEdu = getEnterpriseString(
192                 context, WORK_PROFILE_EDU, R.string.work_profile_edu_work_apps);
193         workProfileEduAccept = getEnterpriseString(
194                 context, WORK_PROFILE_EDU_ACCEPT, R.string.work_profile_edu_accept);
195         workProfilePausedTitle = getEnterpriseString(
196                 context, WORK_PROFILE_PAUSED_TITLE, R.string.work_apps_paused_title);
197         workProfilePausedDescription = getEnterpriseString(
198                 context,
199                 WORK_PROFILE_PAUSED_DESCRIPTION,
200                 () -> getDefaultWorkProfilePausedDescriptionString(context));
201         workProfilePauseButton = getEnterpriseString(
202                 context, WORK_PROFILE_PAUSE_BUTTON, R.string.work_apps_pause_btn_text);
203         workProfileEnableButton = getEnterpriseString(
204                 context, WORK_PROFILE_ENABLE_BUTTON, R.string.work_apps_enable_btn_text);
205         allAppsWorkTab = getEnterpriseString(
206                 context, ALL_APPS_WORK_TAB, R.string.all_apps_work_tab);
207         allAppsPersonalTab = getEnterpriseString(
208                 context, ALL_APPS_PERSONAL_TAB, R.string.all_apps_personal_tab);
209         allAppsWorkTabAccessibility = getEnterpriseString(
210                 context, ALL_APPS_WORK_TAB_ACCESSIBILITY, R.string.all_apps_button_work_label);
211         allAppsPersonalTabAccessibility = getEnterpriseString(
212                 context, ALL_APPS_PERSONAL_TAB_ACCESSIBILITY,
213                 R.string.all_apps_button_personal_label);
214         workFolderName = getEnterpriseString(
215                 context, WORK_FOLDER_NAME, R.string.work_folder_name);
216         widgetsWorkTab = getEnterpriseString(
217                 context, WIDGETS_WORK_TAB, R.string.widgets_full_sheet_work_tab);
218         widgetsPersonalTab = getEnterpriseString(
219                 context, WIDGETS_PERSONAL_TAB, R.string.widgets_full_sheet_personal_tab);
220         disabledByAdminMessage = getEnterpriseString(
221                 context, DISABLED_BY_ADMIN_MESSAGE, R.string.msg_disabled_by_admin);
222     }
223 
getDefaultWorkProfilePausedDescriptionString(Context context)224     private String getDefaultWorkProfilePausedDescriptionString(Context context) {
225         if (Utilities.ATLEAST_U) {
226             DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
227             boolean telephonyIsUnavailable =
228                     dpm.getManagedSubscriptionsPolicy().getPolicyType()
229                             == ManagedSubscriptionsPolicy.TYPE_ALL_MANAGED_SUBSCRIPTIONS;
230             return telephonyIsUnavailable
231                     ? context.getString(R.string.work_apps_paused_telephony_unavailable_body)
232                     : context.getString(R.string.work_apps_paused_info_body);
233         }
234         return context.getString(R.string.work_apps_paused_body);
235     }
236 
237     @SuppressLint("NewApi")
getEnterpriseString( Context context, String updatableStringId, int defaultStringId)238     private String getEnterpriseString(
239             Context context, String updatableStringId, int defaultStringId) {
240         return getEnterpriseString(
241                 context,
242                 updatableStringId,
243                 () -> context.getString(defaultStringId));
244     }
245 
246     @SuppressLint("NewApi")
getEnterpriseString( Context context, String updateableStringId, Supplier<String> defaultStringSupplier)247     private String getEnterpriseString(
248             Context context, String updateableStringId, Supplier<String> defaultStringSupplier) {
249         return Utilities.ATLEAST_T
250                 ? getUpdatableEnterpriseString(context, updateableStringId, defaultStringSupplier)
251                 : defaultStringSupplier.get();
252     }
253 
254     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
getUpdatableEnterpriseString( Context context, String updatableStringId, Supplier<String> defaultStringSupplier)255     private String getUpdatableEnterpriseString(
256             Context context, String updatableStringId, Supplier<String> defaultStringSupplier) {
257         DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
258         return dpm.getResources().getString(updatableStringId, defaultStringSupplier);
259     }
260 
261     @Override
clone()262     public StringCache clone() {
263         StringCache clone = new StringCache();
264         clone.workProfileEdu = this.workProfileEdu;
265         clone.workProfileEduAccept = this.workProfileEduAccept;
266         clone.workProfilePausedTitle = this.workProfilePausedTitle;
267         clone.workProfilePausedDescription = this.workProfilePausedDescription;
268         clone.workProfilePauseButton = this.workProfilePauseButton;
269         clone.workProfileEnableButton = this.workProfileEnableButton;
270         clone.allAppsWorkTab = this.allAppsWorkTab;
271         clone.allAppsPersonalTab = this.allAppsPersonalTab;
272         clone.allAppsWorkTabAccessibility = this.allAppsWorkTabAccessibility;
273         clone.allAppsPersonalTabAccessibility = this.allAppsPersonalTabAccessibility;
274         clone.workFolderName = this.workFolderName;
275         clone.widgetsWorkTab = this.widgetsWorkTab;
276         clone.widgetsPersonalTab = this.widgetsPersonalTab;
277         clone.disabledByAdminMessage = this.disabledByAdminMessage;
278         return clone;
279     }
280 }
281