Skip to content

Instantly share code, notes, and snippets.

@the-jedi-droid
Created September 23, 2015 04:52
Show Gist options
  • Save the-jedi-droid/93abdea964e1dbeaab70 to your computer and use it in GitHub Desktop.
Save the-jedi-droid/93abdea964e1dbeaab70 to your computer and use it in GitHub Desktop.
package io.hypertrack.driverdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.cast.Cast;
import com.google.android.gms.maps.SupportMapFragment;
import com.hypertrack.apps.assettracker.network.HyperTrack;
import com.hypertrack.apps.assettracker.service.TransmitterService;
import com.hypertrack.apps.assettracker.view.TransmitterActivityFragment;
public class MainActivity extends AppCompatActivity implements TransmitterService.StatusCallback {
//trip details
private String courierId;
private int[] orderIdArray;
//Fragment Manager and the fragment
private FragmentManager mMgr;
//Views on Main Acitivity
private Button startTripButton;
private Button endTripButton;
private Button deliveredButton;
//Trip ID
private String tripId;
private TransmitterService transmitterService;
private TransmitterActivityFragment transmitterActivityFragment;
private Button detailsButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set public API key here
HyperTrack.setAPIKey("<API KEY>");
//Setup order details
transmitterService = TransmitterService.getInstance(this, this);
String courierId = "1";
int[] orderIds = {303};
int end_place_id = 2;
transmitterService.setOrderDetails(courierId, orderIds, end_place_id);
/* transmitterActivityFragment =
(TransmitterActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
transmitterActivityFragment.setTransmitterService(transmitterService);*/
//initialize views
initViews();
}
public void initViews() {
startTripButton = (Button)findViewById(R.id.startTripButton);
endTripButton = (Button)findViewById(R.id.endTripButton);
deliveredButton = (Button)findViewById(R.id.deliveredButton);
detailsButton = (Button)findViewById(R.id.detailsButton);
//To start tracking call startTracking method.
startTripButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transmitterService.startTrip();
}
});
//To end tracking call endTracking method.
endTripButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tripId == null) {
SharedPreferences sharedpreferences = getSharedPreferences("com.hypertrack.apps.assettracker.demo", Context.MODE_PRIVATE);
tripId = sharedpreferences.getString("trip_id","");
Log.e("Trip id", tripId);
}
transmitterService.endTrip(Integer.valueOf(tripId));
}
});
//To notify delivery
deliveredButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transmitterService.notifyDelivered("303");
}
});
detailsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
//transmitterService.notifyDelivered("303");
}
});
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filterTripStarted = new IntentFilter(HyperTrack.ACTION_TRIP_STARTED);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnTripStartedReceiver, filterTripStarted);
IntentFilter filterTripStartError = new IntentFilter(HyperTrack.ACTION_TRIP_START_ERROR);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnTripStartErrorReceiver, filterTripStartError);
IntentFilter filterTripEnded = new IntentFilter(HyperTrack.ACTION_TRIP_ENDED);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnTripEndedReceiver, filterTripEnded);
IntentFilter filterTripEndError = new IntentFilter(HyperTrack.ACTION_TRIP_END_ERROR);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnTripEndErrorReceiver, filterTripEndError);
IntentFilter filterOrderDelivered = new IntentFilter(HyperTrack.ACTION_ORDER_DELIVERED);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnDeliveredReceiver, filterOrderDelivered);
IntentFilter filterOrderDeliveryEndError = new IntentFilter(HyperTrack.ACTION_ORDER_DELIVERY_END_ERROR);
LocalBroadcastManager.getInstance(this).registerReceiver(mOnDeliveryEndReceiver, filterOrderDeliveryEndError);
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnTripStartedReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnTripStartErrorReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnTripEndedReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnTripEndErrorReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnDeliveredReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mOnDeliveryEndReceiver);
}
BroadcastReceiver mOnTripStartedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tripId = intent.getStringExtra(HyperTrack.TRIP_ID);
Toast.makeText(context, "From Receiver: trip started with id : " + tripId, Toast.LENGTH_LONG).show();
SharedPreferences sharedpreferences = getSharedPreferences("com.hypertrack.apps.assettracker.demo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("trip_id", tripId);
editor.commit();
}
};
BroadcastReceiver mOnTripStartErrorReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "From Receiver: Error", Toast.LENGTH_LONG).show();
}
};
BroadcastReceiver mOnTripEndedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "From Receiver: trip ended with id : " + tripId, Toast.LENGTH_LONG).show();
}
};
BroadcastReceiver mOnTripEndErrorReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "From Receiver: Error", Toast.LENGTH_LONG).show();
}
};
BroadcastReceiver mOnDeliveredReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String orderId = intent.getStringExtra(HyperTrack.ORDER_ID);
Toast.makeText(context, "From Receiver: Order ended with id : " + orderId, Toast.LENGTH_LONG).show();
}
};
BroadcastReceiver mOnDeliveryEndReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "From Receiver: Error", Toast.LENGTH_LONG).show();
}
};
@Override
public void onTripStarted(String tripId) {
// Callback method for trip started
/* this.tripId = tripId;
Toast.makeText(this, "trip started with id : " + tripId, Toast.LENGTH_LONG).show();
SharedPreferences sharedpreferences = getSharedPreferences("com.hypertrack.apps.assettracker.demo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("trip_id", tripId);
editor.commit();*/
//transmitterActivityFragment.setTripId(tripId);
}
@Override
public void onTripStartError() {
//Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
@Override
public void onTripEnded(String tripId) {
// Callback method for trip ended
//Toast.makeText(this, "trip ended with id : " + tripId, Toast.LENGTH_LONG).show();
}
@Override
public void onTripEndError() {
//Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
@Override
public void onDelivererd(String orderId) {
// Callback method for delivered
//Toast.makeText(this, "Order ended with id : " + orderId, Toast.LENGTH_LONG).show();
}
@Override
public void onDelivererdError() {
//Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment