From 6f182266a0e98c0183d2e93c0317304091b5d85b Mon Sep 17 00:00:00 2001 From: Lyla Date: Fri, 6 Feb 2015 00:20:58 -0800 Subject: [PATCH] 6.10 Update Map Intent --- .../sunshine/app/ForecastFragment.java | 37 ++++++++++++++++++- .../android/sunshine/app/MainActivity.java | 25 ------------- app/src/main/res/menu/forecastfragment.xml | 7 +++- app/src/main/res/menu/main.xml | 8 ++-- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java index 4eb22313f..cf90ce406 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.java @@ -15,6 +15,7 @@ */ package com.example.android.sunshine.app; +import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -22,6 +23,7 @@ import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -38,6 +40,7 @@ * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout. */ public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks { + public static final String LOG_TAG = ForecastFragment.class.getSimpleName(); private ForecastAdapter mForecastAdapter; private ListView mListView; @@ -112,10 +115,15 @@ public boolean onOptionsItemSelected(MenuItem item) { // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); - if (id == R.id.action_refresh) { - updateWeather(); +// if (id == R.id.action_refresh) { +// updateWeather(); +// return true; +// } + if (id == R.id.action_map) { + openPreferredLocationInMap(); return true; } + return super.onOptionsItemSelected(item); } @@ -183,6 +191,31 @@ private void updateWeather() { SunshineSyncAdapter.syncImmediately(getActivity()); } + private void openPreferredLocationInMap() { + // Using the URI scheme for showing a location found on a map. This super-handy + // intent can is detailed in the "Common Intents" page of Android's developer site: + // http://developer.android.com/guide/components/intents-common.html#Maps + if ( null != mForecastAdapter ) { + Cursor c = mForecastAdapter.getCursor(); + if ( null != c ) { + c.moveToPosition(0); + String posLat = c.getString(COL_COORD_LAT); + String posLong = c.getString(COL_COORD_LONG); + Uri geoLocation = Uri.parse("geo:" + posLat + "," + posLong); + + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(geoLocation); + + if (intent.resolveActivity(getActivity().getPackageManager()) != null) { + startActivity(intent); + } else { + Log.d(LOG_TAG, "Couldn't call " + geoLocation.toString() + ", no receiving apps installed!"); + } + } + + } + } + @Override public void onSaveInstanceState(Bundle outState) { // When tablets rotate, the currently selected list item needs to be saved. diff --git a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java index e643283e5..5337580c2 100644 --- a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java @@ -19,7 +19,6 @@ import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -84,33 +83,9 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } - if (id == R.id.action_map) { - openPreferredLocationInMap(); - return true; - } return super.onOptionsItemSelected(item); } - private void openPreferredLocationInMap() { - String location = Utility.getPreferredLocation(this); - - // Using the URI scheme for showing a location found on a map. This super-handy - // intent can is detailed in the "Common Intents" page of Android's developer site: - // http://developer.android.com/guide/components/intents-common.html#Maps - Uri geoLocation = Uri.parse("geo:0,0?").buildUpon() - .appendQueryParameter("q", location) - .build(); - - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(geoLocation); - - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(intent); - } else { - Log.d(LOG_TAG, "Couldn't call " + location + ", no receiving apps installed!"); - } - } - @Override protected void onResume() { super.onResume(); diff --git a/app/src/main/res/menu/forecastfragment.xml b/app/src/main/res/menu/forecastfragment.xml index 50bd1256d..5a3d1b82a 100644 --- a/app/src/main/res/menu/forecastfragment.xml +++ b/app/src/main/res/menu/forecastfragment.xml @@ -2,7 +2,10 @@ - + + + \ No newline at end of file diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index 87d2ed662..e418331fe 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -1,11 +1,9 @@ + xmlns:tools="http://schemas.android.com/tools" + tools:context="com.example.android.sunshine.app.MainActivity" > - - + \ No newline at end of file