Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
6.10 Update Map Intent
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent f4f385f commit 6f18226
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package com.example.android.sunshine.app;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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;
Expand All @@ -38,6 +40,7 @@
* Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
*/
public class ForecastFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String LOG_TAG = ForecastFragment.class.getSimpleName();
private ForecastAdapter mForecastAdapter;

private ListView mListView;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/menu/forecastfragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_refresh"
android:title="@string/action_refresh"
<item android:id="@+id/action_map"
android:title="@string/action_map"
app:showAsAction="never" />
<!--<item android:id="@+id/action_refresh"-->
<!--android:title="@string/action_refresh"-->
<!--app:showAsAction="never" />-->
</menu>
8 changes: 3 additions & 5 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.android.sunshine.app.MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/action_map"
android:title="@string/action_map"
app:showAsAction="never" />
</menu>
</menu>

0 comments on commit 6f18226

Please sign in to comment.