Properly handle thread
This commit is contained in:
parent
c5d943bfdd
commit
c6bcace566
@ -11,8 +11,8 @@ android {
|
|||||||
minSdk 5
|
minSdk 5
|
||||||
//noinspection OldTargetApi
|
//noinspection OldTargetApi
|
||||||
targetSdkVersion 32
|
targetSdkVersion 32
|
||||||
versionCode 8
|
versionCode 9
|
||||||
versionName "3.1"
|
versionName "3.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -12,9 +12,6 @@ import android.widget.Toast;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
public class SetWallpaperActivity extends Activity {
|
public class SetWallpaperActivity extends Activity {
|
||||||
|
|
||||||
@ -44,32 +41,37 @@ public class SetWallpaperActivity extends Activity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
HttpThreadHandler handler = new HttpThreadHandler(url);
|
||||||
|
Thread job = new Thread(handler);
|
||||||
|
|
||||||
executor.execute(() -> {
|
job.start();
|
||||||
try {
|
try {
|
||||||
URLConnection connection = url.openConnection();
|
job.join();
|
||||||
connection.connect();
|
} catch (InterruptedException e) {
|
||||||
InputStream inputStream = connection.getInputStream();
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
launchWallpaperActivity(inputStream);
|
}
|
||||||
} catch (IOException 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) {
|
private void handleStream(Uri uri) {
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getContentResolver().openInputStream(uri);
|
InputStream inputStream = getContentResolver().openInputStream(uri);
|
||||||
launchWallpaperActivity(inputStream);
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
||||||
|
launchWallpaperActivity(bitmap);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void launchWallpaperActivity(InputStream stream) {
|
private void launchWallpaperActivity(Bitmap bitmap) {
|
||||||
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
|
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(stream);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
manager.setBitmap(bitmap);
|
manager.setBitmap(bitmap);
|
||||||
|
Loading…
Reference in New Issue
Block a user