Fix device linking

This commit is contained in:
Niels Andriesse 2020-01-17 12:53:32 +11:00
parent d538d625e3
commit 9efa528377
5 changed files with 166 additions and 7 deletions

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/default_dialog_background_inset"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="32dp"
android:paddingTop="@dimen/medium_spacing"
android:paddingRight="32dp"
android:paddingBottom="@dimen/medium_spacing">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Device Name"
android:textColor="@color/text"
android:textStyle="bold"
android:textSize="@dimen/medium_font_size" />
<EditText
style="@style/SessionEditText"
android:id="@+id/deviceNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/medium_spacing"
android:layout_marginTop="@dimen/large_spacing"
android:layout_marginRight="@dimen/medium_spacing"
android:textAlignment="center"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:hint="Enter a name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/large_spacing"
android:orientation="horizontal">
<Button
style="@style/UnimportantDialogButton"
android:id="@+id/cancelButton"
android:layout_width="0dp"
android:layout_height="@dimen/small_button_height"
android:layout_weight="1"
android:text="Cancel" />
<Button
style="@style/UnimportantDialogButton"
android:id="@+id/okButton"
android:layout_width="0dp"
android:layout_height="@dimen/small_button_height"
android:layout_weight="1"
android:layout_marginLeft="@dimen/medium_spacing"
android:text="OK" />
</LinearLayout>
</LinearLayout>

View File

@ -1,22 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:background="@color/dialog_background">
<TextView
android:id="@+id/editDisplayNameText"
style="@style/ActionItem"
android:drawableStart="@drawable/ic_edit_white_24dp"
android:text="@string/fragment_device_list_edit_device_name_title"/>
android:textSize="@dimen/medium_font_size"
android:textColor="@color/text"
android:text="Change name"/>
<TextView
android:id="@+id/unlinkDeviceText"
style="@style/ActionItem"
android:drawableStart="@drawable/ic_phonelink_erase_white_24dp"
android:textSize="@dimen/medium_font_size"
android:textColor="@color/text"
android:text="@string/fragment_device_list_unlink_device_title" />
</LinearLayout>

View File

