1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-25 22:01:16 +00:00

Use view binding in more activities (#2052)

# Description
Use view binding in more activities:
* QRScannerActivity
* ShareActivity
* PullOrderDialogActivity

Rename also a couple of xml layout files from `fragment_xyz` to
`activity_xyz` as they are being used inside activities.
This commit is contained in:
Adam Szewera 2024-02-10 20:51:04 +01:00 committed by GitHub
parent 03f7b8ccef
commit d415716256
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 33 additions and 45 deletions

View file

@ -5,10 +5,7 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button; import com.nutomic.syncthingandroid.databinding.ActivityPullorderDialogBinding;
import android.widget.Spinner;
import com.nutomic.syncthingandroid.R;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -19,6 +16,8 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {
private String selectedType; private String selectedType;
private ActivityPullorderDialogBinding binding;
private static final List<String> mTypes = Arrays.asList( private static final List<String> mTypes = Arrays.asList(
"random", "random",
"alphabetic", "alphabetic",
@ -31,7 +30,8 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pullorder_dialog); binding = ActivityPullorderDialogBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if (savedInstanceState == null) { if (savedInstanceState == null) {
selectedType = getIntent().getStringExtra(EXTRA_PULL_ORDER); selectedType = getIntent().getStringExtra(EXTRA_PULL_ORDER);
} }
@ -40,8 +40,7 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {
} }
private void initiateFinishBtn() { private void initiateFinishBtn() {
Button finishBtn = findViewById(R.id.finish_btn); binding.finishBtn.setOnClickListener(v -> {
finishBtn.setOnClickListener(v -> {
saveConfiguration(); saveConfiguration();
finish(); finish();
}); });
@ -54,9 +53,8 @@ public class PullOrderDialogActivity extends ThemedAppCompatActivity {
} }
private void initiateSpinner() { private void initiateSpinner() {
Spinner pullOrderTypeSpinner = findViewById(R.id.pullOrderTypeSpinner); binding.pullOrderTypeSpinner.setSelection(mTypes.indexOf(selectedType));
pullOrderTypeSpinner.setSelection(mTypes.indexOf(selectedType)); binding.pullOrderTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
pullOrderTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != mTypes.indexOf(selectedType)) { if (position != mTypes.indexOf(selectedType)) {

View file

@ -6,19 +6,15 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.zxing.ResultPoint; import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback; import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult; import com.journeyapps.barcodescanner.BarcodeResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityQrScannerBinding;
import java.util.List; import java.util.List;
@ -33,17 +29,16 @@ public class QRScannerActivity extends ThemedAppCompatActivity implements Barcod
private final int RC_HANDLE_CAMERA_PERM = 888; private final int RC_HANDLE_CAMERA_PERM = 888;
private DecoratedBarcodeView barcodeView; private ActivityQrScannerBinding binding;
// region === Activity Lifecycle === // region === Activity Lifecycle ===
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_qr_scanner); binding = ActivityQrScannerBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
this.barcodeView = findViewById(R.id.bar_code_scanner_view); binding.cancelButton.setOnClickListener(view -> {
findViewById(R.id.cancel_button).setOnClickListener(view -> {
finishScanning(); finishScanning();
}); });
@ -98,12 +93,12 @@ public class QRScannerActivity extends ThemedAppCompatActivity implements Barcod
} }
private void startScanner() { private void startScanner() {
this.barcodeView.resume(); binding.barCodeScannerView.resume();
this.barcodeView.decodeSingle(this); binding.barCodeScannerView.decodeSingle(this);
} }
private void finishScanning() { private void finishScanning() {
this.barcodeView.pause(); binding.barCodeScannerView.pause();
finish(); finish();
} }
// endregion // endregion

View file

@ -1,5 +1,7 @@
package com.nutomic.syncthingandroid.activities; package com.nutomic.syncthingandroid.activities;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
@ -15,14 +17,13 @@ import android.view.View;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityShareBinding;
import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.Folder;
import com.nutomic.syncthingandroid.service.SyncthingService; import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.util.Util; import com.nutomic.syncthingandroid.util.Util;
@ -39,8 +40,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;
/** /**
* Shares incoming files to syncthing folders. * Shares incoming files to syncthing folders.
* <p> * <p>
@ -59,6 +58,8 @@ public class ShareActivity extends StateDialogActivity
private Spinner mFoldersSpinner; private Spinner mFoldersSpinner;
private ActivityShareBinding binding;
@Override @Override
public void onServiceStateChange(SyncthingService.State currentState) { public void onServiceStateChange(SyncthingService.State currentState) {
if (currentState != SyncthingService.State.ACTIVE || getApi() == null) if (currentState != SyncthingService.State.ACTIVE || getApi() == null)
@ -81,9 +82,8 @@ public class ShareActivity extends StateDialogActivity
this, android.R.layout.simple_spinner_item, folders); this, android.R.layout.simple_spinner_item, folders);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = findViewById(R.id.folders); binding.folders.setAdapter(adapter);
sItems.setAdapter(adapter); binding.folders.setSelection(folderIndex);
sItems.setSelection(folderIndex);
} }
@Override @Override
@ -102,18 +102,13 @@ public class ShareActivity extends StateDialogActivity
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share); binding = ActivityShareBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN); getWindow().setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN);
registerOnServiceConnectedListener(this); registerOnServiceConnectedListener(this);
Button mShareButton = findViewById(R.id.share_button);
Button mCancelButton = findViewById(R.id.cancel_button);
Button browseButton = findViewById(R.id.browse_button);
EditText mShareName = findViewById(R.id.name);
TextView mShareTitle = findViewById(R.id.namesTitle);
mSubDirectoryTextView = findViewById(R.id.sub_directory_Textview); mSubDirectoryTextView = findViewById(R.id.sub_directory_Textview);
mFoldersSpinner = findViewById(R.id.folders); mFoldersSpinner = findViewById(R.id.folders);
@ -143,16 +138,16 @@ public class ShareActivity extends StateDialogActivity
files.put(sourceUri, displayName); files.put(sourceUri, displayName);
} }
mShareName.setText(TextUtils.join("\n", files.values())); binding.name.setText(TextUtils.join("\n", files.values()));
if (files.size() > 1) { if (files.size() > 1) {
mShareName.setFocusable(false); binding.name.setFocusable(false);
mShareName.setKeyListener(null); binding.name.setKeyListener(null);
} }
mShareTitle.setText(getResources().getQuantityString(R.plurals.file_name_title, binding.namesTitle.setText(getResources().getQuantityString(R.plurals.file_name_title,
files.size() > 1 ? 2 : 1)); files.size() > 1 ? 2 : 1));
mShareButton.setOnClickListener(view -> { binding.shareButton.setOnClickListener(view -> {
if (files.size() == 1) if (files.size() == 1)
files.entrySet().iterator().next().setValue(mShareName.getText().toString()); files.entrySet().iterator().next().setValue(binding.name.getText().toString());
Folder folder = (Folder) mFoldersSpinner.getSelectedItem(); Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File directory = new File(folder.path, getSavedSubDirectory()); File directory = new File(folder.path, getSavedSubDirectory());
CopyFilesTask mCopyFilesTask = new CopyFilesTask(this, files, folder, directory); CopyFilesTask mCopyFilesTask = new CopyFilesTask(this, files, folder, directory);
@ -171,7 +166,7 @@ public class ShareActivity extends StateDialogActivity
} }
}); });
browseButton.setOnClickListener(view -> { binding.browseButton.setOnClickListener(view -> {
Folder folder = (Folder) mFoldersSpinner.getSelectedItem(); Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File initialDirectory = new File(folder.path, getSavedSubDirectory()); File initialDirectory = new File(folder.path, getSavedSubDirectory());
startActivityForResult(FolderPickerActivity.createIntent(getApplicationContext(), startActivityForResult(FolderPickerActivity.createIntent(getApplicationContext(),
@ -179,7 +174,7 @@ public class ShareActivity extends StateDialogActivity
FolderPickerActivity.DIRECTORY_REQUEST_CODE); FolderPickerActivity.DIRECTORY_REQUEST_CODE);
}); });
mCancelButton.setOnClickListener(view -> finish()); binding.cancelButton.setOnClickListener(view -> finish());
mSubDirectoryTextView.setText(getSavedSubDirectory()); mSubDirectoryTextView.setText(getSavedSubDirectory());
} }