Android Open Source - ColorWallpaper Color Wallpaper Service






From Project

Back to project page ColorWallpaper.

License

The source code is released under:

Licensed under the Expat License. Copyright (C) 2013 Peter Occil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the...

If you think the Android project ColorWallpaper 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.upokecenter.android.colorwallpaper;
/* www .java 2s  .c om*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.TargetApi;
import android.app.WallpaperManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.location.Location;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Xml;
import android.view.SurfaceHolder;

import com.upokecenter.android.location.ILocationHelper;
import com.upokecenter.android.location.ISimpleLocationListener;
import com.upokecenter.android.location.LocationHelper;
import com.upokecenter.android.net.DownloadService;
import com.upokecenter.android.util.AppManager;
import com.upokecenter.android.util.BitmapUtility;
import com.upokecenter.android.wallpaper.BaseWallpaperService;
import com.upokecenter.net.IHttpHeaders;
import com.upokecenter.net.IOnFinishedListener;
import com.upokecenter.net.IResponseListener;
import com.upokecenter.util.DateTimeUtility;
import com.upokecenter.util.Random;
import com.upokecenter.util.SunriseSunset;
import com.upokecenter.util.XmlHelper;


@TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
public class ColorWallpaperService extends BaseWallpaperService {

  public static Bitmap cacheImageFile(String file,
      int desiredWidth, int desiredHeight) throws IOException {
    BitmapFactory.Options o=new BitmapFactory.Options();
    o.inJustDecodeBounds=true;
    BitmapFactory.decodeFile(file.toString(),o);
    if(o.outWidth>desiredWidth || o.outHeight>desiredHeight){
      float sampleX=o.outWidth*1.0f/desiredWidth;
      float sampleY=o.outHeight*1.0f/desiredHeight;
      o.inSampleSize=Math.round(Math.min(sampleX,sampleY));
    }
    o.inJustDecodeBounds=false;
    Bitmap bitmap=BitmapFactory.decodeFile(file.toString(),o);
    return bitmap;
  }


  public Bitmap cacheImage(InputStream stream, int desiredWidth, int desiredHeight) throws IOException {
    stream.mark(0x10000);
    BitmapFactory.Options o=new BitmapFactory.Options();
    o.inJustDecodeBounds=true;
    BitmapFactory.decodeStream(stream,null,o);
    stream.reset();
    if(o.outWidth>desiredWidth || o.outHeight>desiredHeight){
      float sampleX=o.outWidth*1.0f/desiredWidth;
      float sampleY=o.outHeight*1.0f/desiredHeight;
      o.inSampleSize=Math.round(Math.min(sampleX,sampleY));
    }
    o.inJustDecodeBounds=false;
    Bitmap bitmap=BitmapFactory.decodeStream(stream,null,o);
    return bitmap;
  }

  public static List<String> parseRssFeedImages(InputStream stream) throws IOException {
    XmlPullParser parser=Xml.newPullParser();
    try {
      List<String> ret=new ArrayList<String>();
      parser.setInput(stream,null);
      while(XmlHelper.moveToElement(parser,null,"item")){
        int depth=XmlHelper.GetDepth(parser);
        while(XmlHelper.findChildToDepth(parser,depth,null,null)){
          if(XmlHelper.IsElement(parser,"http://search.yahoo.com/mrss/","content")){
            String urlname=parser.getAttributeValue(null,"url");
            ret.add(urlname);
          }
        }
      }
      return ret;
    } catch (XmlPullParserException e) {
      throw (IOException)new IOException().initCause(e);
    }
  }
  @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
  @Override
  public Engine onCreateEngine() {
    AppManager.initialize(this);
    return new Engine(){
      ILocationHelper locationHelper=null;
      int width=0;
      int height=0;
      Preferences prefs=null;
      Random random=null;
      Location currentLocation=null;
      SunriseSunset.DayState dayState;
      Bitmap scratchBitmap=null;
      Canvas scratchCanvas=null;
      int onDayStateCount=0;
      class Preferences {
        public boolean usedaycycle;
        public boolean uselocation;
        public boolean usemonthcycle;
        public int colorhue;
        public boolean reacttotaps;
        public boolean fadeinboxes;
        public boolean usemodelbg;
        public String picture;
        public int drawspeedfps;
        public int boxsize;
        public void setPreferences(SharedPreferences prefs){
          usedaycycle=prefs.getBoolean("usedaycycle",true);
          uselocation=prefs.getBoolean("uselocation",false);
          usemonthcycle=prefs.getBoolean("usemonthcycle",true);
          colorhue=prefs.getInt("colorhue",0);
          boxsize=prefs.getInt("boxsize",30);
          reacttotaps=prefs.getBoolean("reacttotaps",true);
          fadeinboxes=prefs.getBoolean("fadeinboxes",true);
          usemodelbg=prefs.getBoolean("usemodelbg",true);
          picture=prefs.getString("picture","");
          drawspeedfps=prefs.getInt("drawspeedfps",10);
        }
      }

      /*
       * NOTE: The OnSharedPreferenceChangeListener must be
       * an instance variable, not a local variable, because
       * the SharedPreferences object stores listeners as weak
       * references when it registers them, so when they are
       * local variables, they become eligible for garbage
       * collection once the listeners leave the
       * scope of the method.
       */
      OnSharedPreferenceChangeListener listener=null;

      class ColorTransition {
        Rect rect;
        int endColor;
        int currentFrame;
        int frameCount;
        public int getColor(){
          int frame=Math.min(currentFrame,frameCount);
          frame=Math.max(0,frame);
          int a=(Color.alpha(endColor)*currentFrame/frameCount);
          return Color.argb(a,Color.red(endColor),
              Color.green(endColor),
              Color.blue(endColor));
        }
      }

      ArrayList<ColorTransition> transitions=new ArrayList<ColorTransition>();
      private void updateColorTransitions(){
        for(ColorTransition ct : transitions){
          ct.currentFrame++;
        }
        for(int i=0;i<transitions.size();i++){
          ColorTransition ct=transitions.get(i);
          if(ct.currentFrame>ct.frameCount){
            transitions.remove(i);
            i--;
          }
        }
      }
      private void addColorTransition(Rect rect, int endColor, int frameCount){
        ColorTransition ct=new ColorTransition();
        ct.rect=rect;
        ct.endColor=endColor;
        ct.frameCount=frameCount;
        ct.currentFrame=0;
        transitions.add(ct);
      }

      @Override
      protected void drawFrame(Canvas c, long e) {
        int curWidth=c.getClipBounds().width();
        int curHeight=c.getClipBounds().height();
        if(curWidth!=width || curHeight!=height){
          width=curWidth;
          height=curHeight;
          int background=Color.HSVToColor(new float[]{
              getCurrentHue(),0.2f,getValueOffset()/40f
          });
          scratchBitmap=BitmapUtility.redrawBitmap(
              scratchBitmap,width,height,background);
          scratchCanvas=new Canvas(scratchBitmap);
          if(c!=null){
            c.drawBitmap(scratchBitmap,null,
                new RectF(0,0,
                    scratchBitmap.getWidth(),
                    scratchBitmap.getHeight()),null);
          }
        }
        if(scratchBitmap!=null){
          Rect bitmapRect=new Rect(0,0,scratchBitmap.getWidth(),scratchBitmap.getHeight());
          c.drawBitmap(scratchBitmap,bitmapRect,bitmapRect,null);
        }
        for(int i=0;i<transitions.size();i++){
          ColorTransition ct=transitions.get(i);
          Paint p=new Paint();
          int color=ct.getColor();
          p.setColor(color);
          c.drawRect(ct.rect,p);
        }
        for(int i=0;i<transitions.size();i++){
          ColorTransition ct=transitions.get(i);
          Paint p=new Paint();
          int color=ct.getColor();
          p.setColor(color);
          if(Color.alpha(color)==0xFF && scratchCanvas!=null){
            scratchCanvas.drawRect(ct.rect,p);
          }
        }
        updateColorTransitions();
      }

      @Override
      public int getDelay(){
        int fps=prefs.drawspeedfps;
        if(fps!=0)
          return 1000/Math.max(fps,1);
        return 20;
      }

      @Override
      protected void onFrame() {
        int boxcount=8;
        float hue=getCurrentHue();
        int value=getValueOffset();
        for(int i=0;i<boxcount;i++){
          drawColor(Color.HSVToColor(new float[]{
              hue-10f+random.nextInt(20),
              ((10+random.nextInt(11))/20.0f),
              Math.min(1.0f,((value+random.nextInt(21))/40.0f))
          }));
        }
        if(onDayStateCount==0){
          updateDayState();
        }
        onDayStateCount+=1;
        onDayStateCount%=(2000/getDelay());
      }

      @Override
      public Bundle onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested){
        if(WallpaperManager.COMMAND_TAP.equals(action)){
          if(prefs.reacttotaps){
            for(int i=0;i<8;i++){
              int background=Color.HSVToColor(new float[]{
                  random.nextInt(360),
                  ((10+random.nextInt(11))/20.0f),
                  ((10+random.nextInt(11))/20.0f)
              });
              drawColor2(background,x,y);
            }
          }
        }
        return super.onCommand(action,x,y,z,extras,resultRequested);
      }


      private float getCurrentHue(){
        if(prefs.usemonthcycle){
          long date=DateTimeUtility.getCurrentDate();
          int[] components=DateTimeUtility.getLocalDateComponents(date);
          int nextMonth=(components[1]==12) ? 1 : components[1]+1;
          int yearForNextMonth=(components[1]==12) ? components[0]+1 : components[0];
          long startOfMonth=DateTimeUtility.toLocalDate(
              components[0],components[1],1,0,0,0);
          long startOfNextMonth=DateTimeUtility.toLocalDate(
              yearForNextMonth,nextMonth,1,0,0,0);
          float currentHue=(date-startOfMonth)*1.0f/(startOfNextMonth-startOfMonth);
          currentHue*=360.0;
          return currentHue;
        } else
          return prefs.colorhue;
      }

      private int getValueOffset(){
        int valueOffset=20;
        if(prefs.usedaycycle){
          if(dayState==SunriseSunset.DayState.Night) {
            valueOffset=5;
          }
          if(dayState==SunriseSunset.DayState.NightToDay) {
            valueOffset=20;
          }
          if(dayState==SunriseSunset.DayState.DayToNight) {
            valueOffset=20;
          }
          if(dayState==SunriseSunset.DayState.Day) {
            valueOffset=30;
          }
        }
        return valueOffset;
      }

      @Override public void onDestroy(){
        if(locationHelper!=null){
          locationHelper.removeAllLocationListeners();
          locationHelper=null;
        }
        if(scratchBitmap!=null){
          scratchBitmap.recycle();
          scratchBitmap=null;
        }
        if(modelBitmap!=null){
          modelBitmap.recycle();
          modelBitmap=null;
        }
        DownloadService.shutdown(ColorWallpaperService.this);
        super.onDestroy();
      }

      private void updateDayState(){
        if(currentLocation!=null){
          dayState=SunriseSunset.getCurrentDayState(
              currentLocation.getLatitude(),
              currentLocation.getLongitude());
        } else {
          int[] components=DateTimeUtility.getCurrentLocalDateComponents();
          int hour=components[3];
          if(hour<6) {
            dayState=SunriseSunset.DayState.Night;
          } else if(hour<7) {
            dayState=SunriseSunset.DayState.NightToDay;
          } else if(hour<17) {
            dayState=SunriseSunset.DayState.Day;
          } else if(hour<18) {
            dayState=SunriseSunset.DayState.DayToNight;
          } else {
            dayState=SunriseSunset.DayState.Night;
          }
        }
      }

      Bitmap modelBitmap=null;

      private void loadPictureBitmap(){
        if(!prefs.usemodelbg || prefs.picture==null){
          if(modelBitmap!=null){
            modelBitmap.recycle();
            modelBitmap=null;
          }
          return;
        }
        String pic=prefs.picture;
        if(pic.startsWith("file://")){
          Uri uri=Uri.parse(pic);
          pic=uri.getPath();
        }
        if(pic.startsWith("http://") ||
            pic.startsWith("https://")){
          DownloadService.sendRequest(AppManager.getApplication(),pic,
              new IResponseListener<Object>(){

            @Override
            public Object processResponse(String url,
                InputStream stream, IHttpHeaders headers)
                    throws IOException {
              return cacheImage(stream,200,200);
            }

          },new IOnFinishedListener<Object>(){

            @Override
            public void onFinished(String url, Object value,
                IOException exception) {
              if(modelBitmap!=null) {
                modelBitmap.recycle();
              }
              modelBitmap=(value==null) ? null : (Bitmap)value;
            }

          });
        } else {
          new AsyncTask<String,Void,Bitmap>(){

            @Override
            protected Bitmap doInBackground(String... arg0) {
              String arg=arg0[0];
              if(arg==null)return null;
              if(!new File(arg).isFile())
                return null;
              try {
                Bitmap b=cacheImageFile(arg,200,200);
                return b;
              } catch (IOException e) {
                return null;
              }

            }

            @Override
            protected void onPostExecute(Bitmap b){
              if(modelBitmap!=null) {
                modelBitmap.recycle();
              }
              modelBitmap=b;
            }
          }.execute(pic);
        }
      }

      @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
      @Override public void onCreate(SurfaceHolder surface){
        super.onCreate(surface);
        SharedPreferences p=PreferenceManager.getDefaultSharedPreferences(
            AppManager.getApplication());
        prefs=new Preferences();
        prefs.setPreferences(p);
        random=new Random();
        locationHelper=new LocationHelper(AppManager.getApplication());
        locationHelper.setLocationEnabled(prefs.uselocation);
        locationHelper.addLocationListener(new ISimpleLocationListener(){
          @Override
          public void onLocation(Location loc) {
            currentLocation=loc;
            updateDayState();
          }
        });
        listener=new OnSharedPreferenceChangeListener(){
          @Override
          public void onSharedPreferenceChanged(SharedPreferences p, String key) {
            prefs.setPreferences(p);
            if(key.equals("uselocation")){
              locationHelper.setLocationEnabled(prefs.uselocation);
            }
            if(key.equals("picture") || key.equals("usemodelbg")){
              loadPictureBitmap();
            }
          }
        };
        p.registerOnSharedPreferenceChangeListener(listener);
        loadPictureBitmap();
      }
      @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
      private void drawColor2(int color, int x, int y){
        if(!isVisible())return;
        int widthlevel=prefs.boxsize;
        int heightlevel=prefs.boxsize;
        int x1=x-heightlevel+random.nextInt(widthlevel*2);
        int x2=Math.max(4,random.nextInt(widthlevel));
        int y1=y-heightlevel+random.nextInt(heightlevel*2);
        int y2=Math.max(4,random.nextInt(heightlevel));
        Rect r=new Rect(
            Math.max(0,x1-x2),
            Math.max(0,y1-y2),
            Math.min(width,x1+x2),
            Math.min(height,y1+y2));
        int frames=(prefs.fadeinboxes) ? 5 : 1;
        addColorTransition(r,color,frames);
      }
      @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
      private void drawColor(int color){
        if(!isVisible())return;
        int widthlevel=prefs.boxsize;
        int heightlevel=prefs.boxsize;
        int x1=random.nextInt(Math.max(1,width));
        int x2=random.nextInt(widthlevel);
        int y1=random.nextInt(Math.max(1,height));
        int y2=random.nextInt(heightlevel);
        Rect r=new Rect(
            Math.max(0,x1-x2),
            Math.max(0,y1-y2),
            Math.min(width,x1+x2),
            Math.min(height,y1+y2));
        if(modelBitmap!=null){
          int w=modelBitmap.getWidth();
          int h=modelBitmap.getHeight();
          float xmid=r.left+(r.right-r.left)/2;
          float ymid=r.top+(r.bottom-r.top)/2;
          int bitmapx=Math.round(xmid*w*1.0f/Math.max(1,width));
          int bitmapy=Math.round(ymid*h*1.0f/Math.max(1,height));
          if(bitmapx>=w) {
            bitmapx=w-1;
          }
          if(bitmapy>=h) {
            bitmapy=h-1;
          }
          if(bitmapx<0) {
            bitmapx=0;
          }
          if(bitmapy<0) {
            bitmapy=0;
          }
          color=modelBitmap.getPixel(bitmapx,bitmapy);
          float[] hsv=new float[3];
          Color.colorToHSV(color, hsv);
          hsv[0]-=10f;
          hsv[0]+=random.nextInt(20);
          hsv[1]-=0.05f;
          hsv[1]+=(random.nextInt(100)*0.10f/100f);
          hsv[2]-=0.05f;
          hsv[2]+=(random.nextInt(100)*0.10f/100f);
          hsv[2]=hsv[2]*(1.0f-((30-getValueOffset())*0.02f));
          hsv[1]=Math.max(0,Math.min(1,hsv[1]));
          hsv[2]=Math.max(0,Math.min(1,hsv[2]));
          color=Color.HSVToColor(hsv);
        }
        int frames=(prefs.fadeinboxes) ? 5 : 1;
        addColorTransition(r,color,frames);
      }
    };
  }

}




Java Source Code List

com.upokecenter.android.colorwallpaper.ColorWallpaperService.java
com.upokecenter.android.colorwallpaper.LauncherActivity.java
com.upokecenter.android.colorwallpaper.SettingsActivity.java
com.upokecenter.android.location.DummyLocationHelper.java
com.upokecenter.android.location.ILocationHelper.java
com.upokecenter.android.location.ISimpleLocationListener.java
com.upokecenter.android.location.LocationHelper.java
com.upokecenter.android.net.ConnectivityHelper.java
com.upokecenter.android.net.DownloadService.java
com.upokecenter.android.net.IConnectionListener.java
com.upokecenter.android.ui.AlertDialogActivity.java
com.upokecenter.android.ui.AlertDialogPreference.java
com.upokecenter.android.ui.BaseSettingsActivity.java
com.upokecenter.android.ui.ChoosePicturePreference.java
com.upokecenter.android.ui.ColorPickerDialog.java
com.upokecenter.android.ui.ContinuousValuePreference.java
com.upokecenter.android.ui.DialogUtility.java
com.upokecenter.android.ui.GetContentActivity.java
com.upokecenter.android.ui.IChoiceListener.java
com.upokecenter.android.ui.IDialogUpdater.java
com.upokecenter.android.ui.IntentPreference.java
com.upokecenter.android.ui.PreferenceState.java
com.upokecenter.android.ui.ShareActivity.java
com.upokecenter.android.ui.UriPreference.java
com.upokecenter.android.util.AppManager.java
com.upokecenter.android.util.BitmapUtility.java
com.upokecenter.android.util.StorageUtility.java
com.upokecenter.android.wallpaper.BaseWallpaperService.java
com.upokecenter.util.XmlHelper.java