diff --git a/app/src/main/java/org/briarproject/hotspot/InterfacesFragment.java b/app/src/main/java/org/briarproject/hotspot/InterfacesFragment.java new file mode 100644 index 0000000000000000000000000000000000000000..9a5d06df791760022b43b92ba13b650b2e44aa05 --- /dev/null +++ b/app/src/main/java/org/briarproject/hotspot/InterfacesFragment.java @@ -0,0 +1,28 @@ +package org.briarproject.hotspot; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + +public class InterfacesFragment extends Fragment { + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_interfaces, container, false); + } + + @Override + public void onViewCreated(@NonNull View v, @Nullable Bundle savedInstanceState) { + super.onViewCreated(v, savedInstanceState); + TextView textView = (TextView) v; + textView.setText(NetworkUtils.getNetworkInterfaceSummary()); + } + +} diff --git a/app/src/main/java/org/briarproject/hotspot/NetworkUtils.java b/app/src/main/java/org/briarproject/hotspot/NetworkUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..cadda401df7a364481456825022b1629c1425cac --- /dev/null +++ b/app/src/main/java/org/briarproject/hotspot/NetworkUtils.java @@ -0,0 +1,55 @@ +package org.briarproject.hotspot; + + +import androidx.annotation.Nullable; + +import java.net.InetAddress; +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.List; + +import static java.util.Collections.emptyList; +import static java.util.Collections.list; + +class NetworkUtils { + + @Nullable + static InetAddress getAccessPointAddress() { + for (NetworkInterface i: getNetworkInterfaces()) { + if (i.getName().startsWith("p2p")) { + for (InterfaceAddress a : i.getInterfaceAddresses()) { + if (a.getAddress().getAddress().length == 4) return a.getAddress(); + } + } + } + return null; + } + + static String getNetworkInterfaceSummary() { + StringBuilder sb = new StringBuilder(); + for (NetworkInterface i: getNetworkInterfaces()) { + sb.append(i.getName()).append(":"); + for (InterfaceAddress a : i.getInterfaceAddresses()) { + if (a.getAddress().getAddress().length <= 4) { + sb.append(" ").append(a.getAddress()); + } + } + sb.append("\n"); + } + return sb.toString(); + } + + static List<NetworkInterface> getNetworkInterfaces() { + try { + Enumeration<NetworkInterface> ifaces = + NetworkInterface.getNetworkInterfaces(); + return ifaces == null ? emptyList() : list(ifaces); + } catch (SocketException e) { + e.printStackTrace(); + return emptyList(); + } + } + +} diff --git a/app/src/main/java/org/briarproject/hotspot/ServerFragment.java b/app/src/main/java/org/briarproject/hotspot/ServerFragment.java index ed21b53fecd2da77a41a50a5c3f11020602fdf85..bb7bbd3bca10b1d9be2e6cf764bb2f3dce73324f 100644 --- a/app/src/main/java/org/briarproject/hotspot/ServerFragment.java +++ b/app/src/main/java/org/briarproject/hotspot/ServerFragment.java @@ -2,7 +2,11 @@ package org.briarproject.hotspot; import android.graphics.Bitmap; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -11,22 +15,22 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; -import androidx.lifecycle.ViewModelProvider; + +import java.net.InetAddress; import static android.view.View.GONE; import static android.view.View.VISIBLE; +import static org.briarproject.hotspot.NetworkUtils.getAccessPointAddress; import static org.briarproject.hotspot.QrCodeUtils.createQrCode; public class ServerFragment extends Fragment { - private MainViewModel viewModel; - private ImageView qrCode; - private TextView urlView; + private static String TAG = ServerFragment.class.getName(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - viewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class); + setHasOptionsMenu(true); // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_server, container, false); } @@ -34,10 +38,17 @@ public class ServerFragment extends Fragment { @Override public void onViewCreated(@NonNull View v, @Nullable Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); - qrCode = v.findViewById(R.id.qr_code); - urlView = v.findViewById(R.id.url); + ImageView qrCode = v.findViewById(R.id.qr_code); + TextView urlView = v.findViewById(R.id.url); String text = "http://192.168.49.1:9999"; + InetAddress address = getAccessPointAddress(); + if (address == null) { + Log.i(TAG, "Could not find access point address, assuming 192.168.49.1"); + } else { + Log.i(TAG, "Access point address " + address.getHostAddress()); + text = "http://" + address.getHostAddress() + ":9999"; + } Bitmap qrCodeBitmap = createQrCode(getResources().getDisplayMetrics(), text); if (qrCodeBitmap == null) { @@ -49,4 +60,21 @@ public class ServerFragment extends Fragment { urlView.setText(text); } + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.main, menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.interfaces) { + getParentFragmentManager().beginTransaction() + .replace(R.id.fragment_container, new InterfacesFragment()) + .addToBackStack("INTERFACES") + .commit(); + return true; + } + return super.onOptionsItemSelected(item); + } + } diff --git a/app/src/main/res/layout/fragment_interfaces.xml b/app/src/main/res/layout/fragment_interfaces.xml new file mode 100644 index 0000000000000000000000000000000000000000..90c28c909b0554f1f850a52ba620bf7bc86f8055 --- /dev/null +++ b/app/src/main/res/layout/fragment_interfaces.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fontFamily="monospace" + android:padding="16dp" + tools:text="@tools:sample/lorem/random" /> diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml new file mode 100644 index 0000000000000000000000000000000000000000..36a48745fee5f7ac2aca1bf792f9df6413604cdf --- /dev/null +++ b/app/src/main/res/menu/main.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<menu xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto"> + <item + android:id="@+id/interfaces" + android:title="@string/menu_interfaces" + app:showAsAction="never" /> +</menu> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc9ff1a378bd86a94e95f4681e28efa050120cb6..469dcf44e9497765eab3d73a49ab16f667b22e6d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ <resources> <string name="app_name">Offline Hotspot</string> + <string name="menu_interfaces">Network interfaces</string> <string name="start_hotspot">Start hotspot</string> <string name="stop_hotspot">Stop hotspot</string> <string name="ssid">Name: %s</string>