Android Open Source - WiseRadar Radar Helper






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;
/*from ww  w  . j a  v  a 2  s. c o m*/
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wiseguys.radar.conn.ImageDownloaderThread;
import wiseguys.radar.conn.SourceFetcherThread;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;

import org.jsoup.nodes.Document;

public class RadarHelper {
  
  public static final String baseURL = "http://weather.gc.ca";
  //http://weather.gc.ca/radar/index_e.html?id=<CODE>
  public static final int TEN_MINUTES = 600000;
    public static Location latestLocation;
  
  public static String codeToName(String code, Context systemContext) {
    //Test to make sure we're ready to accept it
    if (systemContext == null) {
      return null;
    }
    
    if (code == null) {
      return null;
    }
    
    String[] radarCodes = systemContext.getResources().getStringArray(R.array.radar_codes);
    String[] radarNames = systemContext.getResources().getStringArray(R.array.radar_cities);
    int index;
    for (index = 0; index < radarCodes.length; index++) {
      if (radarCodes[index].equals(code)) {
        break;
      }
    }
    
    //Couldn't find it. Invalid entry!
    if (index == radarCodes.length) {
      throw new IllegalArgumentException();
    }
    
    
    return radarNames[index];
  }
  
  /**
   * Breaks apart the page source to retrieve gif images
   * @param code HTML code of page we're looking to parse
   * @return a collated collection of gif image names from the URL source
   */
  public static List<String> parseRadarImages(Document code) {
    List<String> imageURLs = new ArrayList<String>();



        /*
    if (!code.contains("image-list-ol")) {
      return null;
    }

    String temp = code.substring(code.indexOf("<ol class=\"image-list-ol\">"));
    temp = temp.substring(0,temp.indexOf("</ol>"));

    //At times, the Env. Canada page does not have available data
    if (!temp.contains("<li>")) {
      return null;
    }

    temp = temp.substring(temp.indexOf("<li><a href"),temp.lastIndexOf("</li>"));

    Pattern p = Pattern.compile("display=(.*GIF)&amp");
    Matcher m = p.matcher(temp);

    while (m.find()) {
      String imgName = m.group(1);
      imageURLs.add(imgName);
    }*/
    
    return imageURLs;
  }
  
  public static Bitmap GetCanadaWideImage(Resources r) {
    ImageDownloaderThread imgDown;
    SourceFetcherThread fetcher = new SourceFetcherThread();
    fetcher.setBaseFetch();    
    Document basicSource;

    try {
      fetcher.start();
      fetcher.join();
      basicSource = fetcher.getSource();
      List<String> allImages = parseRadarImages(basicSource);
      
      imgDown = new ImageDownloaderThread(baseURL + allImages.get(0));
      imgDown.start();
      imgDown.join();
    } catch (Exception ie) {    
      return BitmapFactory.decodeResource(r, R.drawable.radar);
    }
    return imgDown.getImage();
  }
}




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