From c66786e0f1f3448432f1a9bd17d3c43dd60662cc Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 21 Nov 2019 10:50:33 +1100 Subject: [PATCH] Allow users to edit device name. --- .../fragment_device_list_bottom_sheet.xml | 2 +- res/values/strings.xml | 3 +- .../securesms/DeviceListFragment.java | 59 ++++++++++++++----- .../securesms/DeviceListItem.java | 4 ++ .../securesms/loki/LinkedDevicesActivity.kt | 5 ++ 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/res/layout/fragment_device_list_bottom_sheet.xml b/res/layout/fragment_device_list_bottom_sheet.xml index dca86bdbe8..5c1092fa65 100644 --- a/res/layout/fragment_device_list_bottom_sheet.xml +++ b/res/layout/fragment_device_list_bottom_sheet.xml @@ -11,7 +11,7 @@ android:id="@+id/editDisplayNameText" style="@style/ActionItem" android:drawableStart="@drawable/ic_edit_white_24dp" - android:text="@string/fragment_device_list_edit_display_name_title"/> + android:text="@string/fragment_device_list_edit_device_name_title"/> Unlinking device Network failed! Successfully unlinked device + Edit device name Unnamed device @@ -1644,7 +1645,7 @@ Add Public Chat - Edit display name + Edit device name Unlink device diff --git a/src/org/thoughtcrime/securesms/DeviceListFragment.java b/src/org/thoughtcrime/securesms/DeviceListFragment.java index 8592c3fef2..2490012f23 100644 --- a/src/org/thoughtcrime/securesms/DeviceListFragment.java +++ b/src/org/thoughtcrime/securesms/DeviceListFragment.java @@ -15,6 +15,7 @@ import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; import android.widget.ListView; import com.melnykov.fab.FloatingActionButton; @@ -34,6 +35,8 @@ import java.util.Locale; import org.whispersystems.libsignal.util.guava.Function; +import kotlin.Pair; +import kotlin.Unit; import network.loki.messenger.R; public class DeviceListFragment extends ListFragment @@ -50,6 +53,7 @@ public class DeviceListFragment extends ListFragment private FloatingActionButton addDeviceButton; private Button.OnClickListener addDeviceButtonListener; private Function handleDisconnectDevice; + private Function, Void> handleDeviceNameChange; @Override public void onCreate(Bundle savedInstanceState) { @@ -92,6 +96,10 @@ public class DeviceListFragment extends ListFragment this.handleDisconnectDevice = handler; } + public void setHandleDeviceNameChange(Function, Void> handler) { + this.handleDeviceNameChange = handler; + } + @Override public @NonNull Loader> onCreateLoader(int id, Bundle args) { empty.setVisibility(View.GONE); @@ -126,25 +134,44 @@ public class DeviceListFragment extends ListFragment @Override public void onItemClick(AdapterView parent, View view, int position, long id) { + final boolean hasDeviceName = ((DeviceListItem)view).hasDeviceName(); // Tells us whether the name is set to shortId or the device name final String deviceName = ((DeviceListItem)view).getDeviceName(); - final String deviceId = ((DeviceListItem)view).getDeviceId(); + final String deviceId = ((DeviceListItem)view).getDeviceId(); - DeviceListBottomSheetFragment fragment = new DeviceListBottomSheetFragment(); - fragment.show(getFragmentManager(), fragment.getTag()); - /* - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(getActivity().getString(R.string.DeviceListActivity_unlink_s, deviceName)); - builder.setMessage(R.string.DeviceListActivity_by_unlinking_this_device_it_will_no_longer_be_able_to_send_or_receive); - builder.setNegativeButton(android.R.string.cancel, null); - builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (handleDisconnectDevice != null) { handleDisconnectDevice.apply(deviceId); } - } + DeviceListBottomSheetFragment bottomSheet = new DeviceListBottomSheetFragment(); + bottomSheet.setOnEditTapped(() -> { + bottomSheet.dismiss(); + EditText deviceNameText = new EditText(getContext()); + deviceNameText.setText(hasDeviceName ? deviceName : ""); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.DeviceListActivity_edit_device_name); + builder.setView(deviceNameText); + builder.setNegativeButton(android.R.string.cancel, null); + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (handleDeviceNameChange != null) { handleDeviceNameChange.apply(new Pair<>(deviceId, deviceNameText.getText().toString().trim())); } + } + }); + builder.show(); + return Unit.INSTANCE; }); - builder.show(); - - */ + bottomSheet.setOnUnlinkTapped(() -> { + bottomSheet.dismiss(); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(getActivity().getString(R.string.DeviceListActivity_unlink_s, deviceName)); + builder.setMessage(R.string.DeviceListActivity_by_unlinking_this_device_it_will_no_longer_be_able_to_send_or_receive); + builder.setNegativeButton(android.R.string.cancel, null); + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (handleDisconnectDevice != null) { handleDisconnectDevice.apply(deviceId); } + } + }); + builder.show(); + return Unit.INSTANCE; + }); + bottomSheet.show(getFragmentManager(), bottomSheet.getTag()); } public void refresh() { diff --git a/src/org/thoughtcrime/securesms/DeviceListItem.java b/src/org/thoughtcrime/securesms/DeviceListItem.java index 83d2f2a7d9..bfc96b4d7a 100644 --- a/src/org/thoughtcrime/securesms/DeviceListItem.java +++ b/src/org/thoughtcrime/securesms/DeviceListItem.java @@ -50,4 +50,8 @@ public class DeviceListItem extends LinearLayout { return name.getText().toString(); } + public boolean hasDeviceName() { + return shortId.getVisibility() == VISIBLE; + } + } diff --git a/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt b/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt index fd1568733d..28c8556fcd 100644 --- a/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/LinkedDevicesActivity.kt @@ -52,6 +52,11 @@ class LinkedDevicesActivity : PassphraseRequiredActionBarActivity(), DeviceLinki Toast.makeText(this, R.string.DeviceListActivity_unlinked_device, Toast.LENGTH_LONG).show() return@setHandleDisconnectDevice null } + this.deviceListFragment.setHandleDeviceNameChange { pair -> + DatabaseFactory.getLokiUserDatabase(this).setDisplayName(pair.first, pair.second) + this.deviceListFragment.refresh() + return@setHandleDeviceNameChange null + } initFragment(android.R.id.content, deviceListFragment, dynamicLanguage.currentLocale) }