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 {
compileSdkVersion 31
compileSdkVersion 32
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "net.crystalyx.setaswallpaper"
minSdkVersion 23
targetSdkVersion 31
versionCode 6
versionName "2.4"
minSdkVersion 14
targetSdkVersion 32
versionCode 7
versionName "3.0"
}
buildTypes {

View File

@ -3,10 +3,9 @@
package="net.crystalyx.setaswallpaper">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:label="Set as wallpaper"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
@ -24,14 +23,6 @@
<data android:mimeType="text/plain"/>
</intent-filter>
</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>
</manifest>

View File

@ -1,29 +1,27 @@
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.Bundle;
import android.widget.Toast;
import androidx.core.content.FileProvider;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SetWallpaperActivity extends Activity {
private boolean toBeFinished;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toBeFinished = false;
Intent intent = getIntent();
String type = intent.getType();
@ -31,44 +29,11 @@ public class SetWallpaperActivity extends Activity {
if ("text/plain".equals(type)) {
handleUrl(intent.getStringExtra(Intent.EXTRA_TEXT));
} else if (type.startsWith("image/")) {
handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM), type);
} else {
toBeFinished = true;
handleStream(intent.getParcelableExtra(Intent.EXTRA_STREAM));
}
} 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) {
URL url;
@ -76,6 +41,7 @@ public class SetWallpaperActivity extends Activity {
url = new URL(textUrl);
} catch (MalformedURLException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
finish();
return;
}
@ -86,47 +52,37 @@ public class SetWallpaperActivity extends Activity {
URLConnection connection = url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
launchWallpaperActivity(writeImageToCache(inputStream), connection.getContentType());
launchWallpaperActivity(inputStream);
} 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 {
InputStream inputStream = getContentResolver().openInputStream(uri);
launchWallpaperActivity(writeImageToCache(inputStream), type);
launchWallpaperActivity(inputStream);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
}
private Uri writeImageToCache(InputStream inputStream) throws IOException {
File outputFile = File.createTempFile(generateRandomString(), generateRandomString(), getCacheDir());
OutputStream outputStream = new FileOutputStream(outputFile);
private void launchWallpaperActivity(InputStream stream) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] data = new byte[1024];
int count;
while ((count = inputStream.read(data)) != -1) {
outputStream.write(data, 0, count);
try {
manager.setBitmap(bitmap);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
outputStream.flush();
outputStream.close();
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:"));
Toast.makeText(this, "Wallpaper set!", Toast.LENGTH_SHORT).show();
finish();
}
}

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()
}
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
// in the individual module build.gradle files