@ -9,21 +9,34 @@ import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_linked_devices.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.devicelist.Device
import org.thoughtcrime.securesms.loki.DeviceListBottomSheetFragment
import org.thoughtcrime.securesms.loki.redesign.dialogs.EditDeviceNameDialog
import org.thoughtcrime.securesms.loki.redesign.dialogs.EditDeviceNameDialogDelegate
import org.thoughtcrime.securesms.loki.redesign.dialogs.LinkDeviceMasterModeDialog
import org.thoughtcrime.securesms.loki.redesign.dialogs.LinkDeviceMasterModeDialogDelegate
import org.thoughtcrime.securesms.loki.signAndSendPairingAuthorisationMessage
import org.thoughtcrime.securesms.sms.MessageSender
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.thoughtcrime.securesms.util.Util
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
import org.whispersystems.signalservice.loki.api.PairingAuthorisation
class LinkedDevicesActivity : PassphraseRequiredActionBarActivity, LoaderManager.LoaderCallbacks<List<Device>>, LinkDeviceMasterModeDialogDelegate {
private val linkedDevicesAdapter = LinkedDevicesAdapter(this)
class LinkedDevicesActivity : PassphraseRequiredActionBarActivity, LoaderManager.LoaderCallbacks<List<Device>>, DeviceClickListener, EditDeviceNameDialogDelegate, LinkDeviceMasterModeDialogDelegate {
private var devices = listOf<Device>()
set(value) { field = value; linkedDevicesAdapter.devices = value }
private val linkedDevicesAdapter by lazy {
val result = LinkedDevicesAdapter(this)
result.deviceClickListener = this
result
}
// region Lifecycle
constructor() : super()
@ -60,12 +73,16 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity, LoaderManager
this.devices = devices
emptyStateContainer.visibility = if (devices.isEmpty()) View.VISIBLE else View.GONE
}
override fun handleDeviceNameChanged(device: Device) {
LoaderManager.getInstance(this).restartLoader(0, null, this)
}
// endregion
// region Interaction
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when (id) {
when(id) {
R.id.linkDeviceButton -> linkDevice()
else -> { /* Do nothing */ }
}
@ -86,11 +103,38 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity, LoaderManager
}
}
override fun onDeviceClick(device: Device) {
val bottomSheet = DeviceListBottomSheetFragment()
bottomSheet.onEditTapped = {
bottomSheet.dismiss()
val editDeviceNameDialog = EditDeviceNameDialog()
editDeviceNameDialog.device = device
editDeviceNameDialog.delegate = this
editDeviceNameDialog.show(supportFragmentManager, "Edit Device Name Dialog")
}
bottomSheet.onUnlinkTapped = {
bottomSheet.dismiss()
unlinkDevice(device.id)
}
bottomSheet.show(supportFragmentManager, bottomSheet.tag)
}
private fun unlinkDevice(slaveDeviceHexEncodedPublicKey: String) {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
val database = DatabaseFactory.getLokiAPIDatabase(this)
database.removePairingAuthorisation(userHexEncodedPublicKey, slaveDeviceHexEncodedPublicKey)
LokiStorageAPI.shared.updateUserDeviceMappings().success {
MessageSender.sendUnpairRequest(this, slaveDeviceHexEncodedPublicKey)
}
LoaderManager.getInstance(this).restartLoader(0, null, this)
Toast.makeText(this, "Your device was unlinked successfully", Toast.LENGTH_LONG).show()
}
override fun onDeviceLinkRequestAuthorized(authorization: PairingAuthorisation) {
AsyncTask.execute {
signAndSendPairingAuthorisationMessage(this, authorization)
Util.runOnMain {
LoaderManager.getInstance(this).restartLoader(0, null, this)
}
}
}

View File

@ -9,6 +9,7 @@ import org.thoughtcrime.securesms.loki.redesign.views.DeviceView
class LinkedDevicesAdapter(private val context: Context) : RecyclerView.Adapter<LinkedDevicesAdapter.ViewHolder>() {
var devices = listOf<Device>()
set(value) { field = value; notifyDataSetChanged() }
var deviceClickListener: DeviceClickListener? = null
class ViewHolder(val view: DeviceView) : RecyclerView.ViewHolder(view)
@ -23,6 +24,12 @@ class LinkedDevicesAdapter(private val context: Context) : RecyclerView.Adapter<
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val device = devices[position]
viewHolder.view.setOnClickListener { deviceClickListener?.onDeviceClick(device) }
viewHolder.view.bind(device)
}
}
interface DeviceClickListener {
fun onDeviceClick(device: Device)
}

View File

@ -0,0 +1,42 @@
package org.thoughtcrime.securesms.loki.redesign.dialogs
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import kotlinx.android.synthetic.main.dialog_edit_device_name.view.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.devicelist.Device
class EditDeviceNameDialog : DialogFragment() {
private lateinit var contentView: View
var device: Device? = null
var delegate: EditDeviceNameDialogDelegate? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(context!!)
contentView = LayoutInflater.from(context!!).inflate(R.layout.dialog_edit_device_name, null)
contentView.cancelButton.setOnClickListener { dismiss() }
contentView.okButton.setOnClickListener { updateDeviceName() }
builder.setView(contentView)
val result = builder.create()
result.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return result
}
private fun updateDeviceName() {
DatabaseFactory.getLokiUserDatabase(context).setDisplayName(device!!.id, contentView.deviceNameEditText.text.toString())
delegate?.handleDeviceNameChanged(device!!)
dismiss()
}
}
interface EditDeviceNameDialogDelegate {
fun handleDeviceNameChanged(device: Device)
}