From 526c2637c12640145f28d0336a5da167bab326d2 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 13 Jan 2019 15:28:24 +0100 Subject: [PATCH] provide Set as Profile intent. fixes #2594 --- .../ui/ManageAccountActivity.java | 14 +-- .../ui/ShareViaAccountActivity.java | 5 -- src/main/AndroidManifest.xml | 10 +++ .../services/XmppConnectionService.java | 20 ++++- ...hooseAccountForProfilePictureActivity.java | 85 +++++++++++++++++++ .../ui/PublishProfilePictureActivity.java | 28 +++++- .../ui/adapter/AccountAdapter.java | 12 +-- .../res/layout/activity_manage_accounts.xml | 0 src/main/res/values/strings.xml | 2 + src/quicksy/res/values/strings.xml | 1 + 10 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/ChooseAccountForProfilePictureActivity.java rename src/{conversations => main}/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java (95%) rename src/{conversations => main}/res/layout/activity_manage_accounts.xml (100%) diff --git a/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java b/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java index 9e28753a0..e1330fe88 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/ManageAccountActivity.java @@ -35,7 +35,7 @@ import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; import eu.siacs.conversations.xmpp.XmppConnection; import rocks.xmpp.addr.Jid; -public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, KeyChainAliasCallback, XmppConnectionService.OnAccountCreated { +public class ManageAccountActivity extends XmppActivity implements OnAccountUpdate, KeyChainAliasCallback, XmppConnectionService.OnAccountCreated, AccountAdapter.OnTglAccountState { private final String STATE_SELECTED_ACCOUNT = "selected_account"; @@ -88,17 +88,10 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda } } - accountListView = (ListView) findViewById(R.id.account_list); + accountListView = findViewById(R.id.account_list); this.mAccountAdapter = new AccountAdapter(this, accountList); accountListView.setAdapter(this.mAccountAdapter); - accountListView.setOnItemClickListener(new OnItemClickListener() { - - @Override - public void onItemClick(AdapterView arg0, View view, - int position, long arg3) { - switchToAccount(accountList.get(position)); - } - }); + accountListView.setOnItemClickListener((arg0, view, position, arg3) -> switchToAccount(accountList.get(position))); registerForContextMenu(accountListView); } @@ -244,6 +237,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda } } + @Override public void onClickTglAccountState(Account account, boolean enable) { if (enable) { enableAccount(account); diff --git a/src/conversations/java/eu/siacs/conversations/ui/ShareViaAccountActivity.java b/src/conversations/java/eu/siacs/conversations/ui/ShareViaAccountActivity.java index 28e2ea4e7..db4864689 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/ShareViaAccountActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/ShareViaAccountActivity.java @@ -30,11 +30,6 @@ public class ShareViaAccountActivity extends XmppActivity { accountList.clear(); accountList.addAll(xmppConnectionService.getAccounts()); } - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - actionBar.setHomeButtonEnabled(this.accountList.size() > 0); - actionBar.setDisplayHomeAsUpEnabled(this.accountList.size() > 0); - } mAccountAdapter.notifyDataSetChanged(); } diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index d797715ef..b0623dd76 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -164,6 +164,16 @@ + + + + + + + + { try { diff --git a/src/main/java/eu/siacs/conversations/ui/ChooseAccountForProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/ChooseAccountForProfilePictureActivity.java new file mode 100644 index 000000000..cfb4f05fb --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/ChooseAccountForProfilePictureActivity.java @@ -0,0 +1,85 @@ +package eu.siacs.conversations.ui; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.support.v7.app.ActionBar; +import android.widget.ListView; + +import java.util.ArrayList; +import java.util.List; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.ui.adapter.AccountAdapter; +import rocks.xmpp.addr.Jid; + +public class ChooseAccountForProfilePictureActivity extends XmppActivity { + + protected final List accountList = new ArrayList<>(); + protected ListView accountListView; + protected AccountAdapter mAccountAdapter; + + @Override + protected void refreshUiReal() { + loadEnabledAccounts(); + mAccountAdapter.notifyDataSetChanged(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manage_accounts); + setSupportActionBar(findViewById(R.id.toolbar)); + configureActionBar(getSupportActionBar(), false); + accountListView = findViewById(R.id.account_list); + this.mAccountAdapter = new AccountAdapter(this, accountList, false); + accountListView.setAdapter(this.mAccountAdapter); + accountListView.setOnItemClickListener((arg0, view, position, arg3) -> { + final Account account = accountList.get(position); + goToProfilePictureActivity(account); + }); + } + + @Override + protected void onStart() { + super.onStart(); + final int theme = findTheme(); + if (this.mTheme != theme) { + recreate(); + } + } + + @Override + void onBackendConnected() { + loadEnabledAccounts(); + if (accountList.size() == 1) { + goToProfilePictureActivity(accountList.get(0)); + return; + } + mAccountAdapter.notifyDataSetChanged(); + } + + private void loadEnabledAccounts() { + accountList.clear(); + for(Account account : xmppConnectionService.getAccounts()) { + if (account.isEnabled()) { + accountList.add(account); + } + } + } + + private void goToProfilePictureActivity(Account account) { + final Intent startIntent = getIntent(); + final Uri uri = startIntent == null ? null : startIntent.getData(); + if (uri != null) { + Intent intent = new Intent(this, PublishProfilePictureActivity.class); + intent.putExtra(EXTRA_ACCOUNT, account.getJid().asBareJid().toString()); + intent.setData(uri); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(intent); + } + finish(); + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java index bfb8a84ec..d818cdf7e 100644 --- a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java @@ -15,6 +15,8 @@ import android.widget.Toast; import com.theartofdev.edmodo.cropper.CropImage; +import java.util.concurrent.atomic.AtomicBoolean; + import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; @@ -34,6 +36,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC private Account account; private boolean support = false; private boolean publishing = false; + private AtomicBoolean handledExternalUri = new AtomicBoolean(false); private OnLongClickListener backToDefaultListener = new OnLongClickListener() { @Override @@ -103,6 +106,19 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC }); this.avatar.setOnClickListener(v -> chooseAvatar()); this.defaultUri = PhoneHelper.getProfilePictureUri(getApplicationContext()); + if (savedInstanceState != null) { + this.avatarUri = savedInstanceState.getParcelable("uri"); + this.handledExternalUri.set(savedInstanceState.getBoolean("handle_external_uri",false)); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) { + if (this.avatarUri != null) { + outState.putParcelable("uri", this.avatarUri); + } + outState.putBoolean("handle_external_uri", handledExternalUri.get()); + super.onSaveInstanceState(outState); } @@ -160,10 +176,20 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC final Intent intent = getIntent(); this.mInitialAccountSetup = intent != null && intent.getBooleanExtra("setup", false); + final Uri uri = intent != null ? intent.getData() : null; + + if (uri != null && handledExternalUri.compareAndSet(false,true)) { + CropImage.activity(uri).setOutputCompressFormat(Bitmap.CompressFormat.PNG) + .setAspectRatio(1, 1) + .setMinCropResultSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE) + .start(this); + return; + } + if (this.mInitialAccountSetup) { this.cancelButton.setText(R.string.skip); } - configureActionBar(getSupportActionBar(), !this.mInitialAccountSetup); + configureActionBar(getSupportActionBar(), !this.mInitialAccountSetup && !handledExternalUri.get()); } protected void loadImageIntoPreview(Uri uri) { diff --git a/src/conversations/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java similarity index 95% rename from src/conversations/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java rename to src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java index d5dca11b5..8ee20c688 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java @@ -6,9 +6,7 @@ import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; -import android.support.v4.content.ContextCompat; import android.support.v7.widget.SwitchCompat; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,7 +21,6 @@ import java.util.concurrent.RejectedExecutionException; import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Account; -import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.util.StyledAttributes; import eu.siacs.conversations.utils.UIHelper; @@ -85,8 +82,8 @@ public class AccountAdapter extends ArrayAdapter { tglAccountState.setVisibility(View.GONE); } tglAccountState.setOnCheckedChangeListener((compoundButton, b) -> { - if (b == isDisabled && activity instanceof ManageAccountActivity) { - ((ManageAccountActivity) activity).onClickTglAccountState(account, b); + if (b == isDisabled && activity instanceof OnTglAccountState) { + ((OnTglAccountState) activity).onClickTglAccountState(account, b); } }); return view; @@ -139,6 +136,11 @@ public class AccountAdapter extends ArrayAdapter { } } + + public interface OnTglAccountState { + void onClickTglAccountState(Account account, boolean state); + } + public static boolean cancelPotentialWork(Account account, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); diff --git a/src/conversations/res/layout/activity_manage_accounts.xml b/src/main/res/layout/activity_manage_accounts.xml similarity index 100% rename from src/conversations/res/layout/activity_manage_accounts.xml rename to src/main/res/layout/activity_manage_accounts.xml diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index ddbd31426..3b5d5e5e2 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -809,4 +809,6 @@ e-book Original (uncompressed) Open with… + Conversations profile picture + Choose account diff --git a/src/quicksy/res/values/strings.xml b/src/quicksy/res/values/strings.xml index 75ad580ed..5f1eda0fc 100644 --- a/src/quicksy/res/values/strings.xml +++ b/src/quicksy/res/values/strings.xml @@ -19,4 +19,5 @@ Quicksy is unable to send encrypted messages to %1$s. This may be due to your contact using an outdated server or client that can not handle OMEMO. Quicksy needs access to the microphone This notification category is used to display a permanent notification indicating that Quicksy is running. + Quicksy profile picture \ No newline at end of file