diff src/com/five_ten_sg/connectbot/HostListActivity.java @ 132:265a4733edcb

read deployment.connections on startup for new host entries
author Carl Byington <carl@five-ten-sg.com>
date Thu, 19 Jun 2014 15:47:34 -0700
parents 77ac18bc1b2f
children 81cafbe7cd9b
line wrap: on
line diff
--- a/src/com/five_ten_sg/connectbot/HostListActivity.java	Thu Jun 19 09:57:31 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/HostListActivity.java	Thu Jun 19 15:47:34 2014 -0700
@@ -25,6 +25,7 @@
 import com.five_ten_sg.connectbot.transport.TransportFactory;
 import com.five_ten_sg.connectbot.util.HostDatabase;
 import com.five_ten_sg.connectbot.util.PreferenceConstants;
+import java.nio.file.Files;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.ListActivity;
@@ -40,6 +41,7 @@
 import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
@@ -181,6 +183,7 @@
                          || Intent.ACTION_PICK.equals(getIntent().getAction());
         // connect with hosts database and populate list
         hostdb = new HostDatabase(this);
+        createDeploymentHosts();    // build hosts from a deployment text file
         updateList();
         sortedByColor = prefs.getBoolean(PreferenceConstants.SORT_BY_COLOR, false);
         registerForContextMenu(getListView());
@@ -387,6 +390,46 @@
         return true;
     }
 
+    private void createDeploymentHosts() {
+        try {
+            String fn = Environment.getExternalStorageDirectory().getAbsolutePath() +
+                        File.separator + "deployment.connections";
+            BufferedReader reader = new BufferedReader(new FileReader(fn));
+            String line = null;
+            while ((line = reader.readLine()) != null) {
+                if (line.length() == 0) continue;               // empty
+                if (line.substring(0,1).equals("#")) continue;  // comment
+                if (!line.contains("://")) continue;            // invalid uri
+                Uri uri = Uri.parse(line);
+                ContentValues values = null;
+                while ((line = reader.readLine()).length() > 0) {
+                    String [] parts = line.split("=");
+                    if (parts.length() != 2) continue;
+                    if (values == null) values = new ContentValues();
+                    values.put(parts[0], parts[1]);
+                }
+                HostBean host = TransportFactory.findHost(hostdb, uri);
+                if (host == null) {
+                    host = TransportFactory.getTransport(uri.getScheme()).createHost(uri);
+                    host.setColor(HostDatabase.COLOR_GRAY);
+                    host.setPubkeyId(HostDatabase.PUBKEYID_ANY);
+                    hostdb.saveHost(host);
+                }
+                host = TransportFactory.findHost(hostdb, uri);
+                if (host == null) continue;
+                if (values == null) continue;
+                SQLiteDatabase db = hostdb.getWritableDatabase();
+                db.update(HostDatabase.TABLE_HOSTS, values, "_id = ?", new String[] { String.valueOf(host.getId()) });
+                db.close();
+            }
+            reader.close();
+            Files.delete(fn);
+        }
+        catch (Exception e) {
+            Log.d(TAG, "Deployment scan failed.", e);
+        }
+    }
+
     protected void updateList() {
         if (prefs.getBoolean(PreferenceConstants.SORT_BY_COLOR, false) != sortedByColor) {
             Editor edit = prefs.edit();