From 52ff46043c9ad94fe55ddd0b6db323ec951456f2 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Fri, 25 Jan 2019 23:16:28 +0100 Subject: provide Set as Profile intent --- .../messenger/services/XmppConnectionService.java | 20 +++++- .../ui/ChooseAccountForProfilePictureActivity.java | 82 ++++++++++++++++++++++ .../pixart/messenger/ui/ManageAccountActivity.java | 12 +--- .../ui/PublishProfilePictureActivity.java | 28 +++++++- .../messenger/ui/ShareViaAccountActivity.java | 5 -- .../messenger/ui/adapter/AccountAdapter.java | 8 ++- 6 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 src/main/java/de/pixart/messenger/ui/ChooseAccountForProfilePictureActivity.java (limited to 'src/main/java') diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index a30578a67..6ed9b7f14 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -8,6 +8,7 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -117,6 +118,7 @@ import de.pixart.messenger.parser.MessageParser; import de.pixart.messenger.parser.PresenceParser; import de.pixart.messenger.persistance.DatabaseBackend; import de.pixart.messenger.persistance.FileBackend; +import de.pixart.messenger.ui.ChooseAccountForProfilePictureActivity; import de.pixart.messenger.ui.SettingsActivity; import de.pixart.messenger.ui.UiCallback; import de.pixart.messenger.ui.interfaces.OnAvatarPublication; @@ -1153,8 +1155,10 @@ public class XmppConnectionService extends Service { editor.putBoolean(SettingsActivity.SHOW_FOREGROUND_SERVICE, true); Log.d(Config.LOGTAG, Build.MANUFACTURER + " is on blacklist. enabling foreground service"); } - editor.putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts()).apply(); + final boolean hasEnabledAccounts = hasEnabledAccounts(); + editor.putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts).apply(); editor.apply(); + toggleSetProfilePictureActivity(hasEnabledAccounts); restoreFromDatabase(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { @@ -2185,7 +2189,19 @@ public class XmppConnectionService extends Service { } private void syncEnabledAccountSetting() { - getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts()).apply(); + final boolean hasEnabledAccounts = hasEnabledAccounts(); + getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS, hasEnabledAccounts).apply(); + toggleSetProfilePictureActivity(hasEnabledAccounts); + } + + private void toggleSetProfilePictureActivity(final boolean enabled) { + try { + final ComponentName name = new ComponentName(this, ChooseAccountForProfilePictureActivity.class); + final int targetState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; + getPackageManager().setComponentEnabledSetting(name, targetState, PackageManager.DONT_KILL_APP); + } catch (IllegalStateException e) { + Log.d(Config.LOGTAG, "unable to toggle profile picture actvitiy"); + } } public void createAccountFromKey(final String alias, final OnAccountCreated callback) { diff --git a/src/main/java/de/pixart/messenger/ui/ChooseAccountForProfilePictureActivity.java b/src/main/java/de/pixart/messenger/ui/ChooseAccountForProfilePictureActivity.java new file mode 100644 index 000000000..2d109809e --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/ChooseAccountForProfilePictureActivity.java @@ -0,0 +1,82 @@ +package de.pixart.messenger.ui; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.widget.ListView; + +import java.util.ArrayList; +import java.util.List; + +import de.pixart.messenger.R; +import de.pixart.messenger.entities.Account; +import de.pixart.messenger.ui.adapter.AccountAdapter; + +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(); + } +} \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ManageAccountActivity.java b/src/main/java/de/pixart/messenger/ui/ManageAccountActivity.java index bca4d3dbc..607e2626d 100644 --- a/src/main/java/de/pixart/messenger/ui/ManageAccountActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ManageAccountActivity.java @@ -35,7 +35,7 @@ import de.pixart.messenger.utils.MenuDoubleTabUtil; import de.pixart.messenger.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"; @@ -92,14 +92,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda 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); } @@ -227,6 +220,7 @@ public class ManageAccountActivity extends XmppActivity implements OnAccountUpda } } + @Override public void onClickTglAccountState(Account account, boolean enable) { if (enable) { enableAccount(account); diff --git a/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java b/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java index 469ce8021..e38591848 100644 --- a/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java +++ b/src/main/java/de/pixart/messenger/ui/PublishProfilePictureActivity.java @@ -15,6 +15,8 @@ import android.widget.Toast; import com.theartofdev.edmodo.cropper.CropImage; +import java.util.concurrent.atomic.AtomicBoolean; + import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.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/main/java/de/pixart/messenger/ui/ShareViaAccountActivity.java b/src/main/java/de/pixart/messenger/ui/ShareViaAccountActivity.java index 2d535a44c..114532ba1 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareViaAccountActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareViaAccountActivity.java @@ -27,11 +27,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/java/de/pixart/messenger/ui/adapter/AccountAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java index e37ea6121..391fee0b9 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/AccountAdapter.java @@ -85,13 +85,17 @@ 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; } + public interface OnTglAccountState { + void onClickTglAccountState(Account account, boolean state); + } + public static boolean cancelPotentialWork(Account account, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); -- cgit v1.2.3