Skip to content

Commit 9b053e2

Browse files
committed
Ensure broadcast receiver complies with Android 13+
1 parent 46f2301 commit 9b053e2

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

app/build.gradle.kts

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ android {
1010
applicationId = "com.nemesis.mocktraffic"
1111
minSdk = 24
1212
targetSdk = 34
13-
versionCode = 1
14-
versionName = "1.0"
13+
versionCode = 2
14+
versionName = "1.1"
1515

1616
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
17+
18+
dependenciesInfo {
19+
// Disables dependency metadata when building APKs.
20+
includeInApk = false
21+
// Disables dependency metadata when building Android App Bundles.
22+
includeInBundle = false
23+
}
1724
}
1825

1926
buildTypes {
@@ -40,4 +47,4 @@ dependencies {
4047
testImplementation(libs.junit)
4148
androidTestImplementation(libs.ext.junit)
4249
androidTestImplementation(libs.espresso.core)
43-
}
50+
}

app/src/main/assets/config.json

+27-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,33 @@
1414
"https://youtube.com",
1515
"https://github.com",
1616
"https://medium.com",
17-
"https://thepiratebay.org"
17+
"https://thepiratebay.org",
18+
"https://www.facebook.com",
19+
"https://www.instagram.com",
20+
"https://www.twitter.com",
21+
"https://www.linkedin.com",
22+
"https://www.bbc.com",
23+
"https://www.nytimes.com",
24+
"https://www.amazon.com",
25+
"https://www.netflix.com",
26+
"https://www.imdb.com",
27+
"https://www.tumblr.com",
28+
"https://www.aliexpress.com",
29+
"https://www.craigslist.org",
30+
"https://www.quora.com",
31+
"https://www.pinterest.com",
32+
"https://www.foxnews.com",
33+
"https://www.bloomberg.com",
34+
"https://www.wsj.com",
35+
"https://www.forbes.com",
36+
"https://www.espn.com",
37+
"https://www.twitch.tv",
38+
"https://www.msn.com",
39+
"https://www.paypal.com",
40+
"https://www.bankofamerica.com",
41+
"https://www.nike.com",
42+
"https://www.adobe.com",
43+
"https://www.spotify.com"
1844
],
1945
"blacklisted_urls": [
2046
"https://t.co",

app/src/main/java/com/nemesis/mocktraffic/MainActivity.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import android.Manifest;
44
import android.app.AlertDialog;
55
import android.content.BroadcastReceiver;
6-
import android.content.ComponentName;
76
import android.content.Context;
87
import android.content.Intent;
98
import android.content.IntentFilter;
9+
import android.content.SharedPreferences;
1010
import android.content.pm.PackageManager;
1111
import android.os.Build;
1212
import android.os.Bundle;
@@ -36,6 +36,12 @@ public void onReceive(Context context, Intent intent) {
3636
if (TrafficService.ACTION_UPDATE_STATS.equals(intent.getAction())) {
3737
int requestCount = intent.getIntExtra("requestCount", 0);
3838
trafficStatsTextView.setText("Traffic Stats: " + requestCount + " requests");
39+
40+
// Save request count to SharedPreferences
41+
SharedPreferences preferences = getSharedPreferences("app_prefs", MODE_PRIVATE);
42+
SharedPreferences.Editor editor = preferences.edit();
43+
editor.putInt("request_count", requestCount);
44+
editor.apply();
3945
}
4046
}
4147
};
@@ -50,6 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {
5056
trafficStatsTextView = findViewById(R.id.trafficStatsTextView);
5157
statusTextView = findViewById(R.id.statusTextView);
5258

59+
// Restore saved traffic generation setting
60+
SharedPreferences preferences = getSharedPreferences("app_prefs", MODE_PRIVATE);
61+
boolean trafficEnabled = preferences.getBoolean("traffic_enabled", false);
62+
trafficCheckBox.setChecked(trafficEnabled);
63+
64+
// Restore the saved request count
65+
int savedRequestCount = preferences.getInt("request_count", 0);
66+
trafficStatsTextView.setText("Traffic Stats: " + savedRequestCount + " requests");
67+
5368
// Check and request POST_NOTIFICATIONS permission if needed
5469
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13+
5570
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
@@ -59,6 +74,11 @@ protected void onCreate(Bundle savedInstanceState) {
5974

6075
// Toggle traffic generation when checkbox is clicked
6176
trafficCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
77+
// Save the traffic generation setting
78+
SharedPreferences.Editor editor = preferences.edit();
79+
editor.putBoolean("traffic_enabled", isChecked);
80+
editor.apply();
81+
6282
if (isChecked) {
6383
// Check if POST_NOTIFICATIONS permission is granted (Android 13+)
6484
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@@ -104,7 +124,11 @@ protected void onResume() {
104124
super.onResume();
105125
// Register the receiver
106126
IntentFilter filter = new IntentFilter(TrafficService.ACTION_UPDATE_STATS);
107-
registerReceiver(statsReceiver, filter);
127+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13 and above
128+
registerReceiver(statsReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
129+
} else {
130+
registerReceiver(statsReceiver, filter); // Older versions
131+
}
108132
}
109133

110134
@Override

app/src/main/java/com/nemesis/mocktraffic/TrafficService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public void run() {
180180

181181
// Method to make an HTTP request to a given URL
182182
private void makeHttpRequest(final String url) {
183+
if (!url.startsWith("http://") && !url.startsWith("https://")) {
184+
Log.e("TrafficService", "Invalid URL scheme: " + url);
185+
return; // Skip this URL since it's not an HTTP/HTTPS URL
186+
}
187+
183188
Request request = new Request.Builder()
184189
.url(url)
185190
.build();
@@ -188,7 +193,6 @@ private void makeHttpRequest(final String url) {
188193
@Override
189194
public void onFailure(Call call, IOException e) {
190195
Log.e("TrafficService", "Failed to load URL: " + url, e);
191-
// Optionally, you can broadcast failure
192196
}
193197

194198
@Override
@@ -211,6 +215,7 @@ public void onResponse(Call call, Response response) throws IOException {
211215
});
212216
}
213217

218+
214219
// Broadcast the updated stats
215220
private void broadcastStats() {
216221
Intent intent = new Intent(ACTION_UPDATE_STATS);

0 commit comments

Comments
 (0)