package za.ac.unizulu.cs.smartdrivedemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Formatter;
import java.util.Locale;
public class SpeedActivity extends Activity implements IBaseGpsListener{
boolean activateSMSInterceptor, activateCallInterceptor = false;
private float nCurrentSpeed;
TextView txtCurrentSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
this.updateSpeed(null);
}
public void updateSpeed(CLocation location)
{
nCurrentSpeed = 0;
if( location!=null )
{
location.setUseMetricUnits(this.useMetricUnits());
nCurrentSpeed = location.getSpeed();
nCurrentSpeed = nCurrentSpeed + (nCurrentSpeed/10);
}
@SuppressWarnings("resource")
Formatter fmt = new Formatter(new StringBuilder());
fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
String strCurrentSpeed = fmt.toString();
strCurrentSpeed = strCurrentSpeed.replace(' ', '0');
String strUnits = "mph";
if (this.useMetricUnits())
{
strUnits = "km/h";
}
txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
txtCurrentSpeed.setTextColor(Color.BLACK);
txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
deployService(nCurrentSpeed);
}
private void deployService(float currentSpeed) {
if(currentSpeed >=119){
txtCurrentSpeed.setTextColor(Color.RED);
}
else if(currentSpeed >=80){
Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_LONG).show();
}
else if (currentSpeed >=5.1){
Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
Toast.makeText(this, "Message Reception Enable",Toast.LENGTH_LONG).show();
if(activateCallInterceptor == false){
startService(new Intent(SpeedActivity.this,CallInterceptorService.class));
activateCallInterceptor = true;
}
if(activateSMSInterceptor == false){
startService(new Intent(SpeedActivity.this, SMSInterceptorService.class));
activateSMSInterceptor = true;
}
}
else if (currentSpeed <=5){
Toast.makeText(this, "Screen Enabled",Toast.LENGTH_LONG).show();
Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_LONG).show();
Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_LONG).show();
if(activateCallInterceptor == true){
stopService(new Intent(SpeedActivity.this, CallInterceptorService.class));
activateCallInterceptor = false;
}
if(activateSMSInterceptor == true){
stopService(new Intent(SpeedActivity.this, SMSInterceptorService.class));
activateSMSInterceptor = false;
}
}
}
public boolean useMetricUnits()
{
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.speed, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
if (location != null)
{
CLocation myLocation = new CLocation(location, this.useMetricUnits());
this.updateSpeed(myLocation);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onGpsStatusChanged(int event) {
}
}