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 android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.view.animation.Animation;
23 import android.view.animation.AnimationUtils;
24 import android.widget.FrameLayout;
25 
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.R;
28 
29 /**
30  * Work profile toggle switch shown at the bottom of AllApps work tab
31  */
32 public class WorkEduCard extends FrameLayout implements View.OnClickListener,
33         Animation.AnimationListener {
34 
35     private final Launcher mLauncher;
36     Animation mDismissAnim;
37     private int mPosition = -1;
38 
WorkEduCard(Context context)39     public WorkEduCard(Context context) {
40         this(context, null, 0);
41     }
42 
WorkEduCard(Context context, AttributeSet attrs)43     public WorkEduCard(Context context, AttributeSet attrs) {
44         this(context, attrs, 0);
45     }
46 
WorkEduCard(Context context, AttributeSet attrs, int defStyleAttr)47     public WorkEduCard(Context context, AttributeSet attrs, int defStyleAttr) {
48         super(context, attrs, defStyleAttr);
49         mLauncher = Launcher.getLauncher(getContext());
50         mDismissAnim = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
51         mDismissAnim.setDuration(500);
52         mDismissAnim.setAnimationListener(this);
53     }
54 
55     @Override
onAttachedToWindow()56     protected void onAttachedToWindow() {
57         super.onAttachedToWindow();
58         mDismissAnim.reset();
59     }
60 
61     @Override
onDetachedFromWindow()62     protected void onDetachedFromWindow() {
63         super.onDetachedFromWindow();
64         mDismissAnim.cancel();
65     }
66 
67     @Override
onFinishInflate()68     protected void onFinishInflate() {
69         super.onFinishInflate();
70         findViewById(R.id.action_btn).setOnClickListener(this);
71         MarginLayoutParams lp = ((MarginLayoutParams) findViewById(R.id.wrapper).getLayoutParams());
72         lp.width = mLauncher.getAppsView().getActiveRecyclerView().getTabWidth();
73     }
74 
75     @Override
onClick(View view)76     public void onClick(View view) {
77         startAnimation(mDismissAnim);
78         mLauncher.getSharedPrefs().edit().putInt(WorkAdapterProvider.KEY_WORK_EDU_STEP, 1).apply();
79     }
80 
81     @Override
onAnimationEnd(Animation animation)82     public void onAnimationEnd(Animation animation) {
83         removeCard();
84     }
85 
86     @Override
onAnimationRepeat(Animation animation)87     public void onAnimationRepeat(Animation animation) {
88 
89     }
90 
91     @Override
onAnimationStart(Animation animation)92     public void onAnimationStart(Animation animation) {
93 
94     }
95 
removeCard()96     private void removeCard() {
97         if (mPosition == -1) {
98             if (getParent() != null) ((ViewGroup) getParent()).removeView(WorkEduCard.this);
99         } else {
100             AllAppsRecyclerView rv = mLauncher.getAppsView()
101                     .mAH[AllAppsContainerView.AdapterHolder.WORK].recyclerView;
102             rv.getApps().getAdapterItems().remove(mPosition);
103             rv.getAdapter().notifyItemRemoved(mPosition);
104         }
105     }
106 
setPosition(int position)107     public void setPosition(int position) {
108         mPosition = position;
109     }
110 
111 }
112