Vous êtes sur la page 1sur 7

package com.geu.

Securityaap;
import java.util.ArrayList;
import com.geu.Securityaap.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements


OnClickListener,LocationListener
{
AlertDialog dialog;
AlertDialog.Builder builder;
TextView t1,t2,t3,t4,t5;
SharedPreferences prefs;
ConnectivityManager cn;

private LocationManager locationManager;


private String provider;
String x,y,getmap;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

// Get the location manager


locationManager = (LocationManager);
getSystemService(Context.LOCATION_SERVICE);

/*----GPS TEST----*/

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "GPS is Enabled in your device.",
Toast.LENGTH_SHORT).show();
onClick(null);
}
else
{
showGPSDisabledAlertToUser();
}

/*----GPS TEST----*/

// Define the criteria how to select the location provider -> use default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
//Location location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

// Initialize the location fields


if (location != null)
{
Toast.makeText(this, "Provider "+ provider + " has been
selected.", Toast.LENGTH_SHORT).show();
onLocationChanged(location);
}

t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4 = (TextView) findViewById(R.id.textView4);
t5 = (TextView) findViewById(R.id.textView5);

prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(new)
OnSharedPreferenceChangeListener();
{
@Override
public void onSharedPreferenceChanged(SharedPreferences
sharedPreferences,String key)
{
loadSettings();
}
}

/* Request updates at startup */


@Override
protected void onResume()
{
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
locationManager.removeUpdates(this);
}

@Override
protected void onPause()
{
super.onPause();
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location)
{
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
x = Double.toString(lat);
y = Double.toString(lng);

getmap="http://maps.google.com/maps?z=18&q="+x+","+y;
}

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

@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();

@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}

public void showPrefs(View v)


{
Intent intent = new Intent(this,MyPreferences.class);
startActivity(intent);
}

public void send(View view)


{
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
//Toast.makeText(this, "Internet connection Available!",
Toast.LENGTH_LONG).show();
// tvstatus.setText("Network Available");
String num0 = "8171661333";
//String num0 = "9557461183";
String num = t1.getText().toString();
String num1 = t2.getText().toString();
String num2 = t3.getText().toString();
String num3 = t4.getText().toString();

if(num.length()<=0 || num=="First number is not Set" || num1.length()<=0


|| num1=="Second number is not Set" || num2.length()<=0 || num2=="Third number
is not Set" || num3.length()<=0 || num3=="Fourth number is not Set")
{
new AlertDialog.Builder(this)
.setTitle("Empty mobile numbers !")
.setMessage("Please fill all the mobile numbers first !")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
}
public void onClick(DialogInterface dialog, int which)
{
// continue with delete
.show();
}
else
{

String msg;
if(x==null)
{
msg = "I am in trouble!\n\nUnable to retrieve address
location,GPS/Internet services" +" are not properly turned ON in victim's mobile";

}
else
{
msg = "I am in trouble!\nMy address
is:\n\nLatitude:"+x+"\nLongitude"+y+"\nGet actual loaction at :\n"+getmap;
}
/*Stringmsg="SaveMe!\nMyaddress:\n\nLatitude:"+x+"\nLongitude"+y+"\nGet
actual loaction at : http://www.latlong.net/Show-Latitude-Longitude.html" +"\nfor
any query call : 09557461183";*///"\n\nDownload URSafe Pro app for exact address
location";

sendMessage0(num0,msg);
sendMessage(num,msg);
sendMessage1(num1,msg);
sendMessage2(num2,msg);
sendMessage3(num3,msg);

Toast.makeText(MainActivity.this, "Messages are in


progress!", Toast.LENGTH_SHORT).show();

}
}
else
{
// Toast.makeText(this, "Network Not Available",
Toast.LENGTH_LONG).show();
// tvstatus.setText("Network Not Available");
showInternetDisabledAlertToUser();
}

private void sendMessage0(String num0, String msg)


{
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(num0, null, parts, null, null);
}

private void sendMessage(String num, String msg)


{
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(num, null, parts, null, null);
}
private void sendMessage1(String num1, String msg)
{
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(num1, null, parts, null, null);
}
private void sendMessage2(String num2, String msg)
{
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(num2, null, parts, null, null);
}
private void sendMessage3(String num3, String msg)
{
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(msg);
smsManager.sendMultipartTextMessage(num3, null, parts, null, null);
}

private void loadSettings()


{
t1.setText(prefs.getString("number1", "First number is not Set"));
t2.setText(prefs.getString("number2", "Second number is not Set"));
t3.setText(prefs.getString("number3", "Third number is not Set"));
t4.setText(prefs.getString("number4", "Fourth number is not Set"));
boolean status = prefs.getBoolean("add", false);
if(status!=false)

t5.setText("Address sending is On");


else
t5.setText("Address sending is Off");
}

public void exit(View v)


{
if(v.getId()==R.id.button3)
{
finish();
}
}

public void onClick(View arg0)


{
builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm");
builder.setMessage("Do you wish to Send the messages? \nPlease make sure
INTERNET and all the GPS services are ON, " +"for sending your address location");
builder.setPositiveButton("No", new DialogInterface.OnClickListener()
{
@Override

public void onClick(DialogInterface dialog, int which)


{
Toast.makeText(MainActivity.this, "Messages not sent!",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
send(null);
}
});
builder.setCancelable(false);
dialog = builder.create();
dialog.show();

/*----GPS TEST---*/

private void showGPSDisabledAlertToUser()


{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Please click on
settings option below and enable GPS for helping URSafe to send address
locations.")

.setCancelable(false)
.setPositiveButton("Goto Settings Page To Enable GPS",new)
DialogInterface.OnClickListener();
{
public void onClick(DialogInterface dialog, int id)
{
Intent callGPSSettingIntent = new
Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",new
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();

}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

/*----GPS TEST---*/

/*---internet check-----*/

private void showInternetDisabledAlertToUser()


{
builder = new AlertDialog.Builder(this);
builder.setTitle("Turn on the Internet connection!");
builder.setMessage("Internet is disabled in your device. Please click on
settings option below to open it.");
builder.setPositiveButton("Enable Internet with mobile data.", new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent callInternetSettingIntent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(callInternetSettingIntent);
}
});
builder.setNegativeButton("Enable Internet with wifi.", new
DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent callInternetSettingIntent = new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivity(callInternetSettingIntent);
}
});
builder.setCancelable(true);
dialog = builder.create();
dialog.show();

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);


alertDialogBuilder.setMessage("Internet is disabled in your device. Please
click on settings option below to open it.")
.setCancelable(false)
.setPositiveButton("Enable Internet with mobile data.",new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
Intent callInternetSettingIntent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(callInternetSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Enable Internet with wifi.",new
DialogInterface.OnClickListener()

{
public void onClick(DialogInterface dialog, int id)
{
Intent callInternetSettingIntent = new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS);
startActivity(callInternetSettingIntent);
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();*/
}

Vous aimerez peut-être aussi