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

757 lines
31 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/>.
2016-01-01 13:52:09 +00:00
Copyright 2015-2016 by Marcel Bokhorst (M66B)
2015-11-03 17:57:29 +00:00
*/
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
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;
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;
2015-12-25 18:40:59 +00:00
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.CompoundButtonCompat;
2015-10-24 18:01:55 +00:00
import android.support.v7.widget.RecyclerView;
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;
2016-01-30 13:26:30 +00:00
import android.view.MenuItem;
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;
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;
2015-10-24 18:01:55 +00:00
import android.widget.TextView;
import com.squareup.picasso.Picasso;
2015-10-25 10:11:46 +00:00
import java.util.ArrayList;
2015-10-24 18:01:55 +00:00
import java.util.List;
2015-10-25 10:11:46 +00:00
public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.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
private Activity context;
private DatabaseHelper dh;
private RecyclerView rv;
2016-01-31 09:29:43 +00:00
private boolean filter;
2015-11-09 18:28:09 +00:00
private boolean debuggable;
2015-10-25 16:52:33 +00:00
private int colorText;
private int colorAccent;
private int colorChanged;
private int colorOn;
private int colorOff;
private boolean wifiActive = true;
private boolean otherActive = 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;
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 tvDisabled;
public TextView tvInternet;
2015-12-08 16:00:40 +00:00
public ImageView ivWifiLegend;
2015-11-21 08:48:33 +00:00
public CheckBox cbScreenWifi;
2015-12-08 16:00:40 +00:00
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
2015-12-27 08:07:38 +00:00
public ImageButton btnClear;
public ImageButton btnSettings;
2015-10-30 11:41:05 +00:00
public Button btnLaunch;
2015-10-24 18:01:55 +00:00
public ListView lvAccess;
public TextView tvNolog;
public CheckBox cbNotify;
2016-01-30 13:26:30 +00:00
public ImageButton btnClearAccess;
public TextView tvStatistics;
2015-10-24 18:01:55 +00:00
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
2015-11-01 13:16:57 +00:00
llApplication = (LinearLayout) itemView.findViewById(R.id.llApplication);
ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
2015-10-30 07:39:17 +00:00
ivExpander = (ImageView) itemView.findViewById(R.id.ivExpander);
tvName = (TextView) itemView.findViewById(R.id.tvName);
2015-11-01 13:16:57 +00:00
2016-02-02 19:39:26 +00:00
tvHosts = (TextView) itemView.findViewById(R.id.tvHosts);
cbWifi = (CheckBox) itemView.findViewById(R.id.cbWifi);
2015-12-08 16:00:40 +00:00
ivScreenWifi = (ImageView) itemView.findViewById(R.id.ivScreenWifi);
cbOther = (CheckBox) itemView.findViewById(R.id.cbOther);
2015-11-21 08:48:33 +00:00
ivScreenOther = (ImageView) itemView.findViewById(R.id.ivScreenOther);
2015-11-01 13:16:57 +00:00
tvRoaming = (TextView) itemView.findViewById(R.id.tvRoaming);
2015-10-29 11:52:36 +00:00
llConfiguration = (LinearLayout) itemView.findViewById(R.id.llConfiguration);
2015-11-18 18:08:04 +00:00
tvUid = (TextView) itemView.findViewById(R.id.tvUid);
2015-11-01 13:16:57 +00:00
tvPackage = (TextView) itemView.findViewById(R.id.tvPackage);
2015-11-18 18:08:04 +00:00
tvVersion = (TextView) itemView.findViewById(R.id.tvVersion);
2015-11-13 18:46:35 +00:00
tvDisabled = (TextView) itemView.findViewById(R.id.tvDisabled);
tvInternet = (TextView) itemView.findViewById(R.id.tvInternet);
2015-12-08 16:00:40 +00:00
ivWifiLegend = (ImageView) itemView.findViewById(R.id.ivWifiLegend);
2015-11-21 08:48:33 +00:00
cbScreenWifi = (CheckBox) itemView.findViewById(R.id.cbScreenWifi);
2015-12-08 16:00:40 +00:00
ivOtherLegend = (ImageView) itemView.findViewById(R.id.ivOtherLegend);
2015-11-21 08:48:33 +00:00
cbScreenOther = (CheckBox) itemView.findViewById(R.id.cbScreenOther);
2015-11-01 13:16:57 +00:00
cbRoaming = (CheckBox) itemView.findViewById(R.id.cbRoaming);
2015-12-08 16:00:40 +00:00
2015-12-27 08:07:38 +00:00
btnClear = (ImageButton) itemView.findViewById(R.id.btnClear);
btnSettings = (ImageButton) itemView.findViewById(R.id.btnSettings);
2015-10-30 11:41:05 +00:00
btnLaunch = (Button) itemView.findViewById(R.id.btnLaunch);
lvAccess = (ListView) itemView.findViewById(R.id.lvAccess);
tvNolog = (TextView) itemView.findViewById(R.id.tvNolog);
cbNotify = (CheckBox) itemView.findViewById(R.id.cbNotify);
2016-01-30 13:26:30 +00:00
btnClearAccess = (ImageButton) itemView.findViewById(R.id.btnClearAccess);
tvStatistics = (TextView) itemView.findViewById(R.id.tvStatistics);
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
}
}
public RuleAdapter(DatabaseHelper dh, Activity context) {
2016-01-31 09:29:43 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2015-10-25 16:52:33 +00:00
this.context = context;
this.dh = dh;
2016-01-31 09:29:43 +00:00
this.filter = prefs.getBoolean("filter", false);
2015-11-09 18:28:09 +00:00
this.debuggable = Util.isDebuggable(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
2015-10-25 16:52:33 +00:00
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
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.colorAccent, tv, true);
colorAccent = tv.data;
context.getTheme().resolveAttribute(R.attr.colorOn, tv, true);
colorOn = tv.data;
context.getTheme().resolveAttribute(R.attr.colorOff, tv, true);
colorOff = tv.data;
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();
}
@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
2015-10-30 07:39:17 +00:00
public void onBindViewHolder(final ViewHolder holder, final int position) {
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
2015-10-25 15:28:41 +00:00
// Rule change listener
CompoundButton.OnCheckedChangeListener cbListener = new CompoundButton.OnCheckedChangeListener() {
2015-10-24 18:01:55 +00:00
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2015-10-30 07:39:17 +00:00
// Update rule
2015-11-08 10:06:04 +00:00
String network = ((buttonView == holder.cbWifi) ? "wifi" : "other");
updateRule(rule, network, isChecked);
2015-11-24 17:34:41 +00:00
rule.updateChanged(context);
2015-10-24 18:01:55 +00:00
2015-11-08 10:15:18 +00:00
// Update relations
2015-11-08 09:52:48 +00:00
if (rule.related == null)
notifyItemChanged(position);
else {
2015-11-08 10:06:04 +00:00
for (String pkg : rule.related)
for (Rule related : listAll)
if (related.info.packageName.equals(pkg)) {
updateRule(related, network, isChecked);
2015-11-21 08:48:33 +00:00
updateScreenWifi(related, rule.screen_wifi);
updateScreenOther(related, rule.screen_other);
2015-11-08 10:15:18 +00:00
updateRoaming(related, rule.roaming);
2015-11-24 17:34:41 +00:00
related.updateChanged(context);
2015-11-08 09:52:48 +00:00
}
notifyDataSetChanged();
}
2015-10-29 08:56:22 +00:00
2015-10-30 07:39:17 +00:00
// Apply updated rule
SinkholeService.reload(network, "rule changed", context);
2015-10-24 18:01:55 +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;
2015-10-30 07:39:17 +00:00
notifyItemChanged(position);
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
2015-11-09 12:35:18 +00:00
if (rule.info.applicationInfo == null || rule.info.applicationInfo.icon == 0)
Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(holder.ivIcon);
else {
Uri uri = Uri.parse("android.resource://" + rule.info.packageName + "/" + rule.info.applicationInfo.icon);
Picasso.with(context).load(uri).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
2015-11-01 13:16:57 +00:00
int color = rule.system ? colorAccent : 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
// Show rule count
2016-02-04 05:55:24 +00:00
new AsyncTask<Object, Object, Long>() {
@Override
protected void onPreExecute() {
holder.tvHosts.setVisibility(View.GONE);
}
@Override
protected Long doInBackground(Object... objects) {
return dh.getRuleCount(rule.info.applicationInfo.uid);
}
@Override
protected void onPostExecute(Long rules) {
if (rules > 0) {
holder.tvHosts.setVisibility(View.VISIBLE);
holder.tvHosts.setText(Long.toString(rules));
}
}
}.execute();
// Wi-Fi settings
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.wifi_blocked ? colorOff : colorOn);
}
holder.cbWifi.setOnCheckedChangeListener(cbListener);
2015-10-24 18:01:55 +00:00
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, colorOn);
}
2015-11-25 19:51:44 +00:00
// Mobile settings
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.other_blocked ? colorOff : colorOn);
}
holder.cbOther.setOnCheckedChangeListener(cbListener);
2015-11-25 19:51:44 +00:00
holder.ivScreenOther.setAlpha(otherActive ? 1 : 0.5f);
2015-11-21 08:48:33 +00:00
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, colorOn);
}
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
2015-11-18 18:08:04 +00:00
holder.tvUid.setText(rule.info.applicationInfo == null ? "?" : Integer.toString(rule.info.applicationInfo.uid));
holder.tvPackage.setText(rule.info.packageName);
holder.tvVersion.setText(rule.info.versionName + '/' + rule.info.versionCode);
2015-11-01 13:16:57 +00:00
// Show application state
holder.tvDisabled.setVisibility(rule.enabled ? View.GONE : View.VISIBLE);
2015-11-13 18:46:35 +00:00
holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE);
2016-02-03 13:01:07 +00:00
// Launch application settings
final Intent settings = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
settings.setData(Uri.parse("package:" + rule.info.packageName));
holder.btnSettings.setVisibility(
!debuggable || settings.resolveActivity(context.getPackageManager()) == null ? View.GONE : View.VISIBLE);
holder.btnSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(settings);
}
});
// Launch application
holder.btnLaunch.setVisibility(!debuggable || rule.intent == null ? View.GONE : View.VISIBLE);
holder.btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(rule.intent);
}
});
// Show Wi-Fi screen on condition
2015-11-21 08:48:33 +00:00
holder.cbScreenWifi.setOnCheckedChangeListener(null);
holder.cbScreenWifi.setChecked(rule.screen_wifi);
holder.cbScreenWifi.setEnabled(rule.wifi_blocked);
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) {
2015-10-31 20:28:32 +00:00
// Update rule
2015-11-21 08:48:33 +00:00
updateScreenWifi(rule, isChecked);
2015-11-24 17:34:41 +00:00
rule.updateChanged(context);
2015-10-31 20:28:32 +00:00
2015-11-08 10:15:18 +00:00
// Update relations
if (rule.related == null)
notifyItemChanged(position);
else {
for (String pkg : rule.related)
for (Rule related : listAll)
2015-11-24 17:34:41 +00:00
if (related.info.packageName.equals(pkg)) {
2015-11-21 08:48:33 +00:00
updateScreenWifi(related, rule.screen_wifi);
2015-11-24 17:34:41 +00:00
related.updateChanged(context);
}
2015-11-21 08:48:33 +00:00
notifyDataSetChanged();
}
// Apply updated rule
SinkholeService.reload(null, "rule changed", context);
2015-11-21 08:48:33 +00:00
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivOtherLegend.getDrawable());
DrawableCompat.setTint(wrap, colorOn);
}
// Show mobile screen on condition
2015-11-21 08:48:33 +00:00
holder.cbScreenOther.setOnCheckedChangeListener(null);
holder.cbScreenOther.setChecked(rule.screen_other);
holder.cbScreenOther.setEnabled(rule.other_blocked);
holder.cbScreenOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Update rule
updateScreenOther(rule, isChecked);
2015-11-24 17:34:41 +00:00
rule.updateChanged(context);
2015-11-21 08:48:33 +00:00
// Update relations
if (rule.related == null)
notifyItemChanged(position);
else {
for (String pkg : rule.related)
for (Rule related : listAll)
2015-11-24 17:34:41 +00:00
if (related.info.packageName.equals(pkg)) {
2015-11-21 08:48:33 +00:00
updateScreenOther(related, rule.screen_other);
2015-11-24 17:34:41 +00:00
related.updateChanged(context);
}
2015-11-08 10:15:18 +00:00
notifyDataSetChanged();
}
2015-11-01 13:16:57 +00:00
// Apply updated rule
SinkholeService.reload(null, "rule changed", context);
2015-11-01 13:16:57 +00:00
}
});
// Show roaming condition
2015-11-01 13:16:57 +00:00
holder.cbRoaming.setOnCheckedChangeListener(null);
holder.cbRoaming.setChecked(rule.roaming);
2015-11-21 08:48:33 +00:00
holder.cbRoaming.setEnabled(!rule.other_blocked || rule.screen_other);
2015-11-01 13:16:57 +00:00
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) {
// Update rule
2015-11-08 10:15:18 +00:00
updateRoaming(rule, isChecked);
2015-11-24 17:34:41 +00:00
rule.updateChanged(context);
2015-11-01 13:16:57 +00:00
2015-11-08 10:15:18 +00:00
// Update relations
if (rule.related == null)
notifyItemChanged(position);
else {
for (String pkg : rule.related)
for (Rule related : listAll)
2015-11-24 17:34:41 +00:00
if (related.info.packageName.equals(pkg)) {
2015-11-08 10:15:18 +00:00
updateRoaming(related, rule.roaming);
2015-11-24 17:34:41 +00:00
related.updateChanged(context);
}
2015-11-08 10:15:18 +00:00
notifyDataSetChanged();
}
2015-10-30 07:39:17 +00:00
// Apply updated rule
SinkholeService.reload(null, "rule changed", context);
// Request permissions
if (isChecked && !Util.hasPhoneStatePermission(context))
context.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, ActivityMain.REQUEST_ROAMING);
}
});
2015-10-30 11:41:05 +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() {
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);
}
});
2015-12-27 08:07:38 +00:00
}
});
// Show access rules
2016-02-02 19:39:26 +00:00
if (rule.expanded) {
// Access the database when expanded only
final AccessAdapter badapter = new AccessAdapter(context, dh.getAccess(rule.info.applicationInfo.uid));
2016-01-31 09:29:43 +00:00
if (filter)
holder.lvAccess.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
if (IAB.isPurchased(ActivityPro.SKU_FILTER, context)) {
Cursor cursor = (Cursor) badapter.getItem(bposition);
final long id = cursor.getLong(cursor.getColumnIndex("ID"));
int version = cursor.getInt(cursor.getColumnIndex("version"));
int protocol = cursor.getInt(cursor.getColumnIndex("protocol"));
2016-01-31 09:29:43 +00:00
String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
int dport = cursor.getInt(cursor.getColumnIndex("dport"));
2016-01-31 09:29:43 +00:00
int block = cursor.getInt(cursor.getColumnIndex("block"));
2016-01-31 10:04:55 +00:00
PopupMenu popup = new PopupMenu(context, context.findViewById(R.id.vwPopupAnchor));
2016-01-31 09:29:43 +00:00
popup.inflate(R.menu.access);
popup.getMenu().findItem(R.id.menu_host).setTitle(
Util.getProtocolName(protocol, version, false) + " " +
daddr + (dport > 0 ? ":" + dport : ""));
2016-01-31 09:29:43 +00:00
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_allow:
2016-02-04 10:49:43 +00:00
dh.setAccess(id, 0);
2016-01-31 09:29:43 +00:00
SinkholeService.reload(null, "allow host", context);
return true;
case R.id.menu_block:
2016-02-04 10:49:43 +00:00
dh.setAccess(id, 1);
2016-01-31 09:29:43 +00:00
SinkholeService.reload(null, "block host", context);
return true;
2016-02-03 06:04:00 +00:00
case R.id.menu_reset:
2016-02-04 10:49:43 +00:00
dh.setAccess(id, -1);
2016-02-03 06:04:00 +00:00
SinkholeService.reload(null, "reset host", context);
2016-01-31 09:29:43 +00:00
return true;
}
return false;
2016-01-31 08:30:06 +00:00
}
2016-01-31 09:29:43 +00:00
});
if (block == 0)
popup.getMenu().removeItem(R.id.menu_allow);
else if (block == 1)
popup.getMenu().removeItem(R.id.menu_block);
popup.show();
} else
context.startActivity(new Intent(context, ActivityPro.class));
}
});
else
holder.lvAccess.setOnItemClickListener(null);
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);
}
// Show logging is disabled
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean log_app = prefs.getBoolean("log_app", false);
holder.tvNolog.setVisibility(log_app ? View.GONE : View.VISIBLE);
holder.tvNolog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
context.startActivity(new Intent(context, ActivitySettings.class));
}
});
// Show disable access notifications setting
boolean notify = prefs.getBoolean("notify_access", false);
final String key = "notify_" + rule.info.applicationInfo.uid;
holder.cbNotify.setOnCheckedChangeListener(null);
holder.cbNotify.setEnabled(notify);
holder.cbNotify.setChecked(prefs.getBoolean(key, true));
holder.cbNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
prefs.edit().putBoolean(key, isChecked).apply();
}
});
// Clear access log
2016-01-30 13:26:30 +00:00
holder.btnClearAccess.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_reset_access, new Util.DoubtListener() {
2016-02-01 14:03:39 +00:00
@Override
public void onSure() {
dh.clearAccess(rule.info.applicationInfo.uid);
if (rv != null)
rv.scrollToPosition(position);
2016-02-01 14:03:39 +00:00
}
});
2016-01-30 13:26:30 +00:00
}
});
// Show traffic statistics
2015-12-28 08:09:07 +00:00
holder.tvStatistics.setText(context.getString(R.string.msg_mbday, rule.upspeed, rule.downspeed));
}
2015-10-24 18:01:55 +00:00
2015-11-08 10:06:04 +00:00
private void updateRule(Rule rule, String network, boolean blocked) {
SharedPreferences prefs = context.getSharedPreferences(network, Context.MODE_PRIVATE);
if ("wifi".equals(network)) {
rule.wifi_blocked = blocked;
if (rule.wifi_blocked == rule.wifi_default) {
Log.i(TAG, "Removing " + rule.info.packageName + " " + network);
prefs.edit().remove(rule.info.packageName).apply();
} else {
Log.i(TAG, "Setting " + rule.info.packageName + " " + network + "=" + blocked);
prefs.edit().putBoolean(rule.info.packageName, blocked).apply();
}
}
if ("other".equals(network)) {
rule.other_blocked = blocked;
if (rule.other_blocked == rule.other_default) {
Log.i(TAG, "Removing " + rule.info.packageName + " " + network);
prefs.edit().remove(rule.info.packageName).apply();
} else {
Log.i(TAG, "Setting " + rule.info.packageName + " " + network + "=" + blocked);
prefs.edit().putBoolean(rule.info.packageName, blocked).apply();
}
}
2015-12-25 18:40:59 +00:00
NotificationManagerCompat.from(context).cancel(rule.info.applicationInfo.uid);
2015-11-08 10:06:04 +00:00
}
2015-11-21 08:48:33 +00:00
private void updateScreenWifi(Rule rule, boolean enabled) {
rule.screen_wifi = enabled;
SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE);
if (rule.screen_wifi == rule.screen_wifi_default)
screen_wifi.edit().remove(rule.info.packageName).apply();
else
screen_wifi.edit().putBoolean(rule.info.packageName, rule.screen_wifi).apply();
}
private void updateScreenOther(Rule rule, boolean enabled) {
rule.screen_other = enabled;
SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE);
if (rule.screen_other == rule.screen_other_default)
screen_other.edit().remove(rule.info.packageName).apply();
2015-11-08 10:15:18 +00:00
else
2015-11-21 08:48:33 +00:00
screen_other.edit().putBoolean(rule.info.packageName, rule.screen_other).apply();
2015-11-08 10:15:18 +00:00
}
private void updateRoaming(Rule rule, boolean enabled) {
rule.roaming = enabled;
SharedPreferences roaming = context.getSharedPreferences("roaming", Context.MODE_PRIVATE);
if (rule.roaming == rule.roaming_default)
roaming.edit().remove(rule.info.packageName).apply();
else
roaming.edit().putBoolean(rule.info.packageName, rule.roaming).apply();
}
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 {
query = query.toString().toLowerCase();
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)
2016-02-06 07:40:37 +00:00
if (rule.info.applicationInfo.uid == uid ||
rule.info.packageName.toLowerCase().contains(query) ||
(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 RuleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2015-10-25 16:52:33 +00:00
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.rule, parent, false));
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
}
}