package eu.faircode.netguard;
/*
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 .
Copyright 2015-2016 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.CompoundButtonCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class AdapterRule extends RecyclerView.Adapter implements Filterable {
private static final String TAG = "NetGuard.Adapter";
private Activity context;
private RecyclerView rv;
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 List listAll = new ArrayList<>();
private List listFiltered = new ArrayList<>();
public static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public LinearLayout llApplication;
public ImageView ivIcon;
public ImageView ivExpander;
public TextView tvName;
public TextView tvHosts;
public CheckBox cbWifi;
public ImageView ivScreenWifi;
public CheckBox cbOther;
public ImageView ivScreenOther;
public TextView tvRoaming;
public LinearLayout llConfiguration;
public TextView tvUid;
public TextView tvPackage;
public TextView tvVersion;
public TextView tvDescription;
public TextView tvInternet;
public TextView tvDisabled;
public TextView tvStatistics;
public Button btnRelated;
public ImageButton ibSettings;
public ImageButton ibDatasaver;
public ImageButton ibLaunch;
public CheckBox cbApply;
public LinearLayout llScreenWifi;
public ImageView ivWifiLegend;
public CheckBox cbScreenWifi;
public LinearLayout llScreenOther;
public ImageView ivOtherLegend;
public CheckBox cbScreenOther;
public CheckBox cbRoaming;
public ImageButton btnClear;
public TextView tvNoLog;
public TextView tvNoFilter;
public ListView lvAccess;
public ImageButton btnClearAccess;
public CheckBox cbNotify;
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
llApplication = (LinearLayout) itemView.findViewById(R.id.llApplication);
ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
ivExpander = (ImageView) itemView.findViewById(R.id.ivExpander);
tvName = (TextView) itemView.findViewById(R.id.tvName);
tvHosts = (TextView) itemView.findViewById(R.id.tvHosts);
cbWifi = (CheckBox) itemView.findViewById(R.id.cbWifi);
ivScreenWifi = (ImageView) itemView.findViewById(R.id.ivScreenWifi);
cbOther = (CheckBox) itemView.findViewById(R.id.cbOther);
ivScreenOther = (ImageView) itemView.findViewById(R.id.ivScreenOther);
tvRoaming = (TextView) itemView.findViewById(R.id.tvRoaming);
llConfiguration = (LinearLayout) itemView.findViewById(R.id.llConfiguration);
tvUid = (TextView) itemView.findViewById(R.id.tvUid);
tvPackage = (TextView) itemView.findViewById(R.id.tvPackage);
tvVersion = (TextView) itemView.findViewById(R.id.tvVersion);
tvDescription = (TextView) itemView.findViewById(R.id.tvDescription);
tvInternet = (TextView) itemView.findViewById(R.id.tvInternet);
tvDisabled = (TextView) itemView.findViewById(R.id.tvDisabled);
tvStatistics = (TextView) itemView.findViewById(R.id.tvStatistics);
btnRelated = (Button) itemView.findViewById(R.id.btnRelated);
ibSettings = (ImageButton) itemView.findViewById(R.id.ibSettings);
ibDatasaver = (ImageButton) itemView.findViewById(R.id.ibDatasaver);
ibLaunch = (ImageButton) itemView.findViewById(R.id.ibLaunch);
cbApply = (CheckBox) itemView.findViewById(R.id.cbApply);
llScreenWifi = (LinearLayout) itemView.findViewById(R.id.llScreenWifi);
ivWifiLegend = (ImageView) itemView.findViewById(R.id.ivWifiLegend);
cbScreenWifi = (CheckBox) itemView.findViewById(R.id.cbScreenWifi);
llScreenOther = (LinearLayout) itemView.findViewById(R.id.llScreenOther);
ivOtherLegend = (ImageView) itemView.findViewById(R.id.ivOtherLegend);
cbScreenOther = (CheckBox) itemView.findViewById(R.id.cbScreenOther);
cbRoaming = (CheckBox) itemView.findViewById(R.id.cbRoaming);
btnClear = (ImageButton) itemView.findViewById(R.id.btnClear);
tvNoLog = (TextView) itemView.findViewById(R.id.tvNoLog);
tvNoFilter = (TextView) itemView.findViewById(R.id.tvNoFilter);
lvAccess = (ListView) itemView.findViewById(R.id.lvAccess);
btnClearAccess = (ImageButton) itemView.findViewById(R.id.btnClearAccess);
cbNotify = (CheckBox) 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));
}
});
}
}
public AdapterRule(Activity context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.context = 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));
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
try {
colorText = ta.getColor(0, 0);
} finally {
ta.recycle();
}
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);
iconSize = Util.dips2pixels(48, context);
}
public void set(List listRule) {
listAll = listRule;
listFiltered = new ArrayList<>();
listFiltered.addAll(listRule);
notifyDataSetChanged();
}
public void setWifiActive() {
wifiActive = true;
otherActive = false;
notifyDataSetChanged();
}
public void setMobileActive() {
wifiActive = false;
otherActive = true;
notifyDataSetChanged();
}
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;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Get rule
final Rule rule = listFiltered.get(position);
// Handle expanding/collapsing
holder.llApplication.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rule.expanded = !rule.expanded;
notifyItemChanged(position);
}
});
// Show if non default rules
holder.itemView.setBackgroundColor(rule.changed ? colorChanged : Color.TRANSPARENT);
// Show expand/collapse indicator
holder.ivExpander.setImageLevel(rule.expanded ? 1 : 0);
// Show application icon
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).resize(iconSize, iconSize).into(holder.ivIcon);
}
// Show application label
holder.tvName.setText(rule.name);
// Show application state
int color = rule.system ? colorOff : colorText;
if (!rule.internet || !rule.enabled)
color = Color.argb(128, Color.red(color), Color.green(color), Color.blue(color));
holder.tvName.setTextColor(color);
// Show rule count
new AsyncTask