Update list automatically when certificates are changed.

This commit is contained in:
Felix Ableitner 2014-07-23 01:30:53 +02:00
parent 3ffecaa310
commit 287c6462ff
4 changed files with 74 additions and 10 deletions

View file

@ -18,6 +18,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import eu.chainfire.libsuperuser.Shell;
@ -31,11 +32,23 @@ public class CertificateManagerTest extends AndroidTestCase {
private CertificateManager mCertificateManager;
private CountDownLatch mLatch;
@Override
protected void setUp() throws Exception {
super.setUp();
mCertificateManager = new CertificateManager();
mCertificateManager.setOnCertificateChangedListener(
new CertificateManager.OnCertificateChangedListener() {
@Override
public void onCertificateChanged() {
if (mLatch == null)
return;
mLatch.countDown();
}
});
assertTrue(Shell.SU.available());
}
@ -44,13 +57,25 @@ public class CertificateManagerTest extends AndroidTestCase {
public void testUserCertificates() {
Certificate cert = copyCertificate(false);
assertTrue(mCertificateManager.getCertificates(false).contains(cert));
mLatch = new CountDownLatch(1);
assertTrue(mCertificateManager.deleteCertificate(cert));
assertReadOnly();
waitForCallback();
}
private void waitForCallback() {
try {
mLatch.await();
}
catch (InterruptedException e) {
fail();
}
}
@MediumTest
public void testMoveCertificate() {
Certificate cert = copyCertificate(false);
mLatch = new CountDownLatch(2);
Certificate newCertificate = mCertificateManager.moveCertificateToSystem(cert);
assertReadOnly();
assertNotNull(newCertificate);
@ -61,6 +86,7 @@ public class CertificateManagerTest extends AndroidTestCase {
assertTrue(mCertificateManager.getCertificates(true).contains(newCertificate));
assertTrue(mCertificateManager.deleteCertificate(newCertificate));
assertReadOnly();
waitForCallback();
}
/**
@ -100,7 +126,9 @@ public class CertificateManagerTest extends AndroidTestCase {
public void testSystemCertificates() {
Certificate cert = copyCertificate(true);
assertTrue(mCertificateManager.getCertificates(true).contains(cert));
mLatch = new CountDownLatch(1);
assertTrue(mCertificateManager.deleteCertificate(cert));
waitForCallback();
assertReadOnly();
}

View file

@ -1,6 +1,7 @@
package com.nutomic.zertman;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -8,10 +9,13 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
/**
* List Adapter for {@link Certificate}.
*/
public class CertificateAdapter extends ArrayAdapter<Certificate> {
public class CertificateAdapter extends ArrayAdapter<Certificate> implements
CertificateManager.OnCertificateChangedListener{
private CertificateManager mCertificateManager;
@ -73,4 +77,23 @@ public class CertificateAdapter extends ArrayAdapter<Certificate> {
return convertView;
}
@Override
public void onCertificateChanged() {
new UpdateListTask().execute();
}
private class UpdateListTask extends AsyncTask<Void, Void, List<Certificate>> {
@Override
protected List<Certificate> doInBackground(Void... params) {
List<Certificate> ret = mCertificateManager.getCertificates(false);
ret.addAll(mMovedCertificatesStorage.list());
return ret;
}
@Override
protected void onPostExecute(List<Certificate> certificate) {
clear();
addAll(certificate);
}
}
}

View file

@ -19,6 +19,10 @@ public class CertificateManager {
public static final File SYSTEM_CERTIFICATES_DIR = new File("/system/etc/security/cacerts");
public interface OnCertificateChangedListener {
public void onCertificateChanged();
}
/**
* Possible options for partition mounting (used for #remountSystem()).
*/
@ -27,6 +31,12 @@ public class CertificateManager {
ReadWrite
}
private OnCertificateChangedListener mOnCertificateChangedListener;
public void setOnCertificateChangedListener(OnCertificateChangedListener listener) {
mOnCertificateChangedListener = listener;
}
/**
* Lists all certificates for either user or system.
*
@ -56,7 +66,11 @@ public class CertificateManager {
run("chmod 644 " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName());
remountSystem(Mode.ReadOnly);
deleteCertificate(certificate);
return new Certificate(certificate.getFile().getName(), true);
Certificate newCert = new Certificate(certificate.getFile().getName(), true);
if (mOnCertificateChangedListener != null) {
mOnCertificateChangedListener.onCertificateChanged();
}
return newCert;
}
/**
@ -68,6 +82,9 @@ public class CertificateManager {
remountSystem(Mode.ReadWrite);
boolean success = run("rm " + certificate.getFile().getAbsolutePath());
remountSystem(Mode.ReadOnly);
if (mOnCertificateChangedListener != null) {
mOnCertificateChangedListener.onCertificateChanged();
}
return success;
}

View file

@ -30,14 +30,8 @@ public class MainActivity extends ListActivity {
mMovedCertificatesStorage = new MovedCertificatesStorage(this);
mCertificateAdapter =
new CertificateAdapter(this, mCertificateManager, mMovedCertificatesStorage);
new Thread(new Runnable() {
@Override
public void run() {
mCertificateAdapter.addAll(mCertificateManager.getCertificates(false));
mCertificateAdapter.addAll(mMovedCertificatesStorage.list());
}
}).start();
mCertificateAdapter.onCertificateChanged();
mCertificateManager.setOnCertificateChangedListener(mCertificateAdapter);
mListView.setAdapter(mCertificateAdapter);
}
@ -47,6 +41,8 @@ public class MainActivity extends ListActivity {
@Override
protected void onResume() {
super.onResume();
mCertificateAdapter.onCertificateChanged();
final List<Certificate> list = mCertificateManager.getCertificates(false);
if (!list.isEmpty()) {
new AlertDialog.Builder(this)