98 lines
2.9 KiB
Java
98 lines
2.9 KiB
Java
package net.crystalyx.setaswallpaper;
|
|
|
|
import android.app.Activity;
|
|
import android.app.WallpaperManager;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.*;
|
|
import java.net.URI;
|
|
import java.net.URL;
|
|
|
|
public class SetWallpaperActivity extends Activity {
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = getIntent();
|
|
String type = intent.getType();
|
|
|
|
if (Intent.ACTION_SEND.equals(intent.getAction()) && type != null) {
|
|
if ("text/plain".equals(type)) {
|
|
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
|
|
} else if (type.startsWith("image/")) {
|
|
handleStream(getParcelableExtraStream(intent));
|
|
}
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
|
private Uri getParcelableExtraStream(Intent intent) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
return intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri.class);
|
|
}
|
|
|
|
return intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
|
}
|
|
|
|
private void handleUrl(String textUrl) {
|
|
URL url;
|
|
|
|
try {
|
|
url = new URI(textUrl).toURL();
|
|
} catch (Exception e) {
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
return;
|
|
}
|
|
|
|
HttpThreadHandler handler = new HttpThreadHandler(url);
|
|
Thread job = new Thread(handler);
|
|
|
|
job.start();
|
|
try {
|
|
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);
|
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
|
launchWallpaperActivity(bitmap);
|
|
} catch (IOException e) {
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
}
|
|
}
|
|
|
|
private void launchWallpaperActivity(Bitmap bitmap) {
|
|
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
|
|
|
|
try {
|
|
manager.setBitmap(bitmap);
|
|
} catch (IOException e) {
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
}
|
|
|
|
Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
}
|
|
|
|
}
|