Skip to content
This repository has been archived by the owner on Apr 10, 2022. It is now read-only.

Add ActionLauncher Live Wallpaper API support #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.apache.commons:commons-math3:3.5'
compile 'com.actionlauncher:action3-api:1.+'
}
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
<service android:name="com.actionlauncher.api.LiveWallpaperSource"
android:label="LiveWallpaperSource"
android:exported="true">
<intent-filter>
<action android:name="com.actionlauncher.api.action.LiveWallpaperSource" />
</intent-filter>
</service>
</application>

</manifest>
56 changes: 53 additions & 3 deletions app/src/main/java/com/androidexperiments/meter/MeterWallpaper.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.androidexperiments.meter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

import java.util.ArrayList;
Expand All @@ -14,6 +17,8 @@
import com.androidexperiments.meter.drawers.Drawer;
import com.androidexperiments.meter.drawers.NotificationsDrawer;

import com.actionlauncher.api.LiveWallpaperSource;

/**
* The Live Wallpaper Service and rendering Engine
*/
Expand All @@ -26,6 +31,10 @@ public class MeterWallpaper extends WallpaperService {
// Variable containing the index of the drawer last shown
private int mDrawerIndex = -1;

// Whether we have to update actionlauncher with a new wallpaper
// ONLY set to true if actionlauncher was updated SUCCESSFULLY
private boolean mUpdateActionLauncher;

@Override
public Engine onCreateEngine() {
WallpaperEngine engine = new WallpaperEngine(this);
Expand Down Expand Up @@ -81,12 +90,28 @@ private void draw() {
if(mDrawer.shouldDraw()) {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
Bitmap bmp = null;
// Draw into a Bitmap which will then be sent to ActionLauncher
if (mUpdateActionLauncher) {
Rect surfaceFrame = holder.getSurfaceFrame();
bmp = Bitmap.createBitmap(surfaceFrame.width(), surfaceFrame.height(), Bitmap.Config.ARGB_8888);
c = new Canvas(bmp);
if (c != null) {
// Let the drawer render to the canvas
mDrawer.draw(c);
}
updateActionLauncher(bmp);
}
try {
// Lock the drawing canvas
c = holder.lockCanvas();
if (c != null) {
// Let the drawer render to the canvas
mDrawer.draw(c);
if (bmp != null) {
c.drawBitmap(bmp, 0, 0, null);
} else {
// Let the drawer render to the canvas
mDrawer.draw(c);
}
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
Expand All @@ -99,6 +124,28 @@ private void draw() {
}
}

// Taken from the ActionLauncher API Documentation
private void updateActionLauncher(Bitmap bmp) {
if (bmp != null) {
try {
LiveWallpaperSource.with(mContext)
.setBitmapSynchronous(bmp)
.run();
mUpdateActionLauncher = false;
} catch (OutOfMemoryError outOfMemoryError) {
// Palette generation was unable to process the Bitmap passed in to
// setBitmapSynchronous(). Consider using a smaller image.
// See ActionPalette.DEFAULT_RESIZE_BITMAP_MAX_DIMENSION
Log.e(TAG, "Error setting ActionLauncher LiveWallpaper", outOfMemoryError);
} catch (IllegalArgumentException illegalArgumentEx) {
// Raised during palette generation. Check your Bitmap.
Log.e(TAG, "Error setting ActionLauncher LiveWallpaper", illegalArgumentEx);
} catch (IllegalStateException illegalStateException) {
// Raised during palette generation. Check your Bitmap.
Log.e(TAG, "Error setting ActionLauncher LiveWallpaper", illegalStateException);
}
}
}

/**
* Toggle visibility of the wallpaper
Expand Down Expand Up @@ -135,7 +182,9 @@ public void onVisibilityChanged(boolean visible) {
}

mDrawer.start();
// Start the drawing loop

// Start the drawing loop and make sure ActionLauncher will get updated
mUpdateActionLauncher = true;
draw();

} else {
Expand All @@ -149,6 +198,7 @@ public void onVisibilityChanged(boolean visible) {

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mUpdateActionLauncher = true;
draw();
}

Expand Down