Back to project page Android-Apps.
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.
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; } }