From 17c495bc9742fc65ef9f7d461d692e1f3b592d0e Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Wed, 16 May 2012 10:17:31 -0700 Subject: [PATCH] Add track description when sharing a url or a file with friends. --- MyTracks/res/values/strings.xml | 53 ++++++++++--------- .../apps/mytracks/MarkerDetailActivity.java | 4 +- .../content/DescriptionGeneratorImpl.java | 46 +++++++++++----- .../ChooseActivityDialogFragment.java | 5 +- .../apps/mytracks/io/file/KmlTrackWriter.java | 2 +- .../apps/mytracks/io/file/SaveActivity.java | 10 ++-- .../SendFusionTablesAsyncTask.java | 3 +- .../mytracks/io/maps/SendMapsAsyncTask.java | 3 +- .../io/sendtogoogle/UploadResultActivity.java | 2 +- .../apps/mytracks/util/IntentUtils.java | 44 +++++++++++++-- .../content/DescriptionGenerator.java | 3 +- .../content/DescriptionGeneratorImplTest.java | 8 +-- .../mytracks/io/file/KmlTrackWriterTest.java | 2 +- 13 files changed, 123 insertions(+), 62 deletions(-) diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index 1cd15b994..e1d885e3e 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -630,6 +630,12 @@ limitations under the License. translation_description="The text label for entering a description."> Description + + Description: %1$s + Error @@ -638,6 +644,12 @@ limitations under the License. translation_description="The text label for entering a name."> Name + + Name: %1$s + OK @@ -924,18 +936,6 @@ limitations under the License. user that the marker will be permanently deleted from the phone."> The marker will be permanently deleted from the phone. - - Name: %1$s - - - Description: %1$s - - I think you might be interested in this track.\n\nTo view this track use Google Earth or other - mapping software. - Select an application Share track file + + I think you might be interested in this track.\n\nTo view this track, use Google Earth or other + mapping software.\n\n%1$s + Share track URL + + I think you might be interested in this track:\n\n%1$s\n\n%2$s + I would like to share a track with you - - I think you might be interested in this track:\n\n%1$s - distances, Vector elevations) { + Track track, Vector distances, Vector elevations, boolean html) { + String paragraphSeparator = html ? HTML_PARAGRAPH_SEPARATOR : TEXT_PARAGRAPH_SEPARATOR; + String lineBreak = html ? HTML_LINE_BREAK : TEXT_LINE_BREAK; StringBuilder builder = new StringBuilder(); // Created by - String url = context.getString(R.string.my_tracks_web_url); - builder.append(context.getString( - R.string.send_google_by_my_tracks, "", "")); - builder.append("

