Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #10 from flipperdevices/nm/2491_file_browser_module #14

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions applications/applications.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern int32_t vibro_test_app(void* p);
extern int32_t bt_hid_app(void* p);
extern int32_t battery_test_app(void* p);
extern int32_t text_box_test_app(void* p);
extern int32_t file_browser_app(void* p);

// Plugins
extern int32_t music_player_app(void* p);
Expand Down Expand Up @@ -530,6 +531,14 @@ const FlipperApplication FLIPPER_DEBUG_APPS[] = {
.flags = FlipperApplicationFlagDefault},
#endif

#ifdef APP_FILE_BROWSER_TEST
{.app = file_browser_app,
.name = "File Browser test",
.stack_size = 2048,
.icon = &A_BadUsb_14,
.flags = FlipperApplicationFlagDefault},
#endif

#ifdef APP_BATTERY_TEST
{.app = battery_test_app,
.name = "Battery Test",
Expand Down
6 changes: 6 additions & 0 deletions applications/applications.mk
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ APP_USB_MOUSE = 1
APP_BAD_USB = 1
APP_U2F = 1
APP_UART_ECHO = 1
APP_FILE_BROWSER_TEST = 1
endif


Expand Down Expand Up @@ -228,6 +229,11 @@ CFLAGS += -DAPP_KEYPAD_TEST
SRV_GUI = 1
endif

APP_FILE_BROWSER_TEST ?= 0
ifeq ($(APP_FILE_BROWSER_TEST), 1)
CFLAGS += -DAPP_FILE_BROWSER_TEST
SRV_GUI = 1
endif

APP_ACCESSOR ?= 0
ifeq ($(APP_ACCESSOR), 1)
Expand Down
99 changes: 99 additions & 0 deletions applications/debug_tools/file_browser_test/file_browser_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "assets_icons.h"
#include "file_browser_app_i.h"
#include "gui/modules/file_browser.h"
#include "m-string.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <lib/toolbox/path.h>

static bool file_browser_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
FileBrowserApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}

static bool file_browser_app_back_event_callback(void* context) {
furi_assert(context);
FileBrowserApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}

static void file_browser_app_tick_event_callback(void* context) {
furi_assert(context);
FileBrowserApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}

FileBrowserApp* file_browser_app_alloc(char* arg) {
UNUSED(arg);
FileBrowserApp* app = malloc(sizeof(FileBrowserApp));

app->gui = furi_record_open("gui");
app->dialogs = furi_record_open("dialogs");

app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);

app->scene_manager = scene_manager_alloc(&file_browser_scene_handlers, app);

view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, file_browser_app_tick_event_callback, 500);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, file_browser_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, file_browser_app_back_event_callback);

app->widget = widget_alloc();

string_init(app->file_path);
app->file_browser = file_browser_alloc(&(app->file_path));
file_browser_configure(app->file_browser, "*", true, &I_badusb_10px, true);

view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewStart, widget_get_view(app->widget));
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewResult, widget_get_view(app->widget));
view_dispatcher_add_view(
app->view_dispatcher, FileBrowserAppViewBrowser, file_browser_get_view(app->file_browser));

view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);

scene_manager_next_scene(app->scene_manager, FileBrowserSceneStart);

return app;
}

void file_browser_app_free(FileBrowserApp* app) {
furi_assert(app);

// Views
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewStart);
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewResult);
view_dispatcher_remove_view(app->view_dispatcher, FileBrowserAppViewBrowser);
widget_free(app->widget);
file_browser_free(app->file_browser);

// View dispatcher
view_dispatcher_free(app->view_dispatcher);
scene_manager_free(app->scene_manager);

// Close records
furi_record_close("gui");
furi_record_close("notification");
furi_record_close("dialogs");

string_clear(app->file_path);

free(app);
}

int32_t file_browser_app(void* p) {
FileBrowserApp* file_browser_app = file_browser_app_alloc((char*)p);

view_dispatcher_run(file_browser_app->view_dispatcher);

file_browser_app_free(file_browser_app);
return 0;
}
32 changes: 32 additions & 0 deletions applications/debug_tools/file_browser_test/file_browser_app_i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "scenes/file_browser_scene.h"

#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <gui/modules/file_browser.h>
#include <dialogs/dialogs.h>
#include <notification/notification_messages.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/widget.h>

typedef struct FileBrowserApp FileBrowserApp;

