2021-08-08 20:58:08 +00:00
|
|
|
package net.crystalyx.setaswallpaper;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2022-09-03 11:29:11 +00:00
|
|
|
import android.app.WallpaperManager;
|
2021-08-08 20:58:08 +00:00
|
|
|
import android.content.Intent;
|
2022-09-03 11:29:11 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2021-08-08 20:58:08 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2021-08-08 22:57:03 +00:00
|
|
|
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;
|
2021-08-08 20:58:08 +00:00
|
|
|
|
|
|
|
public class SetWallpaperActivity extends Activity {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
Intent intent = getIntent();
|
2021-08-08 22:57:03 +00:00
|
|
|
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/")) {
|
2022-09-03 11:29:11 +00:00
|
|
|
handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM));
|
2021-08-08 22:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleUrl(String textUrl) {
|
|
|
|
URL url;
|
|
|
|
|
|
|
|
try {
|
|
|
|
url = new URL(textUrl);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
2021-08-08 20:58:08 +00:00
|
|
|
}
|
2021-08-08 22:57:03 +00:00
|
|
|
|
|
|
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
|
|
|
|
|
|
executor.execute(() -> {
|
|
|
|
try {
|
|
|
|
URLConnection connection = url.openConnection();
|
|
|
|
connection.connect();
|
|
|
|
InputStream inputStream = connection.getInputStream();
|
2022-09-03 11:29:11 +00:00
|
|
|
launchWallpaperActivity(inputStream);
|
2021-08-08 22:57:03 +00:00
|
|
|
} catch (IOException e) {
|
2022-09-03 13:00:44 +00:00
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
2021-08-08 22:57:03 +00:00
|
|
|
}
|
|
|
|
});
|
2021-08-08 20:58:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:29:11 +00:00
|
|
|
private void handleStream(Uri uri) {
|
2021-09-09 16:16:34 +00:00
|
|
|
try {
|
|
|
|
InputStream inputStream = getContentResolver().openInputStream(uri);
|
2022-09-03 11:29:11 +00:00
|
|
|
launchWallpaperActivity(inputStream);
|
2021-09-09 16:16:34 +00:00
|
|
|
} catch (IOException e) {
|
2022-09-03 13:00:44 +00:00
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
2021-09-09 16:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 11:29:11 +00:00
|
|
|
private void launchWallpaperActivity(InputStream stream) {
|
|
|
|
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(stream);
|
2021-09-09 16:16:34 +00:00
|
|
|
|
2022-09-03 11:29:11 +00:00
|
|
|
try {
|
|
|
|
manager.setBitmap(bitmap);
|
|
|
|
} catch (IOException e) {
|
2022-09-03 13:00:44 +00:00
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
|
2021-09-09 16:16:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 11:29:11 +00:00
|
|
|
Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
|
2021-08-08 20:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|