You are on page 1of 13

package com.example.

mainactivity;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity implements LocationListener {
private WifiManager mainWifiObj;
private WifiScanReceiver wifiReciever;
private ListView list;
private String wifis[];
private LocationManager locationManager;
private String provider;
private List<ScanResult> wifiScanList;
private DBAdapter dbAdapter;
private double latitude;
private double longitude;
private Location location;

private static final String PSK = "PSK";


private static final String WEP = "WEP";
private static final String EAP = "EAP";
private static final String OPEN = "Open";
private static final String ENTERPRISE_CAPABILITY = "-EAP-";

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab = getActionBar();
ab.show();
//setTitle(R.string.access_points_in_current_range);
//ab.setHomeButtonEnabled(false);
list = (ListView)findViewById(R.id.listView1);
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(wifiReciever, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

dbAdapter = DBAdapter.getInstance(this);
dbAdapter.open();
locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);

// Getting the name of the provider that meets the criteria


provider = locationManager.getBestProvider(criteria, true);
Log.i("providerrrrr", provider);

Location location = locationManager.getLastKnownLocation(provider);


Log.w("dopo richiesta",location.getLatitude() +","
+location.getLongitude());
locationManager.requestLocationUpdates(provider, 200, 1, this);
Log.i("dopo richiesta","");
wifiReciever = new WifiScanReceiver();
mainWifiObj.startScan();

// Initialize the location fields


if (location != null) {
Log.i("","Provider " + provider + " has been selected.");
onLocationChanged(location);
}

final Button mapButton = (Button) this.findViewById(R.id.mapButton);


mapButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {

Intent intent = new Intent(MainActivity.this,


MapActivity.class);
startActivity(intent);

} catch (Exception e) {
}

}
});
final Button mapButton2 = (Button) this.findViewById(R.id.mapButton2);
mapButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity.this,
MapRangeActivity.class);
startActivity(intent);

} catch (Exception e) {
}
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {

ScanResult scan = (ScanResult)


list.getAdapter().getItem(position);
Log.i("hai premuto su ",
scan.SSID);
Intent intent = new Intent(MainActivity.this,
ShowScanInfo.class);
intent.putExtra("SSID", scan.SSID);
intent.putExtra("level", scan.level);
intent.putExtra("BSSID", scan.BSSID);
intent.putExtra("frequency", scan.frequency);

intent.putExtra("security", getScanResultSecurityType(scan));
//intent.putExtra("time", scan.timestamp);
startActivity(intent);
}
});

protected void onPause() {


unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
class WifiScanReceiver extends BroadcastReceiver {
@SuppressLint("UseValueOf")
public void onReceive(Context c, Intent intent) {
wifiScanList = mainWifiObj.getScanResults();
wifis = new String[wifiScanList.size()];

for(int i = 0; i < wifiScanList.size(); i++){


//wifis[i] = ((wifiScanList.get(i)).toString());
wifis[i] =(wifiScanList.get(i)).SSID +"\n" +"Protetta con " +
getScanResultSecurityType(wifiScanList.get(i));
Location location =
locationManager.getLastKnownLocation(provider);

if(location!=null ){
Log.w("stampo coordinate della loc", location.getLatitude()
+ "," +location.getLongitude());
dbAdapter.insertScan(wifiScanList.get(i), location,
getDateTime());
}else{
Log.i("location is NULLL", "NULLL");
if(getLongitude()!=0){
Log.i("l'altra non e' nulla ", getLongitude() +"");
}else{
Log.i("l'altra nulla anche", "00000");
}
}
}
//list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
//
android.R.layout.simple_list_item_1,wifis));
list.setAdapter(new MyAdapter(getApplicationContext(),
(ArrayList<ScanResult>)wifiScanList));

dbAdapter.printScans();

}
}
@Override
public void onLocationChanged(Location location) {
if(location!=null){
this.setLocation(location);
setLatitude(location.getLatitude());
setLongitude(location.getLongitude());
}
}

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {


}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {

}
public double calculateDistance(double signalLevelInDb, double freqInMHz) {
double exp = (27.55 - (20 * Math.log10(freqInMHz)) - (signalLevelInDb)) /
20.0;
return Math.pow(10.0, exp);
}

private String getDateTime() {


SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}

public static String getScanResultSecurity(ScanResult scanResult) {


final String cap = scanResult.capabilities;
final String[] securityModes = { WEP,PSK, EAP };
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
return securityModes[i];
}
}

