NetGuard/app/src/main/java/eu/faircode/netguard/AdapterRule.java

1007 lines
43 KiB
Java
Raw Normal View History

2015-10-24 18:01:55 +00:00
package eu.faircode.netguard;
2015-11-03 17:57:29 +00:00
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
2017-12-24 09:12:10 +00:00
Copyright 2015-2018 by Marcel Bokhorst (M66B)
2015-11-03 17:57:29 +00:00
*/
import android.annotation.TargetApi;
2018-01-10 05:26:29 +00:00
import android.content.ClipData;
import android.content.ClipboardManager;
2015-10-24 18:01:55 +00:00
import android.content.Context;
import android.content.Intent;
2015-10-24 18:01:55 +00:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
2015-10-25 16:52:33 +00:00
import android.content.res.TypedArray;
2016-01-30 13:26:30 +00:00
import android.database.Cursor;
2015-10-25 21:12:08 +00:00
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
2016-02-04 05:55:24 +00:00
import android.os.AsyncTask;
import android.os.Build;
2015-11-24 17:34:41 +00:00
import android.preference.PreferenceManager;
2016-07-29 07:46:20 +00:00
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
2015-10-24 18:01:55 +00:00
import android.util.Log;
2016-01-02 12:22:18 +00:00
import android.util.TypedValue;
2015-10-24 18:01:55 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
2016-01-30 13:26:30 +00:00
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.TouchDelegate;
2015-10-24 18:01:55 +00:00
import android.view.View;
import android.view.ViewGroup;
2016-01-30 13:26:30 +00:00
import android.widget.AdapterView;
2015-10-30 11:41:05 +00:00
import android.widget.Button;
2015-10-24 18:01:55 +00:00
import android.widget.CheckBox;
import android.widget.CompoundButton;
2017-08-12 14:26:39 +00:00
import android.widget.CursorAdapter;
2015-10-25 10:11:46 +00:00
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
2015-10-24 18:50:27 +00:00
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
2016-01-30 13:26:30 +00:00
import android.widget.PopupMenu;
2017-03-21 08:06:04 +00:00
import android.widget.RelativeLayout;
2015-10-24 18:01:55 +00:00
import android.widget.TextView;
2018-02-01 13:29:40 +00:00
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
2016-02-06 09:12:11 +00:00
import java.text.SimpleDateFormat;
2015-10-25 10:11:46 +00:00
import java.util.ArrayList;
2015-10-24 18:01:55 +00:00
import java.util.List;
2016-07-17 12:42:20 +00:00
2018-11-17 16:32:36 +00:00
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.widget.CompoundButtonCompat;
import androidx.recyclerview.widget.RecyclerView;
public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> implements Filterable {
2015-10-26 16:32:03 +00:00
private static final String TAG = "NetGuard.Adapter";
2015-10-24 18:01:55 +00:00
2018-02-04 08:04:59 +00:00
private View anchor;
2016-12-29 08:27:58 +00:00
private LayoutInflater inflater;
private RecyclerView rv;
2015-10-25 16:52:33 +00:00
private int colorText;
private int colorChanged;
private int colorOn;
private int colorOff;
private int colorGrayed;
private int iconSize;
private boolean wifiActive = true;
private boolean otherActive = true;
private boolean live = true;
2015-10-31 13:15:26 +00:00
private List<Rule> listAll = new ArrayList<>();
2016-02-01 06:55:04 +00:00
private List<Rule> listFiltered = new ArrayList<>();
2015-10-24 18:01:55 +00:00
public static class ViewHolder extends RecyclerView.ViewHolder {
2015-10-24 18:50:27 +00:00
public View view;
2015-11-01 13:16:57 +00:00
public LinearLayout llApplication;
2015-10-24 18:50:27 +00:00
public ImageView ivIcon;
2015-10-30 07:39:17 +00:00
public ImageView ivExpander;
2015-10-24 18:01:55 +00:00
public TextView tvName;
2016-02-02 19:39:26 +00:00
public TextView tvHosts;
2017-03-21 08:06:04 +00:00
public RelativeLayout rlLockdown;
public ImageView ivLockdown;
2015-10-24 18:01:55 +00:00
public CheckBox cbWifi;
2015-12-08 16:00:40 +00:00
public ImageView ivScreenWifi;
2015-10-24 18:01:55 +00:00
public CheckBox cbOther;
2015-11-21 08:48:33 +00:00
public ImageView ivScreenOther;
2015-11-01 13:16:57 +00:00
public TextView tvRoaming;
2015-10-29 11:52:36 +00:00
public LinearLayout llConfiguration;
2015-11-18 18:08:04 +00:00
public TextView tvUid;
2015-11-13 18:46:35 +00:00
public TextView tvPackage;
2015-11-18 18:08:04 +00:00
public TextView tvVersion;
2015-11-13 18:46:35 +00:00
public TextView tvInternet;
2016-07-14 04:30:10 +00:00
public TextView tvDisabled;
2015-12-08 16:00:40 +00:00
public Button btnRelated;
public ImageButton ibSettings;
public ImageButton ibLaunch;
public CheckBox cbApply;
public LinearLayout llScreenWifi;
public ImageView ivWifiLegend;
2015-11-21 08:48:33 +00:00
public CheckBox cbScreenWifi;
2015-12-08 16:00:40 +00:00
public LinearLayout llScreenOther;
public ImageView ivOtherLegend;
2015-11-21 08:48:33 +00:00
public CheckBox cbScreenOther;
2015-11-01 13:16:57 +00:00
public CheckBox cbRoaming;
2015-12-08 16:00:40 +00:00
2017-03-21 08:06:04 +00:00
public CheckBox cbLockdown;
public ImageView ivLockdownLegend;
2015-12-27 08:07:38 +00:00
public ImageButton btnClear;
2015-10-24 18:01:55 +00:00
public LinearLayout llFilter;
public ImageView ivLive;
public TextView tvLogging;
public Button btnLogging;
2016-07-14 04:30:10 +00:00
public ListView lvAccess;
2016-07-14 04:48:54 +00:00
public ImageButton btnClearAccess;
public CheckBox cbNotify;
2015-10-24 18:01:55 +00:00
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
2015-11-01 13:16:57 +00:00
2017-08-05 08:27:27 +00:00
llApplication = itemView.findViewById(R.id.llApplication);
ivIcon = itemView.findViewById(R.id.ivIcon);
ivExpander = itemView.findViewById(R.id.ivExpander);
tvName = itemView.findViewById(R.id.tvName);
2015-11-01 13:16:57 +00:00
2017-08-05 08:27:27 +00:00
tvHosts = itemView.findViewById(R.id.tvHosts);
2016-02-02 19:39:26 +00:00
2017-08-05 08:27:27 +00:00
rlLockdown = itemView.findViewById(R.id.rlLockdown);
ivLockdown = itemView.findViewById(R.id.ivLockdown);
2017-03-21 08:06:04 +00:00
2017-08-05 08:27:27 +00:00
cbWifi = itemView.findViewById(R.id.cbWifi);
ivScreenWifi = itemView.findViewById(R.id.ivScreenWifi);
2015-12-08 16:00:40 +00:00
2017-08-05 08:27:27 +00:00
cbOther = itemView.findViewById(R.id.cbOther);
ivScreenOther = itemView.findViewById(R.id.ivScreenOther);
tvRoaming = itemView.findViewById(R.id.tvRoaming);
2015-11-01 13:16:57 +00:00
2017-08-05 08:27:27 +00:00
llConfiguration = itemView.findViewById(R.id.llConfiguration);
tvUid = itemView.findViewById(R.id.tvUid);
tvPackage = itemView.findViewById(R.id.tvPackage);
tvVersion = itemView.findViewById(R.id.tvVersion);
tvInternet = itemView.findViewById(R.id.tvInternet);
tvDisabled = itemView.findViewById(R.id.tvDisabled);
2015-12-08 16:00:40 +00:00
2017-08-05 08:27:27 +00:00
btnRelated = itemView.findViewById(R.id.btnRelated);
ibSettings = itemView.findViewById(R.id.ibSettings);
ibLaunch = itemView.findViewById(R.id.ibLaunch);
2017-08-05 08:27:27 +00:00
cbApply = itemView.findViewById(R.id.cbApply);
2017-08-05 08:27:27 +00:00
llScreenWifi = itemView.findViewById(R.id.llScreenWifi);
ivWifiLegend = itemView.findViewById(R.id.ivWifiLegend);
cbScreenWifi = itemView.findViewById(R.id.cbScreenWifi);
2015-12-08 16:00:40 +00:00
2017-08-05 08:27:27 +00:00
llScreenOther = itemView.findViewById(R.id.llScreenOther);
ivOtherLegend = itemView.findViewById(R.id.ivOtherLegend);
cbScreenOther = itemView.findViewById(R.id.cbScreenOther);
2017-08-05 08:27:27 +00:00
cbRoaming = itemView.findViewById(R.id.cbRoaming);
2015-12-08 16:00:40 +00:00
2017-08-05 08:27:27 +00:00
cbLockdown = itemView.findViewById(R.id.cbLockdown);
ivLockdownLegend = itemView.findViewById(R.id.ivLockdownLegend);
2017-03-21 08:06:04 +00:00
2017-08-05 08:27:27 +00:00
btnClear = itemView.findViewById(R.id.btnClear);
2017-08-05 08:27:27 +00:00
llFilter = itemView.findViewById(R.id.llFilter);
ivLive = itemView.findViewById(R.id.ivLive);
tvLogging = itemView.findViewById(R.id.tvLogging);
btnLogging = itemView.findViewById(R.id.btnLogging);
lvAccess = itemView.findViewById(R.id.lvAccess);
btnClearAccess = itemView.findViewById(R.id.btnClearAccess);
cbNotify = itemView.findViewById(R.id.cbNotify);
final View wifiParent = (View) cbWifi.getParent();
wifiParent.post(new Runnable() {
public void run() {
Rect rect = new Rect();
cbWifi.getHitRect(rect);
rect.bottom += rect.top;
rect.right += rect.left;
rect.top = 0;
rect.left = 0;
wifiParent.setTouchDelegate(new TouchDelegate(rect, cbWifi));
}
});
final View otherParent = (View) cbOther.getParent();
otherParent.post(new Runnable() {
public void run() {
Rect rect = new Rect();
cbOther.getHitRect(rect);
rect.bottom += rect.top;
rect.right += rect.left;
rect.top = 0;
rect.left = 0;
otherParent.setTouchDelegate(new TouchDelegate(rect, cbOther));
}
});
2015-10-24 18:01:55 +00:00
}
}
2018-02-04 08:04:59 +00:00
public AdapterRule(Context context, View anchor) {
2016-01-31 09:29:43 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2018-02-04 08:04:59 +00:00
this.anchor = anchor;
2016-12-29 08:27:58 +00:00
this.inflater = LayoutInflater.from(context);
2015-11-24 17:34:41 +00:00
if (prefs.getBoolean("dark_theme", false))
colorChanged = Color.argb(128, Color.red(Color.DKGRAY), Color.green(Color.DKGRAY), Color.blue(Color.DKGRAY));
else
colorChanged = Color.argb(128, Color.red(Color.LTGRAY), Color.green(Color.LTGRAY), Color.blue(Color.LTGRAY));
2015-11-24 17:34:41 +00:00
2016-10-15 23:38:04 +00:00
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
2015-10-25 16:52:33 +00:00
try {
colorText = ta.getColor(0, 0);
} finally {
ta.recycle();
}
2016-01-02 12:22:18 +00:00
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(R.attr.colorOn, tv, true);
colorOn = tv.data;
context.getTheme().resolveAttribute(R.attr.colorOff, tv, true);
colorOff = tv.data;
colorGrayed = ContextCompat.getColor(context, R.color.colorGrayed);
2017-09-19 09:14:57 +00:00
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true);
int height = TypedValue.complexToDimensionPixelSize(typedValue.data, context.getResources().getDisplayMetrics());
this.iconSize = Math.round(height * context.getResources().getDisplayMetrics().density + 0.5f);
setHasStableIds(true);
2015-10-31 13:15:26 +00:00
}
2015-10-31 17:12:57 +00:00
public void set(List<Rule> listRule) {
2015-10-25 10:11:46 +00:00
listAll = listRule;
2016-02-01 06:55:04 +00:00
listFiltered = new ArrayList<>();
listFiltered.addAll(listRule);
2015-10-31 13:15:26 +00:00
notifyDataSetChanged();
2015-10-24 18:01:55 +00:00
}
public void setWifiActive() {
wifiActive = true;
otherActive = false;
notifyDataSetChanged();
}
public void setMobileActive() {
wifiActive = false;
otherActive = true;
notifyDataSetChanged();
}
2015-11-25 20:09:00 +00:00
public void setDisconnected() {
wifiActive = false;
otherActive = false;
notifyDataSetChanged();
}
public boolean isLive() {
return this.live;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
rv = recyclerView;
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
rv = null;
}
2015-10-24 18:01:55 +00:00
@Override
2017-08-06 15:33:08 +00:00
public void onBindViewHolder(final ViewHolder holder, int position) {
2018-02-04 08:04:59 +00:00
final Context context = holder.itemView.getContext();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2016-07-08 06:40:31 +00:00
2015-10-25 15:28:41 +00:00
// Get rule
2016-02-01 06:55:04 +00:00
final Rule rule = listFiltered.get(position);
2015-10-24 18:50:27 +00:00
// Handle expanding/collapsing
holder.llApplication.setOnClickListener(new View.OnClickListener() {
2015-10-29 11:52:36 +00:00
@Override
public void onClick(View view) {
rule.expanded = !rule.expanded;
2017-08-06 15:33:08 +00:00
notifyItemChanged(holder.getAdapterPosition());
2015-10-29 11:52:36 +00:00
}
});
2015-10-29 11:52:36 +00:00
// Show if non default rules
2016-02-04 05:55:24 +00:00
holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT);
// Show expand/collapse indicator
holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0);
// Show application icon
2017-11-05 14:17:09 +00:00
if (rule.icon <= 0)
holder.ivIcon.setImageResource(android.R.drawable.sym_def_app_icon);
2017-08-04 06:23:43 +00:00
else {
Uri uri = Uri.parse("android.resource://" + rule.packageName + "/" + rule.icon);
2018-02-04 08:04:59 +00:00
GlideApp.with(holder.itemView.getContext())
2018-02-01 13:29:40 +00:00
.applyDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565))
.load(uri)
2018-03-23 06:22:17 +00:00
//.diskCacheStrategy(DiskCacheStrategy.NONE)
//.skipMemoryCache(true)
.override(iconSize, iconSize)
.into(holder.ivIcon);
}
// Show application label
2015-10-25 15:28:41 +00:00
holder.tvName.setText(rule.name);
2015-11-01 13:16:57 +00:00
// Show application state
2016-02-14 10:16:50 +00:00
int color = rule.system ? colorOff : colorText;
if (!rule.internet || !rule.enabled)
2015-11-18 08:05:43 +00:00
color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color));
2015-10-25 21:12:08 +00:00
holder.tvName.setTextColor(color);
2015-10-25 15:28:41 +00:00
2017-03-26 11:59:43 +00:00
holder.tvHosts.setVisibility(rule.hosts > 0 ? View.VISIBLE : View.GONE);
holder.tvHosts.setText(Long.toString(rule.hosts));
2017-03-21 08:06:04 +00:00
// Lockdown settings
boolean lockdown = prefs.getBoolean("lockdown", false);
boolean lockdown_wifi = prefs.getBoolean("lockdown_wifi", true);
boolean lockdown_other = prefs.getBoolean("lockdown_other", true);
if ((otherActive && !lockdown_other) || (wifiActive && !lockdown_wifi))
lockdown = false;
2017-03-21 08:06:04 +00:00
holder.rlLockdown.setVisibility(lockdown && !rule.lockdown ? View.VISIBLE : View.GONE);
holder.ivLockdown.setEnabled(rule.apply);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivLockdown.getDrawable());
DrawableCompat.setTint(wrap, rule.apply ? colorOff : colorGrayed);
}
boolean screen_on = prefs.getBoolean("screen_on", true);
// Wi-Fi settings
2016-07-14 04:30:10 +00:00
holder.cbWifi.setEnabled(rule.apply);
2015-11-25 19:51:44 +00:00
holder.cbWifi.setAlpha(wifiActive ? 1 : 0.5f);
holder.cbWifi.setOnCheckedChangeListener(null);
holder.cbWifi.setChecked(rule.wifi_blocked);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbWifi));
DrawableCompat.setTint(wrap, rule.apply ? (rule.wifi_blocked ? colorOff : colorOn) : colorGrayed);
}
holder.cbWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
rule.wifi_blocked = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
}
});
2015-10-24 18:01:55 +00:00
holder.ivScreenWifi.setEnabled(rule.apply);
2015-11-25 19:51:44 +00:00
holder.ivScreenWifi.setAlpha(wifiActive ? 1 : 0.5f);
holder.ivScreenWifi.setVisibility(rule.screen_wifi && rule.wifi_blocked ? View.VISIBLE : View.INVISIBLE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivScreenWifi.getDrawable());
DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed);
}
2015-11-25 19:51:44 +00:00
// Mobile settings
2016-07-14 04:30:10 +00:00
holder.cbOther.setEnabled(rule.apply);
2015-11-25 19:51:44 +00:00
holder.cbOther.setAlpha(otherActive ? 1 : 0.5f);
2015-10-24 18:01:55 +00:00
holder.cbOther.setOnCheckedChangeListener(null);
holder.cbOther.setChecked(rule.other_blocked);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(CompoundButtonCompat.getButtonDrawable(holder.cbOther));
DrawableCompat.setTint(wrap, rule.apply ? (rule.other_blocked ? colorOff : colorOn) : colorGrayed);
}
holder.cbOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
rule.other_blocked = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
}
});
holder.ivScreenOther.setEnabled(rule.apply);
2015-11-25 19:51:44 +00:00
holder.ivScreenOther.setAlpha(otherActive ? 1 : 0.5f);
holder.ivScreenOther.setVisibility(rule.screen_other && rule.other_blocked ? View.VISIBLE : View.INVISIBLE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivScreenOther.getDrawable());
DrawableCompat.setTint(wrap, rule.apply ? colorOn : colorGrayed);
}
holder.tvRoaming.setTextColor(rule.apply ? colorOff : colorGrayed);
2015-11-25 19:51:44 +00:00
holder.tvRoaming.setAlpha(otherActive ? 1 : 0.5f);
2015-11-21 08:48:33 +00:00
holder.tvRoaming.setVisibility(rule.roaming && (!rule.other_blocked || rule.screen_other) ? View.VISIBLE : View.INVISIBLE);
// Expanded configuration section
holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);
2015-11-18 18:08:04 +00:00
// Show application details
2017-11-05 14:17:09 +00:00
holder.tvUid.setText(Integer.toString(rule.uid));
holder.tvPackage.setText(rule.packageName);
holder.tvVersion.setText(rule.version);
2015-11-01 13:16:57 +00:00
// Show application state
2015-11-13 18:46:35 +00:00
holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE);
2016-07-14 04:30:10 +00:00
holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE);
// Show related
holder.btnRelated.setVisibility(rule.relateduids ? View.VISIBLE : View.GONE);
holder.btnRelated.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent main = new Intent(context, ActivityMain.class);
2017-11-05 14:17:09 +00:00
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.uid));
2017-07-05 05:02:43 +00:00
main.putExtra(ActivityMain.EXTRA_RELATED, true);
context.startActivity(main);
}
});
2016-02-03 13:01:07 +00:00
// Launch application settings
if (rule.expanded) {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + rule.packageName));
final Intent settings = (intent.resolveActivity(context.getPackageManager()) == null ? null : intent);
holder.ibSettings.setVisibility(settings == null ? View.GONE : View.VISIBLE);
holder.ibSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(settings);
}
});
} else
holder.ibSettings.setVisibility(View.GONE);
2016-02-03 13:01:07 +00:00
// Launch application
if (rule.expanded) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(rule.packageName);
final Intent launch = (intent == null ||
intent.resolveActivity(context.getPackageManager()) == null ? null : intent);
holder.ibLaunch.setVisibility(launch == null ? View.GONE : View.VISIBLE);
holder.ibLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(launch);
2017-11-05 15:43:51 +00:00
}
});
} else
holder.ibLaunch.setVisibility(View.GONE);
// Apply
holder.cbApply.setEnabled(rule.pkg);
holder.cbApply.setOnCheckedChangeListener(null);
holder.cbApply.setChecked(rule.apply);
holder.cbApply.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
rule.apply = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
2016-02-03 13:01:07 +00:00
}
});
// Show Wi-Fi screen on condition
holder.llScreenWifi.setVisibility(screen_on ? View.VISIBLE : View.GONE);
2016-07-14 04:30:10 +00:00
holder.cbScreenWifi.setEnabled(rule.wifi_blocked && rule.apply);
2015-11-21 08:48:33 +00:00
holder.cbScreenWifi.setOnCheckedChangeListener(null);
holder.cbScreenWifi.setChecked(rule.screen_wifi);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivWifiLegend.getDrawable());
DrawableCompat.setTint(wrap, colorOn);
}
2015-11-21 08:48:33 +00:00
holder.cbScreenWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
rule.screen_wifi = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
2015-11-21 08:48:33 +00:00
}
});
2017-03-19 13:42:41 +00:00
// Show mobile screen on condition
holder.llScreenOther.setVisibility(screen_on ? View.VISIBLE : View.GONE);
holder.cbScreenOther.setEnabled(rule.other_blocked && rule.apply);
holder.cbScreenOther.setOnCheckedChangeListener(null);
holder.cbScreenOther.setChecked(rule.screen_other);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivOtherLegend.getDrawable());
DrawableCompat.setTint(wrap, colorOn);
}
2015-11-21 08:48:33 +00:00
holder.cbScreenOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
rule.screen_other = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
2015-11-01 13:16:57 +00:00
}
});
// Show roaming condition
2016-07-14 04:30:10 +00:00
holder.cbRoaming.setEnabled((!rule.other_blocked || rule.screen_other) && rule.apply);
2015-11-01 13:16:57 +00:00
holder.cbRoaming.setOnCheckedChangeListener(null);
holder.cbRoaming.setChecked(rule.roaming);
holder.cbRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
2015-11-01 13:16:57 +00:00
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
rule.roaming = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
}
});
2015-10-30 11:41:05 +00:00
2017-03-21 08:06:04 +00:00
// Show lockdown
holder.cbLockdown.setEnabled(rule.apply);
holder.cbLockdown.setOnCheckedChangeListener(null);
holder.cbLockdown.setChecked(rule.lockdown);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivLockdownLegend.getDrawable());
DrawableCompat.setTint(wrap, colorOn);
}
holder.cbLockdown.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
rule.lockdown = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
2017-03-21 08:06:04 +00:00
}
});
2015-12-27 08:07:38 +00:00
// Reset rule
holder.btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2016-02-03 06:33:06 +00:00
Util.areYouSure(view.getContext(), R.string.msg_clear_rules, new Util.DoubtListener() {
2016-02-01 14:03:39 +00:00
@Override
public void onSure() {
2016-02-21 08:53:08 +00:00
holder.cbApply.setChecked(true);
2016-02-01 14:03:39 +00:00
holder.cbWifi.setChecked(rule.wifi_default);
holder.cbOther.setChecked(rule.other_default);
holder.cbScreenWifi.setChecked(rule.screen_wifi_default);
holder.cbScreenOther.setChecked(rule.screen_other_default);
holder.cbRoaming.setChecked(rule.roaming_default);
2017-03-21 08:06:04 +00:00
holder.cbLockdown.setChecked(false);
2016-02-01 14:03:39 +00:00
}
});
2015-12-27 08:07:38 +00:00
}
});
holder.llFilter.setVisibility(Util.canFilter(context) ? View.VISIBLE : View.GONE);
2016-12-25 10:03:47 +00:00
// Live
holder.ivLive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
live = !live;
TypedValue tv = new TypedValue();
2016-12-25 10:03:47 +00:00
view.getContext().getTheme().resolveAttribute(live ? R.attr.iconPause : R.attr.iconPlay, tv, true);
holder.ivLive.setImageResource(tv.resourceId);
if (live)
AdapterRule.this.notifyDataSetChanged();
}
});
// Show logging/filtering is disabled
final boolean log_app = prefs.getBoolean("log_app", false);
final boolean filter = prefs.getBoolean("filter", false);
2017-07-25 16:30:04 +00:00
final boolean notify_access = prefs.getBoolean("notify_access", false);
holder.tvLogging.setText(log_app && filter ? R.string.title_logging_enabled : R.string.title_logging_disabled);
holder.btnLogging.setOnClickListener(new View.OnClickListener() {
2016-07-03 16:09:52 +00:00
@Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.enable, null, false);
2016-07-03 16:09:52 +00:00
2017-08-05 08:27:27 +00:00
final CheckBox cbLogging = view.findViewById(R.id.cbLogging);
final CheckBox cbFiltering = view.findViewById(R.id.cbFiltering);
final CheckBox cbNotify = view.findViewById(R.id.cbNotify);
TextView tvFilter4 = view.findViewById(R.id.tvFilter4);
cbLogging.setChecked(log_app);
cbFiltering.setChecked(filter);
cbFiltering.setEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
tvFilter4.setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? View.GONE : View.VISIBLE);
2017-07-25 16:30:04 +00:00
cbNotify.setChecked(notify_access);
cbNotify.setEnabled(log_app);
cbLogging.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("log_app", checked).apply();
2017-07-25 16:30:04 +00:00
cbNotify.setEnabled(checked);
if (!checked) {
cbNotify.setChecked(false);
prefs.edit().putBoolean("notify_access", false).apply();
ServiceSinkhole.reload("changed notify", context, false);
}
AdapterRule.this.notifyDataSetChanged();
}
});
cbFiltering.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked)
cbLogging.setChecked(true);
prefs.edit().putBoolean("filter", checked).apply();
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("changed filter", context, false);
AdapterRule.this.notifyDataSetChanged();
}
});
2017-07-25 16:30:04 +00:00
cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("notify_access", checked).apply();
ServiceSinkhole.reload("changed notify", context, false);
AdapterRule.this.notifyDataSetChanged();
}
});
AlertDialog dialog = new AlertDialog.Builder(context)
.setView(view)
.setCancelable(true)
.create();
dialog.show();
2016-07-03 16:09:52 +00:00
}
});
// Show access rules
2016-02-02 19:39:26 +00:00
if (rule.expanded) {
// Access the database when expanded only
final AdapterAccess badapter = new AdapterAccess(context,
2017-11-05 14:17:09 +00:00
DatabaseHelper.getInstance(context).getAccess(rule.uid));
2016-07-03 16:09:52 +00:00
holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
PackageManager pm = context.getPackageManager();
Cursor cursor = (Cursor) badapter.getItem(bposition);
final long id = cursor.getLong(cursor.getColumnIndex("ID"));
2016-07-07 15:04:38 +00:00
final int version = cursor.getInt(cursor.getColumnIndex("version"));
final int protocol = cursor.getInt(cursor.getColumnIndex("protocol"));
final String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
final int dport = cursor.getInt(cursor.getColumnIndex("dport"));
2016-07-03 16:09:52 +00:00
long time = cursor.getLong(cursor.getColumnIndex("time"));
int block = cursor.getInt(cursor.getColumnIndex("block"));
2018-02-04 08:04:59 +00:00
PopupMenu popup = new PopupMenu(context, anchor);
2016-07-03 16:09:52 +00:00
popup.inflate(R.menu.access);
popup.getMenu().findItem(R.id.menu_host).setTitle(
Util.getProtocolName(protocol, version, false) + " " +
daddr + (dport > 0 ? "/" + dport : ""));
SubMenu sub = popup.getMenu().findItem(R.id.menu_host).getSubMenu();
boolean multiple = false;
Cursor alt = null;
try {
alt = DatabaseHelper.getInstance(context).getAlternateQNames(daddr);
while (alt.moveToNext()) {
multiple = true;
sub.add(Menu.NONE, Menu.NONE, 0, alt.getString(0)).setEnabled(false);
}
} finally {
if (alt != null)
alt.close();
}
popup.getMenu().findItem(R.id.menu_host).setEnabled(multiple);
2018-02-04 08:04:59 +00:00
markPro(context, popup.getMenu().findItem(R.id.menu_allow), ActivityPro.SKU_FILTER);
markPro(context, popup.getMenu().findItem(R.id.menu_block), ActivityPro.SKU_FILTER);
2016-07-29 07:46:20 +00:00
2016-07-03 16:09:52 +00:00
// Whois
2018-04-19 16:07:16 +00:00
final Intent lookupIP = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.dnslytics.com/whois-lookup/" + daddr));
2016-07-03 16:09:52 +00:00
if (pm.resolveActivity(lookupIP, 0) == null)
popup.getMenu().removeItem(R.id.menu_whois);
else
popup.getMenu().findItem(R.id.menu_whois).setTitle(context.getString(R.string.title_log_whois, daddr));
// Lookup port
2017-06-16 03:21:10 +00:00
final Intent lookupPort = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.speedguide.net/port.php?port=" + dport));
2016-07-03 16:09:52 +00:00
if (dport <= 0 || pm.resolveActivity(lookupPort, 0) == null)
popup.getMenu().removeItem(R.id.menu_port);
else
popup.getMenu().findItem(R.id.menu_port).setTitle(context.getString(R.string.title_log_port, dport));
popup.getMenu().findItem(R.id.menu_time).setTitle(
SimpleDateFormat.getDateTimeInstance().format(time));
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
2017-03-26 11:59:43 +00:00
int menu = menuItem.getItemId();
boolean result = false;
switch (menu) {
2016-07-03 16:09:52 +00:00
case R.id.menu_whois:
context.startActivity(lookupIP);
2017-03-26 11:59:43 +00:00
result = true;
break;
2016-07-03 16:09:52 +00:00
case R.id.menu_port:
context.startActivity(lookupPort);
2017-03-26 11:59:43 +00:00
result = true;
break;
2016-07-03 16:09:52 +00:00
case R.id.menu_allow:
if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) {
DatabaseHelper.getInstance(context).setAccess(id, 0);
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("allow host", context, false);
2016-07-03 16:09:52 +00:00
} else
context.startActivity(new Intent(context, ActivityPro.class));
2017-03-26 11:59:43 +00:00
result = true;
break;
2016-07-03 16:09:52 +00:00
case R.id.menu_block:
if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) {
DatabaseHelper.getInstance(context).setAccess(id, 1);
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("block host", context, false);
2016-07-03 16:09:52 +00:00
} else
context.startActivity(new Intent(context, ActivityPro.class));
2017-03-26 11:59:43 +00:00
result = true;
break;
2016-07-03 16:09:52 +00:00
case R.id.menu_reset:
DatabaseHelper.getInstance(context).setAccess(id, -1);
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("reset host", context, false);
2017-03-26 11:59:43 +00:00
result = true;
break;
2018-01-10 05:26:29 +00:00
case R.id.menu_copy:
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("netguard", daddr);
clipboard.setPrimaryClip(clip);
return true;
2016-02-15 07:19:07 +00:00
}
2017-03-26 11:59:43 +00:00
if (menu == R.id.menu_allow || menu == R.id.menu_block || menu == R.id.menu_reset)
new AsyncTask<Object, Object, Long>() {
@Override
protected Long doInBackground(Object... objects) {
2017-11-05 14:17:09 +00:00
return DatabaseHelper.getInstance(context).getHostCount(rule.uid, false);
2017-03-26 11:59:43 +00:00
}
@Override
protected void onPostExecute(Long hosts) {
rule.hosts = hosts;
notifyDataSetChanged();
}
}.execute();
2017-03-26 11:59:43 +00:00
return result;
2016-07-03 16:09:52 +00:00
}
});
2016-01-31 09:29:43 +00:00
2016-07-03 16:09:52 +00:00
if (block == 0)
popup.getMenu().removeItem(R.id.menu_allow);
else if (block == 1)
popup.getMenu().removeItem(R.id.menu_block);
2016-01-31 09:29:43 +00:00
2016-07-03 16:09:52 +00:00
popup.show();
}
});
2016-01-30 13:26:30 +00:00
holder.lvAccess.setAdapter(badapter);
} else {
holder.lvAccess.setAdapter(null);
2016-01-30 13:26:30 +00:00
holder.lvAccess.setOnItemClickListener(null);
}
2016-07-14 04:48:54 +00:00
// Clear access log
holder.btnClearAccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Util.areYouSure(view.getContext(), R.string.msg_reset_access, new Util.DoubtListener() {
@Override
public void onSure() {
2017-11-05 14:17:09 +00:00
DatabaseHelper.getInstance(context).clearAccess(rule.uid, true);
2016-12-25 10:18:44 +00:00
if (!live)
notifyDataSetChanged();
2016-07-14 04:48:54 +00:00
if (rv != null)
2017-08-06 15:33:08 +00:00
rv.scrollToPosition(holder.getAdapterPosition());
2016-07-14 04:48:54 +00:00
}
});
}
});
// Notify on access
2016-02-21 08:53:08 +00:00
holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply);
2016-07-14 04:30:10 +00:00
holder.cbNotify.setOnCheckedChangeListener(null);
holder.cbNotify.setChecked(rule.notify);
holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
rule.notify = isChecked;
2018-02-04 08:04:59 +00:00
updateRule(context, rule, true, listAll);
}
});
}
2015-10-24 18:01:55 +00:00
2017-08-12 14:26:39 +00:00
@Override
public void onViewRecycled(ViewHolder holder) {
super.onViewRecycled(holder);
2018-02-06 14:34:32 +00:00
//Context context = holder.itemView.getContext();
//GlideApp.with(context).clear(holder.ivIcon);
2018-02-01 13:29:40 +00:00
2017-08-12 14:26:39 +00:00
CursorAdapter adapter = (CursorAdapter) holder.lvAccess.getAdapter();
if (adapter != null) {
Log.i(TAG, "Closing access cursor");
adapter.changeCursor(null);
holder.lvAccess.setAdapter(null);
}
}
2018-02-04 08:04:59 +00:00
private void markPro(Context context, MenuItem menu, String sku) {
2016-07-29 07:46:20 +00:00
if (sku == null || !IAB.isPurchased(sku, context)) {
2016-10-06 11:57:07 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean dark = prefs.getBoolean("dark_theme", false);
2016-07-29 07:46:20 +00:00
SpannableStringBuilder ssb = new SpannableStringBuilder(" " + menu.getTitle());
2016-10-06 11:57:07 +00:00
ssb.setSpan(new ImageSpan(context, dark ? R.drawable.ic_shopping_cart_white_24dp : R.drawable.ic_shopping_cart_black_24dp), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2016-07-29 07:46:20 +00:00
menu.setTitle(ssb);
}
}
2018-02-04 08:04:59 +00:00
private void updateRule(Context context, Rule rule, boolean root, List<Rule> listAll) {
SharedPreferences wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
SharedPreferences other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
SharedPreferences apply = context.getSharedPreferences("apply", Context.MODE_PRIVATE);
SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE);
SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE);
SharedPreferences roaming = context.getSharedPreferences("roaming", Context.MODE_PRIVATE);
2017-03-21 08:06:04 +00:00
SharedPreferences lockdown = context.getSharedPreferences("lockdown", Context.MODE_PRIVATE);
SharedPreferences notify = context.getSharedPreferences("notify", Context.MODE_PRIVATE);
2015-11-08 10:06:04 +00:00
if (rule.wifi_blocked == rule.wifi_default)
2017-11-05 14:17:09 +00:00
wifi.edit().remove(rule.packageName).apply();
else
2017-11-05 14:17:09 +00:00
wifi.edit().putBoolean(rule.packageName, rule.wifi_blocked).apply();
2015-12-25 18:40:59 +00:00
if (rule.other_blocked == rule.other_default)
2017-11-05 14:17:09 +00:00
other.edit().remove(rule.packageName).apply();
else
2017-11-05 14:17:09 +00:00
other.edit().putBoolean(rule.packageName, rule.other_blocked).apply();
2015-11-08 10:06:04 +00:00
if (rule.apply)
2017-11-05 14:17:09 +00:00
apply.edit().remove(rule.packageName).apply();
else
2017-11-05 14:17:09 +00:00
apply.edit().putBoolean(rule.packageName, rule.apply).apply();
2015-11-21 08:48:33 +00:00
if (rule.screen_wifi == rule.screen_wifi_default)
2017-11-05 14:17:09 +00:00
screen_wifi.edit().remove(rule.packageName).apply();
2015-11-21 08:48:33 +00:00
else
2017-11-05 14:17:09 +00:00
screen_wifi.edit().putBoolean(rule.packageName, rule.screen_wifi).apply();
2015-11-21 08:48:33 +00:00
if (rule.screen_other == rule.screen_other_default)
2017-11-05 14:17:09 +00:00
screen_other.edit().remove(rule.packageName).apply();
2015-11-08 10:15:18 +00:00
else
2017-11-05 14:17:09 +00:00
screen_other.edit().putBoolean(rule.packageName, rule.screen_other).apply();
2015-11-08 10:15:18 +00:00
if (rule.roaming == rule.roaming_default)
2017-11-05 14:17:09 +00:00
roaming.edit().remove(rule.packageName).apply();
2015-11-08 10:15:18 +00:00
else
2017-11-05 14:17:09 +00:00
roaming.edit().putBoolean(rule.packageName, rule.roaming).apply();
2017-03-21 08:06:04 +00:00
if (rule.lockdown)
2017-11-05 14:17:09 +00:00
lockdown.edit().putBoolean(rule.packageName, rule.lockdown).apply();
2017-03-21 08:06:04 +00:00
else
2017-11-05 14:17:09 +00:00
lockdown.edit().remove(rule.packageName).apply();
2017-03-21 08:06:04 +00:00
if (rule.notify)
2017-11-05 14:17:09 +00:00
notify.edit().remove(rule.packageName).apply();
else
2017-11-05 14:17:09 +00:00
notify.edit().putBoolean(rule.packageName, rule.notify).apply();
rule.updateChanged(context);
Log.i(TAG, "Updated " + rule);
List<Rule> listModified = new ArrayList<>();
for (String pkg : rule.related) {
for (Rule related : listAll)
2017-11-05 14:17:09 +00:00
if (related.packageName.equals(pkg)) {
related.wifi_blocked = rule.wifi_blocked;
related.other_blocked = rule.other_blocked;
related.apply = rule.apply;
related.screen_wifi = rule.screen_wifi;
related.screen_other = rule.screen_other;
related.roaming = rule.roaming;
2017-03-21 08:06:04 +00:00
related.lockdown = rule.lockdown;
related.notify = rule.notify;
listModified.add(related);
}
}
List<Rule> listSearch = (root ? new ArrayList<>(listAll) : listAll);
listSearch.remove(rule);
for (Rule modified : listModified)
listSearch.remove(modified);
for (Rule modified : listModified)
2018-02-04 08:04:59 +00:00
updateRule(context, modified, false, listSearch);
if (root) {
notifyDataSetChanged();
2017-11-05 14:17:09 +00:00
NotificationManagerCompat.from(context).cancel(rule.uid);
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("rule changed", context, false);
}
2015-11-08 10:15:18 +00:00
}
2015-10-25 10:11:46 +00:00
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence query) {
List<Rule> listResult = new ArrayList<>();
if (query == null)
listResult.addAll(listAll);
else {
2016-03-02 06:56:54 +00:00
query = query.toString().toLowerCase().trim();
2016-02-06 07:40:37 +00:00
int uid;
try {
uid = Integer.parseInt(query.toString());
} catch (NumberFormatException ignore) {
uid = -1;
}
2015-10-25 10:11:46 +00:00
for (Rule rule : listAll)
2017-11-05 14:17:09 +00:00
if (rule.uid == uid ||
rule.packageName.toLowerCase().contains(query) ||
2016-02-06 07:40:37 +00:00
(rule.name != null && rule.name.toLowerCase().contains(query)))
2015-10-25 10:11:46 +00:00
listResult.add(rule);
}
FilterResults result = new FilterResults();
result.values = listResult;
result.count = listResult.size();
return result;
}
@Override
protected void publishResults(CharSequence query, FilterResults result) {
2016-02-01 06:55:04 +00:00
listFiltered.clear();
2015-10-25 10:11:46 +00:00
if (result == null)
2016-02-01 06:55:04 +00:00
listFiltered.addAll(listAll);
else {
listFiltered.addAll((List<Rule>) result.values);
if (listFiltered.size() == 1)
listFiltered.get(0).expanded = true;
}
2015-10-25 10:11:46 +00:00
notifyDataSetChanged();
}
};
}
@Override
public AdapterRule.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2016-12-29 08:27:58 +00:00
return new ViewHolder(inflater.inflate(R.layout.rule, parent, false));
2015-10-24 18:01:55 +00:00
}
2017-09-19 09:14:57 +00:00
@Override
public long getItemId(int position) {
Rule rule = listFiltered.get(position);
2017-11-05 14:17:09 +00:00
return rule.packageName.hashCode() * 100000L + rule.uid;
2017-09-19 09:14:57 +00:00
}
2015-10-24 18:01:55 +00:00
@Override
public int getItemCount() {
2016-02-01 06:55:04 +00:00
return listFiltered.size();
2015-10-24 18:01:55 +00:00
}
}