Browse Source

Support Android internal data storage

Useful to save small data files (configuration and so)
For bigger files, external data storage should be used (SDCard)
Ray 9 years ago
parent
commit
8a4e28f81d
2 changed files with 24 additions and 4 deletions
  1. 23 3
      src/core.c
  2. 1 1
      src/utils.c

+ 23 - 3
src/core.c

@@ -147,6 +147,7 @@ static bool windowMinimized = false;
 static struct android_app *app;                 // Android activity
 static struct android_app *app;                 // Android activity
 static struct android_poll_source *source;      // Android events polling source
 static struct android_poll_source *source;      // Android events polling source
 static int ident, events;                       // Android ALooper_pollAll() variables
 static int ident, events;                       // Android ALooper_pollAll() variables
+static const char *internalDataPath;            // Android internal data path to write data (/data/data/<package>/files)
 
 
 static bool windowReady = false;                // Used to detect display initialization
 static bool windowReady = false;                // Used to detect display initialization
 static bool appEnabled = true;                  // Used to detec if app is active
 static bool appEnabled = true;                  // Used to detec if app is active
@@ -363,6 +364,7 @@ void InitWindow(int width, int height, struct android_app *state)
     screenHeight = height;
     screenHeight = height;
 
 
     app = state;
     app = state;
+    internalDataPath = app->activity->internalDataPath;
 
 
     // Set desired windows flags before initializing anything
     // Set desired windows flags before initializing anything
     ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0);  //AWINDOW_FLAG_SCALED, AWINDOW_FLAG_DITHER
     ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0);  //AWINDOW_FLAG_SCALED, AWINDOW_FLAG_DITHER
@@ -838,12 +840,21 @@ void ClearDroppedFiles(void)
 void StorageSaveValue(int position, int value)
 void StorageSaveValue(int position, int value)
 {
 {
     FILE *storageFile = NULL;
     FILE *storageFile = NULL;
+    
+    char path[128];
+#if defined(PLATFORM_ANDROID)
+    strcpy(path, internalDataPath);
+    strcat(path, "/");
+    strcat(path, STORAGE_FILENAME);
+#else
+    strcpy(path, STORAGE_FILENAME);
+#endif
 
 
     // Try open existing file to append data
     // Try open existing file to append data
-    storageFile = fopen(STORAGE_FILENAME, "rb+");      
+    storageFile = fopen(path, "rb+");      
 
 
     // If file doesn't exist, create a new storage data file
     // If file doesn't exist, create a new storage data file
-    if (!storageFile) storageFile = fopen(STORAGE_FILENAME, "wb");
+    if (!storageFile) storageFile = fopen(path, "wb");
 
 
     if (!storageFile) TraceLog(WARNING, "Storage data file could not be created");
     if (!storageFile) TraceLog(WARNING, "Storage data file could not be created");
     else
     else
@@ -870,8 +881,17 @@ int StorageLoadValue(int position)
 {
 {
     int value = 0;
     int value = 0;
     
     
+    char path[128];
+#if defined(PLATFORM_ANDROID)
+    strcpy(path, internalDataPath);
+    strcat(path, "/");
+    strcat(path, STORAGE_FILENAME);
+#else
+    strcpy(path, STORAGE_FILENAME);
+#endif
+    
     // Try open existing file to append data
     // Try open existing file to append data
-    FILE *storageFile = fopen(STORAGE_FILENAME, "rb");      
+    FILE *storageFile = fopen(path, "rb");      
 
 
     if (!storageFile) TraceLog(WARNING, "Storage data file could not be found");
     if (!storageFile) TraceLog(WARNING, "Storage data file could not be found");
     else
     else

+ 1 - 1
src/utils.c

@@ -247,7 +247,7 @@ FILE *android_fopen(const char *fileName, const char *mode)
 
 
     AAsset *asset = AAssetManager_open(assetManager, fileName, 0);
     AAsset *asset = AAssetManager_open(assetManager, fileName, 0);
 
 
-    if(!asset) return NULL;
+    if (!asset) return NULL;
 
 
     return funopen(asset, android_read, android_write, android_seek, android_close);
     return funopen(asset, android_read, android_write, android_seek, android_close);
 }
 }