woheller69 / browser

A privacy oriented web browser with Greasemonkey style script support and Cookie Banner Blocker
GNU General Public License v3.0
160 stars 18 forks source link

Run on Android 7.0 (APK 24) #51

Closed StevanWhite closed 23 hours ago

StevanWhite commented 1 week ago

The minSdkVersion is set to 29 (Android 10). That's like, just yesterday!

For my purposes, I want API 24 (Android 7.0), which goes back 10 years. This can be achieved by just a few modifications.

(As far as the current libraries go, that could be dialed back to 21, but it would take a lot of work to get it running properly.)

API 34

Runs OK out of the box.

API 25

Best to first do a Code--> Inspect Code, which will show some errors concerning new function calls on old devices. The following explains how I dealt with them.

First problem in AdBlock.java, at the line

   copyFile(manager.open(FILE), Files.newOutputStream(file.toPath()));

Replaced that line with

   InputStream newFile = manager.open(FILE);
   if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O )
        copyFile(newFile, Files.newOutputStream(file.toPath()));
   else
        copyFile(newFile, new FileOutputStream(file.toString()));

Next problem, in BannerBlock.java, at the line

    String jsonDataString =  new String(Files.readAllBytes(Paths.get(file.getPath())));

Older versions of Java didn't have this nice readAllBytes(), but this will do:

    String jsonDataString = "";
    if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
        Path jsonFilePath = Paths.get(file.getPath());
        jsonDataString = new String(Files.readAllBytes(jsonFilePath));
    }
    else {
        try(RandomAccessFile f = new RandomAccessFile(file, "r")) {
            final long length = f.length();
            if( length < Integer.MAX_VALUE ) {
                byte[] bytes = new byte[(int)length];
                f.readFully( bytes );
                jsonDataString = new String(bytes);
            }
            else
                Log.e( "loadHosts", "JSON file too long" );
        }
    }

In BackupUnit.java, insert this static helper method:

private static OutputStream getOutputStream(@NonNull File extractedFile ) throws FileNotFoundException, IOException {
    if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
        return Files.newOutputStream(extractedFile.toPath());
    }
    else {
        return new FileOutputStream(extractedFile.toString());
    }
}

and replace the line

try (OutputStream outputStream = Files.newOutputStream(extractedFile.toPath())) {

with

try (OutputStream outputStream = getOutputStream( extractedFile )) {

In HelperUnit.java, all the ShortcutManager.isRequestPinShortcutSupported() stuff makes no sense on pre-Android 8 devices, so disabled it with

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O )

Likewise, in NinjaWebView.java, calls to

this.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_YES);

and

webSettings.setSafeBrowsingEnabled(true);

are Android 8+, so disable those for earlier versions, the same way.

API 24

In BrowserUnit.java, clearBookmark(), likewise disable with

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1 ) {
    ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
    Objects.requireNonNull(shortcutManager).removeAllDynamicShortcuts();
}

I tested this, to the best of my abilities, on emulators and devices running Android 7.0, 7.1, and 7.1.1, as well as an emulator running Android 14, an a phone running Android 12. I saw no bugs, performing basic activities. In fact, it worked great!

Cheers!

woheller69 commented 1 week ago

I have no plan to change the minSdk downwards in this case. But if you have it running, feel free to upload the apk (you need to nename it to .zip) here, so others can use it.

StevanWhite commented 1 week ago

I'm not surprised tat you had no plan to do this.  This is a request from an appreciative user.

I wouldn't call it a "downgrade". It is an expansion of your target users.

You have seen the changes. They refer to your existing code base, and are very limited in scope, and very easy to understand.

If you like, give me instructions, and I can push the changes to you.

Regarding an APK... wouldn't that need to be signed? By whom? Me?

woheller69 commented 1 week ago

I currently have 17 apps on F-Droid and do not want to increase my maintenance effort for old Android versions. You can upload a signed apk if you like.

StevanWhite commented 1 week ago

You mean, I should sign an APK with my own certificate, and upload that?

My aim here was to reduce your load. I am offering to make changes for you.

woheller69 commented 1 week ago

yes, just upload the APK. That is zero effort for me.