-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathAdapterRule.java
More file actions
1141 lines (1002 loc) · 47.1 KB
/
AdapterRule.java
File metadata and controls
1141 lines (1002 loc) · 47.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is from 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/>.
*
* Copyright © 2015–2020 by Marcel Bokhorst (M66B), Konrad
* Kollnig (University of Oxford)
*/
package eu.faircode.netguard;
import static net.kollnig.missioncontrol.DetailsActivity.INTENT_EXTRA_APP_NAME;
import static net.kollnig.missioncontrol.DetailsActivity.INTENT_EXTRA_APP_PACKAGENAME;
import static net.kollnig.missioncontrol.DetailsActivity.INTENT_EXTRA_APP_UID;
import static eu.faircode.netguard.ActivityMain.REQUEST_DETAILS_UPDATED;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
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.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
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.CursorAdapter;
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.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.snackbar.Snackbar;
import net.kollnig.missioncontrol.Common;
import net.kollnig.missioncontrol.DetailsActivity;
import net.kollnig.missioncontrol.R;
import net.kollnig.missioncontrol.data.InternetBlocklist;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> implements Filterable {
private static final String TAG = "TrackerControl.Adapter";
private View anchor;
private LayoutInflater inflater;
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 boolean live = true;
private List<Rule> listAll = new ArrayList<>();
private List<Rule> listFiltered = new ArrayList<>();
private List<String> messaging = Arrays.asList(
"com.discord",
"com.facebook.mlite",
"com.facebook.orca",
"com.instagram.android",
"com.Slack",
"com.skype.raider",
"com.snapchat.android",
"com.whatsapp",
"com.whatsapp.w4b");
private List<String> download = Arrays.asList(
"com.google.android.youtube");
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 RelativeLayout rlLockdown;
public ImageView ivLockdown;
public CheckBox cbWifi;
public ImageView ivScreenWifi;
public CheckBox cbOther;
public ImageView ivScreenOther;
public TextView tvRoaming;
public TextView tvRemarkMessaging;
public TextView tvRemarkDownload;
public LinearLayout llConfiguration;
public TextView tvUid;
public TextView tvPackage;
public TextView tvVersion;
public TextView tvInternet;
public TextView tvDisabled;
public Button btnRelated;
public ImageButton ibSettings;
public ImageButton ibLaunch;
public Switch cbApply;
public LinearLayout llScreenWifi;
public ImageView ivWifiLegend;
public CheckBox cbScreenWifi;
public LinearLayout llScreenOther;
public ImageView ivOtherLegend;
public CheckBox cbScreenOther;
public CheckBox cbRoaming;
public CheckBox cbLockdown;
public ImageView ivLockdownLegend;
public ImageButton btnClear;
public LinearLayout llFilter;
public ImageView ivLive;
public TextView tvLogging;
public Button btnLogging;
public ListView lvAccess;
public ImageButton btnClearAccess;
public CheckBox cbNotify;
// Custom code
private final TextView tvDetails;
public ViewHolder(View itemView) {
super(itemView);
view = itemView;
llApplication = itemView.findViewById(R.id.llApplication);
ivIcon = itemView.findViewById(R.id.ivIcon);
ivExpander = itemView.findViewById(R.id.ivExpander);
tvName = itemView.findViewById(R.id.tvName);
tvHosts = itemView.findViewById(R.id.tvHosts);
rlLockdown = itemView.findViewById(R.id.rlLockdown);
ivLockdown = itemView.findViewById(R.id.ivLockdown);
cbWifi = itemView.findViewById(R.id.cbWifi);
ivScreenWifi = itemView.findViewById(R.id.ivScreenWifi);
cbOther = itemView.findViewById(R.id.cbOther);
ivScreenOther = itemView.findViewById(R.id.ivScreenOther);
tvRoaming = itemView.findViewById(R.id.tvRoaming);
tvRemarkMessaging = itemView.findViewById(R.id.tvRemarkMessaging);
tvRemarkDownload = itemView.findViewById(R.id.tvRemarkDownload);
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);
btnRelated = itemView.findViewById(R.id.btnRelated);
ibSettings = itemView.findViewById(R.id.ibSettings);
ibLaunch = itemView.findViewById(R.id.ibLaunch);
cbApply = itemView.findViewById(R.id.cbApply);
tvDetails = itemView.findViewById(R.id.app_details);
llScreenWifi = itemView.findViewById(R.id.llScreenWifi);
ivWifiLegend = itemView.findViewById(R.id.ivWifiLegend);
cbScreenWifi = itemView.findViewById(R.id.cbScreenWifi);
llScreenOther = itemView.findViewById(R.id.llScreenOther);
ivOtherLegend = itemView.findViewById(R.id.ivOtherLegend);
cbScreenOther = itemView.findViewById(R.id.cbScreenOther);
cbRoaming = itemView.findViewById(R.id.cbRoaming);
cbLockdown = itemView.findViewById(R.id.cbLockdown);
ivLockdownLegend = itemView.findViewById(R.id.ivLockdownLegend);
btnClear = itemView.findViewById(R.id.btnClear);
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));
}
});
}
}
public AdapterRule(Context context, View anchor) {
this.anchor = anchor;
this.inflater = LayoutInflater.from(context);
if (Common.isNight(context))
colorChanged = Color.argb(128, Color.red(Color.DKGRAY), Color.green(Color.DKGRAY),
Color.blue(Color.DKGRAY));
else
colorChanged = Color.argb(128, 230, 230, 230);
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);
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);
}
public void set(List<Rule> 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();
}
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;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Context context = holder.itemView.getContext();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean log_app = prefs.getBoolean("log_app", true);
final boolean filter = prefs.getBoolean("filter", true);
final boolean notify_access = prefs.getBoolean("notify_access", false);
// Get rule
final Rule rule = listFiltered.get(position);
final boolean active = rule.apply && !rule.vpn_exclude;
// Handle expanding/collapsing
holder.llApplication.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rule.expanded = !rule.expanded;
notifyItemChanged(holder.getAdapterPosition());
}
});
// 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.icon <= 0)
holder.ivIcon.setImageResource(android.R.drawable.sym_def_app_icon);
else {
Uri uri = Uri.parse("android.resource://" + rule.packageName + "/" + rule.icon);
GlideApp.with(holder.itemView.getContext())
.applyDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565))
.load(uri)
// .diskCacheStrategy(DiskCacheStrategy.NONE)
// .skipMemoryCache(true)
.override(iconSize, iconSize)
.into(holder.ivIcon);
}
// Show application label
holder.tvName.setText(rule.name);
// Show application state
holder.tvName.setTextColor(rule.system ? colorOff : colorText);
holder.tvHosts.setVisibility(rule.hosts > 0 ? View.VISIBLE : View.GONE);
holder.tvHosts.setText(Long.toString(rule.hosts));
// 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;
holder.rlLockdown.setVisibility(lockdown && !rule.lockdown ? View.VISIBLE : View.GONE);
holder.ivLockdown.setEnabled(active);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrap = DrawableCompat.wrap(holder.ivLockdown.getDrawable());
DrawableCompat.setTint(wrap, active ? colorOff : colorGrayed);
}
boolean screen_on = prefs.getBoolean("screen_on", true);
// BEGIN TRACKERCONTROL: Skipped - parent views are GONE in rule.xml (legacy
// NetGuard UI)
// These views are hidden in TrackerControl but code is kept for upstream merge
// compatibility
/*
* // Wi-Fi settings
* holder.cbWifi.setEnabled(active);
* 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, active ? (rule.wifi_blocked ? colorOff :
* colorOn) : colorGrayed);
* }
* holder.cbWifi.setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override
* public void onCheckedChanged(CompoundButton compoundButton, boolean
* isChecked) {
* rule.wifi_blocked = isChecked;
* updateRule(context, rule, true, listAll);
* }
* });
*
* holder.ivScreenWifi.setEnabled(active);
* 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, active ? colorOn : colorGrayed);
* }
*
* // Mobile settings
* holder.cbOther.setEnabled(active);
* holder.cbOther.setAlpha(otherActive ? 1 : 0.5f);
* 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, active ? (rule.other_blocked ? colorOff :
* colorOn) : colorGrayed);
* }
* holder.cbOther.setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override
* public void onCheckedChanged(CompoundButton compoundButton, boolean
* isChecked) {
* rule.other_blocked = isChecked;
* updateRule(context, rule, true, listAll);
* }
* });
*
* holder.ivScreenOther.setEnabled(active);
* 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, active ? colorOn : colorGrayed);
* }
*
* holder.tvRoaming.setTextColor(active ? colorOff : colorGrayed);
* holder.tvRoaming.setAlpha(otherActive ? 1 : 0.5f);
* holder.tvRoaming.setVisibility(
* rule.roaming && (!rule.other_blocked || rule.screen_other) ? View.VISIBLE :
* View.INVISIBLE);
*
* holder.tvRemarkMessaging.setVisibility(messaging.contains(rule.packageName) ?
* View.VISIBLE : View.GONE);
* holder.tvRemarkDownload.setVisibility(download.contains(rule.packageName) ?
* View.VISIBLE : View.GONE);
*/
// END TRACKERCONTROL
// Expanded configuration section
holder.llConfiguration.setVisibility(rule.expanded ? View.VISIBLE : View.GONE);
// Show application details
holder.tvUid.setText(Integer.toString(rule.uid));
holder.tvPackage.setText(rule.packageName);
holder.tvVersion.setText(rule.version);
// Show application state
holder.tvInternet.setVisibility(rule.internet ? View.GONE : View.VISIBLE);
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);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(rule.uid));
main.putExtra(ActivityMain.EXTRA_RELATED, true);
context.startActivity(main);
}
});
// 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);
}
});
} else
holder.ibLaunch.setVisibility(View.GONE);
// Apply
holder.cbApply.setEnabled(rule.pkg && filter);
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;
updateRule(context, rule, true, listAll);
}
});
// Show if Internet access blocked
final ImageView iv = holder.ivIcon;
InternetBlocklist internetBlocklist = InternetBlocklist.getInstance(context);
setGreyscale(iv, rule.internet && active && internetBlocklist.blockedInternet(rule.uid));
holder.ivIcon.setOnClickListener(view -> {
if (!rule.internet)
return;
if (!active) {
Snackbar s = Common.getSnackbar((Activity) context, R.string.bypass_vpn_error);
if (s != null)
s.show();
return;
}
boolean wasBlocked = internetBlocklist.blockedInternet(rule.uid);
if (wasBlocked) {
internetBlocklist.unblock(rule.uid);
Toast.makeText(context, R.string.internet_unblocked, Toast.LENGTH_SHORT).show();
} else {
internetBlocklist.block(rule.uid);
Toast.makeText(context, R.string.internet_blocked, Toast.LENGTH_SHORT).show();
}
setGreyscale(iv, !wasBlocked);
});
if (Util.isPlayStoreInstall()) {
holder.cbApply.setVisibility(View.INVISIBLE);
holder.cbApply.setWidth(0);
}
String sort = prefs.getString("sort", "trackers_week");
boolean pastWeekOnly = !("trackers_all".equals(sort));
final int trackerCount = rule.getTrackerCount(pastWeekOnly);
if (trackerCount > 0) {
holder.tvDetails.setVisibility(View.VISIBLE);
if (pastWeekOnly)
holder.tvDetails.setText(context.getResources().getQuantityString(
R.plurals.n_companies_found_week, trackerCount, trackerCount));
else
holder.tvDetails.setText(context.getResources().getQuantityString(
R.plurals.n_companies_found, trackerCount, trackerCount));
} else if (!rule.internet) {
holder.tvDetails.setVisibility(View.VISIBLE);
holder.tvDetails.setText(R.string.no_internet);
/*
* } else if (!rule.apply) {
* holder.tvDetails.setVisibility(View.VISIBLE);
* holder.tvDetails.setText(R.string.bypass_vpn);
*/
} else
holder.tvDetails.setVisibility(View.GONE);
holder.itemView.setOnClickListener(view -> {
if (!rule.internet) {
Snackbar s = Common.getSnackbar((Activity) context, R.string.no_internet_message);
if (s != null)
s.show();
} else {
final Intent settings = new Intent(context, DetailsActivity.class);
settings.putExtra(INTENT_EXTRA_APP_NAME, rule.name);
settings.putExtra(INTENT_EXTRA_APP_PACKAGENAME, rule.packageName);
settings.putExtra(INTENT_EXTRA_APP_UID, rule.uid);
((Activity) context).startActivityForResult(settings, REQUEST_DETAILS_UPDATED);
}
});
// BEGIN TRACKERCONTROL: Skipped - parent views are GONE in rule.xml (legacy
// NetGuard UI)
/*
* // Show Wi-Fi screen on condition
* holder.llScreenWifi.setVisibility(screen_on ? View.VISIBLE : View.GONE);
* holder.cbScreenWifi.setEnabled(rule.wifi_blocked && active);
* 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);
* }
*
* holder.cbScreenWifi.setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override
* public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
* rule.screen_wifi = isChecked;
* updateRule(context, rule, true, listAll);
* }
* });
*
* // Show mobile screen on condition
* holder.llScreenOther.setVisibility(screen_on ? View.VISIBLE : View.GONE);
* holder.cbScreenOther.setEnabled(rule.other_blocked && active);
* 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);
* }
*
* holder.cbScreenOther.setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override
* public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
* rule.screen_other = isChecked;
* updateRule(context, rule, true, listAll);
* }
* });
*
* // Show roaming condition
* holder.cbRoaming.setEnabled((!rule.other_blocked || rule.screen_other) &&
* active);
* holder.cbRoaming.setOnCheckedChangeListener(null);
* holder.cbRoaming.setChecked(rule.roaming);
* holder.cbRoaming.setOnCheckedChangeListener(new
* CompoundButton.OnCheckedChangeListener() {
*
* @Override
*
* @TargetApi(Build.VERSION_CODES.M)
* public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
* rule.roaming = isChecked;
* updateRule(context, rule, true, listAll);
* }
* });
*
* // Show lockdown
* holder.cbLockdown.setEnabled(active);
* 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;
* updateRule(context, rule, true, listAll);
* }
* });
*
* // Reset rule
* holder.btnClear.setOnClickListener(new View.OnClickListener() {
*
* @Override
* public void onClick(View view) {
* Util.areYouSure(view.getContext(), R.string.msg_clear_rules, new
* Util.DoubtListener() {
*
* @Override
* public void onSure() {
* holder.cbApply.setChecked(true);
* 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);
* holder.cbLockdown.setChecked(false);
* }
* });
* }
* });
*/
// END TRACKERCONTROL
holder.llFilter.setVisibility(Util.canFilter(context) ? View.VISIBLE : View.GONE);
// Live
holder.ivLive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
live = !live;
TypedValue tv = new TypedValue();
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
holder.tvLogging.setText(log_app && filter ? R.string.title_logging_enabled : R.string.title_logging_disabled);
holder.btnLogging.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.enable, null, false);
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);
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();
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();
ServiceSinkhole.reload("changed filter", context, false);
AdapterRule.this.notifyDataSetChanged();
}
});
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();
}
});
// Show access rules
if (rule.expanded) {
// Access the database when expanded only
final AdapterAccess badapter = new AdapterAccess(context,
DatabaseHelper.getInstance(context).getAccess(rule.uid));
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"));
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"));
long time = cursor.getLong(cursor.getColumnIndex("time"));
int block = cursor.getInt(cursor.getColumnIndex("block"));
PopupMenu popup = new PopupMenu(context, anchor);
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);
// Whois
final Intent lookupIP = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.dnslytics.com/whois-lookup/" + daddr));
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
final Intent lookupPort = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.speedguide.net/port.php?port=" + dport));
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) {
int menu = menuItem.getItemId();
boolean result = false;
if (menu == R.id.menu_whois) {
context.startActivity(lookupIP);
result = true;
} else if (menu == R.id.menu_port) {
context.startActivity(lookupPort);
result = true;
} else if (menu == R.id.menu_allow) {
DatabaseHelper.getInstance(context).setAccess(id, 0);
ServiceSinkhole.reload("allow host", context, false);
result = true;
} else if (menu == R.id.menu_block) {
DatabaseHelper.getInstance(context).setAccess(id, 1);
ServiceSinkhole.reload("block host", context, false);
result = true;
} else if (menu == R.id.menu_reset) {
DatabaseHelper.getInstance(context).setAccess(id, -1);
ServiceSinkhole.reload("reset host", context, false);
result = true;
} else if (menu == R.id.menu_copy) {
ClipboardManager clipboard = (ClipboardManager) context
.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("netguard", daddr);
clipboard.setPrimaryClip(clip);
return true;
}
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) {
return DatabaseHelper.getInstance(context).getHostCount(rule.uid, false);
}
@Override
protected void onPostExecute(Long hosts) {
rule.hosts = hosts;
notifyDataSetChanged();
}
}.execute();
return result;
}
});
if (block == 0)
popup.getMenu().removeItem(R.id.menu_allow);
else if (block == 1)
popup.getMenu().removeItem(R.id.menu_block);
popup.show();
}
});
holder.lvAccess.setAdapter(badapter);
} else {
// Close any existing cursor before setting adapter to null
CursorAdapter existingAdapter = (CursorAdapter) holder.lvAccess.getAdapter();
if (existingAdapter != null) {
existingAdapter.changeCursor(null);
}
holder.lvAccess.setAdapter(null);
holder.lvAccess.setOnItemClickListener(null);
}
// 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() {
DatabaseHelper.getInstance(context).clearAccess(rule.uid, true);
if (!live)
notifyDataSetChanged();
if (rv != null)
rv.scrollToPosition(holder.getAdapterPosition());
}
});
}
});
// Notify on access
holder.cbNotify.setEnabled(prefs.getBoolean("notify_access", false) && rule.apply);
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;
updateRule(context, rule, true, listAll);
}
});
}
private void setGreyscale(ImageView iv, boolean on) {
if (on) {
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0); // 0 means grayscale
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
iv.setColorFilter(cf);
iv.setImageAlpha(128); // 128 = 0.5
} else {
iv.setColorFilter(null);
iv.setImageAlpha(255);
}
}
@Override
public void onViewRecycled(ViewHolder holder) {
super.onViewRecycled(holder);
// Context context = holder.itemView.getContext();
// GlideApp.with(context).clear(holder.ivIcon);
CursorAdapter adapter = (CursorAdapter) holder.lvAccess.getAdapter();
if (adapter != null) {
Log.i(TAG, "Closing access cursor");
adapter.changeCursor(null);
holder.lvAccess.setAdapter(null);
}
}
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);
SharedPreferences lockdown = context.getSharedPreferences("lockdown", Context.MODE_PRIVATE);
SharedPreferences notify = context.getSharedPreferences("notify", Context.MODE_PRIVATE);