Back to project page unknown-pleasures.
The source code is released under:
Creative Commons Attribution NonCommercial NoDerivs (CC-NC-ND) THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTE...
If you think the Android project unknown-pleasures listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * UnknownPleasuresService.java/*ww w.j av a 2 s . co m*/ * Author: marek.brodziak@gmail.com * Created: Apr 16, 2014 * Copyright 2014 by miniti */ package pl.miniti.android.pleasures; import pl.miniti.android.pleasures.animation.Animator; import pl.miniti.android.pleasures.animation.AnimatorFactory; import pl.miniti.android.pleasures.line.LineRenderer; import pl.miniti.android.pleasures.line.LineRendererFactory; import pl.miniti.android.pleasures.palette.Palette; import pl.miniti.android.pleasures.palette.PaletteFactory; import pl.miniti.android.pleasures.preferences.Prefs; import pl.miniti.android.pleasures.preferences.Prefs.V; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; import android.os.Handler; import android.preference.PreferenceManager; import android.service.wallpaper.WallpaperService; import android.view.SurfaceHolder; /** * Wallpaper service for 'Unknown Pleasures' which handles instantiation of the * renderer object according to user prefernces and callbacks in regards to the * external systems like sensors, etc. */ public class UnknownPleasuresService extends WallpaperService { /** * Display margin */ private static final float MARGIN = 0.1f; /* * (non-Javadoc) * * @see android.service.wallpaper.WallpaperService#onCreateEngine() */ @Override public Engine onCreateEngine() { return new UnknownPleasuresEngine(); } /** * Implementation of the 'Unknown Pleasures' live wallpaper engine */ private class UnknownPleasuresEngine extends Engine implements OnSharedPreferenceChangeListener, Runnable { /** */ private static final int MAX_SPEED = 1000; /** * Default animation frequency */ private static final int DEFAULT_FREQUENCY = 200; /** * OS handler */ private final Handler handler = new Handler(); /** * Determines if the service is currently visible or not. If false * animation is not performed. */ private boolean visible = true; /** * Use margin or display in full width */ private boolean margin = false; /** * Animation destination rectangle */ private Rect dest; /** * Animator implementation */ private Animator animator; /** * Line renderer implementation */ private LineRenderer lineRenderer; /** * Palette implementation */ private Palette palette; /** * Animation speed */ private int frequency = DEFAULT_FREQUENCY; /** * Canvas width & height */ private int width, height; /** * Background transparency */ private int transparency; /** * Total number of lines */ private int lines; /** * Default constructor for the engine */ private UnknownPleasuresEngine() { SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(UnknownPleasuresService.this); sharedPreferences.registerOnSharedPreferenceChangeListener(this); // load all preferences onSharedPreferenceChanged(sharedPreferences, null); handler.post(this); } /* * (non-Javadoc) * * @see * android.content.SharedPreferences.OnSharedPreferenceChangeListener * #onSharedPreferenceChanged(android.content.SharedPreferences, * java.lang.String) */ @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // for now just one implementation lineRenderer = LineRendererFactory.newInstance(); final boolean all = (key == null); Prefs p = Prefs.of(prefs); if (all || V.PALETTE.key.equals(key)) { palette = PaletteFactory.newInstance(p.s(V.PALETTE, null)); } if (all || V.DIM.key.equals(key)) { palette.setDimmed(p.b(V.DIM, Boolean.TRUE)); } if (all || V.STRONG_LINE.key.equals(key)) { lineRenderer.setStrong(p.b(V.STRONG_LINE, Boolean.TRUE)); } if (all || V.ANIMATOR.key.equals(key)) { animator = AnimatorFactory.newInstance(p.s(V.ANIMATOR, null)); } if (all || (V.SPEED.key.equals(key) || V.ANIMATOR.key.equals(key))) { frequency = MAX_SPEED - p.i(V.SPEED, 0); if (frequency < 10) { frequency = 0; } } if (all || V.FULL_WIDTH.key.equals(key)) { margin = !p.b(V.FULL_WIDTH, Boolean.TRUE); setSize(width, height, margin ? MARGIN : 0f); } if (all || V.TRANSPARENCY.key.equals(key)) { transparency = p.i(V.TRANSPARENCY, 100); } if (all || V.QUANTITY.key.equals(key)) { lines = p.i(V.QUANTITY, 60); } } /* * (non-Javadoc) * * @see * android.service.wallpaper.WallpaperService.Engine#onVisibilityChanged * (boolean) */ @Override public void onVisibilityChanged(boolean visible) { this.visible = visible; if (visible) { handler.post(this); } else { handler.removeCallbacks(this); } } /* * (non-Javadoc) * * @see * android.service.wallpaper.WallpaperService.Engine#onSurfaceDestroyed * (android.view.SurfaceHolder) */ @Override public void onSurfaceDestroyed(SurfaceHolder holder) { super.onSurfaceDestroyed(holder); this.visible = false; handler.removeCallbacks(this); } /* * (non-Javadoc) * * @see * android.service.wallpaper.WallpaperService.Engine#onSurfaceChanged * (android.view.SurfaceHolder, int, int, int) */ @Override public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { setSize(width, height, margin ? MARGIN : 0f); super.onSurfaceChanged(holder, format, width, height); } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { if (visible) { final SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = null; try { canvas = holder.lockCanvas(); if (canvas != null) { synchronized (holder) { requestAnimationFrame(canvas); } } } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } handler.removeCallbacks(this); if (visible && frequency > 0) { handler.postDelayed(this, frequency); } } /** * @param width * @param height */ public void setSize(int width, int height, float margin) { this.width = width; this.height = height; int diff = width - height; diff = diff > 0 ? diff : -diff; final int m = (int) (Math.min(width, height) * margin); int tx, ty, bx, by; if (height > width) { // vertical tx = m; ty = diff / 2 + m; bx = width - m; by = height - diff / 2 - m; } else { // horizontal tx = diff / 2 + m; ty = m; bx = width - diff / 2 - m; by = height - m; } dest = new Rect(tx, ty, bx, by); } /** * @param canvas */ public void requestAnimationFrame(final Canvas canvas) { final int bgColor = palette.background(); final int withAlpha = Color.argb(transparency, Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor)); canvas.drawColor(withAlpha); for (int i = 0; i < lines; ++i) { lineRenderer.render(canvas, dest, animator.of(i, lines), i, lines, palette.of(i, lines), withAlpha); } if (frequency > 0) { animator.frame(); palette.frame(); } } } }