2017-01-26 01:13:23 +08:00
|
|
|
package com.topjohnwu.magisk.adapters;
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
import android.content.pm.PackageManager;
|
2017-01-26 03:30:12 +08:00
|
|
|
import android.support.design.widget.Snackbar;
|
2017-01-26 01:13:23 +08:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.ViewTreeObserver;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.Switch;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.R;
|
2017-02-15 05:24:02 +08:00
|
|
|
import com.topjohnwu.magisk.components.AlertDialogBuilder;
|
|
|
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
2017-02-12 23:26:30 +08:00
|
|
|
import com.topjohnwu.magisk.database.SuDatabaseHelper;
|
2017-01-26 01:13:23 +08:00
|
|
|
import com.topjohnwu.magisk.superuser.Policy;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2017-01-29 20:50:58 +01:00
|
|
|
import java.util.Set;
|
2017-01-26 01:13:23 +08:00
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.ViewHolder> {
|
|
|
|
|
|
|
|
private List<Policy> policyList;
|
|
|
|
private SuDatabaseHelper dbHelper;
|
|
|
|
private PackageManager pm;
|
2017-01-29 20:50:58 +01:00
|
|
|
private Set<Policy> expandList = new HashSet<>();
|
2017-01-26 01:13:23 +08:00
|
|
|
|
|
|
|
public PolicyAdapter(List<Policy> list, SuDatabaseHelper db, PackageManager pm) {
|
|
|
|
policyList = list;
|
|
|
|
dbHelper = db;
|
|
|
|
this.pm = pm;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_policy, parent, false);
|
|
|
|
return new ViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
2017-01-26 03:30:12 +08:00
|
|
|
Policy policy = policyList.get(position);
|
2017-01-26 01:13:23 +08:00
|
|
|
try {
|
|
|
|
holder.setExpanded(expandList.contains(policy));
|
|
|
|
|
|
|
|
holder.itemView.setOnClickListener(view -> {
|
|
|
|
if (holder.mExpanded) {
|
|
|
|
holder.collapse();
|
|
|
|
expandList.remove(policy);
|
|
|
|
} else {
|
|
|
|
holder.expand();
|
|
|
|
expandList.add(policy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
holder.appName.setText(policy.appName);
|
|
|
|
holder.packageName.setText(policy.packageName);
|
|
|
|
holder.appIcon.setImageDrawable(pm.getPackageInfo(policy.packageName, 0).applicationInfo.loadIcon(pm));
|
|
|
|
holder.masterSwitch.setOnCheckedChangeListener((v, isChecked) -> {
|
2017-01-26 03:30:12 +08:00
|
|
|
if ((isChecked && policy.policy == Policy.DENY) ||
|
|
|
|
(!isChecked && policy.policy == Policy.ALLOW)) {
|
|
|
|
policy.policy = isChecked ? Policy.ALLOW : Policy.DENY;
|
|
|
|
String message = v.getContext().getString(
|
|
|
|
isChecked ? R.string.su_snack_grant : R.string.su_snack_deny, policy.appName);
|
2017-02-15 05:24:02 +08:00
|
|
|
SnackbarMaker.make(holder.itemView, message, Snackbar.LENGTH_SHORT).show();
|
2017-05-31 16:31:33 +08:00
|
|
|
dbHelper.updatePolicy(policy);
|
2017-01-26 01:13:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
holder.notificationSwitch.setOnCheckedChangeListener((v, isChecked) -> {
|
2017-01-26 03:30:12 +08:00
|
|
|
if ((isChecked && !policy.notification) ||
|
|
|
|
(!isChecked && policy.notification)) {
|
|
|
|
policy.notification = isChecked;
|
|
|
|
String message = v.getContext().getString(
|
|
|
|
isChecked ? R.string.su_snack_notif_on : R.string.su_snack_notif_off, policy.appName);
|
2017-02-15 05:24:02 +08:00
|
|
|
SnackbarMaker.make(holder.itemView, message, Snackbar.LENGTH_SHORT).show();
|
2017-05-31 16:31:33 +08:00
|
|
|
dbHelper.updatePolicy(policy);
|
2017-01-26 01:13:23 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
holder.loggingSwitch.setOnCheckedChangeListener((v, isChecked) -> {
|
2017-01-26 03:30:12 +08:00
|
|
|
if ((isChecked && !policy.logging) ||
|
|
|
|
(!isChecked && policy.logging)) {
|
|
|
|
policy.logging = isChecked;
|
|
|
|
String message = v.getContext().getString(
|
|
|
|
isChecked ? R.string.su_snack_log_on : R.string.su_snack_log_off, policy.appName);
|
2017-02-15 05:24:02 +08:00
|
|
|
SnackbarMaker.make(holder.itemView, message, Snackbar.LENGTH_SHORT).show();
|
2017-05-31 16:31:33 +08:00
|
|
|
dbHelper.updatePolicy(policy);
|
2017-01-26 01:13:23 +08:00
|
|
|
}
|
|
|
|
});
|
2017-02-15 05:24:02 +08:00
|
|
|
holder.delete.setOnClickListener(v -> new AlertDialogBuilder(v.getContext())
|
2017-01-26 03:30:12 +08:00
|
|
|
.setTitle(R.string.su_revoke_title)
|
|
|
|
.setMessage(v.getContext().getString(R.string.su_revoke_msg, policy.appName))
|
|
|
|
.setPositiveButton(R.string.yes, (dialog, which) -> {
|
|
|
|
policyList.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
notifyItemRangeChanged(position, policyList.size());
|
2017-02-15 05:24:02 +08:00
|
|
|
SnackbarMaker.make(holder.itemView, v.getContext().getString(R.string.su_snack_revoke, policy.appName),
|
2017-01-26 03:30:12 +08:00
|
|
|
Snackbar.LENGTH_SHORT).show();
|
2017-05-31 16:31:33 +08:00
|
|
|
dbHelper.deletePolicy(policy);
|
2017-01-26 03:30:12 +08:00
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.setCancelable(true)
|
|
|
|
.show());
|
2017-01-26 01:13:23 +08:00
|
|
|
holder.masterSwitch.setChecked(policy.policy == Policy.ALLOW);
|
|
|
|
holder.notificationSwitch.setChecked(policy.notification);
|
|
|
|
holder.loggingSwitch.setChecked(policy.logging);
|
|
|
|
|
2017-01-28 01:10:50 +08:00
|
|
|
// Hide for now
|
|
|
|
holder.moreInfo.setVisibility(View.GONE);
|
|
|
|
|
2017-01-26 01:13:23 +08:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
policyList.remove(position);
|
2017-05-31 16:31:33 +08:00
|
|
|
dbHelper.deletePolicy(policy);
|
2017-01-26 01:13:23 +08:00
|
|
|
onBindViewHolder(holder, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return policyList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
@BindView(R.id.app_name) TextView appName;
|
|
|
|
@BindView(R.id.package_name) TextView packageName;
|
|
|
|
@BindView(R.id.expand_layout) LinearLayout expandLayout;
|
|
|
|
@BindView(R.id.app_icon) ImageView appIcon;
|
|
|
|
@BindView(R.id.master_switch) Switch masterSwitch;
|
|
|
|
@BindView(R.id.notification_switch) Switch notificationSwitch;
|
|
|
|
@BindView(R.id.logging_switch) Switch loggingSwitch;
|
|
|
|
|
2017-01-26 03:30:12 +08:00
|
|
|
@BindView(R.id.delete) ImageView delete;
|
2017-01-26 01:13:23 +08:00
|
|
|
@BindView(R.id.more_info) ImageView moreInfo;
|
|
|
|
|
|
|
|
private ValueAnimator mAnimator;
|
|
|
|
private boolean mExpanded = false;
|
|
|
|
private static int expandHeight = 0;
|
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
ButterKnife.bind(this, itemView);
|
|
|
|
expandLayout.getViewTreeObserver().addOnPreDrawListener(
|
|
|
|
new ViewTreeObserver.OnPreDrawListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreDraw() {
|
|
|
|
if (expandHeight == 0) {
|
|
|
|
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
expandLayout.measure(widthSpec, heightSpec);
|
|
|
|
expandHeight = expandLayout.getMeasuredHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
|
|
|
|
expandLayout.setVisibility(View.GONE);
|
|
|
|
mAnimator = slideAnimator(0, expandHeight);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setExpanded(boolean expanded) {
|
|
|
|
mExpanded = expanded;
|
|
|
|
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
|
|
|
|
layoutParams.height = expanded ? expandHeight : 0;
|
|
|
|
expandLayout.setLayoutParams(layoutParams);
|
|
|
|
expandLayout.setVisibility(expanded ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void expand() {
|
|
|
|
expandLayout.setVisibility(View.VISIBLE);
|
|
|
|
mAnimator.start();
|
|
|
|
mExpanded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void collapse() {
|
|
|
|
if (!mExpanded) return;
|
|
|
|
int finalHeight = expandLayout.getHeight();
|
|
|
|
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
|
|
|
|
mAnimator.addListener(new Animator.AnimatorListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animator animator) {
|
|
|
|
expandLayout.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationStart(Animator animator) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationCancel(Animator animator) {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAnimationRepeat(Animator animator) {}
|
|
|
|
});
|
|
|
|
mAnimator.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|