Click here to Skip to main content
15,897,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package za.ac.unizulu.cs.smartdrivedemo;
C#
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);
		
		/*Button startButton = (Button) findViewById(R.id.startButton);
		startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		/*Button stopButton = (Button) findViewById(R.id.stopButton);
		stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		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();
	      
	      //adjust nCurrentSpeed to reflect the approx 10km/h GPS inaccuracy
	      //if (nCurrentSpeed >= 95){
	    	  //adds approx 10% to the speed measured by the GPS
	    	  nCurrentSpeed = nCurrentSpeed + (nCurrentSpeed/10); 
	    	  
	    	  
	    	  
	      //}
	    }

		//for testing purposes on the emulator
	  	//nCurrentSpeed = 6.3f;
	    
	    @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);
	    //changes the font size based upon the speed being traveled
	    //txtCurrentSpeed.setTextSize((nCurrentSpeed + 10) * 0.5f);
	    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
	    
	    //deploys the relevant notifications
	    deployService(nCurrentSpeed);	    
	  }

	  private void deployService(float currentSpeed) {
		// TODO Auto-generated method stub
		  if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				//Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
		  }
		  else if(currentSpeed >=80){
			  Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			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();
				//see Reto Meier book - page 333
				
				
				//ensures that the interceptors start once only
				if(activateCallInterceptor == false){
					//TODO add call interception service initiation here
					startService(new Intent(SpeedActivity.this,CallInterceptorService.class));
					activateCallInterceptor = true;
				}
				
				if(activateSMSInterceptor == false){
					startService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); // this service works
					//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					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();
				//enableSMSReception();
				
				//ensures that the interceptors end once only
				if(activateCallInterceptor == true){
					stopService(new Intent(SpeedActivity.this, CallInterceptorService.class));
					activateCallInterceptor = false;	
				}
				if(activateSMSInterceptor == true){
					stopService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); //this service works
					//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = false;
				}
			}
	}


	public boolean useMetricUnits()
	  {
	    /*CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
	    return chkUseMetricUnits.isChecked();*/
		  return true;
	  }
	  
	  /*public void displayToasts(float currentSpeed){
			if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
			}
			else if(currentSpeed >=80){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=20){
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_SHORT).show();
				//enableSMSReception();
			}
			
		}*/
	

	/*@Override
	public void finish()
	  {
	    super.finish();
	    System.exit(0);
	  }*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.speed, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
	    {
	      CLocation myLocation = new CLocation(location, this.useMetricUnits());
	      this.updateSpeed(myLocation);
	    }
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGpsStatusChanged(int event) {
		// TODO Auto-generated method stub
		
	}

}
Posted
Updated 18-Oct-13 8:55am
v2
Comments
Kenneth Haugland 18-Oct-13 14:54pm    
Way too much...

1 solution

package za.ac.unizulu.cs.smartdrivedemo;

Java
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);
		
		/*Button startButton = (Button) findViewById(R.id.startButton);
		startButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		/*Button stopButton = (Button) findViewById(R.id.stopButton);
		stopButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class));
			}
		});*/
		
		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();
	      
	      //adjust nCurrentSpeed to reflect the approx 10km/h GPS inaccuracy
	      //if (nCurrentSpeed >= 95){
	    	  //adds approx 10% to the speed measured by the GPS
	    	  nCurrentSpeed = nCurrentSpeed + (nCurrentSpeed/10); 
	    	  
	    	  
	    	  
	      //}
	    }

		//for testing purposes on the emulator
	  	//nCurrentSpeed = 6.3f;
	    
	    @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);
	    //changes the font size based upon the speed being traveled
	    //txtCurrentSpeed.setTextSize((nCurrentSpeed + 10) * 0.5f);
	    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
	    
	    //deploys the relevant notifications
	    deployService(nCurrentSpeed);	    
	  }

	  private void deployService(float currentSpeed) {
		// TODO Auto-generated method stub
		  if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				//Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
		  }
		  else if(currentSpeed >=80){
			  Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_LONG).show();
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_LONG).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			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();
				//see Reto Meier book - page 333
				
				
				//ensures that the interceptors start once only
				if(activateCallInterceptor == false){
					//TODO add call interception service initiation here
					startService(new Intent(SpeedActivity.this,CallInterceptorService.class));
					activateCallInterceptor = true;
				}
				
				if(activateSMSInterceptor == false){
					startService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); // this service works
					//startService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					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();
				//enableSMSReception();
				
				//ensures that the interceptors end once only
				if(activateCallInterceptor == true){
					stopService(new Intent(SpeedActivity.this, CallInterceptorService.class));
					activateCallInterceptor = false;	
				}
				if(activateSMSInterceptor == true){
					stopService(new Intent(SpeedActivity.this, SMSInterceptorService.class)); //this service works
					//stopService(new Intent(SpeedActivity.this, TextMessageReceiver.class)); // receivers cannot be initiated as services
					activateSMSInterceptor = false;
				}
			}
	}


	public boolean useMetricUnits()
	  {
	    /*CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
	    return chkUseMetricUnits.isChecked();*/
		  return true;
	  }
	  
	  /*public void displayToasts(float currentSpeed){
			if(currentSpeed >=119){
				txtCurrentSpeed.setTextColor(Color.RED);
				Toast.makeText(this, "Screen Disabled",Toast.LENGTH_SHORT).show();
			}
			else if(currentSpeed >=80){
				Toast.makeText(this, "Call Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
				
				//hideSMSReception();
			}
			else if (currentSpeed >=20){
				Toast.makeText(this, "Message Reception Hidden",Toast.LENGTH_SHORT).show();
				//see Reto Meier book - page 333
			}
			else if (currentSpeed <=5){
				Toast.makeText(this, "Screen Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Call Reception Enabled",Toast.LENGTH_SHORT).show();
				Toast.makeText(this, "Message Reception Enabled",Toast.LENGTH_SHORT).show();
				//enableSMSReception();
			}
			
		}*/
	

	/*@Override
	public void finish()
	  {
	    super.finish();
	    System.exit(0);
	  }*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.speed, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null)
	    {
	      CLocation myLocation = new CLocation(location, this.useMetricUnits());
	      this.updateSpeed(myLocation);
	    }
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGpsStatusChanged(int event) {
		// TODO Auto-generated method stub
		
	}

}
 
Share this answer
 
v2
Comments
Nelek 18-Oct-13 20:09pm    
How is copying the code of the question going to help or to give any answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900