11package com .nutomic .syncthingandroid .activities ;
22
3+ import static android .text .TextUtils .isEmpty ;
4+ import static android .view .View .GONE ;
5+ import static android .view .View .VISIBLE ;
6+ import static android .view .WindowManager .LayoutParams .SOFT_INPUT_STATE_ALWAYS_HIDDEN ;
7+ import static com .nutomic .syncthingandroid .service .SyncthingService .State .ACTIVE ;
8+ import static com .nutomic .syncthingandroid .util .Compression .METADATA ;
9+
310import android .app .Activity ;
411import android .app .Dialog ;
512import android .content .Context ;
613import android .content .DialogInterface ;
714import android .content .Intent ;
815import android .graphics .drawable .Drawable ;
916import android .os .Bundle ;
10- import androidx .core .content .ContextCompat ;
1117import android .text .Editable ;
1218import android .text .TextUtils ;
1319import android .text .TextWatcher ;
1622import android .view .MenuItem ;
1723import android .view .View ;
1824import android .widget .CompoundButton ;
19- import android .widget .EditText ;
20- import android .widget .TextView ;
2125import android .widget .Toast ;
2226
23- import com .google .android .material .materialswitch .MaterialSwitch ;
27+ import androidx .core .content .ContextCompat ;
28+
2429import com .google .gson .Gson ;
2530import com .nutomic .syncthingandroid .R ;
31+ import com .nutomic .syncthingandroid .databinding .ActivityDeviceBinding ;
2632import com .nutomic .syncthingandroid .model .Connections ;
2733import com .nutomic .syncthingandroid .model .Device ;
2834import com .nutomic .syncthingandroid .service .SyncthingService ;
3440import java .util .Collections ;
3541import java .util .List ;
3642
37- import static android .text .TextUtils .isEmpty ;
38- import static android .view .View .GONE ;
39- import static android .view .View .VISIBLE ;
40- import static android .view .WindowManager .LayoutParams .SOFT_INPUT_STATE_ALWAYS_HIDDEN ;
41- import static com .nutomic .syncthingandroid .service .SyncthingService .State .ACTIVE ;
42- import static com .nutomic .syncthingandroid .util .Compression .METADATA ;
43-
4443/**
4544 * Shows device details and allows changing them.
4645 */
@@ -65,27 +64,7 @@ public class DeviceActivity extends SyncthingActivity implements View.OnClickLis
6564
6665 private Device mDevice ;
6766
68- private View mIdContainer ;
69-
70- private EditText mIdView ;
71-
72- private View mQrButton ;
73-
74- private EditText mNameView ;
75-
76- private EditText mAddressesView ;
77-
78- private TextView mCurrentAddressView ;
79-
80- private TextView mCompressionValueView ;
81-
82- private MaterialSwitch mIntroducerView ;
83-
84- private MaterialSwitch mDevicePaused ;
85-
86- private TextView mSyncthingVersionView ;
87-
88- private View mCompressionContainer ;
67+ private ActivityDeviceBinding binding ;
8968
9069 private boolean mIsCreateMode ;
9170
@@ -105,7 +84,7 @@ public void onClick(DialogInterface dialog, int which) {
10584 mDeviceNeedsToUpdate = true ;
10685
10786 mDevice .compression = compression .getValue (DeviceActivity .this );
108- mCompressionValueView .setText (compression .getTitle (DeviceActivity .this ));
87+ binding . compressionValue .setText (compression .getTitle (DeviceActivity .this ));
10988 }
11089 }
11190 };
@@ -160,26 +139,15 @@ public void onCheckedChanged(CompoundButton view, boolean isChecked) {
160139 @ Override
161140 public void onCreate (Bundle savedInstanceState ) {
162141 super .onCreate (savedInstanceState );
163- setContentView (R .layout .fragment_device );
142+ binding = ActivityDeviceBinding .inflate (getLayoutInflater ());
143+ setContentView (binding .getRoot ());
164144
165145 mIsCreateMode = getIntent ().getBooleanExtra (EXTRA_IS_CREATE , false );
166146 registerOnServiceConnectedListener (this ::onServiceConnected );
167147 setTitle (mIsCreateMode ? R .string .add_device : R .string .edit_device );
168148
169- mIdContainer = findViewById (R .id .idContainer );
170- mIdView = findViewById (R .id .id );
171- mQrButton = findViewById (R .id .qrButton );
172- mNameView = findViewById (R .id .name );
173- mAddressesView = findViewById (R .id .addresses );
174- mCurrentAddressView = findViewById (R .id .currentAddress );
175- mCompressionContainer = findViewById (R .id .compressionContainer );
176- mCompressionValueView = findViewById (R .id .compressionValue );
177- mIntroducerView = findViewById (R .id .introducer );
178- mDevicePaused = findViewById (R .id .devicePause );
179- mSyncthingVersionView = findViewById (R .id .syncthingVersion );
180-
181- mQrButton .setOnClickListener (this );
182- mCompressionContainer .setOnClickListener (this );
149+ binding .qrButton .setOnClickListener (this );
150+ binding .compressionContainer .setOnClickListener (this );
183151
184152 if (savedInstanceState != null ){
185153 if (mDevice == null ) {
@@ -221,9 +189,9 @@ public void onDestroy() {
221189 syncthingService .getNotificationHandler ().cancelConsentNotification (getIntent ().getIntExtra (EXTRA_NOTIFICATION_ID , 0 ));
222190 syncthingService .unregisterOnServiceStateChangeListener (this ::onServiceStateChange );
223191 }
224- mIdView .removeTextChangedListener (mIdTextWatcher );
225- mNameView .removeTextChangedListener (mNameTextWatcher );
226- mAddressesView .removeTextChangedListener (mAddressesTextWatcher );
192+ binding . id .removeTextChangedListener (mIdTextWatcher );
193+ binding . name .removeTextChangedListener (mNameTextWatcher );
194+ binding . addresses .removeTextChangedListener (mAddressesTextWatcher );
227195 }
228196
229197 @ Override
@@ -270,12 +238,12 @@ private void onServiceConnected() {
270238 * version/address changes.
271239 */
272240 private void onReceiveConnections (Connections connections ) {
273- boolean viewsExist = mSyncthingVersionView != null && mCurrentAddressView != null ;
241+ boolean viewsExist = binding . syncthingVersion != null && binding . currentAddress != null ;
274242 if (viewsExist && connections .connections .containsKey (mDevice .deviceID )) {
275- mCurrentAddressView .setVisibility (VISIBLE );
276- mSyncthingVersionView .setVisibility (VISIBLE );
277- mCurrentAddressView .setText (connections .connections .get (mDevice .deviceID ).address );
278- mSyncthingVersionView .setText (connections .connections .get (mDevice .deviceID ).clientVersion );
243+ binding . currentAddress .setVisibility (VISIBLE );
244+ binding . syncthingVersion .setVisibility (VISIBLE );
245+ binding . currentAddress .setText (connections .connections .get (mDevice .deviceID ).address );
246+ binding . syncthingVersion .setText (connections .connections .get (mDevice .deviceID ).clientVersion );
279247 }
280248 }
281249
@@ -307,26 +275,26 @@ private void onServiceStateChange(SyncthingService.State currentState) {
307275 }
308276
309277 private void updateViewsAndSetListeners () {
310- mIdView .removeTextChangedListener (mIdTextWatcher );
311- mNameView .removeTextChangedListener (mNameTextWatcher );
312- mAddressesView .removeTextChangedListener (mAddressesTextWatcher );
313- mIntroducerView .setOnCheckedChangeListener (null );
314- mDevicePaused .setOnCheckedChangeListener (null );
278+ binding . id .removeTextChangedListener (mIdTextWatcher );
279+ binding . name .removeTextChangedListener (mNameTextWatcher );
280+ binding . addresses .removeTextChangedListener (mAddressesTextWatcher );
281+ binding . introducer .setOnCheckedChangeListener (null );
282+ binding . devicePause .setOnCheckedChangeListener (null );
315283
316284 // Update views
317- mIdView .setText (mDevice .deviceID );
318- mNameView .setText (mDevice .name );
319- mAddressesView .setText (displayableAddresses ());
320- mCompressionValueView .setText (Compression .fromValue (this , mDevice .compression ).getTitle (this ));
321- mIntroducerView .setChecked (mDevice .introducer );
322- mDevicePaused .setChecked (mDevice .paused );
285+ binding . id .setText (mDevice .deviceID );
286+ binding . name .setText (mDevice .name );
287+ binding . addresses .setText (displayableAddresses ());
288+ binding . compressionValue .setText (Compression .fromValue (this , mDevice .compression ).getTitle (this ));
289+ binding . introducer .setChecked (mDevice .introducer );
290+ binding . devicePause .setChecked (mDevice .paused );
323291
324292 // Keep state updated
325- mIdView .addTextChangedListener (mIdTextWatcher );
326- mNameView .addTextChangedListener (mNameTextWatcher );
327- mAddressesView .addTextChangedListener (mAddressesTextWatcher );
328- mIntroducerView .setOnCheckedChangeListener (mCheckedListener );
329- mDevicePaused .setOnCheckedChangeListener (mCheckedListener );
293+ binding . id .addTextChangedListener (mIdTextWatcher );
294+ binding . name .addTextChangedListener (mNameTextWatcher );
295+ binding . addresses .addTextChangedListener (mAddressesTextWatcher );
296+ binding . introducer .setOnCheckedChangeListener (mCheckedListener );
297+ binding . devicePause .setOnCheckedChangeListener (mCheckedListener );
330298 }
331299
332300 @ Override
@@ -399,7 +367,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
399367 String scanResult = intent .getStringExtra (QRScannerActivity .QR_RESULT_ARG );
400368 if (scanResult != null ) {
401369 mDevice .deviceID = scanResult ;
402- mIdView .setText (mDevice .deviceID );
370+ binding . id .setText (mDevice .deviceID );
403371 }
404372 }
405373 }
@@ -419,11 +387,11 @@ private void prepareEditMode() {
419387 getWindow ().setSoftInputMode (SOFT_INPUT_STATE_ALWAYS_HIDDEN );
420388
421389 Drawable dr = ContextCompat .getDrawable (this , R .drawable .ic_content_copy_24dp );
422- mIdView .setCompoundDrawablesWithIntrinsicBounds (null , null , dr , null );
423- mIdView .setEnabled (false );
424- mQrButton .setVisibility (GONE );
390+ binding . id .setCompoundDrawablesWithIntrinsicBounds (null , null , dr , null );
391+ binding . id .setEnabled (false );
392+ binding . qrButton .setVisibility (GONE );
425393
426- mIdContainer .setOnClickListener (this );
394+ binding . idContainer .setOnClickListener (this );
427395 }
428396
429397 /**
@@ -450,12 +418,12 @@ private String displayableAddresses() {
450418
451419 @ Override
452420 public void onClick (View v ) {
453- if (v .equals (mCompressionContainer )) {
421+ if (v .equals (binding . compressionContainer )) {
454422 showCompressionDialog ();
455- } else if (v .equals (mQrButton )){
423+ } else if (v .equals (binding . qrButton )){
456424 Intent qrIntent = QRScannerActivity .intent (this );
457425 startActivityForResult (qrIntent , QR_SCAN_REQUEST_CODE );
458- } else if (v .equals (mIdContainer )) {
426+ } else if (v .equals (binding . idContainer )) {
459427 Util .copyDeviceId (this , mDevice .deviceID );
460428 }
461429 }
0 commit comments