Android: Store cached textures in dedicated Cache folder in external storage

This means that from now on in Settings -> Apps, the user can accurately see how much of ClassiCube's data usage on disc comes from caching textures, and can clear the cached data if they choose to
This commit is contained in:
UnknownShadow200 2022-10-16 20:47:04 +11:00
parent 37abd39163
commit 6593ad50ab
4 changed files with 31 additions and 12 deletions

View file

@ -9,7 +9,6 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -34,8 +33,6 @@ import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.InputQueue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
@ -43,12 +40,10 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
// This class contains all the glue/interop code for bridging ClassiCube to the java Android world.
@ -596,10 +591,21 @@ public class MainActivity extends Activity
}
}
public String getExternalAppDir() {
public String getGameDataDirectory() {
// getExternalFilesDir - API level 8
return getExternalFilesDir(null).getAbsolutePath();
}
public String getGameCacheDirectory() {
// getExternalCacheDir - API level 8
File root = getExternalCacheDir();
if (root != null) return root.getAbsolutePath();
// although exceedingly rare, getExternalCacheDir() can technically fail
// "... May return null if shared storage is not currently available."
// getCacheDir - API level 1
return getCacheDir().getAbsolutePath();
}
public String getUUID() {
// getContentResolver - API level 1
@ -803,7 +809,7 @@ public class MainActivity extends Activity
public String shareScreenshot(String path) {
try {
File file = new File(getExternalAppDir() + "/screenshots/" + path);
File file = new File(getGameDataDirectory() + "/screenshots/" + path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
@ -866,7 +872,7 @@ public class MainActivity extends Activity
String saveContentToTemp(Uri uri, String folder, String name) throws IOException {
//File file = new File(getExternalFilesDir(null), folder + "/" + name);
File file = new File(getExternalAppDir() + "/" + folder + "/" + name);
File file = new File(getGameDataDirectory() + "/" + folder + "/" + name);
file.getParentFile().mkdirs();
OutputStream output = null;

View file

@ -93,6 +93,16 @@ void Platform_ShareScreenshot(const cc_string* filename) {
Chat_Add1(" &c%s", &path);
}
void Directory_GetCachePath(cc_string* path, const char* folder) {
cc_string dir; char dirBuffer[FILENAME_SIZE];
String_InitArray(dir, dirBuffer);
// TODO cache method ID
JavaCall_Void_String("getGameCacheDirectory", &dir);
String_Format2(path, "%s/%c", &dir, folder);
Directory_Create(path);
}
/*########################################################################################################################*
*-----------------------------------------------------Configuration-------------------------------------------------------*
@ -113,9 +123,9 @@ cc_result Platform_SetDefaultCurrentDirectory(int argc, char **argv) {
cc_string dir; char dirBuffer[FILENAME_SIZE + 1];
String_InitArray_NT(dir, dirBuffer);
JavaCall_Void_String("getExternalAppDir", &dir);
JavaCall_Void_String("getGameDataDirectory", &dir);
dir.buffer[dir.length] = '\0';
Platform_Log1("EXTERNAL DIR: %s|", &dir);
Platform_Log1("DATA DIR: %s|", &dir);
int res = chdir(dir.buffer) == -1 ? errno : 0;
if (!res) return 0;

View file

@ -143,7 +143,9 @@ cc_uint64 Stopwatch_Measure(void) {
/*########################################################################################################################*
*-----------------------------------------------------Directory/File------------------------------------------------------*
*#########################################################################################################################*/
#if defined CC_BUILD_IOS
#if defined CC_BUILD_ANDROID
/* implemented in Platform_Android.c */
#elif defined CC_BUILD_IOS
/* implemented in interop_ios.m */
#else
void Directory_GetCachePath(cc_string* path, const char* folder) {

View file

@ -375,7 +375,7 @@ static void JNICALL java_processOFDResult(JNIEnv* env, jobject o, jstring str) {
ofd_callback(&path);
if (ofd_action == OFD_UPLOAD_DELETE) {
// TODO better way of doing this?
// TODO better way of doing this? msybe move to java side?
raw = (*env)->GetStringUTFChars(env, str, NULL);
unlink(raw);
(*env)->ReleaseStringUTFChars(env, str, raw);
@ -391,6 +391,7 @@ cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* open_args) {
ofd_callback = open_args->Callback;
ofd_action = open_args->uploadAction;
// TODO use filters
args[0].l = JavaMakeConst(env, open_args->uploadFolder);
int OK = JavaICall_Int(env, JAVA_openFileDialog, args);
(*env)->DeleteLocalRef(env, args[0].l);