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

550 lines
22 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;
2015-10-25 21:12:08 +00:00
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
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;
2015-10-25 16:52:33 +00:00
import android.support.v4.content.ContextCompat;
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;
import android.view.TouchDelegate;
2015-10-24 18:01:55 +00:00
import android.view.View;
import android.view.ViewGroup;
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;
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;
2015-12-08 16:00:40 +00:00
private boolean wifi;
private boolean telephony;
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 boolean wifiActive = true;
private boolean otherActive = true;
2015-10-31 13:15:26 +00:00
private List<Rule> listAll = new ArrayList<>();
private List<Rule> listSelected = 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;
2015-12-08 16:00:40 +00:00
public LinearLayout llWifi;
2015-10-24 18:01:55 +00:00
public CheckBox cbWifi;
2015-12-08 16:00:40 +00:00
public ImageView ivScreenWifi;
public LinearLayout llOther;
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 LinearLayout llWifiAttr;
2015-11-21 08:48:33 +00:00
public CheckBox cbScreenWifi;
2015-12-08 16:00:40 +00:00
public LinearLayout llOtherAttr;
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 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
2015-12-08 16:00:40 +00:00
llWifi = (LinearLayout) itemView.findViewById(R.id.llWifi);
cbWifi = (CheckBox) itemView.findViewById(R.id.cbWifi);
2015-12-08 16:00:40 +00:00
ivScreenWifi = (ImageView) itemView.findViewById(R.id.ivScreenWifi);
llOther = (LinearLayout) itemView.findViewById(R.id.llOther);
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
llWifiAttr = (LinearLayout) itemView.findViewById(R.id.llWifiAttr);
2015-11-21 08:48:33 +00:00
cbScreenWifi = (CheckBox) itemView.findViewById(R.id.cbScreenWifi);
2015-12-08 16:00:40 +00:00
llOtherAttr = (LinearLayout) itemView.findViewById(R.id.llOtherAttr);
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);
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(Activity context) {
2015-10-25 16:52:33 +00:00
this.context = context;
2015-12-08 16:00:40 +00:00
this.wifi = Util.hasWifi(context);
this.telephony = Util.hasTelephony(context);
2015-11-09 18:28:09 +00:00
this.debuggable = Util.isDebuggable(context);
2015-11-24 17:34:41 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
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;
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;
listSelected = new ArrayList<>();
listSelected.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();
}
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
2015-10-25 10:11:46 +00:00
final Rule rule = listSelected.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
}
};
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
holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT);
2015-11-24 17:34:41 +00:00
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);
}
holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0);
2015-10-25 15:28:41 +00:00
holder.tvName.setText(rule.name);
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
2015-12-08 16:00:40 +00:00
holder.llWifi.setVisibility(wifi ? View.VISIBLE : View.GONE);
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);
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);
holder.llOther.setVisibility(telephony ? View.VISIBLE : View.GONE);
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);
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);
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);
holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);
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
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);
2015-11-21 08:48:33 +00:00
holder.cbScreenWifi.setOnCheckedChangeListener(null);
holder.cbScreenWifi.setChecked(rule.screen_wifi);
holder.cbScreenWifi.setEnabled(rule.wifi_blocked);
2015-12-08 16:00:40 +00:00
holder.llWifiAttr.setVisibility(wifi ? View.VISIBLE : View.GONE);
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
}
});
holder.llOtherAttr.setVisibility(telephony ? View.VISIBLE : View.GONE);
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
}
});
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) {
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);
}
});
// 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(
2015-11-09 18:28:09 +00:00
!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
2015-11-09 18:28:09 +00:00
holder.btnLaunch.setVisibility(!debuggable || rule.intent == null ? View.GONE : View.VISIBLE);
2015-10-30 11:41:05 +00:00
holder.btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
2015-10-31 20:25:39 +00:00
context.startActivity(rule.intent);
2015-10-30 11:41:05 +00:00
}
});
// 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();
2015-10-25 10:11:46 +00:00
for (Rule rule : listAll)
if (rule.info.packageName.toLowerCase().contains(query) ||
(rule.name != null && rule.name.toLowerCase().contains(query)) ||
(rule.info.applicationInfo != null && Integer.toString(rule.info.applicationInfo.uid).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) {
listSelected.clear();
if (result == null)
listSelected.addAll(listAll);
else
for (Rule rule : (List<Rule>) result.values)
listSelected.add(rule);
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() {
2015-10-25 10:11:46 +00:00
return listSelected.size();
2015-10-24 18:01:55 +00:00
}
}