return OPEN;
}
public static boolean isEnterprise(ScanResult scanResult) {
return scanResult.capabilities.contains(ENTERPRISE_CAPABILITY);
}
public static String getScanResultSecurityType(ScanResult scanResult) {
final String cap = scanResult.capabilities;
if(cap.contains("WPA") & cap.contains("WPA2")){
return "WPA/WPA2";
}
final String[] securityModes = { WEP, "WPA","WPA2", "EAP" };
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
return securityModes[i];
}
}
return OPEN;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public Location getLocation() {
return location;

public void setLocation(Location location) {


this.location = location;
}

/*private boolean calculateThreeCircleIntersection(double x0, double y0, double


r0,
double x1, double y1, double r1,
double x2, double y2, double r2)
{
double a, dx, dy, d, h, rx, ry;
double point2_x, point2_y;
dx and dy are the vertical and horizontal distances between
the circle centers.
dx = x1 - x0;
dy = y1 - y0;
/* Determine the straight-line distance between the centers.
d = Math.sqrt((dy*dy) + (dx*dx));
/* Check for solvability.
if (d > (r0 + r1))
{
/* no solution. circles do not intersect.
return false;
}
if (d < Math.abs(r0 - r1))
{
no solution. one circle is contained in the other
return false;
}
'point 2' is the point where the line through the circle
intersection points crosses the line between the circle
centers.

Determine the distance from point 0 to point 2.


a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;
Determine the coordinates of point 2.
point2_x = x0 + (dx * a/d);
point2_y = y0 + (dy * a/d);
Determine the distance from point 2 to either of the
intersection points.
h = Math.sqrt((r0*r0) - (a*a));
Now determine the offsets of the intersection points from
point 2.
rx = -dy * (h/d);
ry = dx * (h/d);
// Determine the absolute intersection points.
double intersectionPoint1_x = point2_x + rx;
double intersectionPoint2_x = point2_x - rx;
double intersectionPoint1_y = point2_y + ry;
double intersectionPoint2_y = point2_y - ry;
Log.d("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," +
intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," +
intersectionPoint2_y + ")");
/* Lets determine if circle 3 intersects at either of the above intersection points.
dx = intersectionPoint1_x - x2;
dy = intersectionPoint1_y - y2;
double d1 = Math.sqrt((dy*dy) + (dx*dx));
dx = intersectionPoint2_x - x2;
dy = intersectionPoint2_y - y2;
double d2 = Math.sqrt((dy*dy) + (dx*dx));
if(Math.abs(d1 - r2) < 0.000001) {

Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" +


intersectionPoint1_x + "," + intersectionPoint1_y + ")");
}
else if(Math.abs(d2 - r2) < 0.000001) {
Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" +
intersectionPoint2_x + "," + intersectionPoint2_y + ")"); //here was an error
}
else {
Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "NONE");
}
return true;
}*/

/*private boolean calculateTwoCircleIntersection(double x0, double y0, double


r0,
double x1, double y1, double r1)
{
double a, dx, dy, d, h, rx, ry;
double point2_x, point2_y;
// dx and dy are the vertical and horizontal distances between
//the circle centers.
dx = x1 - x0;
dy = y1 - y0;
// Determine the straight-line distance between the centers.
d = Math.sqrt((dy*dy) + (dx*dx));
// Check for solvability.
if (d > (r0 + r1))
{
//no solution. circles do not intersect.
return false;
}
if (d < Math.abs(r0 - r1))
{
// no solution. one circle is contained in the other

return false;
}
//'point 2' is the point where the line through the circle
//intersection points crosses the line between the circle
//centers.

// Determine the distance from point 0 to point 2.


a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;
// Determine the coordinates of point 2.
point2_x = x0 + (dx * a/d);
point2_y = y0 + (dy * a/d);
//Determine the distance from point 2 to either of the
//intersection points.
h = Math.sqrt((r0*r0) - (a*a));
// Now determine the offsets of the intersection points from
//point 2.
rx = -dy * (h/d);
ry = dx * (h/d);
//Determine the absolute intersection points.
double intersectionPoint1_x = point2_x + rx;
double intersectionPoint2_x = point2_x - rx;
double intersectionPoint1_y = point2_y + ry;
double intersectionPoint2_y = point2_y - ry;
Log.d("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," +
intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," +
intersectionPoint2_y + ")");
return true;
}*/

/*protected void onActivityResult(int requestCode, int resultCode, Intent data) {


super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1){
Log.i("ok","OK");
}
}*/
/*protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
setIntent(intent);
}*/

You might also like