Android Open Source - hacker-live-wallpaper Hacker Wallpaper Service






From Project

Back to project page hacker-live-wallpaper.

License

The source code is released under:

Copyright (C) 2013 Gulshan Singh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Soft...

If you think the Android project hacker-live-wallpaper 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 com.gulshansingh.hackerlivewallpaper;
/*w w  w  .  java2 s.  co  m*/
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

import java.util.ArrayList;
import java.util.List;

import static com.gulshansingh.hackerlivewallpaper.SettingsActivity.KEY_BACKGROUND_COLOR;

public class HackerWallpaperService extends WallpaperService {

  private static boolean reset = false;
  private static boolean previewReset = false;

  private int r, g, b;

  public static void reset() {
    previewReset = true;
    reset = true;
  }

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

  public class HackerWallpaperEngine extends Engine {

    private Handler handler = new Handler();
    private boolean visible = true;

    /** The sequences to draw on the screen */
    private List<BitSequence> sequences = new ArrayList<>();

    private int width;

    /**
     * The main runnable that is given to the Handler to draw the animation
     */
    private Runnable drawRunnable = new Runnable() {
      public void run() {
        draw();
      }
    };

    /** Draws all of the bit sequences on the screen */
    private void draw() {
      if (visible) {
        // We can't have just one reset flag, because then the preview
        // would consume that flag and the actual wallpaper wouldn't be
        // reset
        if (previewReset && isPreview()) {
          previewReset = false;
          resetSequences();
        } else if (reset && !isPreview()) {
          reset = false;
          resetSequences();
        }
        SurfaceHolder holder = getSurfaceHolder();
        Canvas c = holder.lockCanvas();
        try {
          if (c != null) {
            c.drawARGB(255, r, g, b);

            for (int i = 0; i < sequences.size(); i++) {
              sequences.get(i).draw(c);
            }
          }
        } finally {
          if (c != null) {
            holder.unlockCanvasAndPost(c);
          }
        }

        // Remove the runnable, and only schedule the next run if
        // visible
        handler.removeCallbacks(drawRunnable);

        handler.post(drawRunnable);
      } else {
        pause();
      }
    }

    // TODO: Not all of the sequences need to be cleared
    private void resetSequences() {
      SharedPreferences preferences = PreferenceManager
          .getDefaultSharedPreferences(getApplicationContext());
      int color = preferences.getInt(KEY_BACKGROUND_COLOR, 0);
      r = (color >> 16) & 0xFF;
      g = (color >> 8) & 0xFF;
      b = (color >> 0) & 0xFF;
      stop();
      sequences.clear();
      int numSequences = (int) (1.5 * width / BitSequence.getWidth());
      for (int i = 0; i < numSequences; i++) {
        sequences.add(new BitSequence(
            (int) (i * BitSequence.getWidth() / 1.5)));
      }
      start();
    }

    private void pause() {
      handler.removeCallbacks(drawRunnable);
      for (int i = 0; i < sequences.size(); i++) {
        sequences.get(i).pause();
      }
    }

    private void start() {
      handler.post(drawRunnable);
      for (int i = 0; i < sequences.size(); i++) {
        sequences.get(i).unpause();
      }
    }

    private void stop() {
      handler.removeCallbacks(drawRunnable);
      for (int i = 0; i < sequences.size(); i++) {
        sequences.get(i).stop();
      }
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
      super.onSurfaceCreated(holder);
      BitSequence.configure(getApplicationContext());
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
      super.onSurfaceDestroyed(holder);
      pause();
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
        int width, int height) {
      super.onSurfaceChanged(holder, format, width, height);
      this.width = width;

      BitSequence.setScreenDim(width, height);

      // Initialize BitSequences
      resetSequences();
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
      super.onVisibilityChanged(visible);
      if (visible) {
        start();
      } else {
        pause();
      }
      this.visible = visible;
    }
  }
}




Java Source Code List

com.gulshansingh.hackerlivewallpaper.ApplicationTest.java
com.gulshansingh.hackerlivewallpaper.BitSequence.java
com.gulshansingh.hackerlivewallpaper.HackerWallpaperService.java
com.gulshansingh.hackerlivewallpaper.Refreshable.java
com.gulshansingh.hackerlivewallpaper.SettingsActivity.java
com.gulshansingh.hackerlivewallpaper.settings.CharacterSetPreference.java
com.gulshansingh.hackerlivewallpaper.settings.NumberSeekBarPreference.java
com.gulshansingh.hackerlivewallpaper.settings.PercentSeekBarPreference.java
com.gulshansingh.hackerlivewallpaper.settings.SeekBarPreference.java
com.gulshansingh.hackerlivewallpaper.thirdparty.ArrayDeque.java
com.gulshansingh.hackerlivewallpaper.thirdparty.Deque.java
net.margaritov.preference.colorpicker.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.ColorPickerDialog.java
net.margaritov.preference.colorpicker.ColorPickerPanelView.java
net.margaritov.preference.colorpicker.ColorPickerPreference.java
net.margaritov.preference.colorpicker.ColorPickerView.java