Android Open Source - WiseRadar Radar Loader






From Project

Back to project page WiseRadar.

License

The source code is released under:

Apache License

If you think the Android project WiseRadar 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 wiseguys.radar;
// w  w w .  j  ava  2 s .c  om
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.widget.ImageView;
import android.widget.TextView;

public class RadarLoader extends AsyncTask<String, String, LayerDrawable> {

  private String selectedRadarCode;
  private String selectedDuration;
  private ImageFetcher imgFetch;
  private SharedPreferences sharedPrefs;
  private Context context;
  private Resources resources;
  private ImageView sImage;
  private TextView name;
  private String originalName;
  private AnimationDrawable anim;
  
  public RadarLoader(Context c, Resources r, ImageView sImage, TextView name) {
    context = c;
    resources = r;
    anim = null;
    this.sImage = sImage;
    this.name = name;
    this.originalName = name.getText().toString();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
  }
  
  @Override
  protected LayerDrawable doInBackground(String... radarCode) {
    selectedRadarCode = radarCode[0];
    selectedDuration = radarCode[1];
    
    return loadRadar();
  }
  
  /**
     * Loads the radar screen
     */
  private LayerDrawable loadRadar() {
    //Generate list of radar base images
    imgFetch = ImageFetcher.getImageFetcher();
    List<Bitmap> images;
      
    if (selectedRadarCode == null) {
        return null;
      }
      
      publishProgress(resources.getString(R.string.dlFetch));

      int colours = Integer.valueOf(sharedPrefs.getString("pref_radar_colour","14"));
      images = imgFetch.getRadarImages(selectedRadarCode,selectedDuration,colours);
      
      if (images == null) {
        publishProgress(resources.getString(R.string.dlFailure));
        return null;
      }
      
      publishProgress(resources.getString(R.string.dlReceived));
      
      //Drop Images into an animation
      anim = new AnimationDrawable();
      
      if (!imgFetch.finished()) {
        return null;
      }
      
      for (int i = images.size()-1; i >= 0; i--) {
        anim.addFrame(new BitmapDrawable(context.getResources(),images.get(i)), 750);
      }

      publishProgress(resources.getString(R.string.dlOverlays));
      
      //Generate Overlays
      Bitmap overlay = imgFetch.getOverlays(selectedRadarCode,sharedPrefs,context);
      
      publishProgress(resources.getString(R.string.dlSorting));
      
      LayerDrawable layers;
      
      if (overlay != null) {
        //Layer the overlay ontop of the animated radar images
        Drawable[] layering = new Drawable[2];
        BitmapDrawable overlayBitmap = new BitmapDrawable(resources,overlay); //Convert Bitmap to drawable
        int calculatedOffset = images.get(0).getWidth() - overlay.getWidth(); //our overlay doesn't sit over the radar properly. Offset to the left to uncover legend
        layering[0] = anim;
        layering[1] = overlayBitmap;
        layers = new LayerDrawable(layering);
        
        layers.setLayerInset(1, 0, 0, calculatedOffset, 0);
      } else {
        //No layers selected!
        Drawable[] layering = new Drawable[1];
        layering[0] = anim;
        layers = new LayerDrawable(layering);
      }
      
      publishProgress(resources.getString(R.string.dlFinished));
      
      return layers;
  }
  
  @Override
  protected void onPostExecute (LayerDrawable result) {
    
    if (result == null) {
      name.setText(resources.getString(R.string.noRadar));
      
      //Back out of updates if we don't have a running animated object
      if (anim == null) {
        return;
      }
    }
    
    sImage.setImageDrawable(result);
    name.setText(originalName);
    
      anim.setOneShot(false);
      anim.start();
  }
  
  @Override
  protected void onPreExecute () {
    sImage.setImageResource(R.drawable.radar);
    name.setText(originalName);
  }
  
  @Override
  protected void onProgressUpdate (String... values) {
    name.setText(values[0]);    
  }

}




Java Source Code List

wiseguys.radar.ImageFetcher.java
wiseguys.radar.RadarHelper.java
wiseguys.radar.RadarLoader.java
wiseguys.radar.conn.GPSHelper.java
wiseguys.radar.conn.ImageDownloaderThread.java
wiseguys.radar.conn.SourceFetcherThread.java
wiseguys.radar.helpers.GPSHelper.java
wiseguys.radar.helpers.RadarHelper.java
wiseguys.radar.ui.AboutFragment.java
wiseguys.radar.ui.MainActivity.java
wiseguys.radar.ui.PrefFragment.java
wiseguys.radar.ui.RadarFragment.java
wiseguys.radar.ui.adapter.Compat.java
wiseguys.radar.ui.adapter.IPhotoView.java
wiseguys.radar.ui.adapter.PhotoViewAttacher.java
wiseguys.radar.ui.adapter.PhotoView.java
wiseguys.radar.ui.adapter.SDK16.java
wiseguys.radar.ui.adapter.ScrollerProxy.java
wiseguys.radar.ui.adapter.VersionedGestureDetector.java