Skip to content

Commit

Permalink
Go home after setting the wallpaper
Browse files Browse the repository at this point in the history
+ On Android N and above use the new API to set both the general system wallpaper and the lock-screen-specific wallpaper
  • Loading branch information
devliber committed Sep 16, 2016
1 parent be6f4b2 commit 603d4b8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
8 changes: 0 additions & 8 deletions .idea/dictionaries/Aurelian.xml

This file was deleted.

15 changes: 15 additions & 0 deletions .idea/dictionaries/LoneColor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.appgramming.lonecolor"
minSdkVersion 11
targetSdkVersion 24
versionCode 4
versionName '1.2'
versionCode 5
versionName '2.0-beta'
}
buildTypes {
release {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.appgramming.lonecolor">

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

<application
android:fullBackupContent="true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:installLocation="auto"
android:label="@string/app_name"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".StartActivity"
android:excludeFromRecents="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;

/**
* An {@link IntentService} subclass for changing the current system wallpaper to a bitmap
Expand Down Expand Up @@ -47,6 +48,7 @@ public class LoneColorWallpaperService extends IntentService {
/**
* Creates the LoneColorWallpaperService IntentService.
*/
@SuppressWarnings("WeakerAccess")
public LoneColorWallpaperService() {
super("LoneColorWallpaperService");
}
Expand All @@ -62,6 +64,17 @@ public static void setColorWallpaper(Context context, int color) {
context.startService(intent);
}

/**
* Returns a mutable 1px x 1px bitmap filled with the specified color.
*
* @param color The color to fill the bitmap
*/
private static Bitmap createColorBitmap(int color) {
final Bitmap colorBitmap = Bitmap.createBitmap(ONE_PIXEL, ONE_PIXEL, Bitmap.Config.ARGB_8888);
colorBitmap.eraseColor(color);
return colorBitmap;
}

/**
* Do the actual work on the worker thread: create, fill and set the color wallpaper.
*
Expand All @@ -82,29 +95,26 @@ protected void onHandleIntent(Intent intent) {
final Bitmap colorBitmap = createColorBitmap(color);

// Set the wallpaper
wpManager.setBitmap(colorBitmap);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// On Android N and above use the new API to set both the general system wallpaper and
// the lock-screen-specific wallpaper
wpManager.setBitmap(colorBitmap, null, true, WallpaperManager.FLAG_SYSTEM | WallpaperManager.FLAG_LOCK);
} else {
wpManager.setBitmap(colorBitmap);
}

// Success: copy the color code to the clipboard
statusOK(color);

Utils.goHome(this);

} catch (Exception e) {
// Write the stack trace to System.err and copy the reason of the failure to clipboard
e.printStackTrace();
statusFailure(e.toString());
}
}

/**
* Returns a mutable 1px x 1px bitmap filled with the specified color.
*
* @param color The color to fill the bitmap
*/
private static Bitmap createColorBitmap(int color) {
final Bitmap colorBitmap = Bitmap.createBitmap(ONE_PIXEL, ONE_PIXEL, Bitmap.Config.ARGB_8888);
colorBitmap.eraseColor(color);
return colorBitmap;
}

/**
* Copy a text to clipboard.
*
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/appgramming/lonecolor/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2014-2016 Appgramming. All rights reserved.
* http://www.appgramming.com
*/
package com.appgramming.lonecolor;

import android.content.Context;
import android.content.Intent;

/**
* Static utility methods.
*/
class Utils {

/**
* Goes home.
*/
static void goHome(Context context) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startMain);
}
}

0 comments on commit 603d4b8

Please sign in to comment.