Beispiel #1
0
  /**
   * Find if a GPS dst is valid
   *
   * @return
   */
  public static boolean isGPSValid(String dst) {
    if (dst.contains("&")) {
      String tokens[] = dst.split("&");

      try {
        double lon = Double.parseDouble(tokens[1]);
        double lat = Double.parseDouble(tokens[0]);
        if ((Helper.isLatitudeSane(lat)) && (Helper.isLongitudeSane(lon))) {
          return true;
        }
      } catch (Exception e) {
      }
    }
    return false;
  }
Beispiel #2
0
  /**
   * @param name
   * @param type
   */
  private void parseGps(String name, String type) {
    /*
     * GPS
     * GPS coordinates are either x&y (user), or addr@x&y (google maps)
     * get the x&y part, then parse them to lon=y lat=x
     */
    if (name.contains("&")) {
      String token[] = new String[2];
      token[1] = token[0] = name;
      if (name.contains("@")) {
        /*
         * This could be the geo point from maps
         */
        token = name.split("@");
      }
      /*
       * This is lon/lat destination
       */
      String tokens[] = token[1].split("&");

      try {
        mLond = Double.parseDouble(tokens[1]);
        mLatd = Double.parseDouble(tokens[0]);
      } catch (Exception e) {
        /*
         * Bad input from user on GPS
         */
        mName = "";
        mDestType = "";
        return;
      }

      /*
       * Sane input
       */
      if ((!Helper.isLatitudeSane(mLatd)) || (!Helper.isLongitudeSane(mLond))) {
        mName = "";
        mDestType = "";
        return;
      }
      mName = token[0];
      mDestType = type;
    }
  }
Beispiel #3
0
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected Boolean doInBackground(Object... vals) {

      Thread.currentThread().setName("Destination");

      Boolean guess = (Boolean) vals[0];
      String dbType = (String) vals[1];

      /*
       * If we dont know type, find with a guess.
       */
      if (guess) {
        StringPreference s = mService.getDBResource().searchOne(mName);
        if (null == s) {
          return false;
        }
        mDestType = s.getType();
        mName = s.getId();
      }

      /*
       * If GPS/Maps, parse
       */
      if (mName.contains("&")) {
        parseGps(mName, mDestType);
      }

      mWinds = mService.getDBResource().getWindsAloft(mLond, mLatd);

      if (mDestType.equals(UDW)) {
        Waypoint p = mService.getUDWMgr().get(mName);
        if (null != p) {
          mLatd = p.getLat();
          mLond = p.getLon();
          mCmt = p.getCmt();
          mParams.put(DataBaseHelper.LONGITUDE, "" + mLond);
          mParams.put(DataBaseHelper.LATITUDE, "" + mLatd);
          mParams.put(DataBaseHelper.FACILITY_NAME, UDWMgr.UDWDESCRIPTION);
          addTime();
          mAfdFound = null;
          mFound = true;
          mLooking = false;
          mDbType = UDW;
          mTrackShape.updateShape(new GpsParams(getLocationInit()), Destination.this);
          return true;
        }
        return false;
      }

      if (mDestType.equals(GPS)) {
        /*
         * For GPS coordinates, simply put parsed lon/lat in params
         * No need to query database
         */
        mParams = new LinkedHashMap<String, String>();
        mFreq = new LinkedHashMap<String, String>();
        mAwos = new LinkedList<Awos>();
        mParams.put(DataBaseHelper.LONGITUDE, "" + mLond);
        mParams.put(DataBaseHelper.LATITUDE, "" + mLatd);
        mParams.put(DataBaseHelper.FACILITY_NAME, GPS);
        addTime();
        mAfdFound = null;
        mFound = true;
        mLooking = false;
        mDbType = GPS;
        mTrackShape.updateShape(new GpsParams(getLocationInit()), Destination.this);
        if (!isGPSValid(mName)) {
          mFound = false;
        }
        if (!mName.contains("&")) {
          /*
           * This comes from MAPS to GPS for user edited
           */
          mName += "@" + mLatd + "&" + mLond;
        }
        return true;
      }

      if (null == mDataSource) {
        return false;
      }

      /*
       * For Google maps address, if we have already geo decoded it using internet,
       * then no need to do again because internet may not be available on flight.
       * It could be coming from storage and not google maps.
       */
      if (mDestType.equals(MAPS)) {

        if (mLond == 0 && mLatd == 0) {
          /*
           * We have already decomposed it?
           * No.
           */
          String strAddress = mName;

          Geocoder coder = new Geocoder(mService);
          Address location = null;

          /*
           * Decompose
           */
          try {
            List<Address> address = coder.getFromLocationName(strAddress, 1);
            if (address != null) {
              location = address.get(0);
            }
          } catch (Exception e) {
            return false;
          }

          if (null == location) {
            return false;
          }

          /*
           * Decomposed it
           *
           */
          try {
            mLond = Helper.truncGeo(location.getLongitude());
            mLatd = Helper.truncGeo(location.getLatitude());
          } catch (Exception e) {

          }
          if ((!Helper.isLatitudeSane(mLatd)) || (!Helper.isLongitudeSane(mLond))) {
            return false;
          }
        }
        /*
         * Common stuff
         */
        mParams = new LinkedHashMap<String, String>();
        mFreq = new LinkedHashMap<String, String>();
        mAwos = new LinkedList<Awos>();
        mAfdFound = null;
        mDbType = mDestType;
        mParams.put(DataBaseHelper.TYPE, mDestType);
        mParams.put(DataBaseHelper.FACILITY_NAME, mName);
        mParams.put(DataBaseHelper.LONGITUDE, "" + mLond);
        mParams.put(DataBaseHelper.LATITUDE, "" + mLatd);
        addTime();
        mName += "@" + mLatd + "&" + mLond;
        return true;
      }

      /*
       * For all others, find in DB
       */
      mDataSource.findDestination(mName, mDestType, dbType, mParams, mRunways, mFreq, mAwos);

      if (mDestType.equals(BASE)) {

        /*
         * Find A/FD
         */
        mAfdFound = null;
        final LinkedList<String> afdName = mDataSource.findAFD(mName);
        if (afdName.size() > 0) {
          FilenameFilter filter =
              new FilenameFilter() {
                public boolean accept(File directory, String fileName) {
                  boolean match = false;
                  for (final String name : afdName) {
                    match |=
                        fileName.matches(name + Preferences.IMAGE_EXTENSION)
                            || fileName.matches(name + "-[0-9]+" + Preferences.IMAGE_EXTENSION);
                  }
                  return match;
                }
              };
          String afd[] = null;
          afd = new File(mPref.mapsFolder() + "/afd/").list(filter);
          if (null != afd) {
            java.util.Arrays.sort(afd);
            int len1 = afd.length;
            String tmp1[] = new String[len1];
            for (int count = 0; count < len1; count++) {
              /*
               * Add A/FD
               */
              String tokens[] = afd[count].split(Preferences.IMAGE_EXTENSION);
              tmp1[count] = mPref.mapsFolder() + "/afd/" + tokens[0];
            }
            if (len1 > 0) {
              mAfdFound = tmp1;
            }
          }
        }
      }

      return (!mParams.isEmpty());
    }