"); + String beginAnchor = html + ? "" + : ""; + String endAnchor = html ? "" : ""; + builder.append(context.getString(R.string.send_google_by_my_tracks, beginAnchor, endAnchor)); + builder.append(paragraphSeparator); - builder.append(generateTripStatisticsDescription(track.getStatistics(), true)); - - // Activity type - String trackCategory = track.getCategory(); - String category = trackCategory != null && trackCategory.length() > 0 ? trackCategory - : context.getString(R.string.value_unknown); - builder.append(context.getString(R.string.description_activity_type, category)); - builder.append(HTML_LINE_BREAK); + writeString(track.getName(), builder, R.string.generic_name_line, lineBreak); + writeString(track.getCategory(), builder, R.string.description_activity_type, lineBreak); + writeString(track.getDescription(), builder, R.string.generic_description_line, lineBreak); + builder.append(generateTripStatisticsDescription(track.getStatistics(), html)); // Elevation chart - if (distances != null && elevations != null) { + if (html && distances != null && elevations != null) { builder.append(""); builder.append(HTML_LINE_BREAK); @@ -78,6 +80,22 @@ public class DescriptionGeneratorImpl implements DescriptionGenerator { return generateTripStatisticsDescription(waypoint.getStatistics(), false); } + /** + * Writes a string to a string builder. + * + * @param text the string + * @param builder the string builder + * @param resId the resource id containing one string placeholder + * @param lineBreak the line break + */ + private void writeString(String text, StringBuilder builder, int resId, String lineBreak) { + if (text == null || text.length() == 0) { + text = context.getString(R.string.value_unknown); + } + builder.append(context.getString(resId, text)); + builder.append(lineBreak); + } + /** * Generates a description for a {@link TripStatistics}. * diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseActivityDialogFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseActivityDialogFragment.java index e3666b2d9..0224ea61c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseActivityDialogFragment.java +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChooseActivityDialogFragment.java @@ -109,9 +109,10 @@ public class ChooseActivityDialogFragment extends DialogFragment { String packageName = activityInfo.applicationInfo.packageName; String className = activityInfo.name; + long trackId = getArguments().getLong(KEY_TRACK_ID); String trackUrl = getArguments().getString(KEY_TRACK_URL); if (trackUrl == null) { - SendRequest sendRequest = new SendRequest(getArguments().getLong(KEY_TRACK_ID)); + SendRequest sendRequest = new SendRequest(trackId); sendRequest.setSendMaps(true); sendRequest.setNewMap(true); sendRequest.setSharingAppPackageName(packageName); @@ -122,7 +123,7 @@ public class ChooseActivityDialogFragment extends DialogFragment { dismiss(); } else { Intent intent = IntentUtils.newShareUrlIntent( - getActivity(), trackUrl, packageName, className); + getActivity(), trackId, trackUrl, packageName, className); startActivity(intent); getActivity().finish(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java index 1b8cfcb8e..dff35b1f6 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/KmlTrackWriter.java @@ -197,7 +197,7 @@ public class KmlTrackWriter implements TrackFormatWriter { printWriter.println(""); printWriter.println(""); String name = context.getString(R.string.marker_label_end, track.getName()); - String description = descriptionGenerator.generateTrackDescription(track, null, null); + String description = descriptionGenerator.generateTrackDescription(track, null, null, false); writePlacemark(name, description, END_STYLE, lastLocation); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java index 85419ae0f..46cf90e12 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java @@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.io.file; import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat; import com.google.android.apps.mytracks.util.DialogUtils; +import com.google.android.apps.mytracks.util.IntentUtils; import com.google.android.maps.mytracks.R; import android.app.Activity; @@ -135,14 +136,11 @@ public class SaveActivity extends Activity { R.string.share_track_share_file, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Intent intent = new Intent(Intent.ACTION_SEND) - .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(savedPath))) - .putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_track_subject)) - .putExtra(Intent.EXTRA_TEXT, getString(R.string.share_track_file_body_format)) - .putExtra(getString(R.string.track_id_broadcast_extra), trackId) - .setType(trackFileFormat.getMimeType()); + Intent intent = IntentUtils.newShareFileIntent( + SaveActivity.this, trackId, savedPath, trackFileFormat); startActivity( Intent.createChooser(intent, getString(R.string.share_track_picker_title))); + finish(); } }); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/fusiontables/SendFusionTablesAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/fusiontables/SendFusionTablesAsyncTask.java index b93493ca8..12eb3b4ef 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/fusiontables/SendFusionTablesAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/fusiontables/SendFusionTablesAsyncTask.java @@ -277,7 +277,8 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask { elevations.add(elevationBuffer.getAverage()); DescriptionGenerator descriptionGenerator = new DescriptionGeneratorImpl(context); track.setDescription("

" + track.getDescription() + "

" - + descriptionGenerator.generateTrackDescription(track, distances, elevations) + "

"); + + descriptionGenerator.generateTrackDescription(track, distances, elevations, true) + + "

"); String name = context.getString(R.string.marker_label_end, track.getName()); if (!createNewPoint(name, track.getDescription(), lastLocation, MARKER_TYPE_END)) { Log.d(TAG, "Unable to create the end marker"); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java index 8d952c5bb..84719a86f 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java @@ -353,7 +353,8 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask { String getTrackDescription(Track track, Vector distances, Vector elevations) { DescriptionGenerator descriptionGenerator = new DescriptionGeneratorImpl(context); return "

" + track.getDescription() + "

" - + descriptionGenerator.generateTrackDescription(track, distances, elevations) + "

"; + + descriptionGenerator.generateTrackDescription(track, distances, elevations, true) + + "

