Skip to content

Commit

Permalink
Stable USB support
Browse files Browse the repository at this point in the history
- Fixed USB packet reading
- Session is closed on deliberate disconnect
- Fixed unreliable event/video socket opening, code moved to C now
Annoying blocking issue where connect returns 0 but sets errno = 115 (operation in progress)
It should be fine to just let it be since the camera detects the accept()
Later on, this should be fixed
- Improved thumbnail loading a little bit
- Scripts menu doesn't automatically grab focus
- Updated upstream stuff
  • Loading branch information
petabyt committed Mar 3, 2024
1 parent 1df62eb commit 0ccb4ee
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 262 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/Project.xml

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

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

5 changes: 2 additions & 3 deletions .idea/gradle.xml

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

10 changes: 10 additions & 0 deletions .idea/migrations.xml

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

2 changes: 1 addition & 1 deletion app/src/main/java/dev/danielc/fujiapp/Gallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menuItem.setIcon(R.drawable.baseline_terminal_24);

return LibUI.handleMenu(menu);
return true;
}
}

19 changes: 7 additions & 12 deletions app/src/main/java/dev/danielc/fujiapp/ImageAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import libui.LibUI;

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context context;
private static int[] object_ids;
Expand Down Expand Up @@ -66,14 +60,14 @@ static void invalidThumb(Request req, int drawable_id) {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void run() {
// TODO: Open a popup with some file information
req.holder.itemView.setOnClickListener(null);
req.holder.image.setBackground(req.ctx.getDrawable(drawable_id));
}
});
}

static void requestThread() {
Log.d("adpater", "Thread");
while (!Backend.cGetKillSwitch()) {
if (requests.size() == 0) {
try {
Expand All @@ -86,7 +80,7 @@ static void requestThread() {
int id = req.object_id;
byte[] jpegByteArray = Backend.cPtpGetThumb(id);
if (jpegByteArray == null) {
Backend.reportError(Backend.PTP_IO_ERR, "Failed to get image thumbnail, stopping connection");
Backend.reportError(Backend.PTP_IO_ERR, "Failed to get thumbnail");
return;
} else if (jpegByteArray.length == 0) {
// Unable to find thumbnail - assume it's a folder or non-jpeg
Expand All @@ -102,16 +96,15 @@ static void requestThread() {

Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length, opt);
if (bitmap == null) {
Backend.print("Image decode error");
req.holder.itemView.setOnClickListener(null);
// Invalid JPEG
invalidThumb(req, R.drawable.baseline_question_mark_24);
continue;
}
req.holder.itemView.post(new Runnable() {
@SuppressLint("UseCompatLoadingForDrawables")
@Override
public void run() {
req.holder.image.setImageBitmap(bitmap);
req.holder.image.setForeground(req.ctx.getDrawable(R.drawable.ripple));
}
});
} catch (OutOfMemoryError e) {
Expand All @@ -122,7 +115,9 @@ public void run() {

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
holder.image.setBackground(context.getDrawable(R.drawable.light_button)); // :)
holder.image.setForeground(context.getDrawable(R.drawable.ripple));
holder.image.setBackground(context.getDrawable(R.drawable.light_button));
holder.image.setImageBitmap(null);
position = holder.getAdapterPosition();
Request req = new Request();
req.ctx = context;
Expand Down
41 changes: 0 additions & 41 deletions app/src/main/java/dev/danielc/fujiapp/LibU.java

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/dev/danielc/fujiapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menuItem.setIcon(R.drawable.baseline_fact_check_24);

return LibUI.handleMenu(menu);
return true;
}
}
8 changes: 0 additions & 8 deletions app/src/main/java/dev/danielc/fujiapp/Tester.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.hardware.usb.UsbManager;
import android.util.Log;
import android.content.Intent;
import android.text.Html;
Expand All @@ -17,18 +16,11 @@
import android.os.Bundle;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Handler;
import android.os.Build;

import java.net.Socket;
import android.content.ClipboardManager;
import android.widget.Toast;

import libui.LibU;

public class Tester extends AppCompatActivity {
private Handler handler;

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/dev/danielc/fujiapp/Viewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ protected void onDestroy() {
Runtime.getRuntime().gc();
handler = null;
progressBar = null;
popupWindow.dismiss();
popupWindow = null;
super.onDestroy();
}
Expand Down Expand Up @@ -301,6 +302,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
if (fileIsInMemory) {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.viewer, menu);
Expand Down
29 changes: 2 additions & 27 deletions app/src/main/java/libui/LibUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,11 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
public void onNothingSelected(AdapterView<?> parent) {}
}

private static class MyOnClickListener2 implements View.OnClickListener {
private static class MyOnClickListener implements View.OnClickListener {
byte[] struct;
public MyOnClickListener2(byte[] struct) {
public MyOnClickListener(byte[] struct) {
this.struct = struct;
}

@Override
public void onClick(View v) {
LibUI.callFunction(struct);
Expand Down Expand Up @@ -339,30 +338,6 @@ private static LibUI.Popup openWindow(String title, int options) {
return popup;
}

private static ViewGroup linearLayout(int orientation) {
LinearLayout layout = new LinearLayout(ctx);
layout.setOrientation(orientation);
layout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return (ViewGroup)layout;
}

private static String getString(String name) {
Resources res = ctx.getResources();
return res.getString(res.getIdentifier(name, "string", ctx.getPackageName()));
}

private static View getView(String name) {
Resources res = ctx.getResources();
int id = res.getIdentifier(name, "id", ctx.getPackageName());
return ((Activity)ctx).findViewById(id);
}

private static void toast(String text) {
Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show();
}

private static void runRunnable(byte[] data) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
Expand Down
18 changes: 4 additions & 14 deletions app/src/main/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/testerLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Space
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/testerLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logs go here" />
</LinearLayout>
android:text="Logs go here" />
</ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>
29 changes: 19 additions & 10 deletions lib/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,9 @@

struct AndroidBackend {
jobject main; // Backend global obj
// jobject cmd_socket;

jmethodID jni_print;

jmethodID send_text_m;

// jmethodID wifi_cmd_read;
// jmethodID wifi_cmd_write;
// jmethodID wifi_cmd_close;
// jobject wifi_cmd_buffer;

jobject progress_bar;
int download_progress;
int download_size;
Expand All @@ -71,14 +63,31 @@ struct AndroidBackend {

extern struct AndroidBackend backend;

// Thread safe JNIEnv storage
void set_jni_env(JNIEnv *env);
JNIEnv *get_jni_env();

// Verbose print to log file
void jni_verbose_log(char *str);

void reset_connection();

int jni_setup_usb(JNIEnv *env, jobject obj);

static inline void app_increment_progress_bar(int read) {
// Measures progress on all threads
static int last_p = 0;

backend.download_progress += read;

int n = (((double)backend.download_progress) / (double)backend.download_size * 100.0);
if (last_p != n) {
if (n > 100) return;

JNIEnv *env = get_jni_env();

jmethodID method = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, backend.progress_bar), "setProgress", "(I)V");
(*env)->CallVoidMethod(env, backend.progress_bar, method, n);
}
last_p = n;
}

#endif
2 changes: 1 addition & 1 deletion lib/camlib
Submodule camlib updated 3 files
+1 −17 lua/camlua.h
+2 −2 src/cl_backend.h
+42 −33 src/transport.c
2 changes: 1 addition & 1 deletion lib/libuifw
Loading

0 comments on commit 0ccb4ee

Please sign in to comment.