Added MovedCertificatesStorage, only list moved system certs in GUI.

This commit is contained in:
Felix Ableitner 2014-07-17 16:59:42 +02:00
parent 5159fa88b1
commit 9cbfa8bf86
3 changed files with 166 additions and 1 deletions

View file

@ -0,0 +1,66 @@
package com.nutomic.zertman.test;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import com.nutomic.zertman.Certificate;
import com.nutomic.zertman.MovedCertificatesStorage;
import java.util.List;
/**
* NOTE: This test works on the app's actual data.
*/
public class MovedCertificatesStorageTest extends AndroidTestCase {
private MovedCertificatesStorage mMovedCertificatesStorage;
@Override
protected void setUp() throws Exception {
super.setUp();
mMovedCertificatesStorage = new MovedCertificatesStorage(getContext());
assertTrue(mMovedCertificatesStorage.list().isEmpty());
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mMovedCertificatesStorage.close();
}
@SmallTest
public void testInvalidInsert() {
assertFalse(mMovedCertificatesStorage.insert(new Certificate("user", false)));
}
@SmallTest
public void testInvalidDelete() {
assertFalse(mMovedCertificatesStorage.delete(new Certificate("cert", true)));
}
@SmallTest
public void testDuplicateInsert() {
Certificate c = new Certificate("dupe", true);
assertTrue(mMovedCertificatesStorage.insert(c));
assertFalse(mMovedCertificatesStorage.insert(c));
assertTrue(mMovedCertificatesStorage.delete(c));
}
@MediumTest
public void testStorage() {
Certificate c1 = new Certificate("first", true);
Certificate c2 = new Certificate("second", true);
assertTrue(mMovedCertificatesStorage.insert(c1));
assertTrue(mMovedCertificatesStorage.insert(c2));
List<Certificate> list = mMovedCertificatesStorage.list();
assertEquals(list.get(0), c1);
assertEquals(list.get(1), c2);
assertTrue(mMovedCertificatesStorage.delete(c1));
assertTrue(mMovedCertificatesStorage.delete(c2));
assertTrue(mMovedCertificatesStorage.list().isEmpty());
}
}

View file

@ -13,6 +13,8 @@ public class MainActivity extends ListActivity {
private CertificateManager mCertificateManager;
private MovedCertificatesStorage mMovedCertificatesStorage;
/**
* Sets up ListView showing all certificates.
*/
@ -23,8 +25,10 @@ public class MainActivity extends ListActivity {
mListView = getListView();
mCertificateAdapter = new CertificateAdapter(this);
mCertificateManager = new CertificateManager();
mMovedCertificatesStorage = new MovedCertificatesStorage(this);
mCertificateAdapter.addAll(mCertificateManager.getCertificates(false));
mCertificateAdapter.addAll(mCertificateManager.getCertificates(true));
mCertificateAdapter.addAll(mMovedCertificatesStorage.list());
mListView.setAdapter(mCertificateAdapter);
}

View file

@ -0,0 +1,95 @@
package com.nutomic.zertman;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Keeps information about certificates that have been moved to system memory.
*/
public class MovedCertificatesStorage implements Closeable {
private static final String TAG = "MovedCertificatesStorage";
private MovedCertificatesHelper mDbHelper;
public MovedCertificatesStorage(Context context) {
mDbHelper = new MovedCertificatesHelper(context);
}
@Override
public void close() throws IOException {
mDbHelper.close();
}
public boolean insert(Certificate cert) {
if (!cert.isSystemCertificate())
return false;
ContentValues cv = new ContentValues();
cv.put(Table.COLUMN_NAME_FILE_NAME, cert.getFile().getName());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
long row = db.insert(Table.TABLE_NAME, null, cv);
return row != -1;
}
public List<Certificate> list() {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor c = db.query(
Table.TABLE_NAME, new String[]{Table.COLUMN_NAME_FILE_NAME},
null, null, null, null, null);
ArrayList<Certificate> list = new ArrayList<Certificate>(c.getCount());
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
list.add(new Certificate(
c.getString(c.getColumnIndex(Table.COLUMN_NAME_FILE_NAME)), true));
c.moveToNext();
}
c.close();
return list;
}
public boolean delete(Certificate cert) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
int count = db.delete(Table.TABLE_NAME, Table.COLUMN_NAME_FILE_NAME + " = ?",
new String[]{cert.getFile().getName()});
return count == 1;
}
public abstract class Table implements BaseColumns {
public static final String TABLE_NAME = "certificate";
public static final String COLUMN_NAME_FILE_NAME = "file_name";
}
private class MovedCertificatesHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "moved_certificates.db";
public MovedCertificatesHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + Table.TABLE_NAME + " (" +
Table._ID + " INTEGER PRIMARY KEY," +
Table.COLUMN_NAME_FILE_NAME + " TEXT UNIQUE)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}