mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
QR activities improvements (#2103)
* allow to copy QR data and to read QR data from clipboard * add share option * allow to scan QR from gallery * use menu_copy_to_clipboard instead of menu_copy_link_to_clipboard * use AttachmentManager * reuse Bitmap * change REQUEST_CODE_IMAGE value * use getCacheDir() * replace handleOpenPgp4Fpr with handleQrData * show error message if scanning QR failed * use Log.e() instead of Log.w() * Revert "use getCacheDir()" This reverts commit 2a2a801b311e7c4929f5063d3340234413528b93. the impact of this change is eaasily larger than expected, see comments at https://github.com/deltachat/deltachat-android/pull/2103 if needed, we can discuss and re-add that in a separate, more focused pr. Co-authored-by: B. Petersen <r10s@b44t.com>
This commit is contained in:
committed by
GitHub
parent
f34ef4db13
commit
be56cb380e
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/load_qr_code_as_image"
|
||||
android:id="@+id/load_from_image"
|
||||
android:icon="@drawable/ic_image_white_24dp"
|
||||
app:showAsAction="ifRoom"/>
|
||||
|
||||
<item android:title="@string/paste_from_clipboard"
|
||||
android:id="@+id/paste"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
</menu>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:title="@string/menu_share"
|
||||
android:id="@+id/share"
|
||||
android:icon="@drawable/ic_share_white_24dp"
|
||||
app:showAsAction="ifRoom"/>
|
||||
|
||||
<item android:title="@string/menu_copy_to_clipboard"
|
||||
android:id="@+id/copy"
|
||||
app:showAsAction="never"/>
|
||||
|
||||
</menu>
|
||||
@@ -291,7 +291,7 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit
|
||||
String uriString = uri.toString();
|
||||
uriString = uriString.replaceFirst(OPENPGP4FPR, OPENPGP4FPR.toUpperCase());
|
||||
QrCodeHandler qrCodeHandler = new QrCodeHandler(this);
|
||||
qrCodeHandler.handleOpenPgp4Fpr(uriString);
|
||||
qrCodeHandler.handleQrData(uriString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
package org.thoughtcrime.securesms.qr;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.Fragment;
|
||||
@@ -13,25 +21,39 @@ import androidx.fragment.app.FragmentStatePagerAdapter;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import com.google.zxing.NotFoundException;
|
||||
import com.google.zxing.Result;
|
||||
import com.google.zxing.RGBLuminanceSource;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
|
||||
import org.thoughtcrime.securesms.BaseActionBarActivity;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.mms.AttachmentManager;
|
||||
import org.thoughtcrime.securesms.permissions.Permissions;
|
||||
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
||||
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
|
||||
import org.thoughtcrime.securesms.util.DynamicTheme;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class QrActivity extends BaseActionBarActivity {
|
||||
|
||||
private final DynamicTheme dynamicTheme = new DynamicNoActionBarTheme();
|
||||
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
||||
|
||||
private final static String TAG = QrActivity.class.getSimpleName();
|
||||
private final static int REQUEST_CODE_IMAGE = 46243;
|
||||
private final static int TAB_SHOW = 0;
|
||||
private final static int TAB_SCAN = 1;
|
||||
|
||||
private TabLayout tabLayout;
|
||||
private ViewPager viewPager;
|
||||
private QrShowFragment qrShowFragment;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -42,7 +64,7 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
setContentView(R.layout.activity_qr);
|
||||
tabLayout = ViewUtil.findById(this, R.id.tab_layout);
|
||||
viewPager = ViewUtil.findById(this, R.id.pager);
|
||||
ProfilePagerAdapter adapter = new ProfilePagerAdapter(getSupportFragmentManager());
|
||||
ProfilePagerAdapter adapter = new ProfilePagerAdapter(this, getSupportFragmentManager());
|
||||
viewPager.setAdapter(adapter);
|
||||
|
||||
setSupportActionBar(ViewUtil.findById(this, R.id.toolbar));
|
||||
@@ -60,6 +82,7 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
QrActivity.this.invalidateOptionsMenu();
|
||||
checkPermissions(position, adapter, viewPager);
|
||||
}
|
||||
|
||||
@@ -85,6 +108,17 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
menu.clear();
|
||||
if(tabLayout.getSelectedTabPosition() == TAB_SHOW) {
|
||||
getMenuInflater().inflate(R.menu.qr_show, menu);
|
||||
} else {
|
||||
getMenuInflater().inflate(R.menu.qr_scan, menu);
|
||||
}
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
@@ -108,6 +142,19 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
case android.R.id.home:
|
||||
finish();
|
||||
return true;
|
||||
case R.id.share:
|
||||
qrShowFragment.shareQr();
|
||||
break;
|
||||
case R.id.copy:
|
||||
qrShowFragment.copyQrData();
|
||||
break;
|
||||
case R.id.load_from_image:
|
||||
AttachmentManager.selectImage(this, REQUEST_CODE_IMAGE);
|
||||
break;
|
||||
case R.id.paste:
|
||||
QrCodeHandler qrCodeHandler = new QrCodeHandler(this);
|
||||
qrCodeHandler.handleQrData(Util.getTextFromClipboard(this));
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -125,10 +172,55 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int reqCode, int resultCode, final Intent data) {
|
||||
super.onActivityResult(reqCode, resultCode, data);
|
||||
|
||||
if (resultCode != Activity.RESULT_OK)
|
||||
return;
|
||||
|
||||
switch (reqCode) {
|
||||
case REQUEST_CODE_IMAGE:
|
||||
Uri uri = (data != null ? data.getData() : null);
|
||||
if (uri != null) {
|
||||
try {
|
||||
InputStream inputStream = getContentResolver().openInputStream(uri);
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
||||
if (bitmap == null) {
|
||||
Log.e(TAG, "uri is not a bitmap: " + uri.toString());
|
||||
return;
|
||||
}
|
||||
int width = bitmap.getWidth(), height = bitmap.getHeight();
|
||||
int[] pixels = new int[width * height];
|
||||
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
|
||||
bitmap.recycle();
|
||||
bitmap = null;
|
||||
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
|
||||
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||
MultiFormatReader reader = new MultiFormatReader();
|
||||
try {
|
||||
Result result = reader.decode(bBitmap);
|
||||
QrCodeHandler qrCodeHandler = new QrCodeHandler(this);
|
||||
qrCodeHandler.handleQrData(result.getText());
|
||||
} catch (NotFoundException e) {
|
||||
Log.e(TAG, "decode exception", e);
|
||||
Toast.makeText(this, getString(R.string.qrscan_failed), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(TAG, "can not open file: " + uri.toString(), e);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private class ProfilePagerAdapter extends FragmentStatePagerAdapter {
|
||||
|
||||
ProfilePagerAdapter(FragmentManager fragmentManager) {
|
||||
private QrActivity activity;
|
||||
|
||||
ProfilePagerAdapter(QrActivity activity, FragmentManager fragmentManager) {
|
||||
super(fragmentManager, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -138,7 +230,8 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
|
||||
switch (position) {
|
||||
case TAB_SHOW:
|
||||
fragment = new QrShowFragment();
|
||||
activity.qrShowFragment = new QrShowFragment();
|
||||
fragment = activity.qrShowFragment;
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -165,4 +258,4 @@ public class QrActivity extends BaseActionBarActivity {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,10 @@ public class QrCodeHandler {
|
||||
return; // aborted
|
||||
}
|
||||
|
||||
handleOpenPgp4Fpr(scanResult.getContents());
|
||||
handleQrData(scanResult.getContents());
|
||||
}
|
||||
|
||||
public void handleOpenPgp4Fpr(String rawString) {
|
||||
public void handleQrData(String rawString) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
final DcLot qrParsed = dcContext.checkQr(rawString);
|
||||
String nameAndAddress = dcContext.getContact(qrParsed.getId()).getNameNAddr();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.thoughtcrime.securesms.qr;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
@@ -24,6 +25,7 @@ public class QrShowActivity extends AppCompatActivity {
|
||||
DcEventCenter dcEventCenter;
|
||||
|
||||
DcContext dcContext;
|
||||
QrShowFragment fragment;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -32,6 +34,7 @@ public class QrShowActivity extends AppCompatActivity {
|
||||
dynamicLanguage.onCreate(this);
|
||||
|
||||
setContentView(R.layout.activity_qr_show);
|
||||
fragment = (QrShowFragment)getSupportFragmentManager().findFragmentById(R.id.qrScannerFragment);
|
||||
|
||||
dcContext = DcHelper.getContext(this);
|
||||
dcEventCenter = DcHelper.getEventCenter(this);
|
||||
@@ -62,6 +65,12 @@ public class QrShowActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.qr_show, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
@@ -73,9 +82,16 @@ public class QrShowActivity extends AppCompatActivity {
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
super.onOptionsItemSelected(item);
|
||||
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
finish();
|
||||
return true;
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
finish();
|
||||
return true;
|
||||
case R.id.share:
|
||||
fragment.shareQr();
|
||||
break;
|
||||
case R.id.copy:
|
||||
fragment.copyQrData();
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -9,8 +9,10 @@ import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -32,15 +34,23 @@ import com.google.zxing.common.BitMatrix;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.connect.DcEventCenter;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.util.FileProviderUtil;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDelegate {
|
||||
|
||||
private final static String TAG = QrShowFragment.class.getSimpleName();
|
||||
public final static int WHITE = 0xFFFFFFFF;
|
||||
private final static int BLACK = 0xFF000000;
|
||||
private final static int WIDTH = 400;
|
||||
private final static int HEIGHT = 400;
|
||||
private final static String CHAT_ID = "chat_id";
|
||||
|
||||
private int chatId = 0;
|
||||
|
||||
private int numJoiners;
|
||||
|
||||
private DcEventCenter dcEventCenter;
|
||||
@@ -55,6 +65,7 @@ public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDel
|
||||
|
||||
private BroadcastReceiver broadcastReceiver;
|
||||
|
||||
private Bitmap bitmap;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle bundle) {
|
||||
@@ -71,7 +82,6 @@ public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDel
|
||||
dcEventCenter = DcHelper.getEventCenter(getActivity());
|
||||
|
||||
Bundle extras = getActivity().getIntent().getExtras();
|
||||
int chatId = 0;
|
||||
if (extras != null) {
|
||||
chatId = extras.getInt(CHAT_ID);
|
||||
}
|
||||
@@ -110,7 +120,7 @@ public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDel
|
||||
|
||||
ImageView imageView = view.findViewById(R.id.qrImage);
|
||||
try {
|
||||
Bitmap bitmap = encodeAsBitmap(dcContext.getSecurejoinQr(chatId));
|
||||
bitmap = encodeAsBitmap(dcContext.getSecurejoinQr(chatId));
|
||||
imageView.setImageBitmap(bitmap);
|
||||
} catch (WriterException e) {
|
||||
e.printStackTrace();
|
||||
@@ -119,6 +129,31 @@ public class QrShowFragment extends Fragment implements DcEventCenter.DcEventDel
|
||||
return view;
|
||||
}
|
||||
|
||||
public void shareQr() {
|
||||
try {
|
||||
File file = new File(getActivity().getExternalCacheDir(), "share-qr.png");
|
||||
file.createNewFile();
|
||||
file.setReadable(true, false);
|
||||
FileOutputStream stream = new FileOutputStream(file);
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
|
||||
stream.flush();
|
||||
stream.close();
|
||||
Uri uri = FileProviderUtil.getUriFor(getActivity(), file);
|
||||
Intent intent = new Intent(Intent.ACTION_SEND);
|
||||
intent.setType("image/png");
|
||||
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
intent.putExtra(Intent.EXTRA_TEXT, hint);
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
startActivity(Intent.createChooser(intent, getString(R.string.chat_share_with_title)));
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "failed to share QR", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void copyQrData() {
|
||||
Util.writeTextToClipboard(getActivity(), DcHelper.getContext(getActivity()).getSecurejoinQr(chatId));
|
||||
Toast.makeText(getActivity(), getString(R.string.copied_to_clipboard), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void setHintText() {
|
||||
if (!DcHelper.isNetworkConnected(getContext())) {
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.thoughtcrime.securesms.util;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipDescription;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
@@ -286,6 +287,16 @@ public class Util {
|
||||
clipboard.setPrimaryClip(clip);
|
||||
}
|
||||
|
||||
public static String getTextFromClipboard(@NonNull Context context) {
|
||||
android.content.ClipboardManager clipboard =
|
||||
(android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
|
||||
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
|
||||
return item.getText().toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static int toIntExact(long value) {
|
||||
if ((int)value != value) {
|
||||
throw new ArithmeticException("integer overflow");
|
||||
|
||||
Reference in New Issue
Block a user