diff --git a/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.java b/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.java deleted file mode 100644 index 3d14d66b..00000000 --- a/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2019 Juan David Vega - * - * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL -*/ - - -package org.kde.kdeconnect.UserInterface; - -import android.Manifest; -import android.content.pm.PackageManager; -import android.os.Bundle; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.ListView; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; - -import com.android.internal.util.ArrayUtils; - -import org.kde.kdeconnect.Helpers.TrustedNetworkHelper; -import org.kde.kdeconnect_tp.R; -import org.kde.kdeconnect_tp.databinding.TrustedNetworkListBinding; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -public class TrustedNetworksActivity extends AppCompatActivity { - private TrustedNetworkListBinding binding; - private List trustedNetworks; - - private ListView trustedNetworksView; - private CheckBox allowAllCheckBox; - private TrustedNetworkHelper trustedNetworkHelper; - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - if (ArrayUtils.contains(grantResults, PackageManager.PERMISSION_GRANTED)) { - allowAllCheckBox.setChecked(false); - } - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - - super.onCreate(savedInstanceState); - - binding = TrustedNetworkListBinding.inflate(getLayoutInflater()); - setContentView(binding.getRoot()); - trustedNetworksView = binding.list; - - setSupportActionBar(binding.toolbarLayout.toolbar); - Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setDisplayShowHomeEnabled(true); - - trustedNetworkHelper = new TrustedNetworkHelper(getApplicationContext()); - trustedNetworks = new ArrayList<>(); - Collections.addAll(trustedNetworks, trustedNetworkHelper.read()); - - allowAllCheckBox = binding.trustAllNetworksCheckBox; - allowAllCheckBox.setOnCheckedChangeListener((v, isChecked) -> { - if (trustedNetworkHelper.hasPermissions()) { - trustedNetworkHelper.allAllowed(isChecked); - updateTrustedNetworkListView(); - addNetworkButton(); - } else { - allowAllCheckBox.setChecked(true); // Disable unchecking it - new PermissionsAlertDialogFragment.Builder() - .setTitle(R.string.location_permission_needed_title) - .setMessage(R.string.location_permission_needed_desc) - .setPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}) - .setRequestCode(0) - .create().show(getSupportFragmentManager(), null); - } - }); - allowAllCheckBox.setChecked(trustedNetworkHelper.allAllowed()); - - updateTrustedNetworkListView(); - } - - private void updateEmptyListMessage() { - boolean isVisible = trustedNetworks.isEmpty() && !trustedNetworkHelper.allAllowed(); - binding.trustedNetworkListEmpty.setVisibility(isVisible ? View.VISIBLE : View.GONE ); - } - - private void updateTrustedNetworkListView() { - boolean allAllowed = trustedNetworkHelper.allAllowed(); - updateEmptyListMessage(); - trustedNetworksView.setVisibility(allAllowed ? View.GONE : View.VISIBLE); - if (allAllowed){ - return; - } - trustedNetworksView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, trustedNetworks)); - trustedNetworksView.setOnItemClickListener((parent, view, position, id) -> { - String targetItem = trustedNetworks.get(position); - new AlertDialog.Builder(TrustedNetworksActivity.this) - .setMessage("Delete " + targetItem + " ?") - .setPositiveButton("Yes", (dialog, which) -> { - trustedNetworks.remove(position); - trustedNetworkHelper.update(trustedNetworks); - ((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged(); - addNetworkButton(); - updateEmptyListMessage(); - }) - .setNegativeButton("No", null) - .show(); - - }); - addNetworkButton(); - } - - private void addNetworkButton() { - Button addButton = binding.button1; - if (trustedNetworkHelper.allAllowed()) { - addButton.setVisibility(View.GONE); - return; - } - final String currentSSID = trustedNetworkHelper.currentSSID(); - if (!currentSSID.isEmpty() && !trustedNetworks.contains(currentSSID)) { - String buttonText = getString(R.string.add_trusted_network, currentSSID); - addButton.setText(buttonText); - addButton.setOnClickListener(v -> { - if (trustedNetworks.contains(currentSSID)){ - return; - } - trustedNetworks.add(currentSSID); - trustedNetworkHelper.update(trustedNetworks); - ((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged(); - v.setVisibility(View.GONE); - updateEmptyListMessage(); - }); - addButton.setVisibility(View.VISIBLE); - } else { - addButton.setVisibility(View.GONE); - } - } - - @Override - public boolean onSupportNavigateUp() { - super.onBackPressed(); - return true; - } - -} diff --git a/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.kt b/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.kt new file mode 100644 index 00000000..d6fda656 --- /dev/null +++ b/src/org/kde/kdeconnect/UserInterface/TrustedNetworksActivity.kt @@ -0,0 +1,135 @@ +/* + * SPDX-FileCopyrightText: 2019 Juan David Vega + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ +package org.kde.kdeconnect.UserInterface + +import android.Manifest +import android.content.DialogInterface +import android.content.pm.PackageManager +import android.os.Bundle +import android.view.View +import android.widget.* +import android.widget.AdapterView.OnItemClickListener +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import org.kde.kdeconnect.Helpers.TrustedNetworkHelper +import org.kde.kdeconnect_tp.R +import org.kde.kdeconnect_tp.databinding.TrustedNetworkListBinding + +class TrustedNetworksActivity : AppCompatActivity() { + lateinit var binding: TrustedNetworkListBinding + private val trustedNetworks: MutableList = mutableListOf() + + private val trustedNetworksView: ListView + get() = binding.list + private val allowAllCheckBox: CheckBox + get() = binding.trustAllNetworksCheckBox + private val trustedNetworkHelper: TrustedNetworkHelper by lazy { + TrustedNetworkHelper(applicationContext) // Lazy to avoid creating it before onCreate, because it needs the context + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (PackageManager.PERMISSION_GRANTED in grantResults) { + allowAllCheckBox.isChecked = false + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = TrustedNetworkListBinding.inflate(layoutInflater) + setContentView(binding.root) + + setSupportActionBar(binding.toolbarLayout.toolbar) + supportActionBar!!.apply { + setDisplayHomeAsUpEnabled(true) + setDisplayShowHomeEnabled(true) + } + + trustedNetworks.addAll(trustedNetworkHelper.read()) + + allowAllCheckBox.setOnCheckedChangeListener { _, isChecked -> + if (trustedNetworkHelper.hasPermissions()) { + trustedNetworkHelper.allAllowed(isChecked) + updateTrustedNetworkListView() + addNetworkButton() + } else { + allowAllCheckBox.isChecked = true // Disable unchecking it + PermissionsAlertDialogFragment.Builder() + .setTitle(R.string.location_permission_needed_title) + .setMessage(R.string.location_permission_needed_desc) + .setPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)) + .setRequestCode(0) + .create().show(supportFragmentManager, null) + } + } + allowAllCheckBox.isChecked = trustedNetworkHelper.allAllowed() + + updateTrustedNetworkListView() + } + + private fun updateEmptyListMessage() { + val isVisible = trustedNetworks.isEmpty() && !trustedNetworkHelper.allAllowed() + binding.trustedNetworkListEmpty.visibility = if (isVisible) View.VISIBLE else View.GONE + } + + private fun updateTrustedNetworkListView() { + val allAllowed = trustedNetworkHelper.allAllowed() + updateEmptyListMessage() + trustedNetworksView.visibility = if (allAllowed) View.GONE else View.VISIBLE + if (allAllowed) { + return + } + trustedNetworksView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, trustedNetworks) + trustedNetworksView.onItemClickListener = + OnItemClickListener { _, _, position, _ -> + val targetItem = trustedNetworks[position] + AlertDialog.Builder(this@TrustedNetworksActivity) + .setMessage("Delete $targetItem ?") + .setPositiveButton("Yes") { _, _ -> + trustedNetworks.removeAt(position) + trustedNetworkHelper.update(trustedNetworks) + (trustedNetworksView.adapter as ArrayAdapter<*>).notifyDataSetChanged() + addNetworkButton() + updateEmptyListMessage() + } + .setNegativeButton("No", null) + .show() + } + addNetworkButton() + } + + private fun addNetworkButton() { + val addButton = binding.button1 + if (trustedNetworkHelper.allAllowed()) { + addButton.visibility = View.GONE + return + } + val currentSSID = trustedNetworkHelper.currentSSID() + if (currentSSID.isNotEmpty() && !trustedNetworks.contains(currentSSID)) { + val buttonText = getString(R.string.add_trusted_network, currentSSID) + addButton.text = buttonText + addButton.setOnClickListener { v -> + if (trustedNetworks.contains(currentSSID)) { + return@setOnClickListener + } + trustedNetworks.add(currentSSID) + trustedNetworkHelper.update(trustedNetworks) + (trustedNetworksView.adapter as ArrayAdapter<*>).notifyDataSetChanged() + v.visibility = View.GONE + updateEmptyListMessage() + } + addButton.visibility = View.VISIBLE + } else { + addButton.visibility = View.GONE + } + } + + override fun onSupportNavigateUp(): Boolean { + this.onBackPressedDispatcher.onBackPressed() + return true + } +}