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.LauncherPrefs.WORK_EDU_STEP;
19 import static com.android.launcher3.workprofile.PersonalWorkSlidingTabStrip.getTabWidth;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.view.animation.Animation;
26 import android.view.animation.AnimationUtils;
27 import android.widget.FrameLayout;
28 import android.widget.TextView;
29 
30 import com.android.launcher3.LauncherPrefs;
31 import com.android.launcher3.R;
32 import com.android.launcher3.model.StringCache;
33 import com.android.launcher3.views.ActivityContext;
34 
35 /**
36  * Work profile toggle switch shown at the bottom of AllApps work tab
37  */
38 public class WorkEduCard extends FrameLayout implements
39         View.OnClickListener,
40         Animation.AnimationListener {
41 
42     private final ActivityContext mActivityContext;
43     Animation mDismissAnim;
44     private int mPosition = -1;
45 
WorkEduCard(Context context)46     public WorkEduCard(Context context) {
47         this(context, null, 0);
48     }
49 
WorkEduCard(Context context, AttributeSet attrs)50     public WorkEduCard(Context context, AttributeSet attrs) {
51         this(context, attrs, 0);
52     }
53 
WorkEduCard(Context context, AttributeSet attrs, int defStyleAttr)54     public WorkEduCard(Context context, AttributeSet attrs, int defStyleAttr) {
55         super(context, attrs, defStyleAttr);
56         mActivityContext = ActivityContext.lookupContext(getContext());
57         mDismissAnim = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
58         mDismissAnim.setDuration(500);
59         mDismissAnim.setAnimationListener(this);
60     }
61 
62     @Override
onAttachedToWindow()63     protected void onAttachedToWindow() {
64         super.onAttachedToWindow();
65         mDismissAnim.reset();
66     }
67 
68     @Override
onDetachedFromWindow()69     protected void onDetachedFromWindow() {
70         super.onDetachedFromWindow();
71         mDismissAnim.cancel();
72     }
73 
74     @Override
onFinishInflate()75     protected void onFinishInflate() {
76         super.onFinishInflate();
77         findViewById(R.id.action_btn).setOnClickListener(this);
78 
79         updateStringFromCache();
80     }
81 
82     @Override
onClick(View view)83     public void onClick(View view) {
84         startAnimation(mDismissAnim);
85         LauncherPrefs.get(getContext()).put(WORK_EDU_STEP, 1);
86     }
87 
88     @Override
onAnimationEnd(Animation animation)89     public void onAnimationEnd(Animation animation) {
90         removeCard();
91     }
92 
93     @Override
onAnimationRepeat(Animation animation)94     public void onAnimationRepeat(Animation animation) { }
95 
96     @Override
onAnimationStart(Animation animation)97     public void onAnimationStart(Animation animation) { }
98 
removeCard()99     private void removeCard() {
100         if (mPosition == -1) {
101             if (getParent() != null) ((ViewGroup) getParent()).removeView(WorkEduCard.this);
102         } else {
103             AllAppsRecyclerView rv = mActivityContext.getAppsView().mAH.get(
104                     ActivityAllAppsContainerView.AdapterHolder.WORK).mRecyclerView;
105             rv.getApps().getAdapterItems().remove(mPosition);
106             // Remove the educard fast scroll section.
107             rv.getApps().getFastScrollerSections().remove(0);
108             rv.getAdapter().notifyItemRemoved(mPosition);
109         }
110     }
111 
112     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)113     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
114         int size = MeasureSpec.getSize(widthMeasureSpec);
115         findViewById(R.id.wrapper).getLayoutParams().width = getTabWidth(getContext(), size);
116         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
117     }
118 
setPosition(int position)119     public void setPosition(int position) {
120         mPosition = position;
121     }
122 
updateStringFromCache()123     public void updateStringFromCache() {
124         StringCache cache = mActivityContext.getStringCache();
125         if (cache != null) {
126             TextView title = findViewById(R.id.work_apps_paused_title);
127             title.setText(cache.workProfileEdu);
128         }
129     }
130 }
131