Android Open Source - Android-Apps Hosts Data Source






From Project

Back to project page Android-Apps.

License

The source code is released under:

Apache License

If you think the Android project Android-Apps 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

package com.kniezrec.xbmcgear.preferences;
/*  w w  w .j ava2 s.  c o  m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;

import com.kniezrec.xbmcgear.connection.AndroidApplication;
import com.kniezrec.xbmcgear.connection.Connection;
import com.kniezrec.xbmcgear.connection.JSONRequestFactory;

import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;

public class HostsDataSource {
    private final static String TAG = "HostDataSource";
    private static HostsDataSource hostDataSource = null;
    private final HostsDatabaseHelper dbHelper;
    private final String[] allColumns = {
            HostTable.COLUMN_ID,
            HostTable.COL_INSTANCE_NAME, HostTable.COL_HOST_IP,
            HostTable.COL_HTTP_PORT, HostTable.COL_USER_NAME,
            HostTable.COL_PASSWORD, HostTable.COL_MAC
    };
    // Database fields
    private SQLiteDatabase database;
    private ArrayList<Host> dbMirror = null;

    private HostsDataSource(Context context) {
        dbHelper = new HostsDatabaseHelper(context);
    }

    public static synchronized HostsDataSource getInstance() {
        if (hostDataSource == null) {
            hostDataSource = new HostsDataSource(AndroidApplication
                    .getInstance().getApplicationContext());
            hostDataSource.open();
        }
        return hostDataSource;
    }

    public ArrayList<Host> getDbMirror() {
        if (dbMirror == null) {
            loadAllHosts();
        }
        return dbMirror;
    }

    void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
        hostDataSource = null;
    }

    public void removeHost(Host h) {
        dbMirror.remove(h);
        database.delete(HostTable.TABLE_ACT,
                HostTable.COLUMN_ID + "='" + h.getID() + "'", null);
    }

    public void addHost(Host h) {
        ContentValues values = new ContentValues();
        values.put(HostTable.COL_INSTANCE_NAME, h.getProfileName());
        values.put(HostTable.COL_HOST_IP, h.getIp());
        values.put(HostTable.COL_HTTP_PORT, h.getPort());
        values.put(HostTable.COL_USER_NAME, h.getUsername());
        values.put(HostTable.COL_PASSWORD, h.getPassword());
        values.put(HostTable.COL_MAC, h.getHardware());
        long id = database.insert(HostTable.TABLE_ACT, null, values);
        h.setID((int) id);
        if (dbMirror != null) {
            dbMirror.add(h);
        }
        new LongOperation().execute(h);
    }


    private class LongOperation extends AsyncTask<Host, Void, Host> {

        @Override
        protected Host doInBackground(Host... params) {
            Host h = params[0];
            HttpURLConnection urlConn = null;

            for (int i = 0; i < 3; i++) {
                try {
                    urlConn = Connection.getURLConnection(h);
                    OutputStream os = urlConn.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    writer.write(JSONRequestFactory.GET_MAC_ADDR);
                    writer.flush();
                    writer.close();
                    os.close();
                    if (urlConn.getResponseCode() == HttpStatus.SC_OK) {
                        InputStream in = new BufferedInputStream(
                                urlConn.getInputStream());
                        String response = readStream(in);
                        JSONObject json = new JSONObject(response);
                        JSONObject result = json.optJSONObject("result");
                        if (result != null) {
                            String addr = result.optString("Network.MacAddress");
                            if (addr.contains(":")) {
                                h.setHardware(addr);

                                break;
                            }
                        }

                        in.close();
                    }
                } catch (ClientProtocolException e) {
                    Log.v(TAG, e.getMessage());
                } catch (IOException e) {
                    Log.v(TAG, e.getMessage());
                } catch (JSONException e) {
                    Log.v(TAG, e.getMessage());
                } finally {
                    if (urlConn != null) {
                        urlConn.disconnect();
                    }
                }
            }
            return h;
        }

        @Override
        protected void onPostExecute(Host result) {
            updateHost(result);
            SharedPreferencesUtil.updateIfNeeded(result);
        }
    }

    private String readStream(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        String line;
        try {
            while ((line = r.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        r.close();
        return sb.toString();
    }

    public int getPosition(int id) {
        int pos = -1;
        if (dbMirror == null) {
            return pos;
        }
        for (int i = 0; i < dbMirror.size(); i++) {
            if (id == dbMirror.get(i).getID()) {
                pos = i;
                break;
            }
        }
        return pos;
    }

    public int contains(Host h) {
        if (dbMirror != null) {
            for (Host host : dbMirror) {
                if (h.getIp().equals(host.getIp())
                        && h.getProfileName().equals(host.getProfileName())) {
                    return host.getID();
                }
            }
        }
        return -1;
    }

    public void updateHost(Host h) {
        int pos = getPosition(h.getID());
        if (pos >= 0) {
            dbMirror.set(pos, h);
        }

        ContentValues values = new ContentValues();
        values.put(HostTable.COLUMN_ID, h.getID());
        values.put(HostTable.COL_INSTANCE_NAME, h.getProfileName());
        values.put(HostTable.COL_HOST_IP, h.getIp());
        values.put(HostTable.COL_HTTP_PORT, h.getPort());
        values.put(HostTable.COL_USER_NAME, h.getUsername());
        values.put(HostTable.COL_PASSWORD, h.getPassword());
        values.put(HostTable.COL_MAC, h.getHardware());

        database.update(HostTable.TABLE_ACT, values,
                HostTable.COLUMN_ID + "=?",
                new String[]{
                        Integer.toString(h.getID())
                });
    }

    private void loadAllHosts() {
        dbMirror = new ArrayList<Host>();
        Cursor cursor = database.query(HostTable.TABLE_ACT, allColumns, null,
                null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Host host = cursorToHost(cursor);
            dbMirror.add(host);
            cursor.moveToNext();
        }
        cursor.close();
    }

    private Host cursorToHost(Cursor cursor) {
        Host host = new Host();
        host.setID(cursor.getInt(0));
        host.setProfileName(cursor.getString(1));
        host.setIp(cursor.getString(2));
        host.setPort(cursor.getInt(3));
        host.setUsername(cursor.getString(4));
        host.setPassword(cursor.getString(5));
        host.setHardware(cursor.getString(6));
        return host;
    }
}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java