tls-psk: working version, with fake progress too!
This commit is contained in:
@@ -33,6 +33,7 @@ import android.support.v4.content.Loader;
|
||||
import android.support.v7.widget.RecyclerView.Adapter;
|
||||
import android.view.LayoutInflater;
|
||||
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.network.KeyTransferInteractor;
|
||||
import org.sufficientlysecure.keychain.network.KeyTransferInteractor.KeyTransferCallback;
|
||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||
@@ -42,6 +43,7 @@ import org.sufficientlysecure.keychain.ui.transfer.loader.SecretKeyLoader.Secret
|
||||
import org.sufficientlysecure.keychain.ui.transfer.view.TransferSecretKeyList.OnClickTransferKeyListener;
|
||||
import org.sufficientlysecure.keychain.ui.transfer.view.TransferSecretKeyList.TransferKeyAdapter;
|
||||
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
|
||||
|
||||
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
|
||||
@@ -66,43 +68,45 @@ public class TransferPresenter implements KeyTransferCallback, LoaderCallbacks<L
|
||||
view.setSecretKeyAdapter(secretKeyAdapter);
|
||||
}
|
||||
|
||||
public void onStart() {
|
||||
|
||||
public void onUiStart() {
|
||||
loaderManager.restartLoader(loaderId, null, this);
|
||||
|
||||
startServer();
|
||||
if (keyTransferServerInteractor == null && keyTransferClientInteractor == null) {
|
||||
connectionStartListen();
|
||||
}
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
clearConnections();
|
||||
public void onUiStop() {
|
||||
connectionClear();
|
||||
}
|
||||
|
||||
public void onClickScan() {
|
||||
clearConnections();
|
||||
public void onUiClickScan() {
|
||||
connectionClear();
|
||||
|
||||
view.scanQrCode();
|
||||
}
|
||||
|
||||
public void onQrCodeScanned(String qrCodeContent) {
|
||||
keyTransferClientInteractor = new KeyTransferInteractor();
|
||||
keyTransferClientInteractor.connectToServer(qrCodeContent, this);
|
||||
public void onUiQrCodeScanned(String qrCodeContent) {
|
||||
connectionStartConnect(qrCodeContent);
|
||||
}
|
||||
|
||||
private void clearConnections() {
|
||||
if (keyTransferServerInteractor != null) {
|
||||
keyTransferServerInteractor.closeConnection();
|
||||
keyTransferServerInteractor = null;
|
||||
}
|
||||
if (keyTransferClientInteractor != null) {
|
||||
keyTransferClientInteractor.closeConnection();
|
||||
keyTransferClientInteractor = null;
|
||||
@Override
|
||||
public void onUiClickTransferKey(long masterKeyId) {
|
||||
try {
|
||||
byte[] armoredSecretKey =
|
||||
KeyRepository.createDatabaseInteractor(context).getSecretKeyRingAsArmoredData(masterKeyId);
|
||||
connectionSend(armoredSecretKey);
|
||||
} catch (IOException | NotFoundException | PgpGeneralException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void startServer() {
|
||||
keyTransferServerInteractor = new KeyTransferInteractor();
|
||||
keyTransferServerInteractor.startServer(this);
|
||||
public void onUiFakeProgressFinished() {
|
||||
view.showKeySentOk();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onServerStarted(String qrCodeData) {
|
||||
Bitmap qrCodeBitmap = QrCodeUtils.getQRCodeBitmap(Uri.parse("pgp+transfer://" + qrCodeData));
|
||||
@@ -116,10 +120,57 @@ public class TransferPresenter implements KeyTransferCallback, LoaderCallbacks<L
|
||||
|
||||
@Override
|
||||
public void onConnectionLost() {
|
||||
view.showWaitingForConnection();
|
||||
startServer();
|
||||
connectionStartListen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceivedOk(String receivedData) {
|
||||
Log.d(Constants.TAG, "received: " + receivedData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataSentOk(String arg) {
|
||||
Log.d(Constants.TAG, "data sent ok!");
|
||||
view.showFakeSendProgressDialog();
|
||||
}
|
||||
|
||||
|
||||
private void connectionStartConnect(String qrCodeContent) {
|
||||
connectionClear();
|
||||
|
||||
keyTransferClientInteractor = new KeyTransferInteractor();
|
||||
keyTransferClientInteractor.connectToServer(qrCodeContent, this);
|
||||
}
|
||||
|
||||
private void connectionStartListen() {
|
||||
connectionClear();
|
||||
|
||||
keyTransferServerInteractor = new KeyTransferInteractor();
|
||||
keyTransferServerInteractor.startServer(this);
|
||||
|
||||
view.showWaitingForConnection();
|
||||
}
|
||||
|
||||
private void connectionClear() {
|
||||
if (keyTransferServerInteractor != null) {
|
||||
keyTransferServerInteractor.closeConnection();
|
||||
keyTransferServerInteractor = null;
|
||||
}
|
||||
if (keyTransferClientInteractor != null) {
|
||||
keyTransferClientInteractor.closeConnection();
|
||||
keyTransferClientInteractor = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void connectionSend(byte[] armoredSecretKey) {
|
||||
if (keyTransferClientInteractor != null) {
|
||||
keyTransferClientInteractor.sendData(armoredSecretKey);
|
||||
} else if (keyTransferServerInteractor != null) {
|
||||
keyTransferServerInteractor.sendData(armoredSecretKey);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Loader<List<SecretKeyItem>> onCreateLoader(int id, Bundle args) {
|
||||
return secretKeyAdapter.createLoader(context);
|
||||
@@ -135,20 +186,6 @@ public class TransferPresenter implements KeyTransferCallback, LoaderCallbacks<L
|
||||
secretKeyAdapter.setData(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClickTransferKey(long masterKeyId) {
|
||||
try {
|
||||
byte[] armoredSecretKey =
|
||||
KeyRepository.createDatabaseInteractor(context).getSecretKeyRingAsArmoredData(masterKeyId);
|
||||
if (keyTransferClientInteractor != null) {
|
||||
keyTransferClientInteractor.sendData(armoredSecretKey);
|
||||
} else if (keyTransferServerInteractor != null) {
|
||||
keyTransferServerInteractor.sendData(armoredSecretKey);
|
||||
}
|
||||
} catch (IOException | NotFoundException | PgpGeneralException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public interface TransferMvpView {
|
||||
void showWaitingForConnection();
|
||||
@@ -158,5 +195,8 @@ public class TransferPresenter implements KeyTransferCallback, LoaderCallbacks<L
|
||||
void setQrImage(Bitmap qrCode);
|
||||
|
||||
void setSecretKeyAdapter(Adapter adapter);
|
||||
|
||||
void showFakeSendProgressDialog();
|
||||
void showKeySentOk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,16 @@
|
||||
package org.sufficientlysecure.keychain.ui.transfer.view;
|
||||
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.RequiresApi;
|
||||
import android.support.v4.app.Fragment;
|
||||
@@ -40,6 +45,7 @@ import android.widget.ViewAnimator;
|
||||
import com.google.zxing.client.android.Intents;
|
||||
import org.sufficientlysecure.keychain.R;
|
||||
import org.sufficientlysecure.keychain.ui.QrCodeCaptureActivity;
|
||||
import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
|
||||
import org.sufficientlysecure.keychain.ui.transfer.presenter.TransferPresenter;
|
||||
import org.sufficientlysecure.keychain.ui.transfer.presenter.TransferPresenter.TransferMvpView;
|
||||
|
||||
@@ -48,6 +54,8 @@ import org.sufficientlysecure.keychain.ui.transfer.presenter.TransferPresenter.T
|
||||
public class TransferFragment extends Fragment implements TransferMvpView {
|
||||
public static final int VIEW_WAITING = 0;
|
||||
public static final int VIEW_CONNECTED = 1;
|
||||
public static final int VIEW_SEND_OK = 2;
|
||||
|
||||
public static final int REQUEST_CODE_SCAN = 1;
|
||||
public static final int LOADER_ID = 1;
|
||||
|
||||
@@ -74,7 +82,7 @@ public class TransferFragment extends Fragment implements TransferMvpView {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (presenter != null) {
|
||||
presenter.onClickScan();
|
||||
presenter.onUiClickScan();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -94,14 +102,14 @@ public class TransferFragment extends Fragment implements TransferMvpView {
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
presenter.onStart();
|
||||
presenter.onUiStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
|
||||
presenter.onStop();
|
||||
presenter.onUiStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -111,8 +119,13 @@ public class TransferFragment extends Fragment implements TransferMvpView {
|
||||
|
||||
@Override
|
||||
public void showConnectionEstablished(String hostname) {
|
||||
vTransferAnimator.setDisplayedChild(VIEW_CONNECTED);
|
||||
vConnectionStatusText.setText("Connected to: " + hostname);
|
||||
vTransferAnimator.setDisplayedChild(VIEW_CONNECTED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showKeySentOk() {
|
||||
vTransferAnimator.setDisplayedChild(VIEW_SEND_OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,13 +154,44 @@ public class TransferFragment extends Fragment implements TransferMvpView {
|
||||
vTransferKeyList.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showFakeSendProgressDialog() {
|
||||
final ProgressDialogFragment progressDialogFragment =
|
||||
ProgressDialogFragment.newInstance("Sending key…", ProgressDialog.STYLE_HORIZONTAL, false);
|
||||
progressDialogFragment.show(getFragmentManager(), "progress");
|
||||
|
||||
final Handler handler = new Handler();
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
int fakeProgress = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
fakeProgress += 6;
|
||||
if (fakeProgress > 100) {
|
||||
cancel();
|
||||
progressDialogFragment.dismissAllowingStateLoss();
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
presenter.onUiFakeProgressFinished();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
progressDialogFragment.setProgress(fakeProgress, 100);
|
||||
}
|
||||
}, 0, 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
case REQUEST_CODE_SCAN:
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
String qrCodeData = data.getStringExtra(Intents.Scan.RESULT);
|
||||
presenter.onQrCodeScanned(qrCodeData);
|
||||
presenter.onUiQrCodeScanned(qrCodeData);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -129,7 +129,7 @@ public class TransferSecretKeyList extends RecyclerView {
|
||||
vSendButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
onClickTransferKeyListener.onClickTransferKey(item.masterKeyId);
|
||||
onClickTransferKeyListener.onUiClickTransferKey(item.masterKeyId);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -139,6 +139,6 @@ public class TransferSecretKeyList extends RecyclerView {
|
||||
}
|
||||
|
||||
public interface OnClickTransferKeyListener {
|
||||
void onClickTransferKey(long masterKeyId);
|
||||
void onUiClickTransferKey(long masterKeyId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user