2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 21:55:10 +00:00

T8539 - Create a new flow to manage trusted networks

Summary: I have created a new activity that allows you to trust all networks or add networks one by one to a list when you are connected to that network

Test Plan:
Test Cases:
1. By default everything should works like it works before, If you uncheck the "Allow all" option in trusted network menu and there isn't any ssid in the trusted
network list the app doesn't send the first udp package and log "Current WiFi isn't a Trusted Network"
2. With the "Allow all" uncheck, use "Add: YOUR_SSID_NAME" button to add your current ssid to the trusted networks list, once you added, go back and the app should
works as always

{F6152314}

1 -> https://youtu.be/ZpCEFTstbJI

2 -> https://youtu.be/cVB1LXlFVyk

Reviewers: #kde_connect, jdvr

Subscribers: sredman, albertvaka, nicolasfella, apol, kdeconnect

Tags: #kde_connect

Maniphest Tasks: T8539

Differential Revision: https://phabricator.kde.org/D13505
This commit is contained in:
Juan David Vega
2019-10-27 20:40:17 +01:00
committed by Albert Vaca Cintora
parent 1d5c280401
commit 7f8f1bee30
8 changed files with 309 additions and 1 deletions

View File

@@ -15,6 +15,7 @@
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- <uses-permission android:name="android.permission.BLUETOOTH" /> -->
@@ -28,11 +29,12 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:icon="@drawable/icon"
@@ -267,8 +269,19 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="org.kde.kdeconnect.UserInterface.PluginSettingsActivity" />
</activity>
<activity android:name="org.kde.kdeconnect.Plugins.PhotoPlugin.PhotoActivity" />
<activity
android:name="org.kde.kdeconnect.UserInterface.TrustedNetworksActivity"
android:label="@string/trusted_networks"
android:parentActivityName="org.kde.kdeconnect.UserInterface.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="org.kde.kdeconnect.UserInterface.MainActivity" />
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,38 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<CheckBox
android:text="@string/allow_all_networks_text"
android:layout_width="match_parent"
android:checked="true"
android:layout_height="wrap_content"
android:id="@+id/trust_all_networks_checkBox"/>
<TextView
android:id="@+id/trusted_network_list_empty"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="@string/empty_trusted_networks_list_text"
android:gravity="center" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@android:id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>

View File

@@ -14,4 +14,10 @@
android:title="@string/custom_device_list"
kdeconnect:showAsAction="never" />
<item
android:id="@+id/menu_trusted_networks"
android:orderInCategory="900"
android:title="@string/trusted_networks"
kdeconnect:showAsAction="never" />
</menu>

View File

@@ -337,8 +337,14 @@
<string name="plugin_photo_desc">Launch the camera app to ease taking and transferring pictures</string>
<string name="findmyphone_preference_key_ringtone" translatable="false">findmyphone_ringtone</string>
<string name="no_app_for_opening">No suitable app found to open this file</string>
<string name="remote_keyboard_service">KDE Connect Remote Keyboard</string>
<string name="presenter_pointer">Pointer</string>
<string name="trusted_networks">Trusted networks</string>
<string name="add_trusted_network">Add %1$s</string>
<string name="empty_trusted_networks_list_text">You haven\'t added any trusted network yet</string>
<string name="allow_all_networks_text">Allow all</string>
</resources>

View File

@@ -33,6 +33,7 @@ import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.Helpers.DeviceHelper;
import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper;
import org.kde.kdeconnect.Helpers.StringsHelper;
import org.kde.kdeconnect.Helpers.TrustedNetworkHelper;
import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.UserInterface.CustomDevicesActivity;
@@ -117,6 +118,11 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
//I've received their broadcast and should connect to their TCP socket and send my identity.
private void udpPacketReceived(DatagramPacket packet) {
if (TrustedNetworkHelper.isNotTrustedNetwork(context)) {
Log.w("LanLinkProvider", "Current WiFi isn't a Trusted Network");
return;
}
final InetAddress address = packet.getAddress();
try {
@@ -364,6 +370,11 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
}
lastBroadcast = System.currentTimeMillis();
if (TrustedNetworkHelper.isNotTrustedNetwork(context)) {
Log.w("LanLinkProvider", "Current WiFi isn't a Trusted Network");
return;
}
new Thread(() -> {
ArrayList<String> iplist = CustomDevicesActivity
.getCustomDeviceList(PreferenceManager.getDefaultSharedPreferences(context));

View File

@@ -0,0 +1,82 @@
package org.kde.kdeconnect.Helpers;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
public class TrustedNetworkHelper {
private static final String KEY_CUSTOM_TRUSTED_NETWORKS = "trusted_network_preference";
private static final String KEY_CUSTOM_TRUST_ALL_NETWORKS = "trust_all_network_preference";
private static final String NETWORK_SSID_DELIMITER = "#_#";
private static final String NOT_AVAILABLE_SSID_RESULT = "<unknown ssid>";
private final Context context;
public TrustedNetworkHelper(Context context) {
this.context = context;
}
public List<String> read() {
String serializeTrustedNetwork = PreferenceManager.getDefaultSharedPreferences(context).getString(
KEY_CUSTOM_TRUSTED_NETWORKS, "");
if (serializeTrustedNetwork.isEmpty())
return Collections.emptyList();
return Arrays.asList(serializeTrustedNetwork.split(NETWORK_SSID_DELIMITER));
}
public void update(List<String> trustedNetworks) {
String serialized = TextUtils.join(NETWORK_SSID_DELIMITER, trustedNetworks);
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(
KEY_CUSTOM_TRUSTED_NETWORKS, serialized).apply();
}
public Boolean allAllowed() {
return PreferenceManager
.getDefaultSharedPreferences(context)
.getBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, Boolean.TRUE);
}
public void allAllowed(boolean isChecked) {
PreferenceManager
.getDefaultSharedPreferences(context)
.edit()
.putBoolean(KEY_CUSTOM_TRUST_ALL_NETWORKS, isChecked)
.apply();
}
public String currentSSID() {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Log.d("Fou", "get");
if (wifiManager == null) return "";
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() != SupplicantState.COMPLETED) {
Log.d("Fou", "fooo");
return "";
}
String ssid = wifiInfo.getSSID();
if (ssid.equalsIgnoreCase(NOT_AVAILABLE_SSID_RESULT)){
Log.d("Fou", "navail");
return "";
}
Log.d("Fou", "retn");
return ssid;
}
public static boolean isNotTrustedNetwork(Context context) {
TrustedNetworkHelper trustedNetworkHelper = new TrustedNetworkHelper(context);
if (trustedNetworkHelper.allAllowed()){
return false;
}
return trustedNetworkHelper.read().indexOf(trustedNetworkHelper.currentSSID()) == -1;
}
}

View File

@@ -299,6 +299,10 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb
case R.id.menu_custom_device_list:
startActivity(new Intent(mActivity, CustomDevicesActivity.class));
break;
case R.id.menu_trusted_networks:
startActivity(new Intent(mActivity, TrustedNetworksActivity.class));
break;
default:
break;
}

View File

@@ -0,0 +1,148 @@
package org.kde.kdeconnect.UserInterface;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import java.util.ArrayList;
import java.util.List;
import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.Helpers.TrustedNetworkHelper;
import org.kde.kdeconnect_tp.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class TrustedNetworksActivity extends AppCompatActivity {
private ListView trustedNetworksView;
private List<String> trustedNetworks;
private boolean dialogAlreadyShown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
TrustedNetworkHelper trustedNetworkHelper = new TrustedNetworkHelper(getApplicationContext());
super.onCreate(savedInstanceState);
trustedNetworks = new ArrayList<>(trustedNetworkHelper.read());
ThemeUtil.setUserPreferredTheme(this);
setContentView(R.layout.trusted_network_list);
trustedNetworksView = findViewById(android.R.id.list);
emptyListMessage(trustedNetworkHelper);
trustedNetworkListView(trustedNetworkHelper);
CheckBox allowAllCheckBox = findViewById(R.id.trust_all_networks_checkBox);
allowAllCheckBox.setChecked(trustedNetworkHelper.allAllowed());
allowAllCheckBox.setOnCheckedChangeListener((v, isChecked) -> {
trustedNetworkHelper.allAllowed(isChecked);
trustedNetworkListView(trustedNetworkHelper);
});
}
private void emptyListMessage(TrustedNetworkHelper trustedNetworkHelper) {
boolean isVisible = trustedNetworks.isEmpty() && !trustedNetworkHelper.allAllowed();
findViewById(R.id.trusted_network_list_empty)
.setVisibility(isVisible ? VISIBLE : GONE );
}
private void trustedNetworkListView(TrustedNetworkHelper trustedNetworkHelper) {
Boolean allAllowed = trustedNetworkHelper.allAllowed();
emptyListMessage(trustedNetworkHelper);
// trustedNetworksView.setVisibility(allAllowed ? GONE : VISIBLE);
trustedNetworksView.setVisibility(allAllowed ? VISIBLE : VISIBLE);
// if (allAllowed){
// return;
// }
trustedNetworksView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, trustedNetworks));
trustedNetworksView.setOnItemClickListener(onItemClickGenerator(trustedNetworkHelper));
addNetworkButton(trustedNetworkHelper);
}
@NonNull
private AdapterView.OnItemClickListener onItemClickGenerator(TrustedNetworkHelper trustedNetworkHelper) {
return (parent, view, position, id) -> {
if (dialogAlreadyShown) {
return;
}
String targetItem = trustedNetworks.get(position);
// remove touched item after confirmation
DialogInterface.OnClickListener confirmationListener = (dialog, which) -> {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
trustedNetworks.remove(position);
trustedNetworkHelper.update(trustedNetworks);
((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged();
addNetworkButton(trustedNetworkHelper);
emptyListMessage(trustedNetworkHelper);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete " + targetItem + " ?");
builder.setPositiveButton("Yes", confirmationListener);
builder.setNegativeButton("No", confirmationListener);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { //DismissListener
dialogAlreadyShown = true;
builder.setOnDismissListener(dialog -> dialogAlreadyShown = false);
}
builder.show();
};
}
private void addNetworkButton(TrustedNetworkHelper trustedNetworkHelper) {
String currentSSID = trustedNetworkHelper.currentSSID();
if (!currentSSID.isEmpty() && trustedNetworks.indexOf(currentSSID) == -1) {
Button addButton = (Button) findViewById(android.R.id.button1);
String buttonText = getString(R.string.add_trusted_network, currentSSID);
addButton.setText(buttonText);
addButton.setOnClickListener(saveCurrentSSIDAsTrustedNetwork(currentSSID, trustedNetworkHelper));
addButton.setVisibility(VISIBLE);
}
}
@NonNull
private View.OnClickListener saveCurrentSSIDAsTrustedNetwork(String ssid, TrustedNetworkHelper trustedNetworkHelper) {
return v -> {
if (trustedNetworks.indexOf(ssid) != -1){
return;
}
trustedNetworks.add(ssid);
trustedNetworkHelper.update(trustedNetworks);
((ArrayAdapter) trustedNetworksView.getAdapter()).notifyDataSetChanged();
v.setVisibility(GONE);
};
}
@Override
protected void onStart() {
super.onStart();
BackgroundService.addGuiInUseCounter(this);
}
@Override
protected void onStop() {
super.onStop();
BackgroundService.removeGuiInUseCounter(this);
}
}