Skip to content

Commit 9ff64b4

Browse files
committed
Fix bugs, exclude some necessary trackers from blocking.
1 parent 4a2a45b commit 9ff64b4

15 files changed

+77
-65
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ android {
2525
applicationId 'net.kollnig.missioncontrol'
2626
minSdkVersion 22
2727
targetSdkVersion 29
28-
versionCode 7
29-
versionName "1.0.0-alpha6"
28+
versionCode 8
29+
versionName "1.0.0-alpha7"
3030
}
3131

3232
buildTypes {

app/src/main/assets/companyDomains.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@
338338
],
339339
"owner_name": "Akamai",
340340
"root_parent": null,
341-
"parent": null
341+
"parent": null,
342+
"necessary": true
342343
},
343344
{
344345
"country": "us",
@@ -461,7 +462,8 @@
461462
],
462463
"owner_name": "Amazon Web Services",
463464
"root_parent": "Amazon",
464-
"parent": "Amazon"
465+
"parent": "Amazon",
466+
"necessary": true
465467
},
466468
{
467469
"country": "us",

app/src/main/java/net/kollnig/missioncontrol/Common.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.ComponentName;
2222
import android.content.Context;
2323
import android.content.Intent;
24+
import android.content.pm.ApplicationInfo;
2425
import android.content.pm.PackageManager;
2526
import android.content.pm.ResolveInfo;
2627
import android.net.ConnectivityManager;
@@ -148,4 +149,16 @@ public static String getAppNameQ (final ByteBuffer packet,
148149

149150
return getAppName(pm, uid);
150151
}
152+
153+
154+
/**
155+
* Check if the {@code FLAG_SYSTEM} or {@code FLAG_UPDATED_SYSTEM_APP} is set for the application.
156+
*
157+
* @param applicationInfo The application to check.
158+
* @return true if the {@code applicationInfo} belongs to a system or updated system application.
159+
*/
160+
public static boolean isSystemPackage (ApplicationInfo applicationInfo) {
161+
return (((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
162+
|| (applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
163+
}
151164
}

app/src/main/java/net/kollnig/missioncontrol/data/Company.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ public class Company {
2121
public final String country;
2222
public final String name;
2323
public final String owner;
24+
public final Boolean necessary;
2425

25-
public Company (String country, String name, String owner) {
26+
public Company (String country, String name, String owner, Boolean necessary) {
2627
this.country = country;
2728
this.name = name;
2829
this.owner = owner;
30+
this.necessary = necessary;
2931
}
3032

3133
public String getOwner () {

app/src/main/java/net/kollnig/missioncontrol/data/Database.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class Database {
6969
private static final String COLUMN_TIME = "timestampt";
7070
private static final int DATABASE_VERSION = 3;
7171
public static Map<String, Company> hostnameToCompany = new ArrayMap<>();
72+
public static Set<String> necessaryCompanies = new HashSet<>();
7273
private static Database instance;
7374
private final SQLHandler sqlHandler;
7475
private Set<OnDatabaseClearListener> clearListeners = new HashSet<>();
@@ -468,6 +469,7 @@ public void logPacketAsyncTask (Context context,
468469
task.execute();
469470
}
470471

472+
471473
public void loadTrackerDomains (Context context) {
472474
try {
473475
// Read domain list
@@ -489,7 +491,14 @@ public void loadTrackerDomains (Context context) {
489491
if (!jsonCompany.isNull("root_parent")) {
490492
parent = jsonCompany.getString("root_parent");
491493
}
492-
company = new Company(country, name, parent);
494+
Boolean necessary;
495+
if (jsonCompany.has("necessary")) {
496+
necessary = jsonCompany.getBoolean("necessary");
497+
necessaryCompanies.add(name);
498+
} else {
499+
necessary = false;
500+
}
501+
company = new Company(country, name, parent, necessary);
493502

494503
JSONArray domains = jsonCompany.getJSONArray("doms");
495504
for (int j = 0; j < domains.length(); j++) {

app/src/main/java/net/kollnig/missioncontrol/data/Tracker.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public int compareTo (Tracker o) {
3232

3333
@Override
3434
public String toString () {
35-
return name;
35+
if (Database.necessaryCompanies.contains(name))
36+
return name + " (Unblocked)";
37+
else {
38+
return name;
39+
}
3640
}
3741

3842
public String getOwner () {

app/src/main/java/net/kollnig/missioncontrol/details/TransmissionsFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static TransmissionsFragment newInstance (String appId) {
6161
public void onCreate (Bundle savedInstanceState) {
6262
super.onCreate(savedInstanceState);
6363
Bundle bundle = getArguments();
64-
mAppId = getArguments().getString(ARG_APP_ID);
64+
mAppId = bundle.getString(ARG_APP_ID);
6565
}
6666

6767
@Override

app/src/main/java/net/kollnig/missioncontrol/details/TransmissionsListAdapter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* {@link RecyclerView.Adapter} that can display a {@link Tracker}.
4141
*/
4242
public class TransmissionsListAdapter extends RecyclerView.Adapter<TransmissionsListAdapter.ViewHolder> {
43-
43+
private final String TAG = TransmissionsListAdapter.class.getSimpleName();
4444
private final List<Tracker> mValues;
4545
private final RecyclerView recyclerView;
4646
private final String mAppId;
@@ -89,7 +89,9 @@ public void onBindViewHolder (final ViewHolder holder, final int position) {
8989
holder.mSwitch.setChecked(
9090
w.blockedTracker(mAppId, tracker.name)
9191
);
92-
holder.mSwitch.setOnCheckedChangeListener((v, isChecked) -> {
92+
holder.mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
93+
if (!buttonView.isPressed()) return;
94+
9395
if (isChecked) {
9496
if (!w.blockedApp(mAppId)) {
9597
w.addToBlocklist(mAppId);

app/src/main/java/net/kollnig/missioncontrol/main/AppBlocklistController.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.content.SharedPreferences;
2121
import android.content.pm.ApplicationInfo;
2222
import android.content.pm.PackageManager;
23+
import android.graphics.drawable.Drawable;
2324

2425
import net.kollnig.missioncontrol.data.App;
2526

@@ -30,18 +31,22 @@
3031

3132
import androidx.preference.PreferenceManager;
3233

34+
import static net.kollnig.missioncontrol.Common.isSystemPackage;
35+
3336
public class AppBlocklistController extends BlocklistController {
3437
private static final String SHARED_PREFS_BLOCKLIST_APPS_KEY = "APPS_BLOCKLIST_APPS_KEY";
3538
private static AppBlocklistController instance;
36-
final PackageManager pm;
39+
private final PackageManager pm;
3740
private final String ownPackageName;
38-
public Set<String> systemApps = new HashSet<>();
41+
private final Set<String> systemApps = new HashSet<>();
42+
private final Drawable defaultIcon;
3943

4044
private AppBlocklistController (Context c) {
4145
super(c);
4246

4347
pm = c.getPackageManager();
4448
ownPackageName = c.getApplicationContext().getPackageName();
49+
defaultIcon = c.getDrawable(android.R.drawable.sym_def_app_icon);
4550

4651
SharedPreferences settingsPref = PreferenceManager.getDefaultSharedPreferences(c);
4752
boolean showSystemApps = settingsPref.getBoolean
@@ -72,7 +77,8 @@ public List<App> load (boolean showSystemApps) {
7277
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_META_DATA);
7378
for (ApplicationInfo appInfo : appInfos) {
7479
if (//!BuildConfig.DEBUG &&
75-
(isSystemPackage(appInfo) || appInfo.packageName.equals(ownPackageName))) {
80+
(isSystemPackage(appInfo)
81+
|| appInfo.packageName.equals(ownPackageName))) {
7682
if (initialisation) {
7783
systemApps.add(appInfo.packageName);
7884
}
@@ -82,7 +88,7 @@ public List<App> load (boolean showSystemApps) {
8288
App app = new App();
8389

8490
app.id = appInfo.packageName;
85-
app.icon = appInfo.loadIcon(pm);
91+
app.icon = (appInfo.icon != 0) ? appInfo.loadIcon(pm) : defaultIcon;
8692
app.name = appInfo.loadLabel(pm).toString();
8793
app.systemApp = systemApps.contains(appInfo.packageName);
8894

@@ -105,15 +111,4 @@ public boolean blockedTracker (String appId, String tracker) {
105111
public String getPrefKey () {
106112
return SHARED_PREFS_BLOCKLIST_APPS_KEY;
107113
}
108-
109-
/**
110-
* Check if the {@code FLAG_SYSTEM} or {@code FLAG_UPDATED_SYSTEM_APP} is set for the application.
111-
*
112-
* @param applicationInfo The application to check.
113-
* @return true if the {@code applicationInfo} belongs to a system or updated system application.
114-
*/
115-
public boolean isSystemPackage (ApplicationInfo applicationInfo) {
116-
return (((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
117-
|| (applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
118-
}
119114
}

app/src/main/java/net/kollnig/missioncontrol/main/AppsFragment.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Map;
3939
import java.util.Set;
4040

41+
import androidx.annotation.NonNull;
4142
import androidx.appcompat.app.AlertDialog;
4243
import androidx.fragment.app.Fragment;
4344
import androidx.preference.PreferenceManager;
@@ -55,11 +56,11 @@ public class AppsFragment extends Fragment implements SwipeRefreshLayout.OnRefre
5556
Database.OnDatabaseClearListener {
5657

5758
private static final String TAG = AppsFragment.class.getSimpleName();
58-
AppsListAdapter mAppsListAdapter;
59-
RecyclerView mRecyclerView;
60-
Database database;
61-
SwipeRefreshLayout mSwipeRefreshLayout;
62-
ProgressBar pbApps;
59+
private AppsListAdapter mAppsListAdapter;
60+
private RecyclerView mRecyclerView;
61+
private Database database;
62+
private SwipeRefreshLayout mSwipeRefreshLayout;
63+
private ProgressBar pbApps;
6364

6465
public AppsFragment () {
6566
// Required empty public constructor
@@ -86,7 +87,7 @@ public static void savePrefs (Context c) {
8687
editor.apply();
8788
}
8889

89-
SharedPreferences settingsPref;
90+
private SharedPreferences settingsPref;
9091

9192
@Override
9293
public void onCreate (Bundle savedInstanceState) {
@@ -98,7 +99,7 @@ public void onCreate (Bundle savedInstanceState) {
9899
}
99100

100101
@Override
101-
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
102+
public void onCreateOptionsMenu (@NonNull Menu menu, @NonNull MenuInflater inflater) {
102103
super.onCreateOptionsMenu(menu, inflater);
103104
//inflater.inflate(R.menu.menu_main, menu);
104105
}
@@ -140,7 +141,7 @@ public View onCreateView (LayoutInflater inflater, ViewGroup container,
140141
}
141142

142143
@Override
143-
public void onViewCreated (View view, Bundle savedInstanceState) {
144+
public void onViewCreated (@NonNull View view, Bundle savedInstanceState) {
144145
super.onViewCreated(view, savedInstanceState);
145146

146147
mAppsListAdapter = new AppsListAdapter(this);
@@ -177,7 +178,7 @@ public void onDetach () {
177178
database.removeListener(this);
178179
}
179180

180-
public void updateUI (List<App> installedApps) {
181+
private void updateUI (List<App> installedApps) {
181182
mAppsListAdapter.setAppsList(installedApps);
182183
mAppsListAdapter.notifyDataSetChanged();
183184
mRecyclerView.scrollToPosition(0);
@@ -187,7 +188,7 @@ public void updateUI (List<App> installedApps) {
187188
pbApps.setVisibility(View.GONE);
188189
}
189190

190-
void refreshApps () {
191+
private void refreshApps () {
191192
boolean mShowSystemApps = settingsPref.getBoolean
192193
(SettingsActivity.KEY_PREF_SYSTEMAPPS_SWITCH, false);
193194
(new AppsRefreshTask(this, mShowSystemApps)).execute();
@@ -218,7 +219,6 @@ static class AppsRefreshTask extends AsyncTask<Void, Void, Boolean> {
218219

219220
@Override
220221
protected Boolean doInBackground (Void... voids) {
221-
222222
installedApps = appBlocklistController.load(mShowSystemApps);
223223
Map<String, Integer> trackerCounts = database.getApps();
224224

0 commit comments

Comments
 (0)