tls-psk: first steps

This commit is contained in:
Vincent Breitmoser
2017-05-29 23:12:54 +02:00
parent 6e8a768011
commit a55445f5bb
7 changed files with 733 additions and 2 deletions

View File

@@ -41,6 +41,7 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
import org.sufficientlysecure.keychain.remote.ui.AppsListFragment;
import org.sufficientlysecure.keychain.ui.base.BaseSecurityTokenActivity;
import org.sufficientlysecure.keychain.ui.transfer.view.TransferFragment;
import org.sufficientlysecure.keychain.util.FabContainer;
import org.sufficientlysecure.keychain.util.Preferences;
@@ -50,8 +51,9 @@ public class MainActivity extends BaseSecurityTokenActivity implements FabContai
static final int ID_ENCRYPT_DECRYPT = 2;
static final int ID_APPS = 3;
static final int ID_BACKUP = 4;
static final int ID_SETTINGS = 5;
static final int ID_HELP = 6;
static final int ID_TRANSFER = 5;
static final int ID_SETTINGS = 6;
static final int ID_HELP = 7;
// both of these are used for instrumentation testing only
public static final String EXTRA_SKIP_FIRST_TIME = "skip_first_time";
@@ -82,6 +84,8 @@ public class MainActivity extends BaseSecurityTokenActivity implements FabContai
.withIdentifier(ID_APPS).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.nav_backup).withIcon(CommunityMaterial.Icon.cmd_backup_restore)
.withIdentifier(ID_BACKUP).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.nav_transfer).withIcon(CommunityMaterial.Icon.cmd_backup_restore)
.withIdentifier(ID_TRANSFER).withSelectable(false),
new DividerDrawerItem(),
new PrimaryDrawerItem().withName(R.string.menu_preferences).withIcon(GoogleMaterial.Icon.gmd_settings).withIdentifier(ID_SETTINGS).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.menu_help).withIcon(CommunityMaterial.Icon.cmd_help_circle).withIdentifier(ID_HELP).withSelectable(false)
@@ -105,6 +109,9 @@ public class MainActivity extends BaseSecurityTokenActivity implements FabContai
case ID_BACKUP:
onBackupSelected();
break;
case ID_TRANSFER:
onTransferSelected();
break;
case ID_SETTINGS:
intent = new Intent(MainActivity.this, SettingsActivity.class);
break;
@@ -207,6 +214,13 @@ public class MainActivity extends BaseSecurityTokenActivity implements FabContai
setFragment(frag, true);
}
private void onTransferSelected() {
mToolbar.setTitle(R.string.nav_transfer);
mDrawer.setSelection(ID_TRANSFER, false);
Fragment frag = new TransferFragment();
setFragment(frag, true);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// add the values which need to be saved from the drawer to the bundle

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 Vincent Breitmoser <v.breitmoser@mugenguild.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.transfer.presenter;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.support.annotation.RequiresApi;
import org.sufficientlysecure.keychain.network.KeyTransferClientInteractor;
import org.sufficientlysecure.keychain.network.KeyTransferClientInteractor.KeyTransferClientCallback;
import org.sufficientlysecure.keychain.network.KeyTransferServerInteractor;
import org.sufficientlysecure.keychain.network.KeyTransferServerInteractor.KeyTransferServerCallback;
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
public class TransferPresenter implements KeyTransferServerCallback, KeyTransferClientCallback {
private final Context context;
private final TransferMvpView view;
private KeyTransferServerInteractor keyTransferServerInteractor;
private KeyTransferClientInteractor keyTransferClientInteractor;
public TransferPresenter(Context context, TransferMvpView view) {
this.context = context;
this.view = view;
}
public void onDestroy() {
clearConnections();
}
public void onClickScan() {
clearConnections();
keyTransferClientInteractor = new KeyTransferClientInteractor();
keyTransferClientInteractor.connectToServer("10.100.40.126", this);
}
private void clearConnections() {
if (keyTransferServerInteractor != null) {
keyTransferServerInteractor.stopServer();
keyTransferServerInteractor = null;
}
if (keyTransferClientInteractor != null) {
keyTransferClientInteractor.closeConnection();
keyTransferClientInteractor = null;
}
}
public void startServer() {
keyTransferServerInteractor = new KeyTransferServerInteractor();
keyTransferServerInteractor.startServer(this);
}
@Override
public void onServerStarted(String qrCodeData) {
Bitmap qrCodeBitmap = QrCodeUtils.getQRCodeBitmap(Uri.parse("pgp+transfer:" + qrCodeData), 0);
view.setQrImage(qrCodeBitmap);
}
@Override
public void onConnectionEstablished(String otherName) {
view.showConnectionEstablished(otherName);
}
@Override
public void onConnectionLost() {
view.showWaitingForConnection();
startServer();
}
public interface TransferMvpView {
void showConnectionEstablished(String hostname);
void showWaitingForConnection();
void setQrImage(Bitmap qrCode);
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.sufficientlysecure.keychain.ui.transfer.view;
import android.graphics.Bitmap;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewAnimator;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.transfer.presenter.TransferPresenter;
import org.sufficientlysecure.keychain.ui.transfer.presenter.TransferPresenter.TransferMvpView;
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
public class TransferFragment extends Fragment implements TransferMvpView {
public static final int VIEW_WAITING = 0;
public static final int VIEW_CONNECTED = 1;
private ImageView vQrCodeImage;
private View vScanButton;
private TransferPresenter presenter;
private ViewAnimator vTransferAnimator;
private TextView vConnectionStatusText;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.transfer_fragment, container, false);
vTransferAnimator = (ViewAnimator) view.findViewById(R.id.transfer_animator);
vConnectionStatusText = (TextView) view.findViewById(R.id.connection_status);
vQrCodeImage = (ImageView) view.findViewById(R.id.qr_code_image);
vScanButton = view.findViewById(R.id.button_scan);
vScanButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (presenter != null) {
presenter.onClickScan();
}
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
presenter = new TransferPresenter(getContext(), this);
}
@Override
public void onStart() {
super.onStart();
presenter.startServer();
}
@Override
public void onStop() {
super.onStop();
presenter.onDestroy();
}
@Override
public void showWaitingForConnection() {
vTransferAnimator.setDisplayedChild(VIEW_WAITING);
}
@Override
public void showConnectionEstablished(String hostname) {
vTransferAnimator.setDisplayedChild(VIEW_CONNECTED);
vConnectionStatusText.setText("Connected to: " + hostname);
}
@Override
public void setQrImage(final Bitmap qrCode) {
vQrCodeImage.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// create actual bitmap in display dimensions
Bitmap scaled = Bitmap.createScaledBitmap(qrCode,
vQrCodeImage.getWidth(), vQrCodeImage.getWidth(), false);
vQrCodeImage.setImageBitmap(scaled);
}
});
vQrCodeImage.requestLayout();
}
}