Skip to content

Commit

Permalink
download option added
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj-Meharchandani committed Feb 28, 2024
1 parent 896761c commit 9207e84
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation ("io.github.eltos:simpledialogfragments:3.8.3")
}
99 changes: 91 additions & 8 deletions app/src/main/java/com/example/waller/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -14,6 +15,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
Expand All @@ -23,18 +25,29 @@
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import java.io.IOException;
import java.util.Random;

import android.app.WallpaperManager;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


public class MainActivity extends AppCompatActivity {
private int startColor = 0xFF0000FF; // Default start color
private int endColor = 0xFFFF0000; // Default end color
private GridView gridView;
private Bitmap selectedImage;

// Initialize color variables to 0 initially
private int selectedColor1 = 0;
Expand Down Expand Up @@ -87,7 +100,7 @@ public void onClick(View v) {
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Bitmap selectedImage = adapter.getItem(position);
final Bitmap clickedImage = adapter.getItem(position);

// Show AlertDialog for wallpaper options
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
Expand All @@ -97,13 +110,13 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
setWallpaper(selectedImage, WallpaperManager.FLAG_SYSTEM);
setWallpaper(clickedImage, WallpaperManager.FLAG_SYSTEM);
break;
case 1:
setWallpaper(selectedImage, WallpaperManager.FLAG_LOCK);
setWallpaper(clickedImage, WallpaperManager.FLAG_LOCK);
break;
case 2:
setWallpaper(selectedImage, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
setWallpaper(clickedImage, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
break;
}
}
Expand All @@ -114,13 +127,82 @@ public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})

.setNeutralButton("Download", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (clickedImage != null) {
selectedImage = clickedImage; // Update the selectedImage field
checkAndRequestPermissions();
// No need to save the image here, it will be saved after obtaining permission
} else {
Toast.makeText(MainActivity.this, "No image selected", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
})
.create()
.show();
}
});

}

private static final int PERMISSION_REQUEST_CODE = 123;

private void checkAndRequestPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted, request it
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE
);
} else {
// Permission is already granted, save the image
saveImageToGallery(selectedImage);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, save the image
saveImageToGallery(selectedImage);
} else {
Toast.makeText(this, "Permission denied. Unable to save the image.", Toast.LENGTH_SHORT).show();
}
}
}

private void saveImageToGallery(Bitmap image) {
String savedImageURL = MediaStore.Images.Media.insertImage(
getContentResolver(),
image,
"Wallpaper_" + System.currentTimeMillis(),
"Generated wallpaper"
);

// If the image is saved successfully, get the image URI
if (savedImageURL != null) {
Uri savedImageURI = Uri.parse(savedImageURL);

// Broadcast the media scanner to add the new image to the gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(savedImageURI);
sendBroadcast(mediaScanIntent);

// Show Toast message after successful download
Toast.makeText(this, "Image downloaded successfully!", Toast.LENGTH_SHORT).show();
} else {
// Show Toast message if there is an issue with saving the image
Toast.makeText(this, "Failed to save the image.", Toast.LENGTH_SHORT).show();
}
}



private void showColorPickerDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.color_picker_dialog);
Expand Down Expand Up @@ -272,14 +354,15 @@ private Bitmap generateRandomImage(int color2, int color3) {
gradientDrawable.setColors(new int[]{startColor, endColor});

// Convert the drawable to a Bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
selectedImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
gradientDrawable.setBounds(0, 0, width, height);
Canvas canvas = new Canvas(bitmap);
Canvas canvas = new Canvas(selectedImage);
gradientDrawable.draw(canvas);

return bitmap;
return selectedImage;
}


private int shuffleColor(int color) {
Random random = new Random();
int red = Color.red(color) + random.nextInt(201) - 100; // Adjust the range as needed
Expand Down

0 comments on commit 9207e84

Please sign in to comment.