Passing track IDs as URIs instead of extras.

This commit is contained in:
Rodrigo Damazio
2011-05-25 20:13:49 -03:00
parent 8942c31a5c
commit a15932cc7f
7 changed files with 125 additions and 21 deletions
+12 -2
View File
@@ -122,11 +122,21 @@ limitations under the License.
<activity android:name="com.google.android.apps.mytracks.io.sendtogoogle.SendActivity"
android:label="@string/sendtogoogle_title"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:noHistory="true" />
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.track" />
</intent-filter>
</activity>
<activity android:name="com.google.android.apps.mytracks.io.file.SaveActivity"
android:label="@string/tracklist_write_to_sd"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:noHistory="true" />
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.SAVE" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.track" />
</intent-filter>
</activity>
<activity android:name="com.google.android.apps.mytracks.io.file.ImportActivity"
android:label="@string/import_title"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
@@ -33,6 +33,9 @@ public abstract class Constants {
*/
public static final String SDCARD_TOP_DIR = "MyTracks";
// Intent actions
public static final String ACTION_SAVE = "android.intent.action.SAVE";
/*
* onActivityResult request codes:
*/
@@ -26,6 +26,7 @@ import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.apps.mytracks.util.UriUtils;
import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.maps.mytracks.R;
@@ -39,6 +40,7 @@ import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
@@ -275,9 +277,11 @@ public class MyTracks extends TabActivity implements OnTouchListener {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
if ((Intent.ACTION_VIEW.equals(action) || Intent.ACTION_EDIT.equals(action))
&& TracksColumns.CONTENT_ITEMTYPE.equals(intent.getType())) {
long trackId = ContentUris.parseId(intent.getData());
&& TracksColumns.CONTENT_ITEMTYPE.equals(intent.getType())
&& UriUtils.matchesContentUri(data, TracksColumns.CONTENT_URI)) {
long trackId = ContentUris.parseId(data);
dataHub.loadTrack(trackId);
}
}
@@ -20,6 +20,7 @@ import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.DialogManager;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.util.UriUtils;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
@@ -65,21 +66,16 @@ public class ImportActivity extends Activity {
Intent intent = getIntent();
String action = intent.getAction();
if (!Intent.ACTION_VIEW.equals(action) &&
!Intent.ACTION_ATTACH_DATA.equals(action)) {
Log.e(TAG, "Received an intent with unsupported action: " + intent);
Uri data = intent.getData();
if (!(Intent.ACTION_VIEW.equals(action) || Intent.ACTION_ATTACH_DATA.equals(action))
|| !UriUtils.isFileUri(data)) {
Log.e(TAG, "Received an intent with unsupported action or data: " + intent);
finish();
return;
}
if (!"file".equals(intent.getScheme())) {
Log.e(TAG, "Received a VIEW intent with unsupported scheme " + intent.getScheme());
finish();
return;
}
Log.i(TAG, "Received a VIEW intent with file scheme. Importing.");
startTrackImport(intent.getData().getPath());
Log.i(TAG, "Importing GPX file at " + data);
startTrackImport(data.getPath());
}
private void startTrackImport(final String fileName) {
@@ -15,20 +15,27 @@
*/
package com.google.android.apps.mytracks.io.file;
import static com.google.android.apps.mytracks.Constants.ACTION_SAVE;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.DialogManager;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.util.UriUtils;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import java.io.File;
@@ -38,7 +45,6 @@ import java.io.File;
* @author Rodrigo Damazio
*/
public class SaveActivity extends Activity {
public static final String EXTRA_TRACK_ID = "track_id";
public static final String EXTRA_SHARE_FILE = "share_file";
public static final String EXTRA_FILE_FORMAT = "file_format";
@@ -60,7 +66,19 @@ public class SaveActivity extends Activity {
super.onStart();
Intent intent = getIntent();
trackId = intent.getLongExtra(EXTRA_TRACK_ID, -1);
String action = intent.getAction();
String type = intent.getType();
Uri data = intent.getData();
if (!ACTION_SAVE.equals(action) ||
!TracksColumns.CONTENT_ITEMTYPE.equals(type) ||
!UriUtils.matchesContentUri(data, TracksColumns.CONTENT_URI)) {
Log.e(TAG, "Got bad save intent: " + intent);
finish();
return;
}
trackId = ContentUris.parseId(data);
int formatIdx = intent.getIntExtra(EXTRA_FILE_FORMAT, -1);
format = TrackFileFormat.values()[formatIdx];
shareFile = intent.getBooleanExtra(EXTRA_SHARE_FILE, false);
@@ -162,8 +180,11 @@ public class SaveActivity extends Activity {
shareFile = true;
}
Uri uri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, trackId);
Intent intent = new Intent(ctx, SaveActivity.class);
intent.putExtra(EXTRA_TRACK_ID, trackId);
intent.setAction(ACTION_SAVE);
intent.setDataAndType(uri, TracksColumns.CONTENT_ITEMTYPE);
intent.putExtra(EXTRA_FILE_FORMAT, exportFormat.ordinal());
intent.putExtra(EXTRA_SHARE_FILE, shareFile);
ctx.startActivity(intent);
@@ -25,6 +25,7 @@ import com.google.android.apps.mytracks.MyMapsList;
import com.google.android.apps.mytracks.ProgressIndicator;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.io.AuthManager;
import com.google.android.apps.mytracks.io.AuthManagerFactory;
import com.google.android.apps.mytracks.io.SendToDocs;
@@ -33,16 +34,19 @@ import com.google.android.apps.mytracks.io.SendToFusionTables.OnSendCompletedLis
import com.google.android.apps.mytracks.io.SendToMyMaps;
import com.google.android.apps.mytracks.io.mymaps.MapsFacade;
import com.google.android.apps.mytracks.io.mymaps.MyMapsConstants;
import com.google.android.apps.mytracks.util.UriUtils;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
@@ -60,7 +64,6 @@ import java.util.List;
*/
public class SendActivity extends Activity implements ProgressIndicator {
public static final String EXTRA_SHARE_LINK = "share_link";
public static final String EXTRA_TRACK_ID = "track_id";
// Services
private MyTracksProviderUtils providerUtils;
@@ -108,7 +111,19 @@ public class SendActivity extends Activity implements ProgressIndicator {
super.onStart();
resetState();
sendTrackId = getIntent().getLongExtra(EXTRA_TRACK_ID, -1);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Uri data = intent.getData();
if (!Intent.ACTION_SEND.equals(action) ||
!TracksColumns.CONTENT_ITEMTYPE.equals(type) ||
!UriUtils.matchesContentUri(data, TracksColumns.CONTENT_URI)) {
Log.e(TAG, "Got bad send intent: " + intent);
finish();
return;
}
sendTrackId = ContentUris.parseId(data);
sendDialog = new SendDialog(this);
sendDialog.setOwnerActivity(this);
@@ -625,8 +640,11 @@ public class SendActivity extends Activity implements ProgressIndicator {
}
public static void sendToGoogle(Context ctx, long trackId, boolean shareLink) {
Uri uri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, trackId);
Intent intent = new Intent(ctx, SendActivity.class);
intent.putExtra(SendActivity.EXTRA_TRACK_ID, trackId);
intent.setAction(Intent.ACTION_SEND);
intent.setDataAndType(uri, TracksColumns.CONTENT_ITEMTYPE);
intent.putExtra(SendActivity.EXTRA_SHARE_LINK, shareLink);
ctx.startActivity(intent);
}
@@ -0,0 +1,52 @@
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.util;
import android.net.Uri;
import java.util.List;
/**
* Utilities for dealing with content and other types of URIs.
*
* @author Rodrigo Damazio
*/
public class UriUtils {
public static boolean matchesContentUri(Uri uri, Uri baseContentUri) {
if (uri == null) return false;
// Check that scheme and authority are the same.
if (!uri.getScheme().equals(baseContentUri.getScheme())) return false;
if (!uri.getAuthority().equals(baseContentUri.getAuthority())) return false;
// Checks that all the base path components are in the URI.
List<String> uriPathSegments = uri.getPathSegments();
List<String> basePathSegments = baseContentUri.getPathSegments();
if (basePathSegments.size() > uriPathSegments.size()) return false;
for (int i = 0; i < basePathSegments.size(); i++) {
if (!uriPathSegments.get(i).equals(basePathSegments.get(i))) return false;
}
return true;
}
public static boolean isFileUri(Uri uri) {
return "file".equals(uri.getScheme());
}
private UriUtils() {}
}