struct FileBrowserApp {
Gui* gui;
ViewDispatcher* view_dispatcher;
SceneManager* scene_manager;
DialogsApp* dialogs;
Widget* widget;
FileBrowser* file_browser;

string_t file_path;
};

typedef enum {
FileBrowserAppViewStart,
FileBrowserAppViewBrowser,
FileBrowserAppViewResult,
} FileBrowserAppView;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "file_browser_scene.h"

// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const file_browser_scene_on_enter_handlers[])(void*) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const file_browser_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const file_browser_scene_on_exit_handlers[])(void* context) = {
#include "file_browser_scene_config.h"
};
#undef ADD_SCENE

// Initialize scene handlers configuration structure
const SceneManagerHandlers file_browser_scene_handlers = {
.on_enter_handlers = file_browser_scene_on_enter_handlers,
.on_event_handlers = file_browser_scene_on_event_handlers,
.on_exit_handlers = file_browser_scene_on_exit_handlers,
.scene_num = FileBrowserSceneNum,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <gui/scene_manager.h>

// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) FileBrowserScene##id,
typedef enum {
#include "file_browser_scene_config.h"
FileBrowserSceneNum,
} FileBrowserScene;
#undef ADD_SCENE

extern const SceneManagerHandlers file_browser_scene_handlers;

// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "file_browser_scene_config.h"
#undef ADD_SCENE

// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "file_browser_scene_config.h"
#undef ADD_SCENE

// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "file_browser_scene_config.h"
#undef ADD_SCENE
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../file_browser_app_i.h"
#include "furi/check.h"
#include "furi/log.h"
#include "furi_hal.h"
#include "m-string.h"

#define DEFAULT_PATH "/"
#define EXTENSION "*"

bool file_browser_scene_browser_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
FileBrowserApp* app = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
scene_manager_next_scene(app->scene_manager, FileBrowserSceneResult);
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}

static void file_browser_callback(void* context, bool state) {
FileBrowserApp* app = context;
furi_assert(app);
view_dispatcher_send_custom_event(app->view_dispatcher, SceneManagerEventTypeCustom);

UNUSED(state);
}

void file_browser_scene_browser_on_enter(void* context) {
FileBrowserApp* app = context;

file_browser_set_callback(app->file_browser, file_browser_callback, app);

file_browser_start(app->file_browser, app->file_path);

view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewBrowser);
}

void file_browser_scene_browser_on_exit(void* context) {
FileBrowserApp* app = context;

file_browser_stop(app->file_browser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ADD_SCENE(file_browser, start, Start)
ADD_SCENE(file_browser, browser, Browser)
ADD_SCENE(file_browser, result, Result)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "../file_browser_app_i.h"
#include "furi_hal.h"
#include "m-string.h"

void file_browser_scene_result_ok_callback(InputType type, void* context) {
furi_assert(context);
FileBrowserApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, type);
}

bool file_browser_scene_result_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
//FileBrowserApp* app = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}

void file_browser_scene_result_on_enter(void* context) {
FileBrowserApp* app = context;

widget_add_string_multiline_element(
app->widget, 64, 10, AlignCenter, AlignTop, FontSecondary, string_get_cstr(app->file_path));

view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewResult);
}

void file_browser_scene_result_on_exit(void* context) {
UNUSED(context);
FileBrowserApp* app = context;
widget_reset(app->widget);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../file_browser_app_i.h"
#include "furi_hal.h"
#include "gui/modules/widget_elements/widget_element_i.h"

static void
file_browser_scene_start_ok_callback(GuiButtonType result, InputType type, void* context) {
UNUSED(result);
furi_assert(context);
FileBrowserApp* app = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(app->view_dispatcher, type);
}
}

bool file_browser_scene_start_on_event(void* context, SceneManagerEvent event) {
FileBrowserApp* app = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
string_set_str(app->file_path, "/any/badusb/demo_windows.txt");
scene_manager_next_scene(app->scene_manager, FileBrowserSceneBrowser);
consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}

void file_browser_scene_start_on_enter(void* context) {
FileBrowserApp* app = context;

widget_add_string_multiline_element(
app->widget, 64, 20, AlignCenter, AlignTop, FontSecondary, "Press OK to start");

widget_add_button_element(
app->widget, GuiButtonTypeCenter, "Ok", file_browser_scene_start_ok_callback, app);

view_dispatcher_switch_to_view(app->view_dispatcher, FileBrowserAppViewStart);
}

void file_browser_scene_start_on_exit(void* context) {
UNUSED(context);
FileBrowserApp* app = context;
widget_reset(app->widget);
}
Loading