Android Open Source - android_network_discovery Save






From Project

Back to project page android_network_discovery.

License

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.

Java Source Code

/*
 * Copyright (C) 2009-2010 Aubort Jean-Baptiste (Rorist)
 * Licensed under GNU's GPL 2, see README
 *///w w  w. j a  v  a2s. c  o m

package info.lamatricexiste.network.Utils;

import info.lamatricexiste.network.Network.HostBean;

import java.lang.IllegalStateException;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class Save {

    private static final String TAG = "Save";
    private static final String SELECT = "select name from nic where mac=?";
    private static final String INSERT = "insert or replace into nic (name,mac) values (?,?)";
    private static final String DELETE = "delete from nic where mac=?";
    private static SQLiteDatabase db;

    public void closeDb(){
        if(db != null && db.isOpen()){
            db.close();
        }
    }

    public synchronized String getCustomName(HostBean host) {
        String name = null;
        Cursor c = null;
        try {
            db = getDb();
            c = db.rawQuery(SELECT, new String[] { host.hardwareAddress.replace(":", "").toUpperCase() });
            if (c.moveToFirst()) {
                name = c.getString(0);
            } else if(host.hostname != null) {
                name = host.hostname;
            }
        } catch (SQLiteException e) {
            Log.e(TAG, e.getMessage());
        } catch (IllegalStateException e) {
            Log.e(TAG, e.getMessage());
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return name;
    }

    public void setCustomName(final String name, final String mac) {
        db = Db.openDb(Db.DB_SAVES, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READWRITE);
        try {
            if (db.isOpen()) {
                db.execSQL(INSERT, new String[] { name, mac.replace(":", "").toUpperCase() });
            }
        } catch (SQLiteException e) {
            Log.e(TAG, e.getMessage());
        } finally {
            closeDb();
        }
    }

    public boolean removeCustomName(String mac) {
        db = Db.openDb(Db.DB_SAVES, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READWRITE);
        try {
            if (db.isOpen()) {
                db.execSQL(DELETE, new String[] { mac.replace(":", "").toUpperCase() });
                return true;
            }
            return false;
        } catch (SQLiteException e) {
            Log.e(TAG, e.getMessage());
            return false;
        } finally {
            closeDb();
        }
    }

    private static synchronized SQLiteDatabase getDb(){
        if(db == null || !db.isOpen()) {
            // FIXME: read only ?
            db = Db.openDb(Db.DB_SAVES, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
        }
        return db;
    }
}




Java Source Code List

info.lamatricexiste.network.AbstractDiscovery.java
info.lamatricexiste.network.ActivityDiscovery.java
info.lamatricexiste.network.ActivityMain.java
info.lamatricexiste.network.ActivityNet.java
info.lamatricexiste.network.ActivityPortscan.java
info.lamatricexiste.network.AsyncPortscan.java
info.lamatricexiste.network.DatabaseHelper.java
info.lamatricexiste.network.DefaultDiscovery.java
info.lamatricexiste.network.DiscoverActivity.java
info.lamatricexiste.network.DnsDiscovery.java
info.lamatricexiste.network.NetworkChange.java
info.lamatricexiste.network.Network.Banner.java
info.lamatricexiste.network.Network.DownloadFile.java
info.lamatricexiste.network.Network.HardwareAddress.java
info.lamatricexiste.network.Network.HostBean.java
info.lamatricexiste.network.Network.NetInfo.java
info.lamatricexiste.network.Network.OsFingerprint.java
info.lamatricexiste.network.Network.Ping.java
info.lamatricexiste.network.Network.RateControl.java
info.lamatricexiste.network.Network.SendSmbNegotiate.java
info.lamatricexiste.network.Utils.DbUpdate.java
info.lamatricexiste.network.Utils.Db.java
info.lamatricexiste.network.Utils.Export.java
info.lamatricexiste.network.Utils.Help.java
info.lamatricexiste.network.Utils.Prefs.java
info.lamatricexiste.network.Utils.Save.java
info.lamatricexiste.network.Utils.UpdateNicDb.java
info.lamatricexiste.network.connectivity.java
info.lamatricexiste.network.portscan.java
info.lamatricexiste.network.wifiinfo.java