From 745a01e91230db498ae77adae3241f2edee83570 Mon Sep 17 00:00:00 2001 From: Lyla Date: Wed, 17 Dec 2014 14:35:34 -0800 Subject: [PATCH] 3.12 Add map intent from new menu item --- .../android/sunshine/app/MainActivity.java | 34 +++++++++++++++++++ app/src/main/res/menu/detail.xml | 8 +++-- app/src/main/res/menu/main.xml | 9 +++-- app/src/main/res/values/strings.xml | 1 + 4 files changed, 47 insertions(+), 5 deletions(-) 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 213d94f06..d2e5e50fe 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 @@ -16,13 +16,19 @@ package com.example.android.sunshine.app; import android.content.Intent; +import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { + private final String LOG_TAG = MainActivity.class.getSimpleName(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -54,6 +60,34 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } + if (id == R.id.action_map) { + openPreferredLocationInMap(); + return true; + } return super.onOptionsItemSelected(item); } + + private void openPreferredLocationInMap() { + SharedPreferences sharedPrefs = + PreferenceManager.getDefaultSharedPreferences(this); + String location = sharedPrefs.getString( + getString(R.string.pref_location_key), + getString(R.string.pref_location_default)); + + // 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!"); + } + } } diff --git a/app/src/main/res/menu/detail.xml b/app/src/main/res/menu/detail.xml index 6e04aa16a..865ac0539 100644 --- a/app/src/main/res/menu/detail.xml +++ b/app/src/main/res/menu/detail.xml @@ -1,7 +1,9 @@ - + tools:context="com.example.android.sunshine.app.DetailActivity" > + diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index b1cb90811..87d2ed662 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -1,6 +1,11 @@ - + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c943a20f..98a5d3944 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ typically from the action bar. The ActionBar is limited real estate, so shorter is better. --> Settings + Map Location Refresh