Skip to content

Commit 8891230

Browse files
Merge pull request #234 from prey/fix/report-keep-exclude-after-boot
Keep excluded reports when restarting a missing report after boot
2 parents 88fbf34 + f3e81f9 commit 8891230

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎app/src/main/java/com/prey/json/actions/Report.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ public static void run(Context ctx, int intervalReport) {
111111
try {
112112
JSONObject parameters = new JSONObject();
113113
parameters.put("interval", intervalReport);
114+
// Keep the reports the user excluded (e.g. picture) when restarting after boot,
115+
// otherwise get() overwrites the stored exclude and pictures are taken again
116+
String exclude = PreyConfig.getPreyConfig(ctx).getExcludeReport();
117+
if (exclude != null && !"".equals(exclude)) {
118+
parameters.put("exclude", exclude);
119+
}
114120
new Report().get(ctx, null, parameters);
115121
} catch (Exception e) {
116122
PreyLogger.e("Error:"+e.getMessage(),e);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*******************************************************************************
2+
* Created by Patricio Jofré
3+
* Copyright 2026 Prey Inc. All rights reserved.
4+
* License: GPLv3
5+
* Full license at "/LICENSE"
6+
******************************************************************************/
7+
package com.prey.receivers;
8+
9+
import android.content.Context;
10+
import android.content.Intent;
11+
12+
import com.prey.PreyConfig;
13+
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.robolectric.RobolectricTestRunner;
18+
import org.robolectric.annotation.Config;
19+
20+
import androidx.test.core.app.ApplicationProvider;
21+
22+
import java.lang.reflect.Field;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
/**
27+
* Robolectric test suite for the {@link PreyBootController} class.
28+
* <p>
29+
* Tests that a missing report restarted after boot keeps its stored settings.
30+
* Runs on the JVM without an emulator.
31+
*/
32+
@RunWith(RobolectricTestRunner.class)
33+
@Config(sdk = 30)
34+
public class PreyBootControllerRobolectricTest {
35+
36+
private Context context;
37+
private PreyConfig preyConfig;
38+
39+
@Before
40+
public void setUp() throws Exception {
41+
context = ApplicationProvider.getApplicationContext();
42+
Field cached = PreyConfig.class.getDeclaredField("cachedInstance");
43+
cached.setAccessible(true);
44+
cached.set(null, null);
45+
preyConfig = PreyConfig.getPreyConfig(context);
46+
preyConfig.setMissing(false);
47+
preyConfig.setIntervalReport("");
48+
preyConfig.setExcludeReport("");
49+
}
50+
51+
@Test
52+
public void givenMissingReportWithExcludedPicture_whenBootCompleted_thenExcludeIsKept() {
53+
preyConfig.setIntervalReport("10");
54+
preyConfig.setExcludeReport("picture");
55+
56+
bootCompleted();
57+
58+
assertEquals("picture", preyConfig.getExcludeReport());
59+
assertEquals("10", preyConfig.getIntervalReport());
60+
}
61+
62+
@Test
63+
public void givenMissingReportWithoutExclusions_whenBootCompleted_thenNothingIsExcluded() {
64+
preyConfig.setIntervalReport("10");
65+
66+
bootCompleted();
67+
68+
assertEquals("", preyConfig.getExcludeReport());
69+
assertEquals("10", preyConfig.getIntervalReport());
70+
}
71+
72+
@Test
73+
public void givenNoMissingReport_whenBootCompleted_thenExcludeIsUntouched() {
74+
preyConfig.setExcludeReport("picture");
75+
76+
bootCompleted();
77+
78+
assertEquals("picture", preyConfig.getExcludeReport());
79+
assertEquals("", preyConfig.getIntervalReport());
80+
}
81+
82+
private void bootCompleted() {
83+
new PreyBootController().onReceive(context, new Intent(Intent.ACTION_BOOT_COMPLETED));
84+
}
85+
}

0 commit comments

Comments
 (0)