forked from upstream-mirrors/OpenTracks
Addressing review suggestions for third-party apps.
This commit is contained in:
@@ -113,23 +113,29 @@
|
|||||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||||
|
|
||||||
<!-- Exported permissions (for other apps to use our features) -->
|
<!-- Exported permissions (for other apps to use our features) -->
|
||||||
|
<!-- Permission to get track start/end broadcasts -->
|
||||||
<permission android:name="com.google.android.apps.mytracks.TRACK_NOTIFICATIONS"
|
<permission android:name="com.google.android.apps.mytracks.TRACK_NOTIFICATIONS"
|
||||||
android:permissionGroup="android.permission-group.LOCATION"
|
android:permissionGroup="android.permission-group.LOCATION"
|
||||||
android:label="@string/notification_permission_label"
|
android:label="@string/notification_permission_label"
|
||||||
android:description="@string/notification_permission_description"
|
android:description="@string/notification_permission_description"
|
||||||
android:protectionLevel="normal" />
|
android:protectionLevel="normal" />
|
||||||
|
<!-- Permission to read raw track data (points, etc.) -->
|
||||||
<permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA"
|
<permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA"
|
||||||
android:permissionGroup="android.permission-group.LOCATION"
|
android:permissionGroup="android.permission-group.LOCATION"
|
||||||
android:label="@string/read_tracks_permission_label"
|
android:label="@string/read_tracks_permission_label"
|
||||||
android:description="@string/read_tracks_permission_description"
|
android:description="@string/read_tracks_permission_description"
|
||||||
android:protectionLevel="dangerous" />
|
android:protectionLevel="dangerous" />
|
||||||
|
<!-- Permission to write track data, either raw or by making service calls -->
|
||||||
<permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA"
|
<permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA"
|
||||||
android:permissionGroup="android.permission-group.LOCATION"
|
android:permissionGroup="android.permission-group.LOCATION"
|
||||||
android:label="@string/write_tracks_permission_label"
|
android:label="@string/write_tracks_permission_label"
|
||||||
android:description="@string/write_tracks_permission_description"
|
android:description="@string/write_tracks_permission_description"
|
||||||
android:protectionLevel="dangerous" />
|
android:protectionLevel="dangerous" />
|
||||||
|
|
||||||
|
<!-- We read and write raw track data, obviously. -->
|
||||||
<uses-permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA" />
|
<uses-permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA" />
|
||||||
<uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" />
|
<uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" />
|
||||||
|
<uses-permission android:name="com.google.android.apps.mytracks.TRACK_NOTIFICATIONS" />
|
||||||
|
|
||||||
<!-- Features required -->
|
<!-- Features required -->
|
||||||
<uses-feature android:name="android.hardware.location" android:required="true"/>
|
<uses-feature android:name="android.hardware.location" android:required="true"/>
|
||||||
|
|||||||
@@ -70,18 +70,18 @@ public class TrackRecordingService extends Service implements LocationListener {
|
|||||||
|
|
||||||
static final int MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS = 3;
|
static final int MAX_AUTO_RESUME_TRACK_RETRY_ATTEMPTS = 3;
|
||||||
|
|
||||||
// Broadcast-related constants
|
// Broadcast-related constants.
|
||||||
private static final String NOTIFICATION_PERMISSION =
|
static final String NOTIFICATION_PERMISSION =
|
||||||
"com.google.android.apps.mytracks.TRACK_NOTIFICATIONS";
|
"com.google.android.apps.mytracks.TRACK_NOTIFICATIONS";
|
||||||
private static final String START_TRACK_ACTION =
|
static final String START_TRACK_ACTION =
|
||||||
"com.google.android.apps.mytracks.TRACK_STARTED";
|
"com.google.android.apps.mytracks.TRACK_STARTED";
|
||||||
private static final String PAUSE_TRACK_ACTION =
|
static final String PAUSE_TRACK_ACTION =
|
||||||
"com.google.android.apps.mytracks.TRACK_PAUSED";
|
"com.google.android.apps.mytracks.TRACK_PAUSED";
|
||||||
private static final String RESUME_TRACK_ACTION =
|
static final String RESUME_TRACK_ACTION =
|
||||||
"com.google.android.apps.mytracks.TRACK_RESUMED";
|
"com.google.android.apps.mytracks.TRACK_RESUMED";
|
||||||
private static final String STOP_TRACK_ACTION =
|
static final String STOP_TRACK_ACTION =
|
||||||
"com.google.android.apps.mytracks.TRACK_STOPPED";
|
"com.google.android.apps.mytracks.TRACK_STOPPED";
|
||||||
private static final String TRACK_ID_EXTRA =
|
static final String TRACK_ID_EXTRA =
|
||||||
"com.google.android.apps.mytracks.TRACK_ID";
|
"com.google.android.apps.mytracks.TRACK_ID";
|
||||||
|
|
||||||
private NotificationManager notificationManager;
|
private NotificationManager notificationManager;
|
||||||
@@ -1049,8 +1049,8 @@ public class TrackRecordingService extends Service implements LocationListener {
|
|||||||
// Persist the current recording track.
|
// Persist the current recording track.
|
||||||
prefManager.setRecordingTrack(recordingTrackId);
|
prefManager.setRecordingTrack(recordingTrackId);
|
||||||
|
|
||||||
// Notify the world that we're now recording
|
// Notify the world that we're now recording.
|
||||||
sendTrackBroadcast(START_TRACK_ACTION);
|
sendTrackBroadcast(START_TRACK_ACTION, recordingTrackId);
|
||||||
|
|
||||||
return recordingTrackId;
|
return recordingTrackId;
|
||||||
}
|
}
|
||||||
@@ -1081,17 +1081,19 @@ public class TrackRecordingService extends Service implements LocationListener {
|
|||||||
"_id=" + recordingTrack.getId(), null);
|
"_id=" + recordingTrack.getId(), null);
|
||||||
}
|
}
|
||||||
showNotification();
|
showNotification();
|
||||||
|
long recordedTrackId = recordingTrackId;
|
||||||
prefManager.setRecordingTrack(recordingTrackId = -1);
|
prefManager.setRecordingTrack(recordingTrackId = -1);
|
||||||
releaseWakeLock();
|
releaseWakeLock();
|
||||||
|
|
||||||
// Notify the world that we're no longer recording.
|
// Notify the world that we're no longer recording.
|
||||||
sendTrackBroadcast(STOP_TRACK_ACTION);
|
sendTrackBroadcast(STOP_TRACK_ACTION, recordedTrackId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendTrackBroadcast(String action) {
|
private void sendTrackBroadcast(String action, long trackId) {
|
||||||
Intent broadcastIntent = new Intent();
|
Intent broadcastIntent =
|
||||||
broadcastIntent.setAction(action);
|
new Intent()
|
||||||
broadcastIntent.putExtra(TRACK_ID_EXTRA, recordingTrackId);
|
.setAction(action)
|
||||||
|
.putExtra(TRACK_ID_EXTRA, trackId);
|
||||||
sendBroadcast(broadcastIntent, NOTIFICATION_PERMISSION);
|
sendBroadcast(broadcastIntent, NOTIFICATION_PERMISSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+84
@@ -26,10 +26,12 @@ import com.google.android.apps.mytracks.stats.TripStatistics;
|
|||||||
import com.google.android.apps.mytracks.util.ApiFeatures;
|
import com.google.android.apps.mytracks.util.ApiFeatures;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.ContextWrapper;
|
import android.content.ContextWrapper;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.SharedPreferences.Editor;
|
import android.content.SharedPreferences.Editor;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
@@ -39,7 +41,9 @@ import android.test.ServiceTestCase;
|
|||||||
import android.test.mock.MockContentResolver;
|
import android.test.mock.MockContentResolver;
|
||||||
import android.test.suitebuilder.annotation.MediumTest;
|
import android.test.suitebuilder.annotation.MediumTest;
|
||||||
import android.test.suitebuilder.annotation.SmallTest;
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -312,8 +316,57 @@ public class TrackRecordingServiceTest
|
|||||||
assertEquals(-1, service.getRecordingTrackId());
|
assertEquals(-1, service.getRecordingTrackId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronous/waitable broadcast receiver to be used in testing.
|
||||||
|
*/
|
||||||
|
private class BlockingBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
private static final long MAX_WAIT_TIME_MS = 10000;
|
||||||
|
private List<Intent> receivedIntents = new ArrayList<Intent>();
|
||||||
|
|
||||||
|
public List<Intent> getReceivedIntents() {
|
||||||
|
return receivedIntents;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
Log.d("MyTracksTest", "Got broadcast: " + intent);
|
||||||
|
synchronized (receivedIntents) {
|
||||||
|
receivedIntents.add(intent);
|
||||||
|
receivedIntents.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean waitUntilReceived(int receiveCount) {
|
||||||
|
long deadline = System.currentTimeMillis() + MAX_WAIT_TIME_MS;
|
||||||
|
synchronized (receivedIntents) {
|
||||||
|
while (receivedIntents.size() < receiveCount) {
|
||||||
|
try {
|
||||||
|
// Wait releases synchronized lock until it returns
|
||||||
|
receivedIntents.wait(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
if (System.currentTimeMillis() > deadline) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
public void testStartNewTrack_noRecording() throws Exception {
|
public void testStartNewTrack_noRecording() throws Exception {
|
||||||
|
// NOTICE: due to the way Android permissions work, if this fails,
|
||||||
|
// uninstall the test apk then retry - the test must be installed *after*
|
||||||
|
// My Tracks (go figure).
|
||||||
|
// Reference: http://code.google.com/p/android/issues/detail?id=5521
|
||||||
|
BlockingBroadcastReceiver startReceiver = new BlockingBroadcastReceiver();
|
||||||
|
context.registerReceiver(startReceiver,
|
||||||
|
new IntentFilter(TrackRecordingService.START_TRACK_ACTION));
|
||||||
|
|
||||||
List<Track> tracks = providerUtils.getAllTracks();
|
List<Track> tracks = providerUtils.getAllTracks();
|
||||||
assertTrue(tracks.isEmpty());
|
assertTrue(tracks.isEmpty());
|
||||||
|
|
||||||
@@ -329,6 +382,19 @@ public class TrackRecordingServiceTest
|
|||||||
assertEquals(id, sharedPreferences.getLong(
|
assertEquals(id, sharedPreferences.getLong(
|
||||||
context.getString(R.string.recording_track_key), -1));
|
context.getString(R.string.recording_track_key), -1));
|
||||||
assertEquals(id, service.getRecordingTrackId());
|
assertEquals(id, service.getRecordingTrackId());
|
||||||
|
|
||||||
|
// Verify that the start broadcast was received.
|
||||||
|
assertTrue(startReceiver.waitUntilReceived(1));
|
||||||
|
List<Intent> receivedIntents = startReceiver.getReceivedIntents();
|
||||||
|
assertEquals(1, receivedIntents.size());
|
||||||
|
Intent broadcastIntent = receivedIntents.get(0);
|
||||||
|
assertEquals(
|
||||||
|
TrackRecordingService.START_TRACK_ACTION,
|
||||||
|
broadcastIntent.getAction());
|
||||||
|
assertEquals(id,
|
||||||
|
broadcastIntent.getLongExtra(TrackRecordingService.TRACK_ID_EXTRA, -1));
|
||||||
|
|
||||||
|
context.unregisterReceiver(startReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
@@ -351,6 +417,11 @@ public class TrackRecordingServiceTest
|
|||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
public void testEndCurrentTrack_alreadyRecording() throws Exception {
|
public void testEndCurrentTrack_alreadyRecording() throws Exception {
|
||||||
|
// See comment above if this fails randomly.
|
||||||
|
BlockingBroadcastReceiver stopReceiver = new BlockingBroadcastReceiver();
|
||||||
|
context.registerReceiver(stopReceiver,
|
||||||
|
new IntentFilter(TrackRecordingService.STOP_TRACK_ACTION));
|
||||||
|
|
||||||
createDummyTrack(123, -1, true);
|
createDummyTrack(123, -1, true);
|
||||||
|
|
||||||
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
ITrackRecordingService service = bindAndGetService(createStartIntent());
|
||||||
@@ -362,6 +433,19 @@ public class TrackRecordingServiceTest
|
|||||||
assertEquals(-1, sharedPreferences.getLong(
|
assertEquals(-1, sharedPreferences.getLong(
|
||||||
context.getString(R.string.recording_track_key), 0));
|
context.getString(R.string.recording_track_key), 0));
|
||||||
assertEquals(-1, service.getRecordingTrackId());
|
assertEquals(-1, service.getRecordingTrackId());
|
||||||
|
|
||||||
|
// Verify that the stop broadcast was received.
|
||||||
|
assertTrue(stopReceiver.waitUntilReceived(1));
|
||||||
|
List<Intent> receivedIntents = stopReceiver.getReceivedIntents();
|
||||||
|
assertEquals(1, receivedIntents.size());
|
||||||
|
Intent broadcastIntent = receivedIntents.get(0);
|
||||||
|
assertEquals(
|
||||||
|
TrackRecordingService.STOP_TRACK_ACTION,
|
||||||
|
broadcastIntent.getAction());
|
||||||
|
assertEquals(123,
|
||||||
|
broadcastIntent.getLongExtra(TrackRecordingService.TRACK_ID_EXTRA, -1));
|
||||||
|
|
||||||
|
context.unregisterReceiver(stopReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
|
|||||||
+3
@@ -49,5 +49,8 @@ public class SignalStrengthPreferences extends PreferenceActivity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: Check that my tracks is installed - if not, give a warning and
|
||||||
|
// offer to go to the android market.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user