Use Wallpaper manager
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Michel Roux 2022-09-03 13:29:11 +02:00
parent af6f50da30
commit 89494777dd
5 changed files with 29 additions and 86 deletions

View File

@ -3,15 +3,15 @@ plugins {
} }
android { android {
compileSdkVersion 31 compileSdkVersion 32
buildToolsVersion "30.0.3" buildToolsVersion "30.0.3"
defaultConfig { defaultConfig {
applicationId "net.crystalyx.setaswallpaper" applicationId "net.crystalyx.setaswallpaper"
minSdkVersion 23 minSdkVersion 14
targetSdkVersion 31 targetSdkVersion 32
versionCode 6 versionCode 7
versionName "2.4" versionName "3.0"
} }
buildTypes { buildTypes {

View File

@ -3,10 +3,9 @@
package="net.crystalyx.setaswallpaper"> package="net.crystalyx.setaswallpaper">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application <application
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:label="Set as wallpaper" android:label="Set as wallpaper"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
@ -24,14 +23,6 @@
<data android:mimeType="text/plain"/> <data android:mimeType="text/plain"/>
</intent-filter> </intent-filter>
</activity> </activity>
<provider android:authorities="net.crystalyx.setaswallpaper.provider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application> </application>
</manifest> </manifest>

View File

@ -1,29 +1,27 @@
package net.crystalyx.setaswallpaper; package net.crystalyx.setaswallpaper;
import android.app.Activity; import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Toast; import android.widget.Toast;
import androidx.core.content.FileProvider;
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.net.URLConnection;
import java.util.Random;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
public class SetWallpaperActivity extends Activity { public class SetWallpaperActivity extends Activity {
private boolean toBeFinished;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
toBeFinished = false;
Intent intent = getIntent(); Intent intent = getIntent();
String type = intent.getType(); String type = intent.getType();
@ -31,44 +29,11 @@ public class SetWallpaperActivity extends Activity {
if ("text/plain".equals(type)) { if ("text/plain".equals(type)) {
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT)); handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
} else if (type.startsWith("image/")) { } else if (type.startsWith("image/")) {
handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM), type); handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM));
} else {
toBeFinished = true;
} }
} else {
toBeFinished = true;
} }
} }
@Override
protected void onPause() {
super.onPause();
toBeFinished = true;
}
@Override
protected void onResume() {
super.onResume();
if (!toBeFinished) {
finish();
}
}
private String generateRandomString() {
int leftLimit = 97; // letter 'a'
int rightLimit = 122; // letter 'z'
int targetStringLength = 3;
Random random = new Random();
StringBuilder buffer = new StringBuilder(targetStringLength);
for (int i = 0; i < targetStringLength; i++) {
int randomLimitedInt = leftLimit + (int)
(random.nextFloat() * (rightLimit - leftLimit + 1));
buffer.append((char) randomLimitedInt);
}
return buffer.toString();
}
private void handleUrl(String textUrl) { private void handleUrl(String textUrl) {
URL url; URL url;
@ -76,6 +41,7 @@ public class SetWallpaperActivity extends Activity {
url = new URL(textUrl); url = new URL(textUrl);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
finish();
return; return;
} }
@ -86,47 +52,37 @@ public class SetWallpaperActivity extends Activity {
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
connection.connect(); connection.connect();
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
launchWallpaperActivity(writeImageToCache(inputStream), connection.getContentType()); launchWallpaperActivity(inputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
} }
}); });
} }
private void handleStream(Uri uri, String type) { private void handleStream(Uri uri) {
try { try {
InputStream inputStream = getContentResolver().openInputStream(uri); InputStream inputStream = getContentResolver().openInputStream(uri);
launchWallpaperActivity(writeImageToCache(inputStream), type); launchWallpaperActivity(inputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
} }
} }
private Uri writeImageToCache(InputStream inputStream) throws IOException { private void launchWallpaperActivity(InputStream stream) {
File outputFile = File.createTempFile(generateRandomString(), generateRandomString(), getCacheDir()); WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
OutputStream outputStream = new FileOutputStream(outputFile); Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] data = new byte[1024]; try {
int count; manager.setBitmap(bitmap);
} catch (IOException e) {
while ((count = inputStream.read(data)) != -1) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
outputStream.write(data, 0, count); finish();
} }
outputStream.flush(); Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
outputStream.close(); finish();
inputStream.close();
return FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", outputFile);
}
private void launchWallpaperActivity(Uri uri, String mimeType) {
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, mimeType);
intent.putExtra("mimeType", mimeType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Set as:"));
} }
} }

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<paths>
<cache-path name="cache" path="." />
</paths>

View File

@ -5,7 +5,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
classpath "com.android.tools.build:gradle:4.2.2" classpath 'com.android.tools.build:gradle:7.2.2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files