Fixing import, start qr code stuff

This commit is contained in:
Dominik Schürmann 2014-10-04 18:22:55 +02:00
parent 7e5918efa3
commit c614d8d4e0

View File

@ -31,6 +31,9 @@ import android.support.v7.app.ActionBarActivity;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry;
@ -49,14 +52,17 @@ import org.sufficientlysecure.keychain.ui.util.Notify;
import org.sufficientlysecure.keychain.ui.widget.ExchangeKeySpinner; import org.sufficientlysecure.keychain.ui.widget.ExchangeKeySpinner;
import org.sufficientlysecure.keychain.ui.widget.KeySpinner; import org.sufficientlysecure.keychain.ui.widget.KeySpinner;
import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ParcelableFileCache; import org.sufficientlysecure.keychain.util.ParcelableFileCache;
import org.sufficientlysecure.keychain.util.Preferences;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale;
import edu.cmu.cylab.starslinger.exchange.ExchangeActivity; import edu.cmu.cylab.starslinger.exchange.ExchangeActivity;
import edu.cmu.cylab.starslinger.exchange.ExchangeConfig; import edu.cmu.cylab.starslinger.exchange.ExchangeConfig;
@ -75,8 +81,6 @@ public class AddKeysActivity extends ActionBarActivity implements
long mExchangeMasterKeyId = Constants.key.none; long mExchangeMasterKeyId = Constants.key.none;
byte[] mImportBytes; byte[] mImportBytes;
private LongSparseArray<ParcelableKeyRing> mCachedKeyData;
private static final int REQUEST_CODE_SAFE_SLINGER = 1; private static final int REQUEST_CODE_SAFE_SLINGER = 1;
@ -153,10 +157,12 @@ public class AddKeysActivity extends ActionBarActivity implements
} }
private void startQrCode() { private void startQrCode() {
// scan using xzing's Barcode Scanner
new IntentIntegrator(this).initiateScan();
} }
private void searchCloud() { private void searchCloud() {
finish();
Intent importIntent = new Intent(this, ImportKeysActivity.class); Intent importIntent = new Intent(this, ImportKeysActivity.class);
startActivity(importIntent); startActivity(importIntent);
} }
@ -169,16 +175,12 @@ public class AddKeysActivity extends ActionBarActivity implements
result.createNotify(this).show(); result.createNotify(this).show();
} else { } else {
switch (requestCode) { switch (requestCode) {
case REQUEST_CODE_SAFE_SLINGER: case REQUEST_CODE_SAFE_SLINGER: {
switch (resultCode) { switch (resultCode) {
case ExchangeActivity.RESULT_EXCHANGE_OK: case ExchangeActivity.RESULT_EXCHANGE_OK:
// import exchanged keys // import exchanged keys
mImportBytes = getSlingedKeys(data); mImportBytes = getSlingedKeys(data);
getSupportLoaderManager().restartLoader(LOADER_ID_BYTES, null, this); getSupportLoaderManager().restartLoader(LOADER_ID_BYTES, null, this);
// Intent importIntent = new Intent(this, ImportKeysActivity.class);
// importIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY);
// importIntent.putExtra(ImportKeysActivity.EXTRA_KEY_BYTES, getSlingedKeys(data));
// startActivity(importIntent);
break; break;
case ExchangeActivity.RESULT_EXCHANGE_CANCELED: case ExchangeActivity.RESULT_EXCHANGE_CANCELED:
// handle canceled result // handle canceled result
@ -186,11 +188,68 @@ public class AddKeysActivity extends ActionBarActivity implements
break; break;
} }
break; break;
}
case IntentIntegratorSupportV4.REQUEST_CODE: {
IntentResult scanResult = IntentIntegratorSupportV4.parseActivityResult(requestCode,
resultCode, data);
if (scanResult != null && scanResult.getFormatName() != null) {
String scannedContent = scanResult.getContents();
Log.d(Constants.TAG, "scannedContent: " + scannedContent);
// look if it's fingerprint only
if (scannedContent.toLowerCase(Locale.ENGLISH).startsWith(Constants.FINGERPRINT_SCHEME)) {
loadFromFingerprintUri(Uri.parse(scanResult.getContents()));
return;
}
// is this a full key encoded as qr code?
if (scannedContent.startsWith("-----BEGIN PGP")) {
// TODO
// mImportActivity.loadCallback(new ImportKeysListFragment.BytesLoaderState(scannedContent.getBytes(), null));
return;
}
// fail...
Notify.showNotify(this, R.string.import_qr_code_wrong, Notify.Style.ERROR);
}
break;
}
} }
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
} }
public void loadFromFingerprintUri(Uri dataUri) {
String query = "0x" + getFingerprintFromUri(dataUri);
// setCurrentItem does not work directly after onResume (from qr code scanner)
// see http://stackoverflow.com/q/19316729
// so, reset adapter completely!
// if (mViewPager.getAdapter() != null)
// mViewPager.setAdapter(null);
// mViewPager.setAdapter(mTabsAdapter);
// mViewPager.setCurrentItem(TAB_CLOUD);
// ImportKeysCloudFragment f = (ImportKeysCloudFragment)
// getActiveFragment(mViewPager, TAB_CLOUD);
// search config
Preferences prefs = Preferences.getPreferences(this);
Preferences.CloudSearchPrefs cloudPrefs = new Preferences.CloudSearchPrefs(true, true, prefs.getPreferredKeyserver());
// search directly
// loadCallback(new ImportKeysListFragment.CloudLoaderState(query, cloudPrefs));
}
private String getFingerprintFromUri(Uri dataUri) {
String fingerprint = dataUri.toString().split(":")[1].toLowerCase(Locale.ENGLISH);
Log.d(Constants.TAG, "fingerprint: " + fingerprint);
return fingerprint;
}
private static byte[] getSlingedKeys(Intent data) { private static byte[] getSlingedKeys(Intent data) {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -252,7 +311,8 @@ public class AddKeysActivity extends ActionBarActivity implements
Exception error = data.getError(); Exception error = data.getError();
// free old cached key data // free old cached key data
mCachedKeyData = null; // mCachedKeyData = null;
LongSparseArray<ParcelableKeyRing> mCachedKeyData = null;
// TODO: Use parcels!!!!!!!!!!!!!!! // TODO: Use parcels!!!!!!!!!!!!!!!
@ -262,6 +322,8 @@ public class AddKeysActivity extends ActionBarActivity implements
if (error == null) { if (error == null) {
// No error // No error
mCachedKeyData = ((ImportKeysListLoader) loader).getParcelableRings(); mCachedKeyData = ((ImportKeysListLoader) loader).getParcelableRings();
Log.d(Constants.TAG, "no error!:" + mCachedKeyData);
} else if (error instanceof ImportKeysListLoader.NoValidKeysException) { } else if (error instanceof ImportKeysListLoader.NoValidKeysException) {
Notify.showNotify(this, R.string.error_import_no_valid_keys, Notify.Style.ERROR); Notify.showNotify(this, R.string.error_import_no_valid_keys, Notify.Style.ERROR);
} else if (error instanceof ImportKeysListLoader.NonPgpPartException) { } else if (error instanceof ImportKeysListLoader.NonPgpPartException) {
@ -299,7 +361,7 @@ public class AddKeysActivity extends ActionBarActivity implements
break; break;
} }
importKeys(); importKeys(mCachedKeyData);
} }
@Override @Override
@ -318,35 +380,37 @@ public class AddKeysActivity extends ActionBarActivity implements
} }
} }
public ParcelableFileCache.IteratorWithSize<ParcelableKeyRing> getSelectedData() { public ParcelableFileCache.IteratorWithSize<ParcelableKeyRing>
getSelectedData(final LongSparseArray<ParcelableKeyRing> keyData) {
return new ParcelableFileCache.IteratorWithSize<ParcelableKeyRing>() { return new ParcelableFileCache.IteratorWithSize<ParcelableKeyRing>() {
int i = 0; int i = 0;
@Override @Override
public int getSize() { public int getSize() {
return mCachedKeyData.size(); return keyData.size();
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return (mCachedKeyData.get(i + 1) != null); return (i < getSize());
} }
@Override @Override
public ParcelableKeyRing next() { public ParcelableKeyRing next() {
ParcelableKeyRing key = mCachedKeyData.get(i); // get the object by the key.
ParcelableKeyRing key = keyData.valueAt(i);
i++; i++;
return key; return key;
} }
@Override @Override
public void remove() { public void remove() {
mCachedKeyData.remove(i); keyData.remove(i);
} }
}; };
} }
public void importKeys() { public void importKeys(final LongSparseArray<ParcelableKeyRing> keyData) {
// Message is received after importing is done in KeychainIntentService // Message is received after importing is done in KeychainIntentService
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(
this, this,
@ -370,6 +434,8 @@ public class AddKeysActivity extends ActionBarActivity implements
return; return;
} }
// TODO: start certify with received keys
// if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction()) // if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction())
// || ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) { // || ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) {
// Intent intent = new Intent(); // Intent intent = new Intent();
@ -390,45 +456,45 @@ public class AddKeysActivity extends ActionBarActivity implements
}; };
// ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState(); // ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState();
// if (importBytes != null) { // if (importMethod == IMPORT_SAFE_SLINGER) {
Log.d(Constants.TAG, "importKeys started"); Log.d(Constants.TAG, "importKeys started");
// Send all information needed to service to import key in other thread // Send all information needed to service to import key in other thread
Intent intent = new Intent(this, KeychainIntentService.class); Intent intent = new Intent(this, KeychainIntentService.class);
intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING); intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING);
// fill values for this action // fill values for this action
Bundle data = new Bundle(); Bundle data = new Bundle();
// get DATA from selected key entries // instead of giving the entries by Intent extra, cache them into a
// ParcelableFileCache.IteratorWithSize<ParcelableKeyRing> selectedEntries = mListFragment.getSelectedData(); // file to prevent Java Binder problems on heavy imports
// read FileImportCache for more info.
try {
// We parcel this iteratively into a file - anything we can
// display here, we should be able to import.
ParcelableFileCache<ParcelableKeyRing> cache =
new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl");
cache.writeCache(getSelectedData(keyData));
// instead of giving the entries by Intent extra, cache them into a intent.putExtra(KeychainIntentService.EXTRA_DATA, data);
// file to prevent Java Binder problems on heavy imports
// read FileImportCache for more info.
try {
// We parcel this iteratively into a file - anything we can
// display here, we should be able to import.
ParcelableFileCache<ParcelableKeyRing> cache =
new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl");
cache.writeCache(getSelectedData());
intent.putExtra(KeychainIntentService.EXTRA_DATA, data); // Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler);
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
// Create a new Messenger for the communication back // show progress dialog
Messenger messenger = new Messenger(saveHandler); saveHandler.showProgressDialog(this);
intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);
// show progress dialog // start service with intent
saveHandler.showProgressDialog(this); startService(intent);
} catch (IOException e) {
Log.e(Constants.TAG, "Problem writing cache file", e);
Notify.showNotify(this, "Problem writing cache file!", Notify.Style.ERROR);
}
// } else if () {
// start service with intent // }
startService(intent);
} catch (IOException e) {
Log.e(Constants.TAG, "Problem writing cache file", e);
Notify.showNotify(this, "Problem writing cache file!", Notify.Style.ERROR);
}
// } else if (ls instanceof ImportKeysListFragment.CloudLoaderState) { // } else if (ls instanceof ImportKeysListFragment.CloudLoaderState) {
// ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls; // ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls;
// //