Basic integration

With the Consumer SDK, you can show the ETA and status messages for a Task, and track progress in real-time on a map. This guide covers these objectives, by using the HTConsumerClient and HTMapFragment classes.

Instantiate consumer client

For calling the methods in ConsumerSDK, you have to use HTConsumerClient. To instantiate HTConsumerClient, call HTConsumerClient.getInstance(getApplicationContext()).

HTConsumerClient hTConsumerClient = HTConsumerClient.getInstance(getApplicationContext());

[warning] Disable Default ActionBar for Application Theme

In case your AppTheme adds an ActionBar by default, add the following under your AppTheme style-tag in styles.xml file. This is to disable default Action Bar in Activities in order to enable support for Android AppBar. Refer to Android documentation on Setting up the AppBar.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- We will be using the toolbar so no need to show ActionBar -->
        <item name="windowActionBar">false&lt;/item>
        <item name="windowNoTitle">true&lt;/item>
          ...
</style>

Map View for a Task

The Consumer SDK provides the HTMapFragment class to enable real time tracking on a map. Use the HTConsumerClient.trackTask method and pass the relevant taskID.

Step 1

Firstly, from where you want to start the Activity containing ConsumerSDK's MapView, Call HTConsumerClient.trackTask() with either a taskID or a List of taskIDs as depicted below and handle the Success/Error cases appropriately to handle following cases:

  • Not opening Activity containing ConsumerSDK's MapView on error in trackTask call such as No Internet Connection.
  • Not opening Activity containing ConsumerSDK's MapView if Task has not started yet (Task status: HTTask.TASK_STATUS_NOT_STARTED). Once the status property returns HTConsumerClient.TASK_STATUS_DISPATCHING you can instantiate HTMapFragment and show the view to track the realtime location on a map.
trackButton.setOnClickListener(new View.OnClickListener() {
    @Override
      public void onClick(View v) {
          ...
        trackButtonLoader.setVisibility(View.VISIBLE);

          HTConsumerClient htConsumerClient = HTConsumerClient.getInstance(getApplicationContext());
          htConsumerClient.trackTask(taskID, getActivity(), new HTTaskCallBack() {
          @Override
          public void onSuccess(HTTask task) {
                // Handle trackTask success here
                ...
              trackButtonLoader.setVisibility(View.VISIBLE);

                // Start Activity containing HTMapFragment
                Intent intent = new Intent(getActivity(), HyperTrackMapActivity.class);
                startActivity(intent);
          }

          @Override
          public void onError(Exception exception) {
                // Handle trackTask error here
                trackButtonLoader.setVisibility(View.GONE);
          }
    });
}

Step 2

Second, add the following xml snippet in your view layout, to enable the fragment.

<fragment
  android:id="@+id/htMapfragment"
  android:name="io.hypertrack.lib.consumer.view.HTMapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:layout="@layout/fragment_map" />

Step 3

Third, instantiate HTMapFragment in the onCreate method of the activity in which map fragment has been included.

htMapFragment = (HTMapFragment)getSupportFragmentManager()
  .findFragmentById(R.id.htMapfragment);

Step 4

Fourth, to be able to communicate and control the settings for HTMapFragment, you need to create a class overriding HTMapAdapter. This HTMapAdapter class instance then needs to be set using the HTMapFragment.setHTMapAdapter() method.

private class MyMapAdapter extends HTMapAdapter {
  public MyMapAdapter(Context mContext) {
    super(mContext);
  }

  @Override
    public List<String> getTaskIDsToTrack(HTMapFragment mapFragment) {
    // Return a list of TaskIDs that needs to be tracked on the MapFragment
    ArrayList<String> taskIDsToTrack = new ArrayList<>();
    taskIDsToTrack.add(taskID);
    return taskIDsToTrack;
  }
}

Step 5

Optionally, if you want to get callback on various events which happen on HTMapFragment, you need to instantiate an implementation of the class HTMapFragmentCallback. This instance then needs to be set using the HTMapFragment.setMapFragmentCallback() method. For more callback methods, please refer to the reference.

private HTMapFragmentCallback callback = new HTMapFragmentCallback() {
  @Override
  public void onHeroMarkerAdded(HTMapFragment mapFragment, String taskID, Marker heroMarker) {
    Toast.makeText(context, "Hero Marker Added callback", Toast.LENGTH_SHORT).show();
  }
};

Step 6

Once all this is done, the code snippet would look like as below and once you have the taskIDs to be tracked on the map, your are ready to track tasks on the map.

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ...

  htMapFragment = (HTMapFragment) getSupportFragmentManager().findFragmentById(R.id.htMapfragment);
  adapter = new OrderStatusMapAdapter(this);
  htHTMapFragment.setHTMapAdapter(adapter);
  htHTMapFragment.setMapFragmentCallback(callback);

  ...
}

Register for Task Updates

Get Task Details Updates

To track or use updates in Task Details such as ETA, Destination etc, register a BroadcastReceiver to the intent HTConsumerClient.TASK_DETAIL_REFRESHED_NOTIFICATION.

// TaskDetailRefreshed Listener to be registered
BroadcastReceiver taskDetailRefreshedListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
          if (intent != null && intent.hasExtra(HTConsumerClient.INTENT_EXTRA_TASK_ID_LIST)) {
              ArrayList<String> taskIDList = intent.getStringArrayListExtra(HTConsumerClient.INTENT_EXTRA_TASK_ID_LIST);

              if (taskIDList != null) {
                  // Handle Task Detail updates here
                  String taskID = taskIDList.get(0);

                  String connectionStatus = htConsumerClient.getConnectionStatus(taskID);
                  String displayETA = htConsumerClient.getTaskDisplayETA(taskID);

                  ...
            }
        }
    }
};

@Override
public void onResume() {
      super.onResume();

      ...

      IntentFilter filter = new IntentFilter(HTConsumerClient.TASK_DETAIL_REFRESHED_NOTIFICATION);
    LocalBroadcastManager.getInstance(this).registerReceiver(taskDetailRefreshedListener, filter);
}

@Override
public void onPause() {
      super.onPause();

      ...
    LocalBroadcastManager.getInstance(this).unregisterReceiver(taskDetailRefreshedListener);
}

Get Task Status Updates

To track or use updates in Task statuses, register a BroadcastReceiver to the intent HTConsumerClient.TASK_STATUS_CHANGED_NOTIFICATION.

// TaskStatusChanged Listener to be registered
BroadcastReceiver taskStatusChangedListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
          if (intent != null && intent.hasExtra(HTConsumerClient.INTENT_EXTRA_TASK_ID_LIST)) {
              ArrayList<String> taskIDList = intent.getStringArrayListExtra(HTConsumerClient.INTENT_EXTRA_TASK_ID_LIST);

              if (taskIDList != null) {
                  // Handle Task Status updates here
                  String status = htConsumerClient.getStatus(taskID);

                  ...
            }
        }
    }
};

@Override
public void onResume() {
      super.onResume();

      ...

      IntentFilter filter = new IntentFilter(HTConsumerClient.TASK_STATUS_CHANGED_NOTIFICATION);
    LocalBroadcastManager.getInstance(this).registerReceiver(taskStatusChangedListener, filter);
}

@Override
public void onPause() {
      super.onPause();

      ...
    LocalBroadcastManager.getInstance(this).unregisterReceiver(taskStatusChangedListener);
}

Get Task Details

Once you have started tracking the task you can get the latest ETA, with the following call on HTConsumerClient. The ETA is a Date object, and can be null.

Date taskETA = HTConsumerClient.getInstance().getTaskStore().getTask().getETA();

results matching ""

    No results matching ""