Android Open Source - slider Sliding Wallpaper






From Project

Back to project page slider.

License

The source code is released under:

Apache License

If you think the Android project slider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.devisnik.android.sliding;
/*w ww  .  ja v a2 s .  c om*/
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

public class SlidingWallpaper extends WallpaperService {

  public static final String SHARED_PREFS_NAME = "slider";

  private final Handler mHandler = new Handler();

  @Override
  public Engine onCreateEngine() {
    return new SliderEngine();
  }

  class SliderEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener {

    private static final String TAG = "SliderEngine";
    private static final String TAP_COMMAND = "android.wallpaper.tap";
    private static final int TARGET_FPS = 90;
    private static final int TARGET_FRAME_DURATION = 1000 / TARGET_FPS;
    private final Runnable mDrawFrame = new Runnable() {
      public void run() {
        drawFrame();
      }
    };
    private volatile boolean mVisible;
    private FrameDrawer itsFrameDrawer;
    private volatile int itsWidth;
    private volatile int itsHeight;
    private final Settings itsSettings;

    @SuppressLint("NewApi")
    SliderEngine() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        setOffsetNotificationsEnabled(false);
      SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE);
      preferences.registerOnSharedPreferenceChangeListener(this);
      itsSettings = new Settings(SlidingWallpaper.this, preferences);
    }

    @Override
    public void onDestroy() {
      mHandler.removeCallbacks(mDrawFrame);
      super.onDestroy();
    }

    @Override
    public void onVisibilityChanged(final boolean visible) {
      if (mVisible == visible)
        return;
      mVisible = visible;
      Logger.d(TAG, "visibility change to: " + Boolean.toString(visible));
      if (visible)
        makeVisible();
      else
        makeInvisible();
    }

    private void makeInvisible() {
      Logger.d(TAG, "makeInvisible");
      if (itsFrameDrawer != null)
        itsFrameDrawer.stop();
      mHandler.removeCallbacks(mDrawFrame);
    }

    private void makeVisible() {
      Logger.d(TAG, "makeVisible");
      // bug fix: NPE is thrown here on some (which?) devices (reported via google play bug reports)
      if (itsFrameDrawer == null)
        return;
      itsFrameDrawer.start();
      mHandler.removeCallbacks(mDrawFrame);
      mHandler.post(mDrawFrame);
    }

    @Override
    public void onSurfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height) {
      super.onSurfaceChanged(holder, format, width, height);
      Logger.d(TAG, "surface change: " + width + "," + height);
      itsWidth = width;
      itsHeight = height;
      createFrameDrawer();
    }

    private void createFrameDrawer() {
      Logger.d(TAG, "createFrameDrawer");
      makeInvisible();
      itsFrameDrawer = new FrameDrawer(itsSettings, itsWidth, itsHeight, isPreview());
      if (mVisible)
        makeVisible();
    }

    @Override
    public void onSurfaceCreated(final SurfaceHolder holder) {
      Logger.d(TAG, "onSurfaceCreated");
      super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(final SurfaceHolder holder) {
      Logger.d(TAG, "onSurfaceDestroyed");
      mVisible = false;
      makeInvisible();
      super.onSurfaceDestroyed(holder);
    }

    @Override
    public Bundle onCommand(final String action, final int x, final int y, final int z, final Bundle extras,
        final boolean resultRequested) {
      Logger.d(TAG, "onCommand: " + action);
      if (TAP_COMMAND.equals(action))
        itsFrameDrawer.handleTap();
      return super.onCommand(action, x, y, z, extras, resultRequested);
    }

    @SuppressWarnings("boxing")
    @Override
    public void onOffsetsChanged(final float xOffset, final float yOffset, final float xStep, final float yStep,
        final int xPixels, final int yPixels) {
      PropertiesBuilder builder = new PropertiesBuilder();
      builder.append("xOffset", xOffset).append("yOffset", yOffset);
      builder.append("xStep", xStep).append("yStep", yStep);
      builder.append("xPixels", xPixels).append("yPixels", yPixels);
      Logger.d(TAG, "onOffsetChanged: " + builder.toString());
    }

    void drawFrame() {
      long startTime = SystemClock.uptimeMillis();
      itsFrameDrawer.draw(getSurfaceHolder());

      // Reschedule the next redraw
      mHandler.removeCallbacks(mDrawFrame);
      if (mVisible)
        mHandler.postDelayed(mDrawFrame,
            Math.max(1, TARGET_FRAME_DURATION - (SystemClock.uptimeMillis() - startTime)));
    }

    @Override
    public void onSharedPreferenceChanged(final SharedPreferences arg0, final String key) {
      Logger.d(TAG, "onSharedPreferenceChanged: key = " + key);
      itsSettings.clearCachedImage();
      createFrameDrawer();
    }
  }
}




