Skip to content

Commit 5643352

Browse files
Vivek Maskaranicolas-raoul
authored andcommitted
Add option to set image as wallpaper (#1535)
* Add option to set image as wallpaper * Added java docs * Toast message on setting the wallpaper successfully
1 parent 4b7e1e2 commit 5643352

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
1616
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS" />
1717
<uses-permission android:name="android.permission.READ_LOGS"/>
18+
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
1819

1920
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
2021
<uses-feature android:name="android.hardware.location.gps" />

app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.DownloadManager;
5+
import android.app.WallpaperManager;
56
import android.content.Intent;
67
import android.content.SharedPreferences;
78
import android.database.DataSetObserver;
9+
import android.graphics.Bitmap;
810
import android.net.Uri;
911
import android.os.Build;
1012
import android.os.Bundle;
@@ -26,6 +28,8 @@
2628
import android.view.ViewGroup;
2729
import android.widget.Toast;
2830

31+
import java.io.IOException;
32+
2933
import butterknife.BindView;
3034
import butterknife.ButterKnife;
3135
import javax.inject.Inject;
@@ -38,12 +42,15 @@
3842
import fr.free.nrw.commons.contributions.ContributionsActivity;
3943
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
4044
import fr.free.nrw.commons.mwapi.MediaWikiApi;
45+
import fr.free.nrw.commons.utils.ImageUtils;
46+
import timber.log.Timber;
4147

4248
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
4349
import static android.content.Context.DOWNLOAD_SERVICE;
4450
import static android.content.Intent.ACTION_VIEW;
4551
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
4652
import static android.widget.Toast.LENGTH_SHORT;
53+
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
4754

4855
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener {
4956

@@ -140,6 +147,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
140147
// Download
141148
downloadMedia(m);
142149
return true;
150+
case R.id.menu_set_as_wallpaper:
151+
// Set wallpaper
152+
setWallpaper(m);
153+
return true;
143154
case R.id.menu_retry_current_image:
144155
// Retry
145156
((ContributionsActivity) getActivity()).retryUpload(pager.getCurrentItem());
@@ -155,6 +166,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
155166
}
156167
}
157168

169+
/**
170+
* Set the media as the device's wallpaper if the imageUrl is not null
171+
* Fails silently if setting the wallpaper fails
172+
* @param media
173+
*/
174+
private void setWallpaper(Media media) {
175+
if(media.getImageUrl() == null || media.getImageUrl().isEmpty()) {
176+
Timber.d("Media URL not present");
177+
return;
178+
}
179+
ImageUtils.setWallpaperFromImageUrl(getActivity(), Uri.parse(media.getImageUrl()));
180+
}
181+
158182
/**
159183
* Start the media file downloading to the local SD card/storage.
160184
* The file can then be opened in Gallery or other apps.

app/src/main/java/fr/free/nrw/commons/utils/ImageUtils.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
package fr.free.nrw.commons.utils;
22

3+
import android.app.WallpaperManager;
4+
import android.content.Context;
35
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
47
import android.graphics.BitmapRegionDecoder;
58
import android.graphics.Color;
69
import android.graphics.Rect;
7-
10+
import android.net.Uri;
11+
import android.os.Build;
12+
import android.support.annotation.Nullable;
13+
import android.support.annotation.RequiresApi;
14+
15+
import com.facebook.common.executors.CallerThreadExecutor;
16+
import com.facebook.common.references.CloseableReference;
17+
import com.facebook.datasource.DataSource;
18+
import com.facebook.drawee.backends.pipeline.Fresco;
19+
import com.facebook.imagepipeline.core.ImagePipeline;
20+
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
21+
import com.facebook.imagepipeline.image.CloseableImage;
22+
import com.facebook.imagepipeline.request.ImageRequest;
23+
import com.facebook.imagepipeline.request.ImageRequestBuilder;
24+
25+
import java.io.IOException;
26+
27+
import fr.free.nrw.commons.R;
828
import timber.log.Timber;
929

30+
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
31+
1032
/**
1133
* Created by bluesir9 on 3/10/17.
1234
*/
@@ -132,4 +154,52 @@ private static boolean checkIfImageIsDark(Bitmap bitmap) {
132154

133155
return isImageDark;
134156
}
157+
158+
/**
159+
* Downloads the image from the URL and sets it as the phone's wallpaper
160+
* Fails silently if download or setting wallpaper fails.
161+
* @param context
162+
* @param imageUrl
163+
*/
164+
public static void setWallpaperFromImageUrl(Context context, Uri imageUrl) {
165+
Timber.d("Trying to set wallpaper from url %s", imageUrl.toString());
166+
ImageRequest imageRequest = ImageRequestBuilder
167+
.newBuilderWithSource(imageUrl)
168+
.setAutoRotateEnabled(true)
169+
.build();
170+
171+
ImagePipeline imagePipeline = Fresco.getImagePipeline();
172+
final DataSource<CloseableReference<CloseableImage>>
173+
dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);
174+
175+
dataSource.subscribe(new BaseBitmapDataSubscriber() {
176+
177+
@Override
178+
public void onNewResultImpl(@Nullable Bitmap bitmap) {
179+
if (dataSource.isFinished() && bitmap != null){
180+
Timber.d("Bitmap loaded from url %s", imageUrl.toString());
181+
setWallpaper(context, Bitmap.createBitmap(bitmap));
182+
dataSource.close();
183+
}
184+
}
185+
186+
@Override
187+
public void onFailureImpl(DataSource dataSource) {
188+
Timber.d("Error getting bitmap from image url %s", imageUrl.toString());
189+
if (dataSource != null) {
190+
dataSource.close();
191+
}
192+
}
193+
}, CallerThreadExecutor.getInstance());
194+
}
195+
196+
private static void setWallpaper(Context context, Bitmap bitmap) {
197+
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
198+
try {
199+
wallpaperManager.setBitmap(bitmap);
200+
ViewUtil.showLongToast(context, context.getString(R.string.wallpaper_set_successfully));
201+
} catch (IOException e) {
202+
Timber.e(e,"Error setting wallpaper");
203+
}
204+
}
135205
}

app/src/main/res/menu/fragment_image_detail.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
android:id="@+id/menu_download_current_image"
1616
android:title="@string/menu_download"
1717
app:showAsAction="never" />
18+
<item
19+
android:id="@+id/menu_set_as_wallpaper"
20+
android:title="@string/menu_set_wallpaper"
21+
app:showAsAction="never" />
1822
<item
1923
android:id="@+id/menu_retry_current_image"
2024
android:enabled="false"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,6 @@
287287
<string name="images_not_found">No Images matching %1$s found</string>
288288
<string name="title_activity_search">Search</string>
289289

290+
<string name="menu_set_wallpaper">Set wallpaper</string>
291+
<string name="wallpaper_set_successfully">Wallpaper set successfully!</string>
290292
</resources>

0 commit comments

Comments
 (0)