diff --git a/app/src/full/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java b/app/src/full/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java index 5ac968890..603bf1da5 100644 --- a/app/src/full/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java +++ b/app/src/full/java/com/topjohnwu/magisk/adapters/PolicyAdapter.java @@ -15,7 +15,7 @@ import com.topjohnwu.core.container.Policy; import com.topjohnwu.core.database.MagiskDB; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.CustomAlertDialog; -import com.topjohnwu.magisk.components.ExpandableView; +import com.topjohnwu.magisk.components.ExpandableViewHolder; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.utils.FingerprintHelper; @@ -51,13 +51,13 @@ public class PolicyAdapter extends RecyclerView.Adapter { - if (holder.isExpanded()) { - holder.collapse(); + if (holder.settings.isExpanded()) { + holder.settings.collapse(); expandList[position] = false; } else { - holder.expand(); + holder.settings.expand(); expandList[position] = true; } }); @@ -136,7 +136,7 @@ public class PolicyAdapter extends RecyclerView.Adapter { - if (holder.isExpanded()) { - holder.collapse(); + if (holder.expandable.isExpanded()) { + holder.expandable.collapse(); itemExpanded.remove(realIdx); } else { - holder.expand(); + holder.expandable.expand(); itemExpanded.add(realIdx); } }); @@ -122,7 +122,7 @@ public class SuLogAdapter extends SectionedAdapter { - int value = (Integer) valueAnimator.getAnimatedValue(); - ViewGroup.LayoutParams layoutParams = container.expandLayout.getLayoutParams(); - layoutParams.height = value; - container.expandLayout.setLayoutParams(layoutParams); - }); - return animator; - } -} diff --git a/app/src/full/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java b/app/src/full/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java new file mode 100644 index 000000000..da1b92846 --- /dev/null +++ b/app/src/full/java/com/topjohnwu/magisk/components/ExpandableViewHolder.java @@ -0,0 +1,72 @@ +package com.topjohnwu.magisk.components; + +import android.animation.ValueAnimator; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver; + +public class ExpandableViewHolder { + + private ViewGroup expandLayout; + private ValueAnimator expandAnimator, collapseAnimator; + private boolean mExpanded = false; + private int expandHeight = 0; + + public ExpandableViewHolder(ViewGroup viewGroup) { + expandLayout = viewGroup; + setExpanded(false); + expandLayout.getViewTreeObserver().addOnPreDrawListener( + new ViewTreeObserver.OnPreDrawListener() { + + @Override + public boolean onPreDraw() { + if (expandHeight == 0) { + expandLayout.measure(0, 0); + expandHeight = expandLayout.getMeasuredHeight(); + } + + expandLayout.getViewTreeObserver().removeOnPreDrawListener(this); + expandAnimator = slideAnimator(0, expandHeight); + collapseAnimator = slideAnimator(expandHeight, 0); + return true; + } + + }); + } + + public boolean isExpanded() { + return mExpanded; + } + + public void setExpanded(boolean expanded) { + mExpanded = expanded; + ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); + layoutParams.height = expanded ? expandHeight : 0; + expandLayout.setLayoutParams(layoutParams); + } + + public void expand() { + if (mExpanded) return; + expandLayout.setVisibility(View.VISIBLE); + expandAnimator.start(); + mExpanded = true; + } + + public void collapse() { + if (!mExpanded) return; + collapseAnimator.start(); + mExpanded = false; + } + + private ValueAnimator slideAnimator(int start, int end) { + ValueAnimator animator = ValueAnimator.ofInt(start, end); + + animator.addUpdateListener(valueAnimator -> { + int value = (Integer) valueAnimator.getAnimatedValue(); + ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams(); + layoutParams.height = value; + expandLayout.setLayoutParams(layoutParams); + }); + return animator; + } +} diff --git a/app/src/full/java/com/topjohnwu/magisk/fragments/MagiskFragment.java b/app/src/full/java/com/topjohnwu/magisk/fragments/MagiskFragment.java index b2484f38f..6a69e77f1 100644 --- a/app/src/full/java/com/topjohnwu/magisk/fragments/MagiskFragment.java +++ b/app/src/full/java/com/topjohnwu/magisk/fragments/MagiskFragment.java @@ -24,7 +24,7 @@ import com.topjohnwu.magisk.components.BaseActivity; import com.topjohnwu.magisk.components.BaseFragment; import com.topjohnwu.magisk.components.CustomAlertDialog; import com.topjohnwu.magisk.components.EnvFixDialog; -import com.topjohnwu.magisk.components.ExpandableView; +import com.topjohnwu.magisk.components.ExpandableViewHolder; import com.topjohnwu.magisk.components.MagiskInstallDialog; import com.topjohnwu.magisk.components.ManagerInstallDialog; import com.topjohnwu.magisk.components.UninstallDialog; @@ -50,9 +50,8 @@ import butterknife.BindView; import butterknife.OnClick; public class MagiskFragment extends BaseFragment - implements SwipeRefreshLayout.OnRefreshListener, ExpandableView, Topic.Subscriber { + implements SwipeRefreshLayout.OnRefreshListener, Topic.Subscriber { - private Container expandableContainer = new Container(); private static boolean shownDialog = false; @BindView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout; @@ -83,6 +82,7 @@ public class MagiskFragment extends BaseFragment private UpdateCardHolder magisk; private UpdateCardHolder manager; + private ExpandableViewHolder snExpandableHolder; private Transition transition; @OnClick(R.id.safetyNet_title) @@ -92,7 +92,7 @@ public class MagiskFragment extends BaseFragment safetyNetRefreshIcon.setVisibility(View.GONE); safetyNetStatusText.setText(R.string.checking_safetyNet_status); SafetyNet.check(requireActivity()); - collapse(); + snExpandableHolder.collapse(); }; if (!SafetyNet.EXT_APK.exists()) { // Show dialog @@ -134,8 +134,7 @@ public class MagiskFragment extends BaseFragment unbinder = new MagiskFragment_ViewBinding(this, v); requireActivity().setTitle(R.string.magisk); - expandableContainer.expandLayout = expandLayout; - setupExpandable(); + snExpandableHolder = new ExpandableViewHolder(expandLayout); magisk = new UpdateCardHolder(inflater, root); manager = new UpdateCardHolder(inflater, root); @@ -168,7 +167,7 @@ public class MagiskFragment extends BaseFragment @Override public void onRefresh() { safetyNetStatusText.setText(R.string.safetyNet_check_text); - setExpanded(false); + snExpandableHolder.setExpanded(false); mSwipeRefreshLayout.setRefreshing(false); TransitionManager.beginDelayedTransition(root, transition); @@ -207,11 +206,6 @@ public class MagiskFragment extends BaseFragment } } - @Override - public Container getContainer() { - return expandableContainer; - } - private boolean hasGms() { PackageManager pm = app.getPackageManager(); PackageInfo info; @@ -352,7 +346,7 @@ public class MagiskFragment extends BaseFragment basicStatusIcon.setImageResource(b ? R.drawable.ic_check_circle : R.drawable.ic_cancel); basicStatusIcon.setColorFilter(b ? colorOK : colorBad); - expand(); + snExpandableHolder.expand(); } else { @StringRes int resid; switch (response) {