Skip to content

Commit

Permalink
Generate QR codes in the background (close #1237)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Nov 17, 2018
1 parent 58db65c commit d23dade
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
20 changes: 17 additions & 3 deletions app/src/main/java/org/kontalk/ui/MyKeyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.kontalk.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
Expand All @@ -36,14 +37,15 @@
* My key activity.
* @author Daniele Ricci
*/
public class MyKeyActivity extends ToolbarActivity {
public class MyKeyActivity extends ToolbarActivity implements ViewUtils.OnQRCodeGeneratedListener {
private static final String TAG = Kontalk.TAG;

private View mViewport;
private TextView mAccountName;
private TextView mTextName;
private TextView mTextFingerprint;
private ImageView mQRCode;
private View mLoading;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -57,6 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
mTextName = findViewById(R.id.name);
mTextFingerprint = findViewById(R.id.fingerprint);
mQRCode = findViewById(R.id.qrcode);
mLoading = findViewById(R.id.loading);
}

@Override
Expand All @@ -83,13 +86,24 @@ protected void onStart() {
mTextFingerprint.setText(PGP.formatFingerprint(fingerprint)
.replaceFirst(" ", "\n"));

ViewUtils.getQRCodeBitmapAsync(this, mViewport, mQRCode,
PGP.createFingerprintURI(fingerprint));
ViewUtils.getQRCodeBitmapAsync(this, mViewport,
PGP.createFingerprintURI(fingerprint), this);
}

@Override
protected boolean isNormalUpNavigation() {
return true;
}

@Override
public void onQRCodeGenerated(Bitmap qrCode) {
mLoading.setVisibility(View.GONE);
mQRCode.setImageBitmap(qrCode);
}

@Override
public void onQRCodeError(Exception e) {
mLoading.setVisibility(View.GONE);
// TODO error
}
}
21 changes: 18 additions & 3 deletions app/src/main/java/org/kontalk/ui/RegisterDeviceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
Expand All @@ -37,14 +38,15 @@
* Shows the secure token for registering another device with the same key.
* @author Daniele Ricci
*/
public class RegisterDeviceActivity extends ToolbarActivity {
public class RegisterDeviceActivity extends ToolbarActivity implements ViewUtils.OnQRCodeGeneratedListener {
private static final String TAG = Kontalk.TAG;

private View mViewport;
private TextView mAccountName;
private TextView mTextServer;
private TextView mTextToken;
private ImageView mQRCode;
private View mLoading;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -58,6 +60,7 @@ protected void onCreate(Bundle savedInstanceState) {
mTextServer = findViewById(R.id.servername);
mTextToken = findViewById(R.id.token);
mQRCode = findViewById(R.id.qrcode);
mLoading = findViewById(R.id.loading);
}

@SuppressLint("SetTextI18n")
Expand All @@ -81,15 +84,27 @@ protected void onStart() {
mTextToken.setText(token);
}

ViewUtils.getQRCodeBitmapAsync(this, mViewport, mQRCode,
generateTokenText(account, token, from));
ViewUtils.getQRCodeBitmapAsync(this, mViewport,
generateTokenText(account, token, from), this);
}

@Override
protected boolean isNormalUpNavigation() {
return true;
}

@Override
public void onQRCodeGenerated(Bitmap qrCode) {
mLoading.setVisibility(View.GONE);
mQRCode.setImageBitmap(qrCode);
}

@Override
public void onQRCodeError(Exception e) {
mLoading.setVisibility(View.GONE);
// TODO error
}

/** This must match the parsing done in {@link #parseTokenText}. */
private static String generateTokenText(String account, String token, String from) {
return account + ";" + from + ";" + token;
Expand Down
38 changes: 27 additions & 11 deletions app/src/main/java/org/kontalk/util/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.zxing.qrcode.QRCodeWriter;
import com.vanniktech.emoji.EmojiManager;

import org.jivesoftware.smack.util.Async;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
Expand All @@ -36,7 +38,6 @@
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;


Expand All @@ -56,9 +57,9 @@ public static int dp(Context context, float value) {
/**
* Waits for global layout to happen and set a QR code for the minimum size
* between height and width of the given container. The QR code will be
* loaded automatically in {@code destination}.
* returned to the given listener.
*/
public static void getQRCodeBitmapAsync(final Context context, final View container, final ImageView destination, final String text) {
public static void getQRCodeBitmapAsync(final Context context, final View container, final String text, final OnQRCodeGeneratedListener listener) {
// no need to handle memory leaks because the view tree observer
// is created on every activity restart
container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
Expand All @@ -71,14 +72,24 @@ public void onGlobalLayout() {
size = Math.min(displaySize.x, displaySize.y);
}

try {
// TODO load in another thread
Bitmap qrCode = getQRCodeBitmap(size, text);
destination.setImageBitmap(qrCode);
}
catch (WriterException e) {
// TODO set error image on destination
}
final int qrSize = size;
Async.go(new Runnable() {
@Override
public void run() {
try {
final Bitmap qrCode = getQRCodeBitmap(qrSize, text);
container.post(new Runnable() {
@Override
public void run() {
listener.onQRCodeGenerated(qrCode);
}
});
}
catch (WriterException e) {
listener.onQRCodeError(e);
}
}
});
}
});
}
Expand All @@ -91,6 +102,11 @@ public static Bitmap getQRCodeBitmap(int size, String text) throws WriterExcepti
return toBitmap(matrix);
}

public interface OnQRCodeGeneratedListener {
void onQRCodeGenerated(Bitmap qrCode);
void onQRCodeError(Exception e);
}

/**
* Writes the given Matrix on a new Bitmap object.
* http://codeisland.org/2013/generating-qr-codes-with-zxing/
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/mykey_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp" />

<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:indeterminate="true"/>

</LinearLayout>
</ScrollView>
</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/register_device_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp" />

<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:indeterminate="true"/>

</LinearLayout>
</ScrollView>
</LinearLayout>

0 comments on commit d23dade

Please sign in to comment.