"; } /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/UploadResultActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/UploadResultActivity.java index 2a31b235d..30f54e092 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/UploadResultActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/UploadResultActivity.java @@ -59,7 +59,7 @@ public class UploadResultActivity extends FragmentActivity { if (sendRequest.isSendMaps() && sendRequest.isMapsSuccess()) { shareUrl = SendMapsUtils.getMapUrl(getTrack()); if (sendRequest.getSharingAppPackageName() != null) { - Intent intent = IntentUtils.newShareUrlIntent(this, shareUrl, + Intent intent = IntentUtils.newShareUrlIntent(this, sendRequest.getTrackId(), shareUrl, sendRequest.getSharingAppPackageName(), sendRequest.getSharingAppClassName()); startActivity(intent); finish(); diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/IntentUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/IntentUtils.java index fc303efe5..6fa180706 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/IntentUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/IntentUtils.java @@ -16,11 +16,18 @@ package com.google.android.apps.mytracks.util; +import com.google.android.apps.mytracks.content.DescriptionGeneratorImpl; +import com.google.android.apps.mytracks.content.MyTracksProviderUtils; +import com.google.android.apps.mytracks.content.Track; +import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat; import com.google.android.maps.mytracks.R; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.net.Uri; + +import java.io.File; /** * Utilities for creating intents. @@ -47,21 +54,50 @@ public class IntentUtils { } /** - * Creates an intent to share a url with a sharing app. + * Creates an intent to share a track url with an app. * * @param context the context - * @param url the url + * @param trackId the track id + * @param trackUrl the track url * @param packageName the sharing app package name * @param className the sharing app class name */ public static final Intent newShareUrlIntent( - Context context, String url, String packageName, String className) { + Context context, long trackId, String trackUrl, String packageName, String className) { + Track track = MyTracksProviderUtils.Factory.get(context).getTrack(trackId); + String trackDescription = new DescriptionGeneratorImpl(context).generateTrackDescription( + track, null, null, false); + return new Intent(Intent.ACTION_SEND) .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) .putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_track_subject)) .putExtra(Intent.EXTRA_TEXT, TWITTER_PACKAGE_NAME.equals(packageName) - ? url : context.getString(R.string.share_track_url_body_format, url)) + ? trackUrl + : context.getString(R.string.share_track_share_url_body, trackUrl, trackDescription)) .setComponent(new ComponentName(packageName, className)) .setType(TEXT_PLAIN_TYPE); } + + /** + * Creates an intent to share a track file with an app. + * + * @param context the context + * @param trackId the track id + * @param filePath the file path + * @param trackFileFormat the track file format + */ + public static final Intent newShareFileIntent( + Context context, long trackId, String filePath, TrackFileFormat trackFileFormat) { + Track track = MyTracksProviderUtils.Factory.get(context).getTrack(trackId); + String trackDescription = new DescriptionGeneratorImpl(context).generateTrackDescription( + track, null, null, false); + + return new Intent(Intent.ACTION_SEND) + .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))) + .putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_track_subject)) + .putExtra(Intent.EXTRA_TEXT, + context.getString(R.string.share_track_share_file_body, trackDescription)) + .putExtra(context.getString(R.string.track_id_broadcast_extra), trackId) + .setType(trackFileFormat.getMimeType()); + } } diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/DescriptionGenerator.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/DescriptionGenerator.java index 083354de1..7cc3cb5c0 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/DescriptionGenerator.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/DescriptionGenerator.java @@ -31,9 +31,10 @@ public interface DescriptionGenerator { * @param track the track * @param distances a vector of distances to generate the elevation chart * @param elevations a vector of elevations to generate the elevation chart + * @param html true to output html, false to output plain text */ public String generateTrackDescription( - Track track, Vector distances, Vector elevations); + Track track, Vector distances, Vector elevations, boolean html); /** * Generate a waypoint description. diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/content/DescriptionGeneratorImplTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/content/DescriptionGeneratorImplTest.java index 019ac51e1..a0ca29eb2 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/content/DescriptionGeneratorImplTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/content/DescriptionGeneratorImplTest.java @@ -59,6 +59,9 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase { track.setCategory("hiking"); String expected = "Created by" + " My Tracks on Android.

" + + "Name: -
" + + "Activity type: hiking
" + + "Description: -
" + "Total distance: 20.00 km (12.4 mi)
" + "Total time: 10:00
" + "Moving time: 05:00
" @@ -73,9 +76,8 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase { + "Elevation gain: 6000 m (19685 ft)
" + "Max grade: 42 %
" + "Min grade: 11 %
" - + "Recorded: " + StringUtils.formatDateTime(getContext(), START_TIME) + "
" - + "Activity type: hiking
"; - assertEquals(expected, descriptionGenerator.generateTrackDescription(track, null, null)); + + "Recorded: " + StringUtils.formatDateTime(getContext(), START_TIME) + "
"; + assertEquals(expected, descriptionGenerator.generateTrackDescription(track, null, null, true)); } /** diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/KmlTrackWriterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/KmlTrackWriterTest.java index 93c5a9aa2..9ebefcdbb 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/KmlTrackWriterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/KmlTrackWriterTest.java @@ -43,7 +43,7 @@ public class KmlTrackWriterTest extends TrackFormatWriterTest { private class FakeDescriptionGenerator implements DescriptionGenerator { @Override public String generateTrackDescription( - Track aTrack, Vector distances, Vector elevations) { + Track aTrack, Vector distances, Vector elevations, boolean html) { return FULL_TRACK_DESCRIPTION; }