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 StringCache cache = mActivityContext.getStringCache(); 80 if (cache != null) { 81 TextView title = findViewById(R.id.work_apps_paused_title); 82 title.setText(cache.workProfileEdu); 83 } 84 } 85 86 @Override onClick(View view)87 public void onClick(View view) { 88 startAnimation(mDismissAnim); 89 LauncherPrefs.get(getContext()).put(WORK_EDU_STEP, 1); 90 } 91 92 @Override onAnimationEnd(Animation animation)93 public void onAnimationEnd(Animation animation) { 94 removeCard(); 95 } 96 97 @Override onAnimationRepeat(Animation animation)98 public void onAnimationRepeat(Animation animation) { } 99 100 @Override onAnimationStart(Animation animation)101 public void onAnimationStart(Animation animation) { } 102 removeCard()103 private void removeCard() { 104 if (mPosition == -1) { 105 if (getParent() != null) ((ViewGroup) getParent()).removeView(WorkEduCard.this); 106 } else { 107 AllAppsRecyclerView rv = mActivityContext.getAppsView().mAH.get( 108 ActivityAllAppsContainerView.AdapterHolder.WORK).mRecyclerView; 109 rv.getApps().getAdapterItems().remove(mPosition); 110 rv.getAdapter().notifyItemRemoved(mPosition); 111 } 112 } 113 114 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)115 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 116 int size = MeasureSpec.getSize(widthMeasureSpec); 117 findViewById(R.id.wrapper).getLayoutParams().width = getTabWidth(getContext(), size); 118 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 119 } 120 setPosition(int position)121 public void setPosition(int position) { 122 mPosition = position; 123 } 124 } 125