Properly handle thread
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Michel Roux 2022-11-19 23:19:24 +01:00
parent c5d943bfdd
commit c6bcace566
3 changed files with 60 additions and 19 deletions

View File

@ -11,8 +11,8 @@ android {
minSdk 5
//noinspection OldTargetApi
targetSdkVersion 32
versionCode 8
versionName "3.1"
versionCode 9
versionName "3.2"
}
signingConfigs {

View File

@ -0,0 +1,39 @@
package net.crystalyx.setaswallpaper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class HttpThreadHandler implements Runnable {
private final URL url;
private Bitmap bitmap;
private String errorMessage;
public HttpThreadHandler(URL url) {
this.url = url;
}
@Override
public void run() {
try {
URLConnection connection = this.url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
this.bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
errorMessage = e.getMessage();
}
}
public Bitmap gitBitmap() {
return this.bitmap;
}
public String getErrorMessage() {
return this.errorMessage;
}
}

View File

@ -12,9 +12,6 @@ import android.widget.Toast;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SetWallpaperActivity extends Activity {
@ -44,32 +41,37 @@ public class SetWallpaperActivity extends Activity {
return;
}
ExecutorService executor = Executors.newSingleThreadExecutor();
HttpThreadHandler handler = new HttpThreadHandler(url);
Thread job = new Thread(handler);
executor.execute(() -> {
job.start();
try {
URLConnection connection = url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
launchWallpaperActivity(inputStream);
} catch (IOException e) {
job.join();
} catch (InterruptedException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Bitmap bitmap = handler.gitBitmap();
String errorMessage = handler.getErrorMessage();
if (bitmap != null) {
launchWallpaperActivity(bitmap);
} else if (errorMessage != null) {
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}
}
private void handleStream(Uri uri) {
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
launchWallpaperActivity(inputStream);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
launchWallpaperActivity(bitmap);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void launchWallpaperActivity(InputStream stream) {
private void launchWallpaperActivity(Bitmap bitmap) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
try {
manager.setBitmap(bitmap);