1 /*
2  * Copyright (C) 2020 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 package com.android.launcher3.allapps;
17 
18 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TURN_ON_WORK_APPS_TAP;
19 
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 import com.android.launcher3.R;
29 import com.android.launcher3.model.StringCache;
30 import com.android.launcher3.views.ActivityContext;
31 
32 /**
33  * Work profile toggle switch shown at the bottom of AllApps work tab
34  */
35 public class WorkPausedCard extends LinearLayout implements View.OnClickListener {
36 
37     private final ActivityContext mActivityContext;
38     private Button mBtn;
39 
WorkPausedCard(Context context)40     public WorkPausedCard(Context context) {
41         this(context, null, 0);
42     }
43 
WorkPausedCard(Context context, AttributeSet attrs)44     public WorkPausedCard(Context context, AttributeSet attrs) {
45         this(context, attrs, 0);
46     }
47 
WorkPausedCard(Context context, AttributeSet attrs, int defStyleAttr)48     public WorkPausedCard(Context context, AttributeSet attrs, int defStyleAttr) {
49         super(context, attrs, defStyleAttr);
50         mActivityContext = ActivityContext.lookupContext(getContext());
51     }
52 
53     @Override
onFinishInflate()54     protected void onFinishInflate() {
55         super.onFinishInflate();
56         mBtn = findViewById(R.id.enable_work_apps);
57         mBtn.setOnClickListener(this);
58 
59         updateStringFromCache();
60     }
61 
updateStringFromCache()62     public void updateStringFromCache() {
63         StringCache cache = mActivityContext.getStringCache();
64         if (cache != null) {
65             setWorkProfilePausedResources(cache);
66         }
67     }
68 
setWorkProfilePausedResources(StringCache cache)69     private void setWorkProfilePausedResources(StringCache cache) {
70         TextView title = findViewById(R.id.work_apps_paused_title);
71         title.setText(cache.workProfilePausedTitle);
72 
73         TextView body = findViewById(R.id.work_apps_paused_content);
74         body.setText(cache.workProfilePausedDescription);
75 
76         TextView button = findViewById(R.id.enable_work_apps);
77         button.setText(cache.workProfileEnableButton);
78     }
79 
80     @Override
onClick(View view)81     public void onClick(View view) {
82         setEnabled(false);
83         mActivityContext.getAppsView().getWorkManager().setWorkProfileEnabled(true);
84         mActivityContext.getStatsLogManager().logger().log(LAUNCHER_TURN_ON_WORK_APPS_TAP);
85     }
86 
87     @Override
onLayout(boolean changed, int l, int t, int r, int b)88     protected void onLayout(boolean changed, int l, int t, int r, int b) {
89         int orientation = getResources().getConfiguration().orientation;
90         getLayoutParams().height = orientation == Configuration.ORIENTATION_PORTRAIT
91                 ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT;
92         super.onLayout(changed, l, t, r, b);
93     }
94 }
95