Skip to content

Commit

Permalink
Major refactor - out with MyWiFiComm, use camlib.SimpleSocket instead
Browse files Browse the repository at this point in the history
Also - sneaky - I added libui-android - eventually I will be able to start writing new parts of the
app in C rather than fooling around with Android's crap
  • Loading branch information
petabyt committed Jan 1, 2024
1 parent 96d77d7 commit 33eb8ac
Show file tree
Hide file tree
Showing 36 changed files with 5,918 additions and 584 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
PKG=dev.danielc.fujiapp

install:
bash gradlew :app:buildCMakeDebug[arm64-v8a] installDebug -Pandroid.optional.compilation=INSTANT_DEV -Pandroid.injected.build.api=24
#bash gradlew installDebug -Pandroid.optional.compilation=INSTANT_DEV -Pandroid.injected.build.api=24
#bash gradlew :app:buildCMakeDebug[arm64-v8a] installDebug -Pandroid.optional.compilation=INSTANT_DEV -Pandroid.injected.build.api=24
bash gradlew installDebug -Pandroid.optional.compilation=INSTANT_DEV -Pandroid.injected.build.api=24
adb shell monkey -p $(PKG) -c android.intent.category.LAUNCHER 1

log:
adb logcat | grep -F "`adb shell ps | grep $(PKG) | tr -s [:space:] ' ' | cut -d' ' -f2`"

ln:
rm -f app/src/main/java/camlib/*.java
cd app/src/main/java/camlib/ && ln ../../../../../../camlibjava/*.java .

rm -f app/src/main/java/libui/*.java
cd app/src/main/java/libui/ && ln ../../../../../../libui-android/*.java .

cd lib && ln ../../libui-android/*.c ../../libui-android/*.h .
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@
android:exported="false" />
<activity
android:name=".Viewer"
android:screenOrientation="portrait"
android:exported="false" />
<activity
android:name=".Tester"
android:screenOrientation="portrait"
android:exported="false" />
<activity
android:name=".Gallery"
android:screenOrientation="portrait"
android:exported="false" />
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -42,4 +46,4 @@
</activity>
</application>

</manifest>
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Basic backend parent class for camlib with JNI
// Copyright Daniel Cook - Apache License
package dev.petabyt.camlib;
package camlib;

public class CamlibBackend {
// Integer error exception - see camlib PTP_ error codes
Expand Down Expand Up @@ -34,4 +34,4 @@ public PtpErr(int code) {
public static final int PTP_LV_EOS = 1;
public static final int PTP_LV_CANON = 2;
public static final int PTP_LV_ML = 3;
};
};
110 changes: 110 additions & 0 deletions app/src/main/java/camlib/SimpleSocket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package camlib;

import android.net.ConnectivityManager;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import dev.danielc.fujiapp.Backend;

public class SimpleSocket {
private static ConnectivityManager m = null;
public static void setConnectivityManager(ConnectivityManager m) {
SimpleSocket.m = m;
}

public int port;
public String ip;
public boolean alive;

int timeout = 2000;

Socket socket;
InputStream inputStream;
OutputStream outputStream;

public String failReason;

private byte[] buffer = null;
private int bufferSize = 512;

public SimpleSocket() {
this.buffer = new byte[bufferSize];
}

public Object getBuffer() {
return this.buffer;
}

public int getBufferSize() {
return this.bufferSize;
}

public void connectWiFi(String ip, int port) throws Exception {
Socket s;
try {
s = WiFiComm.connectWiFiSocket(m, ip, port);
} catch (Exception e) {
throw e;
}

this.ip = ip;
this.port = port;

s.setSoTimeout(timeout);
this.inputStream = s.getInputStream();
this.outputStream = s.getOutputStream();

alive = true;
}

public int read(int size) {
try {
return inputStream.read(buffer, 0, size);
} catch (IOException e) {
failReason = e.toString();
alive = false;
return -1;
}
}

public int write(int size) {
try {
outputStream.write(buffer, 0, size);
outputStream.flush();
return size;
} catch (IOException e) {
failReason = e.toString();
alive = false;
return -1;
}
}

public void close() {
alive = false;
try {
// Suck the remaining bytes out of socket
byte[] remaining = new byte[100];
inputStream.read(remaining);
} catch (Exception e) {
// I don't care
}

try {
if (socket != null) {
socket.close();
}
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
Backend.print("Socket close error: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Basic libusb-like driver intented for PTP devices
// Copyright Daniel Cook - Apache License
package dev.petabyt.camlib;
package camlib;

import android.app.PendingIntent;
import android.content.Context;
Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/camlib/WiFiComm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Basic wifi-priority socket interface for camlib
// Copyright Daniel Cook - Apache License
package camlib;

import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.util.Log;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;

import dev.danielc.fujiapp.Backend;
import libui.LibUI;

public class WiFiComm {
public static final String TAG = "camlib";

public boolean killSwitch = true;

static Network wifiDevice = null;

static Socket tryConnectToSocket(Network net, String ip, int port) throws Exception {
Socket sock;
try {
// Create and connect to socket
sock = new Socket();

// Bind socket to the network device we selected
net.bindSocket(sock);

//sock.setKeepAlive(true);
sock.setTcpNoDelay(true);
sock.setReuseAddress(true);

sock.connect(new InetSocketAddress(ip, port), 1000);
} catch (SocketTimeoutException e) {
Log.d(TAG, e.toString());
throw new Exception("Connection timed out");
} catch (Exception e) {
Log.d(TAG, e.toString());
throw new Exception("Failed to connect.");
}

return sock;
}

public static void startNetworkListeners(ConnectivityManager connectivityManager) {
NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.d(TAG, "Wifi network is available");
wifiDevice = network;
}
@Override
public void onLost(Network network) {
Log.e(TAG, "Lost network\n");
wifiDevice = null;
}
@Override
public void onUnavailable() {
Log.e(TAG, "Network unavailable\n");
wifiDevice = null;
}
};

connectivityManager.requestNetwork(requestBuilder.build(), networkCallback);
}

public static Socket connectWiFiSocket(ConnectivityManager connectivityManager, String ip, int port) throws Exception {
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!wifiInfo.isAvailable()) {
throw new Exception("WiFi is not available.");
} else if (!wifiInfo.isConnected()) {
throw new Exception("Not connected to a WiFi network.");
}

if (wifiDevice == null) {
throw new Exception("Not connected to WiFi.");
}

return tryConnectToSocket(wifiDevice, ip, port);
}
}
Loading

0 comments on commit 33eb8ac

Please sign in to comment.