diff options
Diffstat (limited to 'navit/android/src/org/navitproject/navit/Navit.java')
-rw-r--r-- | navit/android/src/org/navitproject/navit/Navit.java | 190 |
1 files changed, 147 insertions, 43 deletions
diff --git a/navit/android/src/org/navitproject/navit/Navit.java b/navit/android/src/org/navitproject/navit/Navit.java index 87ad5d9a8..2fa2f312d 100644 --- a/navit/android/src/org/navitproject/navit/Navit.java +++ b/navit/android/src/org/navitproject/navit/Navit.java @@ -71,14 +71,86 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; - public class Navit extends Activity { + /** + * Nested class storing the intent that was sent to the main navit activity at startup. + **/ + private class StartupIntent { + /** + * Constructor. + * + * @param intent The intent to store in this object + **/ + public StartupIntent(Intent intent) { + mStartupIntent = intent; + mStartupIntentTimestamp = System.currentTimeMillis(); + } + + /** + * Check if the encapsulated intent still valid or too old. + * + * @return true if the encapsulated intent is recent enough + **/ + public boolean isRecentEnough() { + if (mStartupIntent == null) { + return false; + } + /* We consider the intent is valid for 4s */ + return (System.currentTimeMillis() <= getExpirationTimeMillis()); + } + + /** + * Compute the system time when the stored intent will become invalid. + * + * @return The system time for invalidation (in ms) + **/ + private long getExpirationTimeMillis() { + if (mStartupIntent == null) { + return 0; + } + /* We give 4s to navit to process the intent */ + return mStartupIntentTimestamp + 4000L; + } + + /** + * Getter for the encapsulated intent. + * + * @return The encapsulated intent + **/ + public Intent getIntent() { + return mStartupIntent; + } + + /** + * Represent this object as a string. + * + * @return A string containing the summary of the data we store here + **/ + public String toString() { + if (mStartupIntent == null) { + return "{null}"; + } else { + String validForStr; + long remainingValidity = getExpirationTimeMillis() - System.currentTimeMillis(); + if (remainingValidity < 0) { + validForStr = "(expired since " + -remainingValidity + "ms)"; + } else { + validForStr = "(valid for " + remainingValidity + "ms)"; + } + return "{ act=" + mStartupIntent.getAction() + " data=" + mStartupIntent.getDataString() + + " " + validForStr + " }"; + } + } + + private Intent mStartupIntent; /*!< The intent we store */ + private long mStartupIntentTimestamp; /*!< A timestamp (in ms) for when mStartupIntent was recorded */ + } + public static DisplayMetrics sMetrics; public static boolean sShowSoftKeyboardShowing; - private static Intent sStartupIntent; - private static long sStartupIntentTimestamp; + private static StartupIntent sStartupIntent; private static final int MY_PERMISSIONS_REQ_FINE_LOC = 103; private static final int NavitDownloaderSelectMap_id = 967; private static final int NavitAddressSearch_id = 70; @@ -214,6 +286,9 @@ public class Navit extends Activity { return true; } + /** + * Show the first start infoxbox (presentation of navit and link website for more info). + **/ private void showInfos() { SharedPreferences settings = getSharedPreferences(NavitAppConfig.NAVIT_PREFS, MODE_PRIVATE); boolean firstStart = settings.getBoolean("firstStart", true); @@ -260,22 +335,21 @@ public class Navit extends Activity { @Override public void onCreate(Bundle savedInstanceState) { + Log.d(TAG, "OnCreate"); super.onCreate(savedInstanceState); windowSetup(); mDialogs = new NavitDialogs(this); - // only take arguments here, onResume gets called all the time (e.g. when screenblanks, etc.) - Navit.sStartupIntent = this.getIntent(); - // hack! Remember time stamps, and only allow 4 secs. later in onResume to set target! - Navit.sStartupIntentTimestamp = System.currentTimeMillis(); - Log.d(TAG, "**1**A " + sStartupIntent.getAction()); - Log.d(TAG, "**1**D " + sStartupIntent.getDataString()); + /* Only store the startup intent, onResume() gets called all the time (e.g. when screenblanks, etc.) and + will process this intent later on if needed */ + sStartupIntent = new StartupIntent(this.getIntent()); + Log.d(TAG, "Recording intent " + sStartupIntent.toString()); createNotificationChannel(); buildNotification(); verifyPermissions(); - // get the local language ------------- + // get the local language Locale locale = Locale.getDefault(); String lang = locale.getLanguage(); String langc = lang; @@ -426,6 +500,13 @@ public class Navit extends Activity { } @Override + public void onNewIntent(Intent intent) { + Log.d(TAG, "OnNewIntent"); + sStartupIntent = new StartupIntent(intent); + Log.d(TAG, "Recording intent " + sStartupIntent.toString()); + } + + @Override public void onResume() { super.onResume(); Log.d(TAG, "OnResume"); @@ -435,24 +516,33 @@ public class Navit extends Activity { // intent_data = "google.navigation:q=48.25676,16.643"; // intent_data = "google.navigation:ll=48.25676,16.643&q=blabla-strasse"; // intent_data = "google.navigation:ll=48.25676,16.643"; + // intent_data = "geo:48.25676,16.643"; if (sStartupIntent != null) { - if (System.currentTimeMillis() <= Navit.sStartupIntentTimestamp + 4000L) { - Log.d(TAG, "**2**A " + sStartupIntent.getAction()); - Log.d(TAG, "**2**D " + sStartupIntent.getDataString()); - String naviScheme = sStartupIntent.getScheme(); - if (naviScheme != null && naviScheme.equals("google.navigation")) { - parseNavigationURI(sStartupIntent.getData().getSchemeSpecificPart()); + Log.d(TAG, "Using stored startup intent " + sStartupIntent.toString()); + if (sStartupIntent.isRecentEnough()) { + Intent startupIntent = sStartupIntent.getIntent(); + String naviScheme = startupIntent.getScheme(); + if (naviScheme != null) { + if (naviScheme.equals("google.navigation")) { + parseNavigationURI(startupIntent.getData().getSchemeSpecificPart()); + } else if (naviScheme.equals("geo") + && startupIntent.getAction().equals("android.intent.action.VIEW")) { + invokeCallbackOnGeo(startupIntent.getData().getSchemeSpecificPart(), + NavitGraphics.MsgType.CLB_SET_DESTINATION, + ""); + } } } else { - Log.e(TAG, "timestamp for navigate_to expired! not using data"); + Log.e(TAG, "timestamp for startup intent expired! not using data"); } + sStartupIntent = null; } } @Override public void onPause() { super.onPause(); - Log.d(TAG, "onPause"); + Log.d(TAG, "OnPause"); } @Override @@ -476,6 +566,42 @@ public class Navit extends Activity { } } + /** + * Invoke NavitGraphics.sCallbackHandler on a geographical position + * + * @param geoString A string containing the target geographical position with a format like "48.25676,16.643" + * @param msgType The type of message to send to the callback (see NavitGraphics.MsgType for possible values) + * @param name The name/label to associate to the geographical position + **/ + private void invokeCallbackOnGeo(String geoString, NavitGraphics.MsgType msgType, String name) { + String[] geo = geoString.split(","); + if (geo.length == 2) { + try { + Bundle b = new Bundle(); + Float lat = Float.valueOf(geo[0]); + Float lon = Float.valueOf(geo[1]); + b.putFloat("lat", lat); + b.putFloat("lon", lon); + b.putString("q", name); + Message msg = Message.obtain(NavitGraphics.sCallbackHandler, + msgType.ordinal()); + + msg.setData(b); + msg.sendToTarget(); + Log.d(TAG, "target found (b): " + geoString); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + } else { + Log.w(TAG, "Ignoring invalid geo string: " + geoString); + } + } + + /** + * Parse google navigation URIs (usually starting with "google.navigation:") and take the appropriate actions + * + * @param schemeSpecificPart A string containing the URI scheme, for example "ll=48.25676,16.643&q=blabla-strasse" + **/ private void parseNavigationURI(String schemeSpecificPart) { String[] naviData = schemeSpecificPart.split("&"); Pattern p = Pattern.compile("(.*)=(.*)"); @@ -492,39 +618,17 @@ public class Navit extends Activity { // c: google.navigation:ll=48.25676,16.643 // b: google.navigation:q=48.25676,16.643 - float lat; - float lon; - Bundle b = new Bundle(); - String geoString = params.get("ll"); + String address = null; if (geoString != null) { - String address = params.get("q"); - if (address != null) { - b.putString("q", address); - } + address = params.get("q"); } else { geoString = params.get("q"); } if (geoString != null) { if (geoString.matches("^[+-]{0,1}\\d+(|\\.\\d*),[+-]{0,1}\\d+(|\\.\\d*)$")) { - String[] geo = geoString.split(","); - if (geo.length == 2) { - try { - lat = Float.valueOf(geo[0]); - lon = Float.valueOf(geo[1]); - b.putFloat("lat", lat); - b.putFloat("lon", lon); - Message msg = Message.obtain(NavitGraphics.sCallbackHandler, - NavitGraphics.MsgType.CLB_SET_DESTINATION.ordinal()); - - msg.setData(b); - msg.sendToTarget(); - Log.i(TAG, "target found (b): " + geoString); - } catch (NumberFormatException e) { - e.printStackTrace(); - } - } + invokeCallbackOnGeo(geoString, NavitGraphics.MsgType.CLB_SET_DESTINATION, address); } else { start_targetsearch_from_intent(geoString); } |