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
|
|
|
|
|
|
|
import org.kde.kdeconnect.Helpers.TrustedNetworkHelper;
|
|
|
|
import org.kde.kdeconnect_tp.R;
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-10-27 20:40:17 +01:00
|
|
|
|
|
|
|
public class TrustedNetworksActivity extends AppCompatActivity {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
boolean grantedPermission = false;
|
|
|
|
for (int result : grantResults) {
|
|
|
|
if (result == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
grantedPermission = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (grantedPermission) {
|
|
|
|
allowAllCheckBox.setChecked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:40:17 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
ThemeUtil.setUserPreferredTheme(this);
|
2019-10-27 23:37:40 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2019-10-27 20:40:17 +01:00
|
|
|
setContentView(R.layout.trusted_network_list);
|
|
|
|
trustedNetworksView = findViewById(android.R.id.list);
|
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
trustedNetworkHelper = new TrustedNetworkHelper(getApplicationContext());
|
|
|
|
trustedNetworks = new ArrayList<>(trustedNetworkHelper.read());
|
2019-10-27 20:40:17 +01:00
|
|
|
|
2019-10-27 23:37:40 +01:00
|
|
|
allowAllCheckBox = findViewById(R.id.trust_all_networks_checkBox);
|
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)
|
|
|
|
.setPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION})
|
|
|
|
.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();
|
|
|
|
findViewById(R.id.trusted_network_list_empty)
|
2019-10-27 23:37:40 +01:00
|
|
|
.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() {
|
2019-10-27 20:40:17 +01:00
|
|
|
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() {
|
|
|
|
Button addButton = findViewById(android.R.id.button1);
|
|
|
|
if (trustedNetworkHelper.allAllowed()) {
|
|
|
|
addButton.setVisibility(View.GONE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String currentSSID = trustedNetworkHelper.currentSSID();
|
2019-10-27 20:40:17 +01:00
|
|
|
if (!currentSSID.isEmpty() && trustedNetworks.indexOf(currentSSID) == -1) {
|
|
|
|
String buttonText = getString(R.string.add_trusted_network, currentSSID);
|
|
|
|
addButton.setText(buttonText);
|
2019-10-27 23:37:40 +01:00
|
|
|
addButton.setOnClickListener(v -> {
|
|
|
|
if (trustedNetworks.indexOf(currentSSID) != -1){
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|