aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/HostDbProvider.java
blob: c51a356c986c6c609bc94eb93d8cff31b6ccd0b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2007 Kenny Root (kenny at the-b.org)
 * 
 * This file is part of Connectbot.
 *
 *  Connectbot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Connectbot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Connectbot.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.theb.ssh;

import java.util.HashMap;

import org.theb.provider.HostDb;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class HostDbProvider extends ContentProvider {

	private SQLiteDatabase mDB;
	
	private static final String TAG = "HostDbProvider";
	private static final String DATABASE_NAME = "ssh_hosts.db";
	private static final int DATABASE_VERSION = 2;
	
	private static HashMap<String, String> HOSTS_LIST_PROJECTION_MAP;
	
	private static final int HOSTS = 1;
	private static final int HOST_ID = 2;
	
	private static final UriMatcher URL_MATCHER;
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE hosts (_id INTEGER PRIMARY KEY,"
					+ "hostname TEXT," + "username TEXT," + "port INTEGER,"
					+ "hostkey TEXT" + ")");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS hosts");
			onCreate(db);
		}
		
	}
	
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		int count;
		switch (URL_MATCHER.match(uri)) {
		case HOSTS:
			count = mDB.delete("ssh_hosts", where, whereArgs);
			break;
		
		case HOST_ID:
			String segment = uri.getPathSegments().get(1);
			count = mDB.delete("hosts", "_id="
					+ segment
					+ (!TextUtils.isEmpty(where) ? " AND (" + where
							+ ')' : ""), whereArgs);
			break;
			
		default:
			throw new IllegalArgumentException("Unknown Delete " + uri);
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (URL_MATCHER.match(uri)) {
		case HOSTS:
			return "vnd.android.cursor.dir/vnd.theb.host";
		case HOST_ID:
			return "vnd.android.cursor.item/vnd.theb.host";
		default:
			throw new IllegalArgumentException("Unknown getType " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		long rowID;
		
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		/*
		if (URL_MATCHER.match(uri) != HOSTS) {
			throw new IllegalArgumentException("Unknown Insert " + uri);
		}
		*/
		if (values.containsKey(HostDb.Hosts.HOSTNAME) == false) {
			values.put(HostDb.Hosts.HOSTNAME, "");
		}
		
		if (values.containsKey(HostDb.Hosts.USERNAME) == false) {
			values.put(HostDb.Hosts.USERNAME, "");
		}
		
		if (values.containsKey(HostDb.Hosts.PORT) == false) {
			values.put(HostDb.Hosts.PORT, 22);
		}
		
		if (values.containsKey(HostDb.Hosts.HOSTKEY) == false) {
			values.put(HostDb.Hosts.HOSTKEY, "");
		}
		
		rowID = mDB.insert("hosts", "host", values);
		if (rowID > 0) {
			Uri newUri = ContentUris.withAppendedId(HostDb.Hosts.CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(newUri, null);
			return newUri;
		}
		
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		DatabaseHelper dbHelper = new DatabaseHelper();
		mDB = dbHelper.openDatabase(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
		return (mDB == null) ? false : true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		
		switch (URL_MATCHER.match(uri)) {
		case HOSTS:
			qb.setTables("hosts");
			qb.setProjectionMap(HOSTS_LIST_PROJECTION_MAP);
			break;
			
		case HOST_ID:
			qb.setTables("hosts");
			qb.appendWhere("_id=" + uri.getPathSegments().get(1));
			break;
			
		default:
			throw new IllegalArgumentException("Unknown Query " + uri);
		}
		
		String orderBy;
		if (TextUtils.isEmpty(sortOrder)) {
			orderBy = HostDb.Hosts.DEFAULT_SORT_ORDER;
		} else {
			orderBy = sortOrder;
		}
		
		Cursor c = qb.query(mDB, projection, selection, selectionArgs, null,
				null, orderBy);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where,
			String[] whereArgs) {
		int count;
		
		switch (URL_MATCHER.match(uri)) {
		case HOSTS:
			count = mDB.update("hosts", values, where, whereArgs);
			break;
			
        case HOST_ID:
            String segment = uri.getPathSegments().get(1);
            count = mDB
                    .update("hosts", values, "_id="
                            + segment
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown Update " + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        return count;

	}

	static {
		URL_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URL_MATCHER.addURI("org.theb.provider.HostDb", "hosts", HOSTS);
		URL_MATCHER.addURI("org.theb.provider.HostDb", "hosts/#", HOST_ID);
		
		HOSTS_LIST_PROJECTION_MAP = new HashMap<String, String>();
		HOSTS_LIST_PROJECTION_MAP.put(HostDb.Hosts._ID, "_id");
		HOSTS_LIST_PROJECTION_MAP.put(HostDb.Hosts.HOSTNAME, "hostname");
		HOSTS_LIST_PROJECTION_MAP.put(HostDb.Hosts.USERNAME, "username");
		HOSTS_LIST_PROJECTION_MAP.put(HostDb.Hosts.PORT, "port");
		HOSTS_LIST_PROJECTION_MAP.put(HostDb.Hosts.HOSTKEY, "hostkey");
	}
}