File indexing completed on 2024-12-22 04:41:42
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Juan David Vega <jdvr.93@hotmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 0008 package org.kde.kdeconnect.UserInterface; 0009 0010 import android.Manifest; 0011 import android.content.pm.PackageManager; 0012 import android.os.Bundle; 0013 import android.view.View; 0014 import android.widget.ArrayAdapter; 0015 import android.widget.Button; 0016 import android.widget.CheckBox; 0017 import android.widget.ListView; 0018 0019 import androidx.annotation.NonNull; 0020 import androidx.appcompat.app.AlertDialog; 0021 import androidx.appcompat.app.AppCompatActivity; 0022 0023 import com.android.internal.util.ArrayUtils; 0024 0025 import org.kde.kdeconnect.Helpers.TrustedNetworkHelper; 0026 import org.kde.kdeconnect_tp.R; 0027 import org.kde.kdeconnect_tp.databinding.TrustedNetworkListBinding; 0028 0029 import java.util.ArrayList; 0030 import java.util.Collections; 0031 import java.util.List; 0032 import java.util.Objects; 0033 0034 public class TrustedNetworksActivity extends AppCompatActivity { 0035 private TrustedNetworkListBinding binding; 0036 private List<String> trustedNetworks; 0037 0038 private ListView trustedNetworksView; 0039 private CheckBox allowAllCheckBox; 0040 private TrustedNetworkHelper trustedNetworkHelper; 0041 0042 @Override 0043 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 0044 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 0045 if (ArrayUtils.contains(grantResults, PackageManager.PERMISSION_GRANTED)) { 0046 allowAllCheckBox.setChecked(false); 0047 } 0048 } 0049 0050 @Override 0051 protected void onCreate(Bundle savedInstanceState) { 0052 0053 super.onCreate(savedInstanceState); 0054 0055 binding = TrustedNetworkListBinding.inflate(getLayoutInflater()); 0056 setContentView(binding.getRoot()); 0057 trustedNetworksView = binding.list; 0058 0059 setSupportActionBar(binding.toolbarLayout.toolbar); 0060 Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); 0061 getSupportActionBar().setDisplayShowHomeEnabled(true); 0062 0063 trustedNetworkHelper = new TrustedNetworkHelper(getApplicationContext()); 0064 trustedNetworks = new ArrayList<>(); 0065 Collections.addAll(trustedNetworks, trustedNetworkHelper.read()); 0066 0067 allowAllCheckBox = binding.trustAllNetworksCheckBox; 0068 allowAllCheckBox.setOnCheckedChangeListener((v, isChecked) -> { 0069 if (trustedNetworkHelper.hasPermissions()) { 0070 trustedNetworkHelper.allAllowed(isChecked); 0071 updateTrustedNetworkListView(); 0072 addNetworkButton(); 0073 } else { 0074 allowAllCheckBox.setChecked(true); // Disable unchecking it 0075 new PermissionsAlertDialogFragment.Builder() 0076 .setTitle(R.string.location_permission_needed_title) 0077 .setMessage(R.string.location_permission_needed_desc) 0078 .setPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}) 0079 .setRequestCode(0) 0080 .create().show(getSupportFragmentManager(), null); 0081 } 0082 }); 0083 allowAllCheckBox.setChecked(trustedNetworkHelper.allAllowed()); 0084 0085 updateTrustedNetworkListView(); 0086 } 0087 0088 private void updateEmptyListMessage() { 0089 boolean isVisible = trustedNetworks.isEmpty() && !trustedNetworkHelper.allAllowed(); 0090 binding.trustedNetworkListEmpty.setVisibility(isVisible ? View.VISIBLE : View.GONE ); 0091 } 0092 0093 private void updateTrustedNetworkListView() { 0094 boolean allAllowed = trustedNetworkHelper.allAllowed(); 0095 updateEmptyListMessage(); 0096 trustedNetworksView.setVisibility(allAllowed ? View.GONE : View.VISIBLE); 0097 if (allAllowed){ 0098 return; 0099 } 0100 trustedNetworksView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, trustedNetworks)); 0101 trustedNetworksView.setOnItemClickListener((parent, view, position, id) -> { 0102 String targetItem = trustedNetworks.get(position); 0103 new AlertDialog.Builder(TrustedNetworksActivity.this) 0104 .setMessage("Delete " + targetItem + " ?") 0105 .setPositiveButton("Yes", (dialog, which) -> { 0106 trustedNetworks.remove(position); 0107 trustedNetworkHelper.update(trustedNetworks); 0108 ((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged(); 0109 addNetworkButton(); 0110 updateEmptyListMessage(); 0111 }) 0112 .setNegativeButton("No", null) 0113 .show(); 0114 0115 }); 0116 addNetworkButton(); 0117 } 0118 0119 private void addNetworkButton() { 0120 Button addButton = binding.button1; 0121 if (trustedNetworkHelper.allAllowed()) { 0122 addButton.setVisibility(View.GONE); 0123 return; 0124 } 0125 final String currentSSID = trustedNetworkHelper.currentSSID(); 0126 if (!currentSSID.isEmpty() && !trustedNetworks.contains(currentSSID)) { 0127 String buttonText = getString(R.string.add_trusted_network, currentSSID); 0128 addButton.setText(buttonText); 0129 addButton.setOnClickListener(v -> { 0130 if (trustedNetworks.contains(currentSSID)){ 0131 return; 0132 } 0133 trustedNetworks.add(currentSSID); 0134 trustedNetworkHelper.update(trustedNetworks); 0135 ((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged(); 0136 v.setVisibility(View.GONE); 0137 updateEmptyListMessage(); 0138 }); 0139 addButton.setVisibility(View.VISIBLE); 0140 } else { 0141 addButton.setVisibility(View.GONE); 0142 } 0143 } 0144 0145 @Override 0146 public boolean onSupportNavigateUp() { 0147 super.onBackPressed(); 0148 return true; 0149 } 0150 0151 }