Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for setting both screens and cropping #13

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Build;
import android.os.Environment;

Expand Down Expand Up @@ -55,9 +56,15 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
case "setWallpaperFromFile":
result.success(setWallpaperFromFile((String) call.argument("filePath"), (int) call.argument("wallpaperLocation")));
break;
case "setWallpaperFromFileWithCrop":
result.success(setWallpaperFromFileWithCrop((String)call.argument("filePath"), (int)call.argument("wallpaperLocation"), (int) call.argument("left"), (int) call.argument("top"), (int) call.argument("right"), (int) call.argument("bottom")));
break;
case "setWallpaperFromAsset":
result.success(setWallpaperFromAsset((String) call.argument("assetPath"), (int) call.argument("wallpaperLocation")));
break;
case "setWallpaperFromAssetWithCrop":
result.success(setWallpaperFromAssetWithCrop((String)call.argument("assetPath"), (int)call.argument("wallpaperLocation"), (int) call.argument("left"), (int) call.argument("top"), (int) call.argument("right"), (int) call.argument("bottom")));
break;
default:
result.notImplemented();
break;
Expand Down Expand Up @@ -87,6 +94,24 @@ private int setWallpaperFromFile(String filePath, int wallpaperLocation){
return result;
}

@SuppressLint("MissingPermission")
private int setWallpaperFromFileWithCrop(String filePath, int wallpaperLocation, int left, int top, int right, int bottom) {
int result = -1;
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
WallpaperManager wm = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) {
wm = WallpaperManager.getInstance(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
result = wm.setBitmap(bitmap, Rect(left, top, right, bottom), false, wallpaperLocation);
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}

@SuppressLint("MissingPermission")
private int setWallpaperFromAsset(String assetPath, int wallpaperLocation) {
InputStream fd = null;
Expand All @@ -110,4 +135,28 @@ private int setWallpaperFromAsset(String assetPath, int wallpaperLocation) {
}
return result;
}

@SuppressLint("MissingPermission")
private int setWallpaperFromAssetWithCrop(String assetPath, int wallpaperLocation, int left, int top, int right, int bottom) {
InputStream fd = null;
int result = -1;
try {
fd = context.getAssets().open("flutter_assets/" + assetPath);
Bitmap bitmap = BitmapFactory.decodeStream(fd);
WallpaperManager wm = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR) {
wm = WallpaperManager.getInstance(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
result = wm.setBitmap(bitmap, Rect(left, top, right, bottom), false, wallpaperLocation);
}catch (IOException e) {
e.printStackTrace();
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
37 changes: 37 additions & 0 deletions lib/wallpaper_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class WallpaperManager {

/// Static code for Lock Screen Wallpaper Choice
static const int LOCK_SCREEN = 2;

// Static code for both Home Screen and Lock Screen Wallpaper Choice
static const int BOTH_SCREENS = 3;

static Future<String> get platformVersion async {
/// String to store the version number before returning. This is just to test working/validity.
Expand All @@ -31,6 +34,23 @@ class WallpaperManager {
/// Function returns the set String as result, use for debugging
return result > 0 ? "Wallpaper set" : "There was an error.";
}

/// Function takes input file's path & location choice
static Future<String> setWallpaperFromFileWithCrop(
String filePath, int wallpaperLocation, int left, int top, int right, int bottom) async {
/// Variable to store operation result
final int result = await _channel.invokeMethod('setWallpaperFromFile', {
'filePath': filePath,
'wallpaperLocation': wallpaperLocation,
'left': left,
'top': top,
'right': right,
'bottom': bottom
});

/// Function returns the set String as result, use for debugging
return result > 0 ? "Wallpaper set" : "There was an error.";
}

/// Function takes input file's asset (Dart/Flutter; pre-indexed in pubspec.yaml) & location choice
static Future<String> setWallpaperFromAsset(
Expand All @@ -42,4 +62,21 @@ class WallpaperManager {
/// Function returns the set String as result, use for debugging
return result > 0 ? "Wallpaper set" : "There was an error.";
}

/// Function takes input file's asset (Dart/Flutter; pre-indexed in pubspec.yaml) & location choice
static Future<String> setWallpaperFromAssetWithCrop(
String assetPath, int wallpaperLocation, int left, int top, int right, int bottom) async {
/// Variable to store operation result
final int result = await _channel.invokeMethod('setWallpaperFromAssetWithCrop', {
'assetPath': assetPath,
'wallpaperLocation': wallpaperLocation,
'left': left,
'top': top,
'right': right,
'bottom': bottom
});

/// Function returns the set String as result, use for debugging
return result > 0 ? "Wallpaper set" : "There was an error.";
}
}