allow setting Wallpaper with the app

This commit is contained in:
tibbi 2016-06-26 22:38:25 +02:00
parent fee7704dae
commit 8d63aedf2a
5 changed files with 80 additions and 13 deletions

View file

@ -4,6 +4,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -30,6 +31,11 @@
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/video"/> <data android:mimeType="vnd.android.cursor.dir/video"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity> </activity>
<activity <activity

View file

@ -5,4 +5,5 @@ public class Constants {
public static final String MEDIUM = "medium"; public static final String MEDIUM = "medium";
public static final String PICK_IMAGE_INTENT = "is_image_pick_intent"; public static final String PICK_IMAGE_INTENT = "is_image_pick_intent";
public static final String PICK_VIDEO_INTENT = "is_video_pick_intent"; public static final String PICK_VIDEO_INTENT = "is_video_pick_intent";
public static final String SET_WALLPAPER_INTENT = "is_set_wallpaper_intent";
} }

View file

@ -50,6 +50,7 @@ public class MainActivity extends AppCompatActivity
private static final int STORAGE_PERMISSION = 1; private static final int STORAGE_PERMISSION = 1;
private static final int PICK_MEDIA = 2; private static final int PICK_MEDIA = 2;
private static final int PICK_WALLPAPER = 3;
private static List<Directory> mDirs; private static List<Directory> mDirs;
private static Snackbar mSnackbar; private static Snackbar mSnackbar;
@ -60,6 +61,7 @@ public class MainActivity extends AppCompatActivity
private static boolean mIsSnackbarShown; private static boolean mIsSnackbarShown;
private static boolean mIsPickImageIntent; private static boolean mIsPickImageIntent;
private static boolean mIsPickVideoIntent; private static boolean mIsPickVideoIntent;
private static boolean mIsSetWallpaperIntent;
private static int mSelectedItemsCnt; private static int mSelectedItemsCnt;
@Override @Override
@ -67,12 +69,18 @@ public class MainActivity extends AppCompatActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);
mIsPickImageIntent = isPickImageIntent(getIntent());
mIsPickVideoIntent = isPickVideoIntent(getIntent()); final Intent intent = getIntent();
mIsPickImageIntent = isPickImageIntent(intent);
mIsPickVideoIntent = isPickVideoIntent(intent);
mIsSetWallpaperIntent = isSetWallpaperIntent(intent);
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
if (mIsSetWallpaperIntent)
return false;
getMenuInflater().inflate(R.menu.menu, menu); getMenuInflater().inflate(R.menu.menu, menu);
return true; return true;
} }
@ -352,13 +360,22 @@ public class MainActivity extends AppCompatActivity
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && intent.getData() != null; return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK) && intent.getData() != null;
} }
private boolean isSetWallpaperIntent(Intent intent) {
return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SET_WALLPAPER);
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_MEDIA && resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK) {
final Intent result = new Intent(); if (requestCode == PICK_MEDIA && data != null) {
result.setData(data.getData()); final Intent result = new Intent();
setResult(RESULT_OK, result); result.setData(data.getData());
finish(); setResult(RESULT_OK, result);
finish();
} else if (requestCode == PICK_WALLPAPER) {
setResult(RESULT_OK);
finish();
}
} }
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
@ -367,9 +384,15 @@ public class MainActivity extends AppCompatActivity
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(this, MediaActivity.class); final Intent intent = new Intent(this, MediaActivity.class);
intent.putExtra(Constants.DIRECTORY, mDirs.get(position).getPath()); intent.putExtra(Constants.DIRECTORY, mDirs.get(position).getPath());
intent.putExtra(Constants.PICK_IMAGE_INTENT, mIsPickImageIntent);
intent.putExtra(Constants.PICK_VIDEO_INTENT, mIsPickVideoIntent); if (mIsSetWallpaperIntent) {
startActivityForResult(intent, PICK_MEDIA); intent.putExtra(Constants.SET_WALLPAPER_INTENT, true);
startActivityForResult(intent, PICK_WALLPAPER);
} else {
intent.putExtra(Constants.PICK_IMAGE_INTENT, mIsPickImageIntent);
intent.putExtra(Constants.PICK_VIDEO_INTENT, mIsPickVideoIntent);
startActivityForResult(intent, PICK_MEDIA);
}
} }
@Override @Override

View file

@ -1,8 +1,10 @@
package com.simplemobiletools.gallery.activities; package com.simplemobiletools.gallery.activities;
import android.app.WallpaperManager;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
@ -12,6 +14,7 @@ import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
@ -22,6 +25,9 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.GridView; import android.widget.GridView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
@ -29,6 +35,7 @@ import com.simplemobiletools.gallery.adapters.MediaAdapter;
import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.models.Medium;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -39,6 +46,7 @@ import butterknife.ButterKnife;
public class MediaActivity extends AppCompatActivity public class MediaActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener { implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener {
private static final String TAG = MediaActivity.class.getSimpleName();
@BindView(R.id.media_grid) GridView mGridView; @BindView(R.id.media_grid) GridView mGridView;
private static List<Medium> mMedia; private static List<Medium> mMedia;
@ -247,16 +255,44 @@ public class MediaActivity extends AppCompatActivity
} }
} }
private boolean isSetWallpaperIntent() {
return getIntent().getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false);
}
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mIsPickImageIntent || mIsPickVideoIntent) { final String curItemPath = mMedia.get(position).getPath();
if (isSetWallpaperIntent()) {
Utils.showToast(this, R.string.setting_wallpaper);
final int wantedWidth = getWallpaperDesiredMinimumWidth();
final int wantedHeight = getWallpaperDesiredMinimumHeight();
final float ratio = (float) wantedWidth / wantedHeight;
Glide.with(this)
.load(new File(curItemPath))
.asBitmap()
.override((int) (wantedWidth * ratio), wantedHeight)
.fitCenter()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
try {
WallpaperManager.getInstance(getApplicationContext()).setBitmap(bitmap);
setResult(RESULT_OK);
} catch (IOException e) {
Log.e(TAG, "item click " + e.getMessage());
}
finish();
}
});
} else if (mIsPickImageIntent || mIsPickVideoIntent) {
final Intent result = new Intent(); final Intent result = new Intent();
result.setData(Uri.parse(mMedia.get(position).getPath())); result.setData(Uri.parse(curItemPath));
setResult(RESULT_OK, result); setResult(RESULT_OK, result);
finish(); finish();
} else { } else {
final Intent intent = new Intent(this, ViewPagerActivity.class); final Intent intent = new Intent(this, ViewPagerActivity.class);
intent.putExtra(Constants.MEDIUM, mMedia.get(position).getPath()); intent.putExtra(Constants.MEDIUM, curItemPath);
startActivity(intent); startActivity(intent);
} }
} }

View file

@ -17,6 +17,7 @@
<string name="renaming_folder">Renaming folder</string> <string name="renaming_folder">Renaming folder</string>
<string name="extension">Extension</string> <string name="extension">Extension</string>
<string name="file_deleted">File deleted</string> <string name="file_deleted">File deleted</string>
<string name="setting_wallpaper">Setting wallpaper</string>
<plurals name="folders_deleted"> <plurals name="folders_deleted">
<item quantity="one">1 folder deleted</item> <item quantity="one">1 folder deleted</item>