Search in sources :

Example 11 with Cleanup

use of lombok.Cleanup in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class ImportTask method doInBackground.

/**
     * Imports data from cell_towers.csv
     * <p/>
     * <blockquote>
     * opencellid.csv layout:
     * lat,lon,mcc,mnc,lac,cellid,averageSignalStrength,range,samples,changeable,radio,rnc,cid,psc,
     * tac,pci,sid,nid,bid
     * <p/>
     * example:
     * 52.201454,21.065345,260,2,58140,42042781,-59,1234,3,1,UMTS,641,34205,,,,
     * <p/>
     * cell_towers.csv layout:
     * radio,mcc,net,area,cell,unit,lon,lat,range,samples,changeable,created,updated,averageSignal
     * 0 radio
     * 1 mcc
     * 2 net (mnc)
     * 3 area (lac)
     * 4 cell (long)
     * 5 unit
     * 6 lon
     * 7 lat
     * 8 range
     * 9 samples
     * 10 changeable
     * 11 created
     * 12 updated
     * 13 averageSignal
     * <p/>
     * example:
     * UMTS,260,2,58140,42042781,,21.03006,52.207811,21,2,1,1379428153,1458591497,-92
     * </blockquote>
     */
@Override
protected String doInBackground(String... commandString) {
    try {
        @Cleanup Realm realm = Realm.getDefaultInstance();
        Long elapsedSeconds = System.currentTimeMillis() / 1000;
        // Prepare filtering values
        final String mccFilter = String.valueOf(mobileCountryCode);
        final String mncFilter = String.valueOf(mobileNetworkCode);
        long progress = 0;
        long failedRecords = 0;
        CSVReader csvReader = null;
        try {
            String[] next;
            csvReader = new CSVReader(createFileReader());
            // skip header
            csvReader.readNext();
            String[] opencellid_csv = new String[14];
            while ((next = csvReader.readNext()) != null) {
                if (next.length < 14) {
                    log.warn("Not enough values in string: " + Arrays.toString(next));
                    ++failedRecords;
                    continue;
                }
                if (!next[1].equals(mccFilter) || !next[2].equals(mncFilter)) {
                    continue;
                }
                if (next[6].isEmpty() || next[7].isEmpty()) {
                    continue;
                }
                GeoLocation location = GeoLocation.fromDegrees(Double.parseDouble(next[7]), Double.parseDouble(next[6]));
                if (location.distanceTo(currentLocation, EARTH_RADIUS) > locationRadius) {
                    continue;
                }
                try {
                    // set non-existent range, avgSignal, etc to "0" so they
                    // will be possibly filtered by checkDBe
                    // lat
                    opencellid_csv[0] = next[7];
                    // lon
                    opencellid_csv[1] = next[6];
                    // mcc
                    opencellid_csv[2] = next[1];
                    // mnc
                    opencellid_csv[3] = next[2];
                    // lac
                    opencellid_csv[4] = next[3];
                    // cellid, long
                    opencellid_csv[5] = next[4];
                    // averageSignalStrength
                    opencellid_csv[6] = stringOrZero(next[13]);
                    // range
                    opencellid_csv[7] = stringOrZero(next[8]);
                    // samples
                    opencellid_csv[8] = stringOrZero(next[9]);
                    // changeable
                    opencellid_csv[9] = stringOrZero(next[10]);
                    // radio
                    opencellid_csv[10] = next[0];
                    // rnc, not used
                    opencellid_csv[11] = null;
                    // cid, not used
                    opencellid_csv[12] = null;
                    // psc, not present
                    opencellid_csv[13] = null;
                    Date dateCreated = dateOrNow(next[11]);
                    Date dateUpdated = dateOrNow(next[12]);
                    mDbAdapter.addCSVRecord(realm, opencellid_csv, dateCreated, dateUpdated);
                    ++progress;
                } catch (NumberFormatException e) {
                    log.warn("Problem parsing a record: " + Arrays.toString(opencellid_csv), e);
                    ++failedRecords;
                }
                if ((progress % 100) == 0) {
                    log.debug("Imported records for now: " + String.valueOf(progress));
                // do not know progress because determining line count in gzipped
                // multi-gigabyte file is slow
                //publishProgress((int) progress, (int) totalRecords);
                }
                if ((progress % 1000) == 0) {
                    try {
                        // wait 1 second to allow user to see progress bar.
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        } finally {
            if (csvReader != null) {
                csvReader.close();
            }
        }
        elapsedSeconds = (System.currentTimeMillis() / 1000) - elapsedSeconds;
        log.debug("Importing took " + String.valueOf(elapsedSeconds) + " seconds");
        log.debug("Imported records: " + String.valueOf(progress));
        log.debug("Failed records: " + String.valueOf(failedRecords));
        return "Successful";
    } catch (IOException e) {
        log.warn("Problem reading data from CSV", e);
        return null;
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) IOException(java.io.IOException) Cleanup(lombok.Cleanup) Realm(io.realm.Realm) Date(java.util.Date)

Example 12 with Cleanup

use of lombok.Cleanup in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class MapFragment method loadOcidMarkersByNetwork.

private void loadOcidMarkersByNetwork() {
    // Check if OpenCellID data exists and if so load this now
    List<CellTowerMarker> items = new LinkedList<>();
    String networkOperator = tm.getNetworkOperator();
    int currentMmc = 0;
    int currentMnc = 0;
    if (networkOperator != null && networkOperator.length() > 3) {
        currentMmc = Integer.parseInt(networkOperator.substring(0, 3));
        currentMnc = Integer.parseInt(networkOperator.substring(3));
    }
    Drawable cellTowerMarkerIcon = getResources().getDrawable(R.drawable.ic_map_pin_green);
    @Cleanup Realm realm = Realm.getDefaultInstance();
    RealmResults<Import> importRealmResults = mDbHelper.returnOcidBtsByNetwork(realm, currentMmc, currentMnc).findAll();
    for (Import anImport : importRealmResults) {
        final int cellID = anImport.getCellId();
        final int lac = anImport.getLocationAreaCode();
        final int mcc = anImport.getMobileCountryCode();
        final int mnc = anImport.getMobileNetworkCode();
        final int psc = anImport.getPrimaryScramblingCode();
        final String rat = anImport.getRadioAccessTechnology();
        final double dLat = anImport.getGpsLocation().getLatitude();
        final double dLng = anImport.getGpsLocation().getLongitude();
        final GeoPoint location = new GeoPoint(dLat, dLng);
        //where is c.getString(6)AvgSigStr
        final int samples = anImport.getSamples();
        // Add map marker for CellID
        CellTowerMarker ovm = new CellTowerMarker(getActivity(), mMap, "Cell ID: " + cellID, "", location, new MarkerData(getContext(), String.valueOf(cellID), String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), String.valueOf(lac), String.valueOf(mcc), String.valueOf(mnc), String.valueOf(psc), rat, String.valueOf(samples), false));
        ovm.setIcon(cellTowerMarkerIcon);
        items.add(ovm);
    }
    mCellTowerGridMarkerClusterer.addAll(items);
}
Also used : MarkerData(com.secupwn.aimsicd.map.MarkerData) Import(com.secupwn.aimsicd.data.model.Import) CellTowerMarker(com.secupwn.aimsicd.map.CellTowerMarker) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Cleanup(lombok.Cleanup) LinkedList(java.util.LinkedList) GeoPoint(org.osmdroid.util.GeoPoint) GeoPoint(org.osmdroid.util.GeoPoint) Realm(io.realm.Realm)

Example 13 with Cleanup

use of lombok.Cleanup in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class CellTracker method onLocationChanged.

/**
     * Add entries to the {@link com.secupwn.aimsicd.data.model.Measure Measure} realm
     */
public void onLocationChanged(Location loc) {
    // TODO: See issue #555 (DeviceApi17.java is using API 18 CellInfoWcdma calls.
    if (Build.VERSION.SDK_INT > 17) {
        DeviceApi18.loadCellInfo(tm, device);
    }
    if (!device.cell.isValid()) {
        CellLocation cellLocation = tm.getCellLocation();
        if (cellLocation != null) {
            switch(device.getPhoneId()) {
                case TelephonyManager.PHONE_TYPE_NONE:
                case TelephonyManager.PHONE_TYPE_SIP:
                case TelephonyManager.PHONE_TYPE_GSM:
                    GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
                    // CID
                    device.cell.setCellId(gsmCellLocation.getCid());
                    // LAC
                    device.cell.setLocationAreaCode(gsmCellLocation.getLac());
                    // PSC
                    device.cell.setPrimaryScramblingCode(gsmCellLocation.getPsc());
                    break;
                case TelephonyManager.PHONE_TYPE_CDMA:
                    CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;
                    // BSID ??
                    device.cell.setCellId(cdmaCellLocation.getBaseStationId());
                    // NID
                    device.cell.setLocationAreaCode(cdmaCellLocation.getNetworkId());
                    // SID
                    device.cell.setSid(cdmaCellLocation.getSystemId());
                    // MNC <== BUG!??
                    device.cell.setMobileNetworkCode(cdmaCellLocation.getSystemId());
                    break;
            }
        }
    }
    if (loc != null && (Double.doubleToRawLongBits(loc.getLatitude()) != 0 && Double.doubleToRawLongBits(loc.getLongitude()) != 0)) {
        // gpsd_lon
        device.cell.setLon(loc.getLongitude());
        // gpsd_lat
        device.cell.setLat(loc.getLatitude());
        // speed        // TODO: Remove, we're not using it!
        device.cell.setSpeed(loc.getSpeed());
        // gpsd_accu
        device.cell.setAccuracy(loc.getAccuracy());
        // -- [deg]??   // TODO: Remove, we're not using it!
        device.cell.setBearing(loc.getBearing());
        //
        device.setLastLocation(loc);
        // Store last known location in preference
        SharedPreferences.Editor prefsEditor;
        prefsEditor = prefs.edit();
        prefsEditor.putString(context.getString(R.string.data_last_lat_lon), String.valueOf(loc.getLatitude()) + ":" + String.valueOf(loc.getLongitude()));
        prefsEditor.apply();
        // TODO: Is correct behaviour? We should consider logging all cells, even without GPS.
        if (trackingCell) {
            // This also checks that the locationAreaCode are cid are not in DB before inserting
            @Cleanup Realm realm = Realm.getDefaultInstance();
            dbHelper.insertBTS(realm, device.cell);
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) CdmaCellLocation(android.telephony.cdma.CdmaCellLocation) GsmCellLocation(android.telephony.gsm.GsmCellLocation) CellLocation(android.telephony.CellLocation) CdmaCellLocation(android.telephony.cdma.CdmaCellLocation) GsmCellLocation(android.telephony.gsm.GsmCellLocation) Cleanup(lombok.Cleanup) Realm(io.realm.Realm)

Example 14 with Cleanup

use of lombok.Cleanup in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class LocationTracker method lastKnownLocation.

public GeoLocation lastKnownLocation() {
    GeoLocation loc = null;
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null && (Double.doubleToLongBits(location.getLatitude()) != 0 && Double.doubleToLongBits(location.getLongitude()) != 0)) {
        TruncatedLocation TruncatedLocation = new TruncatedLocation(location);
        loc = GeoLocation.fromDegrees(TruncatedLocation.getLatitude(), TruncatedLocation.getLongitude());
    } else {
        location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null && (Double.doubleToLongBits(location.getLatitude()) != 0 && Double.doubleToLongBits(location.getLongitude()) != 0)) {
            TruncatedLocation TruncatedLocation = new TruncatedLocation(location);
            loc = GeoLocation.fromDegrees(TruncatedLocation.getLatitude(), TruncatedLocation.getLongitude());
        } else {
            String coords = prefs.getString(context.getString(R.string.data_last_lat_lon), null);
            if (coords != null) {
                String[] coord = coords.split(":");
                loc = GeoLocation.fromDegrees(Double.valueOf(coord[0]), Double.valueOf(coord[1]));
            } else {
                // get location from MCC
                try {
                    Cell cell = context.getCell();
                    if (cell != null) {
                        log.debug("Looking up MCC " + cell.getMobileCountryCode());
                        @Cleanup Realm realm = Realm.getDefaultInstance();
                        GpsLocation defLoc = mDbHelper.getDefaultLocation(realm, cell.getMobileCountryCode());
                        loc = GeoLocation.fromDegrees(defLoc.getLatitude(), defLoc.getLongitude());
                    }
                } catch (Exception e) {
                    log.error("Unable to get location from MCC", e);
                }
            }
        }
    }
    if (loc != null) {
        log.info("Last known location " + loc.toString());
    }
    return loc;
}
Also used : TruncatedLocation(com.secupwn.aimsicd.utils.TruncatedLocation) GpsLocation(com.secupwn.aimsicd.data.model.GpsLocation) GeoLocation(com.secupwn.aimsicd.utils.GeoLocation) Cell(com.secupwn.aimsicd.utils.Cell) Cleanup(lombok.Cleanup) Realm(io.realm.Realm) GeoLocation(com.secupwn.aimsicd.utils.GeoLocation) GpsLocation(com.secupwn.aimsicd.data.model.GpsLocation) TruncatedLocation(com.secupwn.aimsicd.utils.TruncatedLocation) Location(android.location.Location)

Example 15 with Cleanup

use of lombok.Cleanup in project Android-IMSI-Catcher-Detector by CellularPrivacy.

the class UncaughtExceptionLogger method processException.

private void processException(Thread thread, Throwable ex) {
    Optional<File> optional = getDir();
    if (optional.isPresent()) {
        Date date = new Date();
        File file = new File(getDir().get(), "error-" + date.getTime() + "-" + BuildConfig.VERSION_CODE + ".log");
        try {
            @Cleanup PrintWriter printWriter = new PrintWriter(file);
            printWriter.println("System Information:");
            printClass(printWriter, Build.VERSION.class);
            printWriter.println();
            printWriter.println();
            printWriter.println("App Information:");
            printClass(printWriter, BuildConfig.class);
            printWriter.println();
            printWriter.println();
            printWriter.println("Crash Information");
            printWriter.print("Timestamp: ");
            printWriter.println(DateFormat.getDateTimeInstance(LONG, LONG, ENGLISH).format(date));
            printWriter.print("Thread: ");
            printWriter.println(thread.toString());
            printWriter.println("Stacktrace:");
            ex.printStackTrace(printWriter);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Also used : Build(android.os.Build) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) Cleanup(lombok.Cleanup) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Aggregations

Cleanup (lombok.Cleanup)16 Realm (io.realm.Realm)13 SmsDetectionString (com.secupwn.aimsicd.data.model.SmsDetectionString)4 SmsData (com.secupwn.aimsicd.data.model.SmsData)3 IOException (java.io.IOException)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)2 Drawable (android.graphics.drawable.Drawable)2 CdmaCellLocation (android.telephony.cdma.CdmaCellLocation)2 GsmCellLocation (android.telephony.gsm.GsmCellLocation)2 Import (com.secupwn.aimsicd.data.model.Import)2 CellTowerMarker (com.secupwn.aimsicd.map.CellTowerMarker)2 MarkerData (com.secupwn.aimsicd.map.MarkerData)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2 GeoPoint (org.osmdroid.util.GeoPoint)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1