Skip to content

Commit 7062852

Browse files
CopilotTomTasche
andcommitted
Improve MainActivity logic and add basic test for file type switch
Co-authored-by: TomTasche <128734+TomTasche@users.noreply.github.com>
1 parent 2cc1dac commit 7062852

2 files changed

Lines changed: 66 additions & 23 deletions

File tree

app/src/androidTest/java/at/tomtasche/reader/test/MainActivityTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.net.Uri;
2020
import android.util.ArrayMap;
2121

22+
import androidx.appcompat.widget.SwitchCompat;
2223
import androidx.core.content.FileProvider;
2324
import androidx.test.espresso.IdlingRegistry;
2425
import androidx.test.espresso.IdlingResource;
@@ -184,4 +185,22 @@ public void testPDF() {
184185
.perform(click());
185186
});
186187
}
188+
189+
@Test
190+
public void testFileTypeSwitch() {
191+
// Test the file type registration switch functionality
192+
SwitchCompat fileTypeSwitch = mainActivityActivityTestRule.getActivity().findViewById(R.id.landing_catch_all);
193+
Assert.assertNotNull("File type switch should exist", fileTypeSwitch);
194+
195+
// Test toggling the switch
196+
onView(withId(R.id.landing_catch_all))
197+
.perform(click());
198+
199+
// Switch should now be in opposite state
200+
onView(withId(R.id.landing_catch_all))
201+
.perform(click());
202+
203+
// Should be back to original state
204+
// This test verifies the switch is functional and doesn't crash
205+
}
187206
}

app/src/main/java/at/tomtasche/reader/ui/activity/MainActivity.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,23 @@ private void initializeCatchAllSwitch() {
224224
// Determine current mode based on enabled components
225225
boolean isCatchAllEnabled = getPackageManager().getComponentEnabledSetting(catchAllComponent) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
226226
boolean isCommonTypesEnabled = getPackageManager().getComponentEnabledSetting(commonTypesComponent) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
227+
boolean isStrictEnabled = getPackageManager().getComponentEnabledSetting(strictCatchComponent) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
227228

228-
// Default to common types if this is first run (all are disabled)
229-
if (!isCatchAllEnabled && !isCommonTypesEnabled) {
230-
isCommonTypesEnabled = true;
229+
// Determine current mode - prioritize in order: CATCH_ALL -> COMMON_TYPES -> STRICT
230+
String currentMode;
231+
if (isCatchAllEnabled) {
232+
currentMode = "CATCH_ALL";
233+
} else if (isCommonTypesEnabled) {
234+
currentMode = "COMMON_TYPES";
235+
} else if (isStrictEnabled) {
236+
currentMode = "STRICT";
237+
} else {
238+
// Default for new installations or if all are disabled
239+
currentMode = "COMMON_TYPES";
231240
}
232-
233-
// Ensure only one mode is enabled
234-
toggleComponent(catchAllComponent, isCatchAllEnabled && !isCommonTypesEnabled);
235-
toggleComponent(commonTypesComponent, isCommonTypesEnabled && !isCatchAllEnabled);
236-
toggleComponent(strictCatchComponent, !isCatchAllEnabled && !isCommonTypesEnabled);
241+
242+
// Set the appropriate mode
243+
setFileTypeMode(currentMode);
237244

238245
SwitchCompat catchAllSwitch = findViewById(R.id.landing_catch_all);
239246

@@ -242,30 +249,47 @@ private void initializeCatchAllSwitch() {
242249
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
243250
if (isChecked) {
244251
// Switch to common types mode
245-
toggleComponent(catchAllComponent, false);
246-
toggleComponent(commonTypesComponent, true);
247-
toggleComponent(strictCatchComponent, false);
252+
setFileTypeMode("COMMON_TYPES");
248253
} else {
249254
// Switch to strict mode
250-
toggleComponent(catchAllComponent, false);
251-
toggleComponent(commonTypesComponent, false);
252-
toggleComponent(strictCatchComponent, true);
255+
setFileTypeMode("STRICT");
253256
}
254257
}
255258
});
256259

257260
// Set switch state: checked if common types is enabled, unchecked if strict is enabled
258-
catchAllSwitch.setChecked(isCommonTypesEnabled);
261+
// Note: We don't expose CATCH_ALL mode in the UI anymore, as per issue #374 request
262+
catchAllSwitch.setChecked(currentMode.equals("COMMON_TYPES"));
259263

260-
String analyticsEvent;
261-
if (isCatchAllEnabled) {
262-
analyticsEvent = "catch_all_enabled";
263-
} else if (isCommonTypesEnabled) {
264-
analyticsEvent = "common_types_enabled";
265-
} else {
266-
analyticsEvent = "strict_enabled";
264+
analyticsManager.report("file_mode_" + currentMode.toLowerCase());
265+
}
266+
267+
private void setFileTypeMode(String mode) {
268+
ComponentName catchAllComponent = new ComponentName(this, "at.tomtasche.reader.ui.activity.MainActivity.CATCH_ALL");
269+
ComponentName commonTypesComponent = new ComponentName(this, "at.tomtasche.reader.ui.activity.MainActivity.COMMON_TYPES");
270+
ComponentName strictCatchComponent = new ComponentName(this, "at.tomtasche.reader.ui.activity.MainActivity.STRICT_CATCH");
271+
272+
// Disable all components first
273+
toggleComponent(catchAllComponent, false);
274+
toggleComponent(commonTypesComponent, false);
275+
toggleComponent(strictCatchComponent, false);
276+
277+
// Enable the appropriate component
278+
switch (mode) {
279+
case "CATCH_ALL":
280+
toggleComponent(catchAllComponent, true);
281+
break;
282+
case "COMMON_TYPES":
283+
toggleComponent(commonTypesComponent, true);
284+
break;
285+
case "STRICT":
286+
toggleComponent(strictCatchComponent, true);
287+
break;
288+
default:
289+
// Default to common types
290+
toggleComponent(commonTypesComponent, true);
291+
break;
267292
}
268-
analyticsManager.report(analyticsEvent);
269293
}
270294

271295
private void toggleComponent(ComponentName component, boolean enabled) {

0 commit comments

Comments
 (0)