From 86e8e639a3f6b6336e84b2344c2c501a19fc2659 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 21 Mar 2024 17:24:28 +0100 Subject: [PATCH] Create QR-Code for profile URL --- app/build.gradle | 2 +- .../mastodon/activities/ProfileActivity.java | 60 +++++++++++++++++++ .../android/mastodon/helper/Helper.java | 1 + .../mastodon/drawable/baseline_qr_code_24.xml | 27 +++++++++ .../mastodon/layout/activity_profile.xml | 17 +++++- .../layouts/mastodon/layout/popup_qrcode.xml | 17 ++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/layouts/mastodon/drawable/baseline_qr_code_24.xml create mode 100644 app/src/main/res/layouts/mastodon/layout/popup_qrcode.xml diff --git a/app/build.gradle b/app/build.gradle index 798579e2..14b6b69e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -190,7 +190,7 @@ dependencies { implementation 'androidx.media:media:1.7.0' implementation 'com.github.mancj:MaterialSearchBar:0.8.5' - + implementation 'com.github.androidmads:QRGenerator:1.0.1' implementation 'io.noties.markwon:core:4.6.2' implementation 'io.noties.markwon:ext-tables:4.6.2' diff --git a/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java index bc1c386d..1b981729 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/mastodon/activities/ProfileActivity.java @@ -15,6 +15,10 @@ package app.fedilab.android.mastodon.activities; * see . */ + + +import static app.fedilab.android.mastodon.helper.LogoHelper.getMainLogo; + import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.ClipboardManager; @@ -23,9 +27,13 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; @@ -39,6 +47,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; import android.view.inputmethod.EditorInfo; +import android.webkit.URLUtil; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; @@ -62,6 +71,9 @@ import com.bumptech.glide.request.transition.Transition; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.tabs.TabLayout; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Date; @@ -72,12 +84,15 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import androidmads.library.qrgenearator.QRGContents; +import androidmads.library.qrgenearator.QRGEncoder; import app.fedilab.android.BaseMainActivity; import app.fedilab.android.BuildConfig; import app.fedilab.android.R; import app.fedilab.android.activities.MainActivity; import app.fedilab.android.databinding.ActivityProfileBinding; import app.fedilab.android.databinding.NotificationsRelatedAccountsBinding; +import app.fedilab.android.databinding.PopupQrcodeBinding; import app.fedilab.android.databinding.TabProfileCustomViewBinding; import app.fedilab.android.mastodon.client.entities.api.Account; import app.fedilab.android.mastodon.client.entities.api.Attachment; @@ -275,6 +290,51 @@ public class ProfileActivity extends BaseActivity { } }); + binding.qrCodeGenerator.setVisibility(View.VISIBLE); + + binding.qrCodeGenerator.setOnClickListener(v->{ + QRGEncoder qrgEncoder = new QRGEncoder(account.url, null, QRGContents.Type.TEXT, 400); + Drawable logoDrawable = ContextCompat.getDrawable(ProfileActivity.this, R.drawable.fedilab_logo_bubbles); + if (logoDrawable != null) { + Bitmap bitmap = qrgEncoder.getBitmap(); + MaterialAlertDialogBuilder alertadd = new MaterialAlertDialogBuilder(ProfileActivity.this); + PopupQrcodeBinding popupQrcodeBinding = PopupQrcodeBinding.inflate(getLayoutInflater()); + popupQrcodeBinding.qrcodeImage.setImageBitmap(bitmap); + alertadd.setView(popupQrcodeBinding.getRoot()); + alertadd.setNeutralButton(R.string.close, (dialog, which) -> dialog.dismiss()); + alertadd.setPositiveButton(R.string.save, (dlg, which) -> { + File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + File targeted_folder = new File(path, getString(R.string.app_name)); + if (!targeted_folder.exists()) { + boolean created = targeted_folder.mkdir(); + if (!created) { + Toasty.error(ProfileActivity.this, getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); + return; + } + } + String fileName = URLUtil.guessFileName(account.url, null, null); + if (fileName.endsWith(".bin")) { + fileName = fileName.replace(".bin", ".png"); + } + fileName = fileName.replaceAll("@",""); + File backupFile = new File(targeted_folder.getAbsolutePath() + "/" + fileName); + try (FileOutputStream out = new FileOutputStream(backupFile)) { + final Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + Uri uri = Uri.fromFile(backupFile); + intent.setDataAndType(uri, "image/jpeg"); + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); + Helper.notify_user(ProfileActivity.this, Helper.getCurrentAccount(ProfileActivity.this), intent, BitmapFactory.decodeResource(getResources(), + getMainLogo(ProfileActivity.this)), Helper.NotifType.STORE, getString(R.string.save_over), getString(R.string.download_from, fileName)); + Toasty.success(ProfileActivity.this, getString(R.string.save_over), Toasty.LENGTH_LONG).show(); + } catch (IOException e) { + e.printStackTrace(); + } + }); + alertadd.show(); + } + + }); //Retrieve relationship with the connected account List accountListToCheck = new ArrayList<>(); diff --git a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java index a620f249..04eb9070 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/mastodon/helper/Helper.java @@ -219,6 +219,7 @@ public class Helper { public static final String ARG_NOTIFICATION_TYPE = "ARG_NOTIFICATION_TYPE"; public static final String ARG_EXCLUDED_NOTIFICATION_TYPE = "ARG_EXCLUDED_NOTIFICATION_TYPE"; public static final String ARG_STATUS = "ARG_STATUS"; + public static final String ARG_QR_CODE_URL = "ARG_QR_CODE_URL"; public static final String ARG_FOCUSED_STATUS_URI = "ARG_FOCUSED_STATUS_URI"; public static final String ARG_TIMELINE_REFRESH_ALL = "ARG_TIMELINE_REFRESH_ALL"; public static final String ARG_REFRESH_NOTFICATION = "ARG_REFRESH_NOTFICATION"; diff --git a/app/src/main/res/layouts/mastodon/drawable/baseline_qr_code_24.xml b/app/src/main/res/layouts/mastodon/drawable/baseline_qr_code_24.xml new file mode 100644 index 00000000..9bd940ba --- /dev/null +++ b/app/src/main/res/layouts/mastodon/drawable/baseline_qr_code_24.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layouts/mastodon/layout/activity_profile.xml b/app/src/main/res/layouts/mastodon/layout/activity_profile.xml index 67fe4ae1..344b1edd 100644 --- a/app/src/main/res/layouts/mastodon/layout/activity_profile.xml +++ b/app/src/main/res/layouts/mastodon/layout/activity_profile.xml @@ -140,7 +140,22 @@ app:layout_constraintStart_toStartOf="@id/banner_container" app:layout_constraintTop_toBottomOf="@id/avatar_container" tools:text="@tools:sample/first_names" /> - + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 22177660..c3f604a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -583,6 +583,7 @@ Color of bottom icons in timelines Logo of the instance Edit profile + QR generator Make an action Translation Text color