Back to project page android_network_discovery.
The source code is released under:
GNU General Public License
If you think the Android project android_network_discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2009-2010 Aubort Jean-Baptiste (Rorist) * Licensed under GNU's GPL 2, see README *//*from ww w. ja v a 2 s .co m*/ package info.lamatricexiste.network; import info.lamatricexiste.network.ActivityPortscan.PortsAdapter; import info.lamatricexiste.network.Network.HostBean; import info.lamatricexiste.network.Network.NetInfo; import info.lamatricexiste.network.Utils.Db; import info.lamatricexiste.network.Utils.Export; import info.lamatricexiste.network.Utils.Help; import info.lamatricexiste.network.Utils.Prefs; import info.lamatricexiste.network.Utils.Save; import java.lang.ref.WeakReference; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.net.ConnectivityManager; import android.net.Uri; import android.os.Bundle; import android.os.Vibrator; import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; final public class ActivityDiscovery extends ActivityNet implements OnItemClickListener { private final String TAG = "ActivityDiscovery"; public final static long VIBRATE = (long) 250; public final static int SCAN_PORT_RESULT = 1; public static final int MENU_SCAN_SINGLE = 0; public static final int MENU_OPTIONS = 1; public static final int MENU_HELP = 2; private static final int MENU_EXPORT = 3; private static LayoutInflater mInflater; private int currentNetwork = 0; private long network_ip = 0; private long network_start = 0; private long network_end = 0; private List<HostBean> hosts = null; private List<String> knownServices; public static String scan_UUID; private HostsAdapter adapter; private Button btn_discover; private AbstractDiscovery mDiscoveryTask = null; DatabaseHelper db; //***************************************************************************************** private static final String REQ_SERVICE = "select service from services where port=? limit 1"; private static final String REQ_PROBES = "select service, regex from probes"; //private static final int PROGRESS_MAX = 10000; private final String PLACEHOLDER = "placeholder"; //private Context ctxt; public ScanPortTask scanPortTask; private HostBean host; private PortsAdapter adapter_open; private PortsAdapter adapter_closed; private int cnt_open; private int cnt_closed; private TextView mTabOpen; private TextView mTabClosed; private Button btn_scan; //***************************************************************************************** @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_PROGRESS); // requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.discovery); //cnt_open = (host.portsOpen == null) ? 0 : host.portsOpen.size(); //cnt_closed = (host.portsClosed == null) ? 0 : host.portsClosed.size(); ConnectivityManager networkConnectivity = (ConnectivityManager) getApplicationContext() .getSystemService(Context.CONNECTIVITY_SERVICE); if (networkConnectivity.getActiveNetworkInfo() != null && networkConnectivity.getActiveNetworkInfo().isAvailable() && networkConnectivity.getActiveNetworkInfo().isConnected()) { UUID scan = UUID.randomUUID(); scan_UUID = scan.toString(); Log.d("UUID", scan_UUID); } knownServices = new ArrayList<String>(); knownServices.add("ssh"); knownServices.add("telnet"); knownServices.add("http"); knownServices.add("https"); db = new DatabaseHelper(getApplicationContext()); mInflater = LayoutInflater.from(ctxt); Context ctx = getApplicationContext(); Log.i("Context of ActivityDiscovery", "" + ctx); // Discover btn_discover = (Button) findViewById(R.id.btn_discover); btn_discover.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startDiscovering(); } }); // Options Button btn_options = (Button) findViewById(R.id.btn_options); btn_options.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent(ctxt, Prefs.class)); } }); // Hosts list adapter = new HostsAdapter(ctxt); ListView list = (ListView) findViewById(R.id.output); list.setAdapter(adapter); list.setItemsCanFocus(false); list.setOnItemClickListener(this); list.setEmptyView(findViewById(R.id.list_empty)); } @Override public void onResume() { super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, ActivityDiscovery.MENU_SCAN_SINGLE, 0, R.string.scan_single_title).setIcon( android.R.drawable.ic_menu_mylocation); menu.add(0, ActivityDiscovery.MENU_EXPORT, 0, R.string.preferences_export).setIcon( android.R.drawable.ic_menu_save); menu.add(0, ActivityDiscovery.MENU_OPTIONS, 0, R.string.btn_options) .setIcon(android.R.drawable.ic_menu_preferences); menu.add(0, ActivityDiscovery.MENU_HELP, 0, R.string.preferences_help) .setIcon(android.R.drawable.ic_menu_help); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ActivityDiscovery.MENU_SCAN_SINGLE: scanSingle(this, null); return true; case ActivityDiscovery.MENU_OPTIONS: startActivity(new Intent(ctxt, Prefs.class)); return true; case ActivityDiscovery.MENU_HELP: startActivity(new Intent(ctxt, Help.class)); return true; case ActivityDiscovery.MENU_EXPORT: export(); return true; } return false; } protected void setInfo() { // Info ((TextView) findViewById(R.id.info_ip)).setText(info_ip_str); ((TextView) findViewById(R.id.info_in)).setText(info_in_str); ((TextView) findViewById(R.id.info_mo)).setText(info_mo_str); // Scan button state if (mDiscoveryTask != null) { setButton(btn_discover, R.drawable.cancel, false); btn_discover.setText(R.string.btn_discover_cancel); btn_discover.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { cancelTasks(); } }); } if (currentNetwork != net.hashCode()) { currentNetwork = net.hashCode(); // Cancel running tasks cancelTasks(); } else { return; } // Get ip information network_ip = NetInfo.getUnsignedLongFromIp(net.ip); if (prefs.getBoolean(Prefs.KEY_IP_CUSTOM, Prefs.DEFAULT_IP_CUSTOM)) { // Custom IP network_start = NetInfo.getUnsignedLongFromIp(prefs.getString( Prefs.KEY_IP_START, Prefs.DEFAULT_IP_START)); network_end = NetInfo.getUnsignedLongFromIp(prefs.getString( Prefs.KEY_IP_END, Prefs.DEFAULT_IP_END)); } else { // Custom CIDR if (prefs.getBoolean(Prefs.KEY_CIDR_CUSTOM, Prefs.DEFAULT_CIDR_CUSTOM)) { net.cidr = Integer.parseInt(prefs.getString(Prefs.KEY_CIDR, Prefs.DEFAULT_CIDR)); } // Detected IP int shift = (32 - net.cidr); if (net.cidr < 31) { network_start = (network_ip >> shift << shift) + 1; network_end = (network_start | ((1 << shift) - 1)) - 1; } else { network_start = (network_ip >> shift << shift); network_end = (network_start | ((1 << shift) - 1)); } // Reset ip start-end (is it really convenient ?) Editor edit = prefs.edit(); edit.putString(Prefs.KEY_IP_START, NetInfo.getIpFromLongUnsigned(network_start)); edit.putString(Prefs.KEY_IP_END, NetInfo.getIpFromLongUnsigned(network_end)); edit.commit(); } } protected void setButtons(boolean disable) { if (disable) { setButtonOff(btn_discover, R.drawable.disabled); } else { setButtonOn(btn_discover, R.drawable.discover); } } protected void cancelTasks() { if (mDiscoveryTask != null) { mDiscoveryTask.cancel(true); mDiscoveryTask = null; } } // Function for PortScan protected void startScan() { Log.d("Start Scan","Entered start scan"); if (!NetInfo.isConnected(ctxt)) { Log.i("Context : ",""+ctxt); return; } makeToast(R.string.scan_start); // adapter_open.clear(); // adapter_closed.clear(); cnt_open = 0; cnt_closed = 0; Log.d("Start Scan","Exiting start scan"); } // Function to stop PortScan protected void stopScan() { } private int getTimeout() { if (prefs.getBoolean(Prefs.KEY_TIMEOUT_FORCE, Prefs.DEFAULT_TIMEOUT_FORCE)) { return Integer.parseInt(prefs.getString(Prefs.KEY_TIMEOUT_PORTSCAN, Prefs.DEFAULT_TIMEOUT_PORTSCAN)); } return host.responseTime; } // Listen for Activity results protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case SCAN_PORT_RESULT: if (resultCode == RESULT_OK) { // Get scanned ports if (data != null && data.hasExtra(HostBean.EXTRA)) { HostBean host = data.getParcelableExtra(HostBean.EXTRA); if (host != null) { hosts.set(host.position, host); if (NetworkChange.scan_UUID == null) { portscan ps = new portscan(host.hardwareAddress, host.ipAddress, host.services.toString(), scan_UUID); db.createPortScan(ps); Log.i("", "MAC : " + host.hardwareAddress + " Services : " + host.services + " IP : " + host.ipAddress); } else { portscan ps = new portscan(host.hardwareAddress, host.ipAddress, host.services.toString(), NetworkChange.scan_UUID); db.createPortScan(ps); Log.i("", "MAC : " + host.hardwareAddress + " Services : " + host.services + " IP : " + host.ipAddress); } } } } default: break; } } public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { final HostBean host = hosts.get(position); AlertDialog.Builder dialog = new AlertDialog.Builder( ActivityDiscovery.this); dialog.setTitle(R.string.discover_action_title); dialog.setItems(new CharSequence[] { getString(R.string.discover_action_scan), getString(R.string.discover_action_rename) }, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // Start portscan Intent intent = new Intent(ctxt, ActivityPortscan.class); intent.putExtra(EXTRA_WIFI, NetInfo.isConnected(ctxt)); intent.putExtra(HostBean.EXTRA, host); startActivityForResult(intent, SCAN_PORT_RESULT); break; case 1: // Change name final View v = mInflater.inflate( R.layout.dialog_edittext, null); final EditText txt = (EditText) v .findViewById(R.id.edittext); final Save s = new Save(); txt.setText(s.getCustomName(host)); final AlertDialog.Builder rename = new AlertDialog.Builder( ActivityDiscovery.this); rename.setView(v); rename.setTitle(R.string.discover_action_rename); rename.setPositiveButton(R.string.btn_ok, new OnClickListener() { public void onClick( DialogInterface dialog, int which) { final String name = txt.getText() .toString(); host.hostname = name; s.setCustomName(name, host.hardwareAddress); adapter.notifyDataSetChanged(); Toast.makeText( ActivityDiscovery.this, R.string.discover_action_saved, Toast.LENGTH_SHORT).show(); } }); rename.setNegativeButton(R.string.btn_remove, new OnClickListener() { public void onClick( DialogInterface dialog, int which) { host.hostname = null; s.removeCustomName(host.hardwareAddress); adapter.notifyDataSetChanged(); Toast.makeText( ActivityDiscovery.this, R.string.discover_action_deleted, Toast.LENGTH_SHORT).show(); } }); rename.show(); break; } } }); dialog.setNegativeButton(R.string.btn_discover_cancel, null); dialog.show(); } static class ViewHolder { TextView host; TextView mac; TextView vendor; ImageView logo; } // Custom ArrayAdapter private class HostsAdapter extends ArrayAdapter<Void> { public HostsAdapter(Context ctxt) { super(ctxt, R.layout.list_host, R.id.list); } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.list_host, null); holder = new ViewHolder(); holder.host = (TextView) convertView.findViewById(R.id.list); holder.mac = (TextView) convertView.findViewById(R.id.mac); holder.vendor = (TextView) convertView .findViewById(R.id.vendor); holder.logo = (ImageView) convertView.findViewById(R.id.logo); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final HostBean host = hosts.get(position); if (host.deviceType == HostBean.TYPE_GATEWAY) { holder.logo.setImageResource(R.drawable.router); } else if (host.isAlive == 1 || !host.hardwareAddress.equals(NetInfo.NOMAC)) { holder.logo.setImageResource(R.drawable.computer); } else { holder.logo.setImageResource(R.drawable.computer_down); } if (host.hostname != null && !host.hostname.equals(host.ipAddress)) { holder.host .setText(host.hostname + " (" + host.ipAddress + ")"); } else { holder.host.setText(host.ipAddress); } if (!host.hardwareAddress.equals(NetInfo.NOMAC)) { holder.mac.setText(host.hardwareAddress); if (host.nicVendor != null) { holder.vendor.setText(host.nicVendor); } else { holder.vendor.setText(R.string.info_unknown); } holder.mac.setVisibility(View.VISIBLE); holder.vendor.setVisibility(View.VISIBLE); } else { holder.mac.setVisibility(View.GONE); holder.vendor.setVisibility(View.GONE); } return convertView; } } /** * Discover hosts */ private void startDiscovering() { int method = 0; try { method = Integer.parseInt(prefs.getString( Prefs.KEY_METHOD_DISCOVER, Prefs.DEFAULT_METHOD_DISCOVER)); } catch (NumberFormatException e) { Log.e(TAG, e.getMessage()); } switch (method) { case 1: mDiscoveryTask = new DnsDiscovery(ActivityDiscovery.this); break; case 2: // Root break; case 0: default: mDiscoveryTask = new DefaultDiscovery(ActivityDiscovery.this); } mDiscoveryTask.setNetwork(network_ip, network_start, network_end); mDiscoveryTask.execute(); btn_discover.setText(R.string.btn_discover_cancel); setButton(btn_discover, R.drawable.cancel, false); btn_discover.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { cancelTasks(); } }); makeToast(R.string.discover_start); //setProgressBarVisibility(true); //setProgressBarIndeterminateVisibility(true); initList(); } public void stopDiscovering() { Log.e(TAG, "stopDiscovering()"); mDiscoveryTask = null; setButtonOn(btn_discover, R.drawable.discover); btn_discover.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startDiscovering(); } }); //setProgressBarVisibility(false); //setProgressBarIndeterminateVisibility(false); btn_discover.setText(R.string.btn_discover); scanPortTask = new ScanPortTask(this, host.ipAddress, getTimeout()); scanPortTask.execute(); } private void initList() { // setSelectedHosts(false); adapter.clear(); hosts = new ArrayList<HostBean>(); } public void addHost(HostBean host) { host.position = hosts.size(); hosts.add(host); adapter.add(null); } public static void scanSingle(final Context ctxt, String ip) { // Alert dialog View v = LayoutInflater.from(ctxt).inflate(R.layout.scan_single, null); final EditText txt = (EditText) v.findViewById(R.id.ip); if (ip != null) { txt.setText(ip); } AlertDialog.Builder dialogIp = new AlertDialog.Builder(ctxt); dialogIp.setTitle(R.string.scan_single_title); dialogIp.setView(v); dialogIp.setPositiveButton(R.string.btn_scan, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { // start scanportactivity Intent intent = new Intent(ctxt, ActivityPortscan.class); intent.putExtra(HostBean.EXTRA_HOST, txt.getText() .toString()); try { intent.putExtra(HostBean.EXTRA_HOSTNAME, (InetAddress.getByName(txt.getText() .toString()).getHostName())); } catch (UnknownHostException e) { intent.putExtra(HostBean.EXTRA_HOSTNAME, txt .getText().toString()); } ctxt.startActivity(intent); } }); dialogIp.setNegativeButton(R.string.btn_discover_cancel, null); dialogIp.show(); } private void export() { final Export e = new Export(ctxt, hosts); final String file = e.getFileName(); View v = mInflater.inflate(R.layout.dialog_edittext, null); final EditText txt = (EditText) v.findViewById(R.id.edittext); txt.setText(file); AlertDialog.Builder getFileName = new AlertDialog.Builder(this); getFileName.setTitle(R.string.export_choose); getFileName.setView(v); getFileName.setPositiveButton(R.string.export_save, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { final String fileEdit = txt.getText().toString(); if (e.fileExists(fileEdit)) { AlertDialog.Builder fileExists = new AlertDialog.Builder( ActivityDiscovery.this); fileExists.setTitle(R.string.export_exists_title); fileExists.setMessage(R.string.export_exists_msg); fileExists.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which) { if (e.writeToSd(fileEdit)) { makeToast(R.string.export_finished); } else { export(); } } }); fileExists.setNegativeButton(R.string.btn_no, null); fileExists.show(); } else { if (e.writeToSd(fileEdit)) { makeToast(R.string.export_finished); } else { export(); } } } }); getFileName.setNegativeButton(R.string.btn_discover_cancel, null); getFileName.show(); } public void makeToast(int msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); } private void makeToast(String msg) { Toast.makeText(getApplicationContext(), (CharSequence) msg, Toast.LENGTH_SHORT).show(); } private void setButton(Button btn, int res, boolean disable) { if (disable) { setButtonOff(btn, res); } else { setButtonOn(btn, res); } } private void setButtonOff(Button b, int drawable) { b.setClickable(false); b.setEnabled(false); b.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0); } private void setButtonOn(Button b, int drawable) { b.setClickable(true); b.setEnabled(true); b.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0); } //******************openportservice***************************************// private void openPortService(String service, int port) { // Action for the service String pk = ""; String search = null; Intent intent = null; if (service.equals("ssh")) { pk = "ConnectBot (ssh)"; Log.i("ActivityPortScan","SSH found for "+host.ipAddress); search = "market://search?q=pname:org.connectbot"; String user = prefs.getString(Prefs.KEY_SSH_USER, Prefs.DEFAULT_SSH_USER); intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("ssh://" + user + "@" + host.ipAddress + ":" + port + "/#" + user + "@" + host.ipAddress + ":" + port)); } else if (service.equals("telnet")) { pk = "ConnectBot (telnet)"; search = "market://search?q=pname:org.connectbot"; intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("telnet://" + host.ipAddress + ":" + port)); } else if (service.equals("http") || service.equals("https")) { intent = new Intent(Intent.ACTION_VIEW); Log.i("ActivityPortScan","HTTP found for "+host.ipAddress); intent.setData(Uri.parse(service + "://" + (host.hostname != null ? host.hostname : host.ipAddress) + ":" + port)); } else { makeToast(R.string.scan_noaction); } if (intent != null) { try { startActivity(intent); } catch (ActivityNotFoundException e) { if (search != null) { makeToast(getString(R.string.package_missing, pk)); try { startActivity(new Intent(Intent.ACTION_VIEW) .setData(Uri.parse(search))); } catch (ActivityNotFoundException e2) { Log.e(TAG, "Market not found !"); } } Log.e(TAG, e.getMessage()); } } } //*******************openportservice*************************************// //*******************findaddlocation*************************************// private boolean findLocationAndAdd(ArrayList<Integer> array, int value) { int index = 0; int size = array.size(); for (index = 0; index < size; index++) { int current = array.get(index); if (value > current) { continue; } else if (value < current) { // Add new value array.add(index, value); return true; } else if (value == current) { // Value already exists return false; } } if (index == size) { array.add(value); return true; } return false; } //*******************findaddlocation*************************************// //*******************SCAN PORT TASK**************************************// private class ScanPortTask extends AsyncPortscan { // FIXME: Create AbstractPortscan class private SQLiteDatabase dbServices; private SQLiteDatabase dbProbes; private String service; private Cursor c; @SuppressWarnings("static-access") ScanPortTask(Activity activity, String ip, int timeout) { super(activity, ip, timeout); WeakReference<Activity> a = new WeakReference<Activity>(activity); final Activity d = a.get(); if (d != null) { Db db = new Db(d.getApplicationContext()); dbServices = db.openDb(Db.DB_SERVICES); dbProbes = db.openDb(Db.DB_PROBES); } } @SuppressLint("UseSparseArrays") @Override protected void onPreExecute() { // Get start/end ports try { port_start = Integer.parseInt(prefs.getString( Prefs.KEY_PORT_START, Prefs.DEFAULT_PORT_START)); port_end = Integer.parseInt(prefs.getString(Prefs.KEY_PORT_END, Prefs.DEFAULT_PORT_END)); } catch (NumberFormatException e) { port_start = Integer.parseInt(Prefs.DEFAULT_PORT_START); port_end = Integer.parseInt(Prefs.DEFAULT_PORT_END); } nb_port = port_end - port_start + 2; // Initialize arrays and views host.banners = new HashMap<Integer, String>(); host.services = new HashMap<Integer, String>(); host.portsOpen = new ArrayList<Integer>(); host.portsClosed = new ArrayList<Integer>(); // mTabOpen.setText(getString(R.string.scan_open, 0)); // mTabClosed.setText(getString(R.string.scan_closed, 0)); // setProgress(0); } @SuppressLint("UseValueOf") @Override protected void onProgressUpdate(Object... values) { Log.i("In progress","Started scanPortTask"); if (!isCancelled()) { if (values.length == 3) { final Integer port = (Integer) values[0]; final int type = (Integer) values[1]; if (!port.equals(new Integer(0))) { if (type == AsyncPortscan.OPEN) { // Open if (values[2] != null) { host.banners.put(port, (String) values[2]); host.services.put(port, getPortService(port)); } if (findLocationAndAdd(host.portsOpen, port)) { host.services.put(port, getPortService(port)); adapter_open.add(PLACEHOLDER); cnt_open++; // mTabOpen.setText(getString(R.string.scan_open, // cnt_open)); } adapter_open.notifyDataSetChanged(); } else if (type == AsyncPortscan.CLOSED) { // Closed if (findLocationAndAdd(host.portsClosed, port)) { host.services.put(port, getPortService(port)); adapter_closed.add(PLACEHOLDER); cnt_closed++; // mTabClosed.setText(getString( // R.string.scan_closed, cnt_closed)); } adapter_closed.notifyDataSetChanged(); } else if (type == AsyncPortscan.UNREACHABLE) { cancel(true); makeToast(R.string.scan_host_unreachable); Log.e(TAG, "Host Unreachable: " + ipAddr + ":" + port); } // FIXME: do something ? else if (type == AsyncPortscan.TIMEOUT) { } else if (type == AsyncPortscan.FILTERED) { } } else { cancel(true); makeToast(R.string.scan_host_unreachable); Log.e(TAG, "Host Unreachable: " + ipAddr); } } // progress_current++; // setProgress(progress_current * PROGRESS_MAX / nb_port); } } @Override protected void onPostExecute(Void unused) { // Finishing if (prefs.getBoolean(Prefs.KEY_VIBRATE_FINISH, Prefs.DEFAULT_VIBRATE_FINISH) == true) { Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(ActivityDiscovery.VIBRATE); } if (host.portsOpen.size() == 0) { makeToast(R.string.scan_noport); } if (dbServices != null) { dbServices.close(); } if (dbProbes != null) { dbProbes.close(); } stopScan(); makeToast(R.string.scan_finished); Intent intent = new Intent(); intent.putExtra(HostBean.EXTRA, host); setResult(RESULT_OK, intent); Log.i("The other finish","Finished in onPostExecute"); //finish(); } @Override protected void onCancelled() { super.onCancelled(); makeToast(R.string.scan_canceled); if (dbServices != null) { dbServices.close(); } if (dbProbes != null) { dbProbes.close(); } stopScan(); } private String getPortService(int port) { service = null; // Determinate service with banners if (host.banners != null && host.banners.containsKey(port) && dbProbes != null) { try { Cursor c = dbProbes.rawQuery(REQ_PROBES, null); if (c.moveToFirst()) { do { try { final Pattern pattern = Pattern.compile(c .getString(1)); final Matcher matcher = pattern .matcher(host.banners.get(port)); if (matcher.find()) { service = c.getString(0); break; } } catch (PatternSyntaxException e) { // Log.e(TAG, e.getMessage()); } } while (c.moveToNext()); } c.close(); } catch (SQLiteException e) { Log.e(TAG, e.getMessage()); Editor edit = PreferenceManager .getDefaultSharedPreferences(ctxt).edit(); edit.putInt(Prefs.KEY_RESET_SERVICESDB, 1); edit.commit(); } } // Get the service from port number if (service == null && dbServices != null) { c = dbServices .rawQuery(REQ_SERVICE, new String[] { "" + port }); if (c.moveToFirst()) { service = c.getString(0); } else { service = getString(R.string.info_unknown); } c.close(); } return service; } } //*******************SCAN PORT TASK**************************************// }