Skip to content

Commit

Permalink
Adding Android 6 support (ie reverting lambda and cool stuff)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightjohn committed Apr 26, 2022
1 parent 93f5964 commit 6e22d8b
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 119 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ plugins {

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
applicationId "com.light.fileresizer"
minSdkVersion 26
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
Expand Down
51 changes: 31 additions & 20 deletions app/src/main/java/com/light/fileresizer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

Expand All @@ -25,36 +26,45 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String tachiyomiPath = getTachiyomiPath();
int screenHeight = ThreadUtils.getHeight(MainActivity.this);
final String tachiyomiPath = getTachiyomiPath();
final int screenHeight = ThreadUtils.getHeight(MainActivity.this);

checkPermissions();

Button split = findViewById(R.id.split);

split.setOnClickListener(view -> {
SplitThread thread = new SplitThread(tachiyomiPath, screenHeight, MainActivity.this);
thread.start();
split.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SplitThread thread = new SplitThread(tachiyomiPath, screenHeight, MainActivity.this);
thread.start();
}
});

Button safeSplit = findViewById(R.id.safesplit);

safeSplit.setOnClickListener(view -> {
SplitThread thread = new SplitThread(tachiyomiPath, screenHeight, MainActivity.this);
thread.setSafe();
thread.start();
safeSplit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SplitThread thread = new SplitThread(tachiyomiPath, screenHeight, MainActivity.this);
thread.setSafe();
thread.start();
}
});

Button folderSplit = findViewById(R.id.foldersplit);

folderSplit.setOnClickListener(view -> {

// Asking user for folder path
Intent intent = new Intent(getBaseContext(), FilePickerActivity.class);
intent.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
intent.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
intent.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR);
startActivityForResult(intent, READ_REQUEST_CODE);
folderSplit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// Asking user for folder path
Intent intent = new Intent(MainActivity.this.getBaseContext(), FilePickerActivity.class);
intent.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
intent.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
intent.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR);
MainActivity.this.startActivityForResult(intent, READ_REQUEST_CODE);
}
});
}

Expand All @@ -64,6 +74,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten

switch(requestCode) {
case READ_REQUEST_CODE:
if (data == null) {
System.out.println("Select folder was cancelled");
return;
}
System.out.println("Test Result URI " + data.getData());
Uri uri = data.getData();
String folderPath = uri.getPath();
Expand All @@ -81,7 +95,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
}
}


private void checkPermissions() {
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
Expand All @@ -92,6 +105,4 @@ private void checkPermissions() {
}

}


}
56 changes: 26 additions & 30 deletions app/src/main/java/com/light/fileresizer/SplitThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.light.fileresizer.ThreadUtils.delete;
import static com.light.fileresizer.ThreadUtils.getAllImages;
import static com.light.fileresizer.ThreadUtils.getTachiyomiPath;
import static com.light.fileresizer.ThreadUtils.setButtonFolderSplit;
import static com.light.fileresizer.ThreadUtils.setButtonSafeSplit;
import static com.light.fileresizer.ThreadUtils.setButtonSplit;
Expand All @@ -15,15 +14,12 @@

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

class SplitThread extends Thread {
int SPLIT_THREADS = 4;
Expand All @@ -37,7 +33,6 @@ class SplitThread extends Thread {
this.workingPath = workingPath;
}


public void run() {
try {
System.out.println("DEBUG: working path: " + this.workingPath);
Expand All @@ -46,12 +41,14 @@ public void run() {
setButtonSafeSplit(activity, false);
setButtonFolderSplit(activity, false);

List<Path> imgPaths = getAllImages(this.workingPath);
int current = 0;
List<String> imgPaths = getAllImages(this.workingPath);
// Trying to split in //
List<Path> imagesTooBig = imgPaths.stream()
.filter(this::shouldBeSpliced)
.collect(Collectors.toList());
List<String> imagesTooBig = new ArrayList<>();
for (String path : imgPaths) {
if (shouldBeSpliced(path)) {
imagesTooBig.add(path);
}
}

updateText(activity, "Found " + imgPaths.size() + " images.\nSplitting "
+ imagesTooBig.size() + " images");
Expand All @@ -60,12 +57,12 @@ public void run() {
// First free some memory
System.gc();

List<Path> imagesFarTooBig = splitImages(imagesTooBig);
List<String> imagesFarTooBig = splitImages(imagesTooBig);
if (SPLIT_THREADS != 1) {
setSafe();
updateText(activity, "There was " + imagesFarTooBig.size() + " Errors.\n" +
"Retrying in Safe Mode");
List<Path> imagesThatFailed = splitImages(imagesFarTooBig);
List<String> imagesThatFailed = splitImages(imagesFarTooBig);
updateText(activity, "Images that could not be splitted: " + imagesThatFailed.size());
}
} catch (IOException e) {
Expand All @@ -91,8 +88,8 @@ private int[] getImageSizeWithoutLoading(String imagePath) {
return new int[]{width, height};
}

private boolean shouldBeSpliced(Path path) {
int[] imageDimension = getImageSizeWithoutLoading(path.toString());
private boolean shouldBeSpliced(String path) {
int[] imageDimension = getImageSizeWithoutLoading(path);
int imageHeight = imageDimension[1];
return imageHeight > expectedHeight;

Expand All @@ -105,11 +102,8 @@ private Bitmap loadBitmap(String imagePath) {
return BitmapFactory.decodeFile(imagePath);
}


private void splitOneImage(Path path) {
private void splitOneImage(String imagePath) {
// At this point we know the image is too big
String imagePath = path.toString();

Bitmap image = loadBitmap(imagePath);
int bitmapSizeMb = image.getByteCount() / (1024 * 1024);
int imageWidth = image.getWidth();
Expand Down Expand Up @@ -143,24 +137,27 @@ private void splitOneImage(Path path) {
System.out.println(splitImagePath + " written");
}

delete(path);
delete(imagePath);
}

private List<Path> splitImages(List<Path> imagesPath) {
private List<String> splitImages(final List<String> imagesPath) {
final int[] count = {0, 0}; // {countDone, countErr}
List<Path> imagesFarTooBig = new ArrayList<>();
final List<String> imagesFarTooBig = new ArrayList<>();
// int nbCores = Runtime.getRuntime().availableProcessors();
ExecutorService executorService = Executors.newFixedThreadPool(SPLIT_THREADS);
int size = imagesPath.size();
for (Path path : imagesPath) {
executorService.submit(() -> {
try {
splitOneImage(path);
} catch (Exception e) {
imagesFarTooBig.add(path);
count[1]++;
for (final String path : imagesPath) {
executorService.submit(new Runnable() {
@Override
public void run() {
try {
SplitThread.this.splitOneImage(path);
} catch (Exception e) {
imagesFarTooBig.add(path);
count[1]++;
}
updateBar(activity, imagesPath.size(), ++count[0]);
}
updateBar(activity, imagesPath.size(), ++count[0]);
});
}
updateBar(activity, size, size); // Making sure that bar is fully loaded
Expand All @@ -187,5 +184,4 @@ private String getSplitImageName(String imagePath, int imageNumber) {
}
throw new IllegalArgumentException("Input image is not a known format: " + imagePath);
}

}
Loading

0 comments on commit 6e22d8b

Please sign in to comment.