Java Source Code List

de.devisnik.android.sliding.ARandom.java
de.devisnik.android.sliding.FPSCounter.java
de.devisnik.android.sliding.FrameDrawer.java
de.devisnik.android.sliding.ISpeed.java
de.devisnik.android.sliding.ImageCache.java
de.devisnik.android.sliding.ImageFactory.java
de.devisnik.android.sliding.Logger.java
de.devisnik.android.sliding.OpenActivity.java
de.devisnik.android.sliding.OpenPreviewActivity.java
de.devisnik.android.sliding.PropertiesBuilder.java
de.devisnik.android.sliding.RenderHandler.java
de.devisnik.android.sliding.Replayer.java
de.devisnik.android.sliding.Settings.java
de.devisnik.android.sliding.SlidingPreferences.java
de.devisnik.android.sliding.SlidingWallpaper.java
de.devisnik.android.sliding.SpeedFactory.java
de.devisnik.android.sliding.SummaryUpdater.java
de.devisnik.android.sliding.tile.BitmapCachingPieceDrawer.java
de.devisnik.android.sliding.tile.CachingPieceDrawer.java
de.devisnik.android.sliding.tile.IPieceDrawer.java
de.devisnik.android.sliding.tile.ImagePieceDrawer.java
de.devisnik.android.sliding.tile.NumberPieceDrawer.java
de.devisnik.android.sliding.tile.PieceDrawerFactory.java
de.devisnik.android.sliding.tile.TileShifter.java
de.devisnik.android.sliding.tile.TileStore.java
de.devisnik.android.sliding.tile.Tile.java
de.devisnik.sliding.FrameFactory.java
de.devisnik.sliding.FrameScrambler.java
de.devisnik.sliding.FromHomeShiftingEvent.java
de.devisnik.sliding.IFrameListener.java
de.devisnik.sliding.IFrame.java
de.devisnik.sliding.IHole.java
de.devisnik.sliding.IMove.java
de.devisnik.sliding.IPiece.java
de.devisnik.sliding.IRandom.java
de.devisnik.sliding.IRobotFrame.java
de.devisnik.sliding.IShifterListener.java
de.devisnik.sliding.MoveFactory.java
de.devisnik.sliding.Point.java
de.devisnik.sliding.ShiftingEvent.java
de.devisnik.sliding.ToHomeShiftingEvent.java
de.devisnik.sliding.animation.AccelerateDecelerateInterpolator.java
de.devisnik.sliding.animation.AnimationRunner.java
de.devisnik.sliding.animation.Animation.java
de.devisnik.sliding.animation.IInterpolator.java
de.devisnik.sliding.animation.IMovement.java
de.devisnik.sliding.animation.IShiftable.java
de.devisnik.sliding.animation.LinearInterpolator.java
de.devisnik.sliding.animation.MetaShiftable.java
de.devisnik.sliding.animation.ShiftAnimation.java
de.devisnik.sliding.animation.Translation.java
de.devisnik.sliding.impl.Frame.java
de.devisnik.sliding.impl.Hole.java
de.devisnik.sliding.impl.Move.java
de.devisnik.sliding.impl.PieceIterator.java
de.devisnik.sliding.impl.Piece.java
de.devisnik.sliding.impl.RobotFrame.java
de.devisnik.sliding.impl.Scrambler.java