-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor - out with MyWiFiComm, use camlib.SimpleSocket instead
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
Showing
36 changed files
with
5,918 additions
and
584 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...main/java/dev/petabyt/camlib/UsbComm.java → app/src/main/java/camlib/UsbComm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.