My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
MyTracksApi  
MyTracks API allows third-party Android applications to access MyTracks data and start/stop a recording.
Featured, Phase-Implementation
Updated Oct 13, 2011 by jshih@google.com

Introduction

MyTracks API consists of three mechanisms, content provider, service, and notifications, to allow third-party apps to access and control MyTracks data.

Access to the MyTracks API is protected through the Android permissions. In addition to the permissions, MyTracks has a setting inside the MyTracks app to allow users to enable and revoke access to third-party apps. To modify the setting, from the MyTracks menu, go to Settings -> Sharing -> Allow access.

Note that Android will only grant permissions to a third-party apps if MyTracks is already installed when installing the third-party app.

Through the content provider, third-party apps can read/write the MyTracks database. Through the service, these apps can start/stop the MyTracks recording service. Finally, through the notifications, these apps can get notified when a recording has started/stopped.

Setup

MyTracks provides the MyTracksLib to make it easier for third-party apps to access MyTracks.

The following assumes you are using Eclipse for your Android development.

  1. Download the MyTracks code to get MyTracksLib
  2. hg clone https://mytracks.googlecode.com/hg mytracks
  3. Import the MyTracksLib project
    • File->Import...
    • General->Existing Projects into Workspace
    • Select root directory: mytracks/MyTracksLib
    • Finish
  4. Clean and rebuild the MyTracksLib project
  5. Add MyTracksLib to your Android app
    • Project->Properties
    • Android
    • Under the "Library" section
    • Add...
    • pick MyTracksLib
    • OK
  6. Reference protobuf-java-2.3.0-lite.jar in your Android app
    • Project->Properties
    • Java Build Path
    • Libraries
    • Add JARs...
    • MyTracksLib/libs/protobuf-java-2.3.0-lite.jar
    • OK

MyTracks Content Provider

  1. Add the following permission to AndroidManifest.xml for read access
  2. <uses-permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA" />
  3. Add the following permission to AndroidManifest.xml for write access
  4. <uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" />
  5. Sample

You can access the content provider by getting a ContentResolver, through getContentResolver, and calling the various methods like query and insert and passing in the appropriate URIs. However, MyTracks provides an utility library, MyTracksProviderUtils, to make it easier to invoke the content provider.

  private MyTracksProviderUtils myTracksProviderUtils;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
  }
  
  @Override
  protected void onStart() {
    super.onStart();

    List<Track> tracks = myTracksProviderUtils.getAllTracks();
    for (Track track : tracks) {
      System.out.println("Track id " + track.getId());
    }
  }

MyTracks Service

  1. Add the following permission to AndroidManifest.xml
  2.  <uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" />
  3. Sample

To use the service, you need to first start the service, then bind it. Afterwards, you can invoke the various methods of the service. To cleanup, you need to unbind and stop the service.

  private ITrackRecordingService myTracksService;
 
  private Intent intent;

  private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
      myTracksService = ITrackRecordingService.Stub.asInterface(service);
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
      myTracksService = null;
    }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    intent = new Intent();
    intent.setComponent(new ComponentName(
        getString(R.string.mytracks_service_package),
        getString(R.string.mytracks_service_class)));
  }
  
  @Override
  protected void onStart() {
    super.onStart();

    startService(intent);
    bindService(intent, serviceConnection, 0);
  }

  @Override
  protected void onStop() {
    super.onStop();
    
    if (myTracksService != null) {
      unbindService(serviceConnection);
    }
    stopService(intent);
  }
  
  /*
   * Note that bindService is asynchronous. After it completes,
   * then can invoke the service's methods.
   */
  myTracksService.startNewTrack();
  myTracksService.endCurrentTrack();

MyTracks Notifications

  1. Add the following permission to AndroidManifest.xml
  2. <uses-permission android:name="com.google.android.apps.mytracks.TRACK_NOTIFICATIONS" />
  3. Sample
  4. public class MyTracksReceiver extends BroadcastReceiver {
    
      @Override
      public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L);
        Toast.makeText(context, action + " " + trackId, Toast.LENGTH_LONG).show();
      }
    }

Sample App

See MyTracksApiSample on the top level directory of the MyTracks code base.

Comment by umranium...@gmail.com, Nov 29, 2011

Currently, MyTracks? has the "Allow access" option in the sharing settings that governs whether or not third-party applications can start/stop MyTracks? recording. It's good that the setting is there since it protects MyTracks? from being misused by third-party applications. But at the same time, it causes a lot of problems for third-party applications that wish to make use of MyTracks?. The main problem is that users just install the third-party apps, and forget to set that option. Is there a way for the third-party apps to query whether or not the "allow access" option has been set? If there was a way, we could query then remind the user to go to the MyTracks? settings and allow access. Without that our applications just seem to fail from the user's point of view, just to find out later that it was because they needed to set that option.

Comment by sc...@blomqui.st, Apr 24, 2012

It seems that to get the MyTracks? Service sample to work, I have to request the READ_TRACK_DATA permission as well. Any reason I should expect otherwise?


Sign in to add a comment
Powered by Google Project Hosting