From ba78fcb9b115cfde9e92dbfcb2eff8dd899f574d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 20 Nov 2019 11:44:44 +1100 Subject: [PATCH] Move device linking logic into LinkedDeviceActivity. --- .../ApplicationPreferencesActivity.java | 1 - .../securesms/DeviceListFragment.java | 15 ++++++++++---- .../loki/DeviceLinkingDialogDelegate.kt | 1 - .../securesms/loki/LinkedDevicesActivity.kt | 20 +++++++++---------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java index ead8bc1bac..05f45e52ce 100644 --- a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java +++ b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java @@ -353,7 +353,6 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarA case PREFERENCE_CATEGORY_LINKED_DEVICES: Intent intent = new Intent(getActivity(), LinkedDevicesActivity.class); startActivity(intent); - DeviceLinkingDialog.Companion.show(getContext(), DeviceLinkingView.Mode.Master, this); break; case PREFERENCE_CATEGORY_SEED: Analytics.Companion.getShared().track("Seed Modal Shown"); diff --git a/src/org/thoughtcrime/securesms/DeviceListFragment.java b/src/org/thoughtcrime/securesms/DeviceListFragment.java index be3758e6c4..08fd4040b3 100644 --- a/src/org/thoughtcrime/securesms/DeviceListFragment.java +++ b/src/org/thoughtcrime/securesms/DeviceListFragment.java @@ -19,6 +19,7 @@ import android.widget.ListView; import com.melnykov.fab.FloatingActionButton; +import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.loaders.DeviceListLoader; import org.thoughtcrime.securesms.dependencies.InjectableType; import org.thoughtcrime.securesms.devicelist.Device; @@ -69,6 +70,7 @@ public class DeviceListFragment extends ListFragment this.progressContainer = view.findViewById(R.id.progress_container); this.addDeviceButton = ViewUtil.findById(view, R.id.add_device); this.addDeviceButton.setOnClickListener(this); + updateAddDeviceButtonVisibility(); return view; } @@ -89,10 +91,6 @@ public class DeviceListFragment extends ListFragment this.handleDisconnectDevice = handler; } - public void setAddDeviceButtonVisible(boolean visible) { - addDeviceButton.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); - } - @Override public @NonNull Loader> onCreateLoader(int id, Bundle args) { empty.setVisibility(View.GONE); @@ -144,9 +142,18 @@ public class DeviceListFragment extends ListFragment } public void refresh() { + updateAddDeviceButtonVisibility(); getLoaderManager().restartLoader(0, null, DeviceListFragment.this); } + private void updateAddDeviceButtonVisibility() { + if (addDeviceButton != null) { + String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(getContext()); + boolean isDeviceLinkingEnabled = DatabaseFactory.getLokiAPIDatabase(getContext()).getPairingAuthorisations(userHexEncodedPublicKey).isEmpty(); + addDeviceButton.setVisibility(isDeviceLinkingEnabled ? View.VISIBLE : View.INVISIBLE); + } + } + private void handleLoaderFailed() { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.DeviceListActivity_network_connection_failed); diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialogDelegate.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialogDelegate.kt index 48493e25e2..5083b013b3 100644 --- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialogDelegate.kt +++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialogDelegate.kt @@ -3,7 +3,6 @@ package org.thoughtcrime.securesms.loki import org.whispersystems.signalservice.loki.api.PairingAuthorisation interface DeviceLinkingDialogDelegate { - fun handleDeviceLinkAuthorized(pairingAuthorisation: PairingAuthorisation) { } fun handleDeviceLinkingDialogDismissed() { } fun sendPairingAuthorizedMessage(pairingAuthorisation: PairingAuthorisation) { } diff --git a/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt b/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt index 7a61384ad8..a0fd9ae183 100644 --- a/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt @@ -11,8 +11,9 @@ import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.sms.MessageSender import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.signalservice.loki.api.LokiStorageAPI +import org.whispersystems.signalservice.loki.api.PairingAuthorisation -class LinkedDevicesActivity : PassphraseRequiredActionBarActivity() { +class LinkedDevicesActivity : PassphraseRequiredActionBarActivity(), DeviceLinkingDialogDelegate { companion object { private val TAG = DeviceActivity::class.java.simpleName @@ -33,7 +34,7 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity() { supportActionBar?.setTitle(R.string.AndroidManifest__linked_devices) this.deviceListFragment = DeviceListFragment() this.deviceListFragment.setAddDeviceButtonListener { - // TODO: Hook up add device + DeviceLinkingDialog.show(this, DeviceLinkingView.Mode.Master, this) } this.deviceListFragment.setHandleDisconnectDevice { devicePublicKey -> // Purge the device pairing from our database @@ -45,19 +46,11 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity() { // Send a background message to let the device know that it has been revoked MessageSender.sendBackgroundMessage(this, devicePublicKey) // Refresh the list - refresh() + this.deviceListFragment.refresh() Toast.makeText(this, R.string.DeviceListActivity_unlinked_device, Toast.LENGTH_LONG).show() return@setHandleDisconnectDevice null } initFragment(android.R.id.content, deviceListFragment, dynamicLanguage.currentLocale) - refresh() - } - - private fun refresh() { - val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this) - val isDeviceLinkingEnabled = DatabaseFactory.getLokiAPIDatabase(this).getPairingAuthorisations(userHexEncodedPublicKey).isEmpty() - this.deviceListFragment.setAddDeviceButtonVisible(isDeviceLinkingEnabled) - this.deviceListFragment.refresh() } public override fun onResume() { @@ -73,4 +66,9 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity() { } return false } + + override fun sendPairingAuthorizedMessage(pairingAuthorisation: PairingAuthorisation) { + signAndSendPairingAuthorisationMessage(this, pairingAuthorisation) + this.deviceListFragment.refresh() + } }