Android Open Source - abstract-art Create Image Cache Preference






From Project

Back to project page abstract-art.

License

The source code is released under:

GNU General Public License

If you think the Android project abstract-art 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 net.georgewhiteside.android.abstractart.settings;
//w  w w  . ja va2  s  .  c  o  m
import net.georgewhiteside.android.abstractart.R;
import net.georgewhiteside.android.abstractart.Renderer;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Toast;

public class CreateImageCachePreference extends Preference
{
  Context context;
  Renderer renderer;
  
  public CreateImageCachePreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    renderer = new Renderer(context);
  }
  
  public CreateImageCachePreference(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.dialogPreferenceStyle);
  }
  
  public CreateImageCachePreference(Context context) {
    this(context, null, android.R.attr.dialogPreferenceStyle);
  }
  
  @Override
    protected void onClick() {
        //super.onClick();
        //Log.i("Layer", "a click! :)");
    new GenerateImageCacheTask().execute(23, 23, null);
    }
  
  // TODO: this is a slight hack... it's easier at the moment to simply load all the backgrounds with their extra
  // data than it is to iterate over the ~100 unique images
  class GenerateImageCacheTask extends AsyncTask<Integer, Integer, Void> {
    
    private ProgressDialog dialog;
    private int numberOfBackgrounds = renderer.getBackgroundsTotal();
    private int loaded = 0;

    @Override
    protected Void doInBackground(Integer... params) {
      loaded = 0;
      
      long startTime = SystemClock.uptimeMillis();
      
      for(int i = 0; i < numberOfBackgrounds; i++)
      {
        renderer.cacheImage(i);
        publishProgress( i );
        loaded++;
        
        if(isCancelled()) {
          break;
        }
      }
      
      long endTime = SystemClock.uptimeMillis();
      
      Log.i("cacheTime", "total cache time: " + ((float)endTime - startTime) / 1000 + "s");

      return null;
    }
    
    @Override
    protected void onCancelled()
    {
      displayMessage();
    }
    
    @Override
    protected void onPreExecute()
    {
      dialog = new ProgressDialog(context);
      dialog.setIndeterminate(false);
      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      dialog.setMax(numberOfBackgrounds);
      dialog.setTitle(R.string.processing);
      //dialog.setMessage("");
      dialog.setCancelable(true);
          dialog.setOnCancelListener(new OnCancelListener() {
              public void onCancel(DialogInterface dialog) {
                  cancel(true);
              }
          });
          
          dialog.show();
          
          //Toast.makeText(context, "Press your back button to cancel.", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    protected void onPostExecute(Void result)
    {
      dialog.dismiss();
      displayMessage();
    }
    
    @Override
    protected void onProgressUpdate(Integer... progress) {
      
      dialog.setProgress(progress[0]);
    }
    
    private void displayMessage()
    {
      String message; 
      Resources res = context.getResources();
      
      if(loaded == numberOfBackgrounds) {
        message = res.getString(R.string.processed_all_backgrounds);
      } else {
        message = String.format(res.getString(R.string.processed_n_of_n_backgrounds), loaded, numberOfBackgrounds);
      }

      Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
  }
}




Java Source Code List

net.georgewhiteside.android.abstractart.AbstractArt.java
net.georgewhiteside.android.abstractart.BattleBackground.java
net.georgewhiteside.android.abstractart.BattleGroup.java
net.georgewhiteside.android.abstractart.Cache.java
net.georgewhiteside.android.abstractart.Distortion.java
net.georgewhiteside.android.abstractart.Enemy.java
net.georgewhiteside.android.abstractart.FPSCounter.java
net.georgewhiteside.android.abstractart.GLOffscreenSurface.java
net.georgewhiteside.android.abstractart.ImageLoader.java
net.georgewhiteside.android.abstractart.Layer.java
net.georgewhiteside.android.abstractart.Renderer.java
net.georgewhiteside.android.abstractart.RomUtil.java
net.georgewhiteside.android.abstractart.ServiceDialog.java
net.georgewhiteside.android.abstractart.Settings.java
net.georgewhiteside.android.abstractart.ShaderFactory.java
net.georgewhiteside.android.abstractart.Translation.java
net.georgewhiteside.android.abstractart.UniformGridView.java
net.georgewhiteside.android.abstractart.Wallpaper.java
net.georgewhiteside.android.abstractart.settings.BackgroundSelector.java
net.georgewhiteside.android.abstractart.settings.ClearCachePreference.java
net.georgewhiteside.android.abstractart.settings.CreateImageCachePreference.java
net.georgewhiteside.android.abstractart.settings.FrameRatePreference.java
net.georgewhiteside.android.abstractart.settings.ThumbnailAdapter.java
net.georgewhiteside.utility.Dimension.java
net.starmen.pkhack.HackModule.java
org.jf.GLWallpaper.GLWallpaperService.java
sheetrock.panda.changelog.ChangeLog.java