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

View file

@ -1,6 +1,7 @@
package com.nutomic.zertman; package com.nutomic.zertman;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -8,10 +9,13 @@ import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import java.util.List;
/** /**
* List Adapter for {@link Certificate}. * List Adapter for {@link Certificate}.
*/ */
public class CertificateAdapter extends ArrayAdapter<Certificate> { public class CertificateAdapter extends ArrayAdapter<Certificate> implements
CertificateManager.OnCertificateChangedListener{
private CertificateManager mCertificateManager; private CertificateManager mCertificateManager;
@ -73,4 +77,23 @@ public class CertificateAdapter extends ArrayAdapter<Certificate> {
return convertView; 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 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()). * Possible options for partition mounting (used for #remountSystem()).
*/ */
@ -27,6 +31,12 @@ public class CertificateManager {
ReadWrite ReadWrite
} }
private OnCertificateChangedListener mOnCertificateChangedListener;
public void setOnCertificateChangedListener(OnCertificateChangedListener listener) {
mOnCertificateChangedListener = listener;
}
/** /**
* Lists all certificates for either user or system. * Lists all certificates for either user or system.
* *
@ -56,7 +66,11 @@ public class CertificateManager {
run("chmod 644 " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName()); run("chmod 644 " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName());
remountSystem(Mode.ReadOnly); remountSystem(Mode.ReadOnly);
deleteCertificate(certificate); 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); remountSystem(Mode.ReadWrite);
boolean success = run("rm " + certificate.getFile().getAbsolutePath()); boolean success = run("rm " + certificate.getFile().getAbsolutePath());
remountSystem(Mode.ReadOnly); remountSystem(Mode.ReadOnly);
if (mOnCertificateChangedListener != null) {
mOnCertificateChangedListener.onCertificateChanged();
}
return success; return success;
} }

View file

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