From 1f392a688d1439fdf163cc767e3630d566726d83 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 1 Dec 2020 20:31:30 +0100 Subject: [PATCH] initial (untested) support for easy onboarding invites --- src/conversations/AndroidManifest.xml | 4 + .../ui/EasyOnboardingInviteActivity.java | 157 ++++++++++++++++++ .../res/layout/activity_easy_invite.xml | 83 +++++++++ .../res/menu/easy_onboarding_invite.xml | 10 ++ src/conversations/res/values/strings.xml | 4 + .../services/XmppConnectionService.java | 38 +++++ .../ui/ConversationFragment.java | 2 +- .../ui/ConversationsActivity.java | 1 + .../ui/ConversationsOverviewFragment.java | 31 ++++ .../utils/EasyOnboardingInvite.java | 94 +++++++++++ .../eu/siacs/conversations/xml/Namespace.java | 1 + .../conversations/xmpp/XmppConnection.java | 52 ++++++ .../menu/fragment_conversations_overview.xml | 4 + src/main/res/values/strings.xml | 3 + 14 files changed, 483 insertions(+), 1 deletion(-) create mode 100644 src/conversations/java/eu/siacs/conversations/ui/EasyOnboardingInviteActivity.java create mode 100644 src/conversations/res/layout/activity_easy_invite.xml create mode 100644 src/conversations/res/menu/easy_onboarding_invite.xml create mode 100644 src/main/java/eu/siacs/conversations/utils/EasyOnboardingInvite.java diff --git a/src/conversations/AndroidManifest.xml b/src/conversations/AndroidManifest.xml index 90b78ed4c..62396bed1 100644 --- a/src/conversations/AndroidManifest.xml +++ b/src/conversations/AndroidManifest.xml @@ -20,6 +20,10 @@ android:name=".ui.MagicCreateActivity" android:label="@string/create_new_account" android:launchMode="singleTask" /> + share()); + if (bundle != null && bundle.containsKey("invite")) { + this.easyOnboardingInvite = bundle.getParcelable("invite"); + if (this.easyOnboardingInvite != null) { + showInvite(this.easyOnboardingInvite); + return; + } + } + this.showLoading(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.easy_onboarding_invite, menu); + final MenuItem share = menu.findItem(R.id.action_share); + share.setVisible(easyOnboardingInvite != null); + return super.onCreateOptionsMenu(menu); + } + + public boolean onOptionsItemSelected(MenuItem menuItem) { + if (menuItem.getItemId() == R.id.action_share) { + share(); + return true; + } else { + return super.onOptionsItemSelected(menuItem); + } + } + + private void share() { + final String shareText = getString( + R.string.easy_invite_share_text, + easyOnboardingInvite.getDomain(), + easyOnboardingInvite.getLandingUrl() + ); + final Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, shareText); + sendIntent.setType("text/plain"); + startActivity(Intent.createChooser(sendIntent, getString(R.string.share_invite_with))); + } + + @Override + protected void refreshUiReal() { + invalidateOptionsMenu(); + if (easyOnboardingInvite != null) { + showInvite(easyOnboardingInvite); + } else { + showLoading(); + } + } + + private void showLoading() { + this.binding.inProgress.setVisibility(View.VISIBLE); + this.binding.invite.setVisibility(View.GONE); + } + + private void showInvite(final EasyOnboardingInvite invite) { + this.binding.inProgress.setVisibility(View.GONE); + this.binding.invite.setVisibility(View.VISIBLE); + this.binding.tapToShare.setText(getString(R.string.tap_share_button_send_invite, invite.getDomain())); + final Point size = new Point(); + getWindowManager().getDefaultDisplay().getSize(size); + final int width = Math.min(size.x, size.y); + final String content; + if (Strings.isNullOrEmpty(invite.getLandingUrl())) { + content = invite.getUri(); + } else { + content = invite.getLandingUrl(); + } + final Bitmap bitmap = BarcodeProvider.create2dBarcodeBitmap(content, width); + binding.qrCode.setImageBitmap(bitmap); + } + + @Override + public void onSaveInstanceState(Bundle bundle) { + super.onSaveInstanceState(bundle); + if (easyOnboardingInvite != null) { + bundle.putParcelable("invite", easyOnboardingInvite); + } + } + + @Override + void onBackendConnected() { + if (easyOnboardingInvite != null) { + return; + } + final Intent launchIntent = getIntent(); + final String accountExtra = launchIntent.getStringExtra(EXTRA_ACCOUNT); + final Jid jid = accountExtra == null ? null : Jid.ofEscaped(accountExtra); + if (jid == null) { + return; + } + final Account account = xmppConnectionService.findAccountByJid(jid); + xmppConnectionService.requestEasyOnboardingInvite(account, this); + } + + public static void launch(final Account account, final Activity context) { + final Intent intent = new Intent(context, EasyOnboardingInviteActivity.class); + intent.putExtra(EXTRA_ACCOUNT, account.getJid().asBareJid().toEscapedString()); + context.startActivity(intent); + } + + @Override + public void inviteRequested(EasyOnboardingInvite invite) { + this.easyOnboardingInvite = invite; + Log.d(Config.LOGTAG, "invite requested"); + refreshUi(); + } + + @Override + public void inviteRequestFailed(final String message) { + runOnUiThread(() -> { + if (!Strings.isNullOrEmpty(message)) { + Toast.makeText(this, message, Toast.LENGTH_LONG).show(); + } + finish(); + }); + } +} diff --git a/src/conversations/res/layout/activity_easy_invite.xml b/src/conversations/res/layout/activity_easy_invite.xml new file mode 100644 index 000000000..8bbf11c03 --- /dev/null +++ b/src/conversations/res/layout/activity_easy_invite.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + +