2019-10-27 20:40:17 +01:00
|
|
|
package org.kde.kdeconnect.UserInterface;
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
import android.Manifest;
|
2020-01-05 17:27:16 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2019-10-27 23:37:40 +01:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.ListView;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2020-01-05 17:27:16 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2019-10-27 23:37:40 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2020-07-10 08:21:25 +05:30
|
|
|
import com.android.internal.util.ArrayUtils;
|
|
|
|
|
2019-10-27 20:40:17 +01:00
|
|
|
import org.kde.kdeconnect.Helpers.TrustedNetworkHelper;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
2020-07-08 05:43:36 +05:30
|
|
|
import org.kde.kdeconnect_tp.databinding.TrustedNetworkListBinding;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
import java.util.ArrayList;
|
2020-07-10 08:21:25 +05:30
|
|
|
import java.util.Collections;
|
2019-10-27 23:37:40 +01:00
|
|
|
import java.util.List;
|
2021-03-07 10:55:47 +00:00
|
|
|
import java.util.Objects;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
|
|
|
public class TrustedNetworksActivity extends AppCompatActivity {
|
2020-07-08 05:43:36 +05:30
|
|
|
private TrustedNetworkListBinding binding;
|
2019-10-27 20:40:17 +01:00
|
|
|
private List<String> trustedNetworks;
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
private ListView trustedNetworksView;
|
|
|
|
private CheckBox allowAllCheckBox;
|
|
|
|
private TrustedNetworkHelper trustedNetworkHelper;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2020-01-05 17:27:16 +01:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
2020-07-10 08:21:25 +05:30
|
|
|
if (ArrayUtils.contains(grantResults, PackageManager.PERMISSION_GRANTED)) {
|
2020-01-05 17:27:16 +01:00
|
|
|
allowAllCheckBox.setChecked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:40:17 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
2020-07-16 06:28:33 +00:00
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
2019-10-27 23:37:40 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-07-08 05:43:36 +05:30
|
|
|
|
|
|
|
binding = TrustedNetworkListBinding.inflate(getLayoutInflater());
|
|
|
|
setContentView(binding.getRoot());
|
|
|
|
trustedNetworksView = binding.list;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2021-03-07 10:55:47 +00:00
|
|
|
setSupportActionBar(binding.toolbarLayout.toolbar);
|
|
|
|
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
trustedNetworkHelper = new TrustedNetworkHelper(getApplicationContext());
|
2020-07-10 08:21:25 +05:30
|
|
|
trustedNetworks = new ArrayList<>();
|
|
|
|
Collections.addAll(trustedNetworks, trustedNetworkHelper.read());
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2020-07-08 05:43:36 +05:30
|
|
|
allowAllCheckBox = binding.trustAllNetworksCheckBox;
|
2019-10-27 20:40:17 +01:00
|
|
|
allowAllCheckBox.setOnCheckedChangeListener((v, isChecked) -> {
|
2019-10-27 23:37:40 +01:00
|
|
|
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)
|
|
|
|
.setPositiveButton(R.string.ok)
|
|
|
|
.setNegativeButton(R.string.cancel)
|
2020-04-21 20:47:35 +02:00
|
|
|
.setPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION})
|
2019-10-27 23:37:40 +01:00
|
|
|
.setRequestCode(0)
|
|
|
|
.create().show(getSupportFragmentManager(), null);
|
|
|
|
}
|
2019-10-27 20:40:17 +01:00
|
|
|
});
|
2019-10-27 23:37:40 +01:00
|
|
|
allowAllCheckBox.setChecked(trustedNetworkHelper.allAllowed());
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
updateTrustedNetworkListView();
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
private void updateEmptyListMessage() {
|
2019-10-27 20:40:17 +01:00
|
|
|
boolean isVisible = trustedNetworks.isEmpty() && !trustedNetworkHelper.allAllowed();
|
2020-07-08 05:43:36 +05:30
|
|
|
binding.trustedNetworkListEmpty.setVisibility(isVisible ? View.VISIBLE : View.GONE );
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
private void updateTrustedNetworkListView() {
|
2020-07-08 05:43:36 +05:30
|
|
|
boolean allAllowed = trustedNetworkHelper.allAllowed();
|
2019-10-27 23:37:40 +01:00
|
|
|
updateEmptyListMessage();
|
|
|
|
trustedNetworksView.setVisibility(allAllowed ? View.GONE : View.VISIBLE);
|
|
|
|
if (allAllowed){
|
|
|
|
return;
|
|
|
|
}
|
2019-10-27 20:40:17 +01:00
|
|
|
trustedNetworksView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, trustedNetworks));
|
2019-10-27 23:37:40 +01:00
|
|
|
trustedNetworksView.setOnItemClickListener((parent, view, position, id) -> {
|
2019-10-27 20:40:17 +01:00
|
|
|
String targetItem = trustedNetworks.get(position);
|
2019-10-27 23:37:40 +01:00
|
|
|
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();
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
});
|
|
|
|
addNetworkButton();
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
private void addNetworkButton() {
|
2020-07-08 05:43:36 +05:30
|
|
|
Button addButton = binding.button1;
|
2019-10-27 23:37:40 +01:00
|
|
|
if (trustedNetworkHelper.allAllowed()) {
|
|
|
|
addButton.setVisibility(View.GONE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String currentSSID = trustedNetworkHelper.currentSSID();
|
2020-07-08 05:43:36 +05:30
|
|
|
if (!currentSSID.isEmpty() && !trustedNetworks.contains(currentSSID)) {
|
2019-10-27 20:40:17 +01:00
|
|
|
String buttonText = getString(R.string.add_trusted_network, currentSSID);
|
|
|
|
addButton.setText(buttonText);
|
2019-10-27 23:37:40 +01:00
|
|
|
addButton.setOnClickListener(v -> {
|
2020-07-08 05:43:36 +05:30
|
|
|
if (trustedNetworks.contains(currentSSID)){
|
2019-10-27 23:37:40 +01:00
|
|
|
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);
|
2019-10-27 20:40:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|