diff --git a/res/drawable/ic_wifi.xml b/res/drawable/ic_wifi.xml new file mode 100644 index 00000000..82a48113 --- /dev/null +++ b/res/drawable/ic_wifi.xml @@ -0,0 +1,9 @@ + + + diff --git a/res/layout/activity_device.xml b/res/layout/activity_device.xml index eba7c95b..1850833c 100644 --- a/res/layout/activity_device.xml +++ b/res/layout/activity_device.xml @@ -99,15 +99,6 @@ android:text="@string/unreachable_description" android:textAppearance="?android:attr/textAppearanceMedium" android:visibility="gone" /> - - diff --git a/res/layout/pairing_explanation_text.xml b/res/layout/pairing_explanation_text.xml new file mode 100644 index 00000000..97f86d2a --- /dev/null +++ b/res/layout/pairing_explanation_text.xml @@ -0,0 +1,13 @@ + + + + diff --git a/res/layout/pairing_explanation_text_no_wifi.xml b/res/layout/pairing_explanation_text_no_wifi.xml new file mode 100644 index 00000000..adb7fb0a --- /dev/null +++ b/res/layout/pairing_explanation_text_no_wifi.xml @@ -0,0 +1,16 @@ + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index a02f12c3..ad0cfc15 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -256,7 +256,7 @@ Rename Refresh This paired device is not reachable. Make sure it is connected to your same network. - It looks like you are on a mobile data connection. KDE Connect only works on local networks. + You\'re not connected to a Wi-Fi network, so you may not be able to see any devices. Click here to enable Wi-Fi. There are no file browsers installed. Send SMS Send text messages from your desktop diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 83e6003a..0fa30287 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -31,7 +31,6 @@ import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.DeviceHelper; -import org.kde.kdeconnect.Helpers.NetworkHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; import org.kde.kdeconnect.Helpers.StringsHelper; import org.kde.kdeconnect.NetworkPacket; @@ -356,12 +355,6 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis } private void broadcastUdpPacket() { - - if (NetworkHelper.isOnMobileNetwork(context)) { - Log.w("LanLinkProvider", "On 3G network, not sending broadcast."); - return; - } - new Thread(() -> { ArrayList iplist = CustomDevicesActivity .getCustomDeviceList(PreferenceManager.getDefaultSharedPreferences(context)); diff --git a/src/org/kde/kdeconnect/Helpers/NetworkHelper.java b/src/org/kde/kdeconnect/Helpers/NetworkHelper.java deleted file mode 100644 index 7861ef20..00000000 --- a/src/org/kde/kdeconnect/Helpers/NetworkHelper.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.kde.kdeconnect.Helpers; - -import android.content.Context; - -public class NetworkHelper { - - public static boolean isOnMobileNetwork(Context context) { - - return false; - - /* - if (context == null) { - return false; - } - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - return false; //No good way to know it - } - try { - boolean mobile = false; - final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - Network[] networks = connMgr.getAllNetworks(); - for (Network network : networks) { - NetworkInfo info = connMgr.getNetworkInfo(network); - if (info == null) { - continue; - } - if (info.getType() == ConnectivityManager.TYPE_MOBILE) { - mobile = info.isConnected(); - continue; - } - //Log.e(info.getTypeName(),""+info.isAvailable()); - if (info.isAvailable()) - return false; //We are connected to at least one non-mobile network - } - if (mobile) { //We suspect we are on a mobile net - try (LineNumberReader is = new LineNumberReader(new FileReader("/proc/net/arp"))) { - //Check the number of network neighbours, on data it should be 0 - is.skip(Long.MAX_VALUE); - //Log.e("NetworkHelper", "procnetarp has " + is.getLineNumber() + " lines"); - if (is.getLineNumber() > 1) { //The first line are the headers - return false; //I have neighbours, so this doesn't look like a mobile network - } - } catch (Exception e) { - Log.e("NetworkHelper", "Exception reading procnetarp", e); - } - } - return mobile; - } catch (Exception e) { - Log.e("isOnMobileNetwork", "Something went wrong, but this is non-critical.", e); - } - return false; - - */ - } - -} diff --git a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java index 4fd1ea52..2f772b3b 100644 --- a/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/DeviceFragment.java @@ -41,20 +41,17 @@ import androidx.fragment.app.Fragment; import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; -import org.kde.kdeconnect.Helpers.NetworkHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.UserInterface.List.PluginListHeaderItem; import org.kde.kdeconnect.UserInterface.List.FailedPluginListItem; import org.kde.kdeconnect.UserInterface.List.ListAdapter; import org.kde.kdeconnect.UserInterface.List.PluginItem; -import org.kde.kdeconnect.UserInterface.List.SmallEntryItem; import org.kde.kdeconnect_tp.R; import java.util.ArrayList; import java.util.Collection; import java.util.ConcurrentModificationException; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import butterknife.BindView; @@ -90,7 +87,6 @@ public class DeviceFragment extends Fragment { @BindView(R.id.pair_request_buttons) View pairRequestButtons; @BindView(R.id.error_message_container) View errorMessageContainer; @BindView(R.id.not_reachable_message) TextView notReachableMessage; - @BindView(R.id.on_data_message) TextView onDataMessage; @BindView(R.id.buttons_list) ListView buttonsList; private Unbinder unbinder; @@ -310,12 +306,10 @@ public class DeviceFragment extends Fragment { boolean paired = device.isPaired(); boolean reachable = device.isReachable(); - boolean onData = NetworkHelper.isOnMobileNetwork(DeviceFragment.this.getContext()); pairingButtons.setVisibility(paired ? View.GONE : View.VISIBLE); errorMessageContainer.setVisibility((paired && !reachable) ? View.VISIBLE : View.GONE); - notReachableMessage.setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE); - onDataMessage.setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE); + notReachableMessage.setVisibility((paired && !reachable) ? View.VISIBLE : View.GONE); try { pluginListItems = new ArrayList<>(); diff --git a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java index 176045d1..8d911dc9 100644 --- a/src/org/kde/kdeconnect/UserInterface/PairingFragment.java +++ b/src/org/kde/kdeconnect/UserInterface/PairingFragment.java @@ -24,7 +24,14 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkInfo; +import android.net.NetworkRequest; +import android.os.Build; import android.os.Bundle; +import android.provider.Settings; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -36,7 +43,6 @@ import android.widget.TextView; import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.Device; -import org.kde.kdeconnect.Helpers.NetworkHelper; import org.kde.kdeconnect.UserInterface.List.ListAdapter; import org.kde.kdeconnect.UserInterface.List.PairingDeviceItem; import org.kde.kdeconnect.UserInterface.List.SectionItem; @@ -65,6 +71,8 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb private boolean listRefreshCalledThisFrame = false; private TextView headerText; + private TextView noWifiHeader; + private Object networkChangeListener; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @@ -83,14 +91,49 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb mSwipeRefreshLayout.setOnRefreshListener( this::updateComputerListAction ); - headerText = new TextView(inflater.getContext()); - headerText.setText(getString(R.string.pairing_description)); - headerText.setPadding(0, (int) (16 * getResources().getDisplayMetrics().density), 0, (int) (12 * getResources().getDisplayMetrics().density)); + headerText = (TextView) inflater.inflate(R.layout.pairing_explanation_text, null); + headerText.setOnClickListener(null); + headerText.setOnLongClickListener(null); + noWifiHeader = (TextView) inflater.inflate(R.layout.pairing_explanation_text_no_wifi, null); + noWifiHeader.setOnClickListener(view -> { + startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); + }); ((ListView) listRootView).addHeaderView(headerText); + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + networkChangeListener = new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(Network network) { + updateDeviceList(); + } + + @Override + public void onLost(Network network) { + updateDeviceList(); + } + + @Override + public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { + updateDeviceList(); + } + }; + ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + connManager.registerNetworkCallback(new NetworkRequest.Builder().build(), (ConnectivityManager.NetworkCallback) networkChangeListener); + } + return rootView; } + @Override + public void onDestroyView() { + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + connManager.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkChangeListener); + } + + super.onDestroyView(); + } + @Override public void onAttach(Context context) { super.onAttach(context); @@ -126,13 +169,27 @@ public class PairingFragment extends Fragment implements PairingDeviceItem.Callb } listRefreshCalledThisFrame = true; - headerText.setText(getString(NetworkHelper.isOnMobileNetwork(getContext()) ? R.string.on_data_message : R.string.pairing_description)); - //Disable tap animation - headerText.setOnClickListener(null); - headerText.setOnLongClickListener(null); + Collection devices = service.getDevices().values(); + boolean someDevicesReachable = false; + for (Device device : devices) { + if (device.isReachable()) { + someDevicesReachable = true; + } + } + + ((ListView) rootView.findViewById(R.id.devices_list)).removeHeaderView(headerText); + ((ListView) rootView.findViewById(R.id.devices_list)).removeHeaderView(noWifiHeader); + + ConnectivityManager connManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + //Check if we're on Wi-Fi. If we still see a device, don't do anything special + if (someDevicesReachable || wifi.isConnected()) { + ((ListView) rootView.findViewById(R.id.devices_list)).addHeaderView(headerText); + } else { + ((ListView) rootView.findViewById(R.id.devices_list)).addHeaderView(noWifiHeader); + } try { - Collection devices = service.getDevices().values(); final ArrayList items = new ArrayList<>(); SectionItem connectedSection;