Skip to content

Commit

Permalink
Refactor Backend.print to automatically put newline, other minor twea…
Browse files Browse the repository at this point in the history
…ks and fixes
  • Loading branch information
petabyt committed Oct 5, 2023
1 parent 867e43f commit fb18178
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 129 deletions.
22 changes: 14 additions & 8 deletions app/src/main/java/dev/danielc/fujiapp/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public static void reportError(int code, String reason) {
if (wifi.killSwitch == false) {
wifi.killSwitch = true;

print("Safely killed connection: " + code + "\n");
if (reason != null) {
print("Reason: " + reason + "\n");
print("Disconnect: " + reason);
}

Backend.wifi.close();
Expand Down Expand Up @@ -66,15 +65,20 @@ public static void clear() {
public native synchronized static int cFujiConfigInitMode();
public native synchronized static String cPtpRun(String req);
public native synchronized static byte[] cPtpGetThumb(int handle);
public native synchronized static byte[] cFujiGetFile(int handle);
public native synchronized static boolean cIsMultipleMode();
public native synchronized static boolean cIsUntestedMode();
public native synchronized static boolean cCameraWantsRemote();
public native synchronized static int[] cGetObjectHandles();
public native synchronized static int cFujiTestStartRemoteSockets();
public native synchronized static int cFujiEndRemoteMode();
public native synchronized static int cFujiConfigImageGallery();

// For tester only
public native synchronized static int cFujiTestStartRemoteSockets();

// Must be called in order - first one enables compression, second disables compression
// This is a cheap fix for now, will be fixed in the next refactoring
public native synchronized static String cFujiGetUncompressedObjectInfo(int handle);
public native synchronized static byte[] cFujiGetFile(int handle);

// For test suite only
public native synchronized static void cTesterInit(Tester t);
Expand Down Expand Up @@ -105,7 +109,7 @@ public static JSONObject run(String req, int[] arr) throws Exception {
try {
JSONObject jsonObject = new JSONObject(resp);
if (jsonObject.getInt("error") != 0) {
Backend.print("Non zero error: " + Integer.toString(jsonObject.getInt("error")) + "\n");
Backend.print("Non zero error: " + Integer.toString(jsonObject.getInt("error")));
throw new Exception("Error code");
}

Expand All @@ -129,8 +133,9 @@ public static JSONObject fujiGetUncompressedObjectInfo(int handle) throws Except
}
}

// JNI -> UI log communication
// C/Java -> async UI logging
public static String logLocation = "main";
final static int MAX_LOG_LINES = 5;

public static void clearPrint() {
basicLog = "";
Expand All @@ -141,10 +146,11 @@ public static void clearPrint() {
private static String basicLog = "";
public static void print(String arg) {
Log.d("fudge", arg);
basicLog += arg;

basicLog += arg + "\n";

String[] lines = basicLog.split("\n");
if (lines.length > 5) {
if (lines.length > MAX_LOG_LINES) {
basicLog = String.join("\n", Arrays.copyOfRange(lines, 1, lines.length)) + "\n";
}

Expand Down
51 changes: 22 additions & 29 deletions app/src/main/java/dev/danielc/fujiapp/Gallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
public class Gallery extends AppCompatActivity {
private static Gallery instance;

final int GRID_SIZE = 4;

public static Gallery getInstance() {
return instance;
}
Expand Down Expand Up @@ -71,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
ConnectivityManager m = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

recyclerView = findViewById(R.id.galleryView);
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
recyclerView.setLayoutManager(new GridLayoutManager(this, GRID_SIZE));

handler = new Handler(Looper.getMainLooper());

Expand All @@ -80,14 +82,13 @@ protected void onCreate(Bundle savedInstanceState) {
public void run() {
int rc;
if (Backend.cPtpFujiInit() == 0) {
Backend.print("Initialized connection.\n");
Backend.print("Initialized connection.");
} else {
Backend.print("Failed to init socket\n");
Backend.reportError(Backend.PTP_IO_ERR, "Graceful disconnect\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to init socket");
return;
}

// Sleep because Fuji camera is sensitive
// Fuji cameras require delay after init
try {
Thread.sleep(500);
} catch (Exception e) {
Expand All @@ -97,24 +98,21 @@ public void run() {
try {
Camera.openSession();
} catch (Exception e) {
Backend.print("Failed to open session.\n");
Backend.reportError(Backend.PTP_IO_ERR, "Graceful disconnect\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to open session.");
return;
}

Backend.print("Waiting for device access...\n");
Backend.print("Waiting for device access...");
if (Backend.cPtpFujiWaitUnlocked() == 0) {
Backend.print("Gained access to device.\n");
Backend.print("Gained access to device.");
} else {
Backend.print("Failed to gain access to device.\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to gain access\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to gain access to device.");
return;
}

// Camera mode must be set before anything else
if (Backend.cFujiConfigInitMode() != 0) {
Backend.print("Failed to configure mode with the camera.\n");
Backend.reportError(Backend.PTP_IO_ERR, "Graceful disconnect\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to configure mode with the camera.");
return;
}

Expand All @@ -124,50 +122,45 @@ public void run() {
showWarning("This camera is untested, support is under development.");
}

Backend.print("Configuring versions and stuff..\n");
Backend.print("Configuring versions and stuff..");
rc = Backend.cFujiConfigVersion();
if (rc != 0) {
Backend.print("Failed to configure camera versions.\n");
Backend.reportError(rc, "Graceful disconnect\n");
Backend.reportError(rc, "Failed to configure camera versions.");
return;
}

// Enter (and exit?) remote mode
// Enter and 'exit' remote mode
if (Backend.cCameraWantsRemote()) {
Backend.print("Entering remote mode..");
rc = Backend.cFujiTestStartRemoteSockets();
if (rc != 0) {
Backend.print("Failed to init remote mode\n");
Backend.reportError(rc, "Graceful disconnect\n");
Backend.reportError(rc, "Failed to init remote mode");
return;
}

if (Backend.wifi.fujiConnectEventAndVideo(m)) {
Backend.print("Failed to enter remote mode\n");
Backend.reportError(Backend.PTP_RUNTIME_ERR, "Graceful disconnect\n");
Backend.reportError(Backend.PTP_RUNTIME_ERR, "Failed to enter remote mode");
return;
}

rc = Backend.cFujiEndRemoteMode();
if (rc != 0) {
Backend.print("Failed to exit remote mode\n");
Backend.reportError(rc, "Graceful disconnect\n");
Backend.reportError(rc, "Failed to exit remote mode");
return;
}
}

Backend.print("Entering image gallery..\n");
Backend.print("Entering image gallery..");
rc = Backend.cFujiConfigImageGallery();
if (rc != 0) {
Backend.print("Failed to start image gallery\n");
Backend.reportError(rc, "Graceful disconnect\n");
Backend.reportError(rc, "Failed to start image gallery");
return;
}

int[] objectHandles = Backend.cGetObjectHandles();

if (objectHandles == null) {
Backend.print("No JPEG images available.\n");
Backend.print("No JPEG images available.");
} else {
handler.post(new Runnable() {
@Override
Expand Down Expand Up @@ -197,7 +190,7 @@ public void run() {

Intent intent = new Intent(Gallery.this, MainActivity.class);
startActivity(intent);
Backend.reportError(Backend.PTP_IO_ERR, "Failed to ping camera, disconnected\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to ping camera, disconnected");
}
});
return;
Expand All @@ -218,7 +211,7 @@ public void onBackPressed() {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Backend.reportError(Backend.PTP_OK, "Graceful disconnect\n");
Backend.reportError(Backend.PTP_OK, "Graceful disconnect");
finish();
return true;
}
Expand Down
14 changes: 5 additions & 9 deletions app/src/main/java/dev/danielc/fujiapp/ImageAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void onClick(View v) {
// to prevent re-downloading (slow)
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Log.d("adapt", "Creation image");
holder.image.setImageResource(0);
int adapterPosition = holder.getAdapterPosition();
if (adapterPosition >= object_ids.length) return;
Expand All @@ -58,16 +57,15 @@ public void run() {
int id = object_ids[adapterPosition];
byte[] jpegByteArray = Backend.cPtpGetThumb(id);
if (jpegByteArray == null) {
Backend.reportError(Backend.PTP_IO_ERR, "Failed to get image thumbnail, stopping connection\n");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to get image thumbnail, stopping connection");
return;
} else if (jpegByteArray.length == 0) {
// Unable to find thumbnail - assume it's a folder or non-jpeg
holder.itemView.setOnClickListener(null);
Backend.print("Failed to get image #" + id + "\n");
// TODO: reset the image to unknown or default
// Maybe run getobjinfo to see what it is
Backend.print("Failed to get thumbnail #" + id);
return;
}

try {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = true;
Expand All @@ -76,7 +74,7 @@ public void run() {

Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length, opt);
if (bitmap == null) {
Backend.print("Image decode error\n");
Backend.print("Image decode error");
return;
}
holder.itemView.post(new Runnable() {
Expand All @@ -89,7 +87,7 @@ public void run() {
}
});
} catch (OutOfMemoryError e) {
Backend.print("Out of memory\n");
Backend.reportError(Backend.PTP_RUNTIME_ERR, "Out of memory");
return;
}
}
Expand All @@ -112,8 +110,6 @@ public static class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public int handle;

// public void setSource() {}

public ImageViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageView);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/dev/danielc/fujiapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public void connectClick(View v) {
@Override
public void run() {
if (Backend.wifi.fujiConnectToCmd((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE))) {
Backend.print(MyWiFiComm.failReason + "\n");
Backend.print(MyWiFiComm.failReason);
} else {
Backend.print("Connected to the camera\n");
Backend.print("Connected to the camera");
Backend.logLocation = "gallery";
Intent intent = new Intent(MainActivity.this, Gallery.class);
startActivity(intent);
Expand Down
44 changes: 7 additions & 37 deletions app/src/main/java/dev/danielc/fujiapp/MyWiFiComm.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import dev.petabyt.camlib.*;

public class MyWiFiComm extends WiFiComm {
public final String TAG = "MyWiFiComm";
private Socket cmdSocket = null;
private InputStream cmdInputStream = null;
private OutputStream cmdOutputStream = null;
Expand Down Expand Up @@ -45,13 +46,13 @@ public boolean fujiConnectToCmd(ConnectivityManager m) {
public boolean fujiConnectEventAndVideo(ConnectivityManager m) {
eventSocket = connectWiFiSocket(m, Backend.FUJI_IP, Backend.FUJI_EVENT_PORT);
if (eventSocket == null) {
Backend.print("Failed to connect to event socket: \n" + failReason);
Backend.print("Failed to connect to event socket: " + failReason);
return true;
}

videoSocket = connectWiFiSocket(m, Backend.FUJI_IP, Backend.FUJI_VIDEO_PORT);
if (videoSocket == null) {
Backend.print("Failed to connect to video socket: \n" + failReason);
Backend.print("Failed to connect to video socket: " + failReason);
return true;
}

Expand All @@ -68,7 +69,7 @@ public int cmdWrite(byte[] data) {
cmdOutputStream.flush();
return data.length;
} catch (IOException e) {
Backend.print("Error writing to the server: " + e.getMessage() + "\n");
Backend.print("Error writing to the server: " + e.getMessage());
return -1;
}
}
Expand All @@ -94,7 +95,7 @@ public void run() {
}
});
} catch (IOException e) {
Backend.print("Error reading " + length + " bytes: " + e.getMessage() + "\n");
Backend.print("Error reading " + length + " bytes: " + e.getMessage());
return -1;
}
}
Expand All @@ -107,40 +108,11 @@ public int generic_write(byte data[]) {
cmdOutputStream.flush();
return data.length;
} catch (IOException e) {
Backend.print("Error writing to the server: " + e.getMessage() + "\n");
Backend.print("Error writing to the server: " + e.getMessage());
return -1;
}
}

// public int genericWrite(OutputStream output, byte[] data) {
// if (killSwitch) return -1;
// try {
// output.write(data);
// output.flush();
// return data.length;
// } catch (IOException e) {
// Backend.print("Error writing to the server: " + e.getMessage() + "\n");
// return -1;
// }
// }
//
// public int genericRead(InputStream input, byte[] buffer, int length) {
// int read = 0;
// while (true) {
// try {
// if (killSwitch) return -1;
//
// int rc = input.read(buffer, read, length - read);
// if (rc == -1) return rc;
// read += rc;
// if (read == length) return read;
// } catch (IOException e) {
// Backend.print("Error reading " + length + " bytes: " + e.getMessage() + "\n");
// return -1;
// }
// }
// }

public synchronized void close() {
killSwitch = true;

Expand All @@ -153,8 +125,6 @@ public synchronized void close() {
}

try {
Log.e("fudge", "Closing sockets");

if (cmdSocket != null) {
cmdSocket.close();
}
Expand All @@ -165,7 +135,7 @@ public synchronized void close() {
cmdOutputStream.close();
}
} catch (IOException e) {
Backend.print("Error closing the socket: " + e.getMessage() + "\n");
Backend.print("Error closing the socket: " + e.getMessage());
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/dev/danielc/fujiapp/Tester.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void connectBluetooth() {
bt.getConnectedDevice();
}

// Deprecated WiFi/socket tester, for testing only
Socket testSock = null;
void socketTest(ConnectivityManager m) {
try {
Expand All @@ -62,7 +63,7 @@ void socketTest(ConnectivityManager m) {
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.e("sad", "Wifi available");
Log.e("tester", "Wifi available");
ConnectivityManager.setProcessDefaultNetwork(network);

try {
Expand Down
Loading

0 comments on commit fb18178

Please sign in to comment.