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

Android: Use system cache directory as cache directory #8949

Merged
merged 1 commit into from
Jul 13, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ public static native String GetConfig(String configFile, String Section, String
*/
public static native String GetUserDirectory();

public static native void SetCacheDirectory(String directory);

public static native int DefaultCPUCore();

public static native void ReloadConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dolphinemu.dolphinemu.model;

import android.content.Context;
import android.os.Environment;

public class GameFile
Expand Down Expand Up @@ -54,10 +55,9 @@ private GameFile(long pointer)

public native int getBannerHeight();

public String getCoverPath()
public String getCoverPath(Context context)
{
return Environment.getExternalStorageDirectory().getPath() +
"/dolphin-emu/Cache/GameCovers/" + getGameTdbId() + ".png";
return context.getExternalCacheDir().getPath() + "/GameCovers/" + getGameTdbId() + ".png";
}

public String getCustomCoverPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static void init(Context context)
{
if (PermissionsHandler.hasWriteAccess(context))
{
if (setDolphinUserDirectory())
if (setDolphinUserDirectory(context))
{
initializeInternalStorage(context);
initializeExternalStorage(context);
Expand All @@ -88,22 +88,27 @@ private static void init(Context context)
sendBroadcastState(directoryState, context);
}

private static boolean setDolphinUserDirectory()
private static boolean setDolphinUserDirectory(Context context)
{
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
File externalPath = Environment.getExternalStorageDirectory();
if (externalPath != null)
{
userPath = externalPath.getAbsolutePath() + "/dolphin-emu";
Log.debug("[DirectoryInitialization] User Dir: " + userPath);
NativeLibrary.SetUserDirectory(userPath);
return true;
}
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
return false;

}
File externalPath = Environment.getExternalStorageDirectory();
if (externalPath == null)
return false;
Tilka marked this conversation as resolved.
Show resolved Hide resolved

userPath = externalPath.getAbsolutePath() + "/dolphin-emu";
Log.debug("[DirectoryInitialization] User Dir: " + userPath);
NativeLibrary.SetUserDirectory(userPath);

File cacheDir = context.getExternalCacheDir();
if (cacheDir == null)
return false;

Log.debug("[DirectoryInitialization] Cache Dir: " + cacheDir.getPath());
NativeLibrary.SetCacheDirectory(cacheDir.getPath());

return false;
return true;
}

private static void initializeInternalStorage(Context context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dolphinemu.dolphinemu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
Expand Down Expand Up @@ -33,6 +34,7 @@ public static void loadGameBanner(ImageView imageView, GameFile gameFile)

public static void loadGameCover(ImageView imageView, GameFile gameFile)
{
Context context = imageView.getContext();
File cover = new File(gameFile.getCustomCoverPath());
if (cover.exists())
{
Expand All @@ -46,7 +48,7 @@ public static void loadGameCover(ImageView imageView, GameFile gameFile)
.error(R.drawable.no_banner)
.into(imageView);
}
else if ((cover = new File(gameFile.getCoverPath())).exists())
else if ((cover = new File(gameFile.getCoverPath(context))).exists())
{
Picasso.get()
.load(cover)
Expand Down Expand Up @@ -76,7 +78,7 @@ else if ((cover = new File(gameFile.getCoverPath())).exists())
public void onSuccess()
{
CoverHelper.saveCover(((BitmapDrawable) imageView.getDrawable()).getBitmap(),
gameFile.getCoverPath());
gameFile.getCoverPath(context));
}

@Override
Expand All @@ -98,7 +100,7 @@ public void onSuccess()
{
CoverHelper.saveCover(
((BitmapDrawable) imageView.getDrawable()).getBitmap(),
gameFile.getCoverPath());
gameFile.getCoverPath(context));
}

@Override
Expand All @@ -121,7 +123,7 @@ public void onSuccess()
CoverHelper.saveCover(
((BitmapDrawable) imageView.getDrawable())
.getBitmap(),
gameFile.getCoverPath());
gameFile.getCoverPath(context));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static Uri buildBanner(GameFile game, Context context)
{
contentUri = getUriForFile(context, getFileProvider(context), cover);
}
else if ((cover = new File(game.getCoverPath())).exists())
else if ((cover = new File(game.getCoverPath(context))).exists())
{
contentUri = getUriForFile(context, getFileProvider(context), cover);
}
Expand Down
9 changes: 9 additions & 0 deletions Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetUserDirec
JNIEnv* env, jobject obj, jstring jDirectory);
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_NativeLibrary_GetUserDirectory(JNIEnv* env, jobject obj);
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetCacheDirectory(
JNIEnv* env, jobject obj, jstring jDirectory);
JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_DefaultCPUCore(JNIEnv* env,
jobject obj);
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetProfiling(JNIEnv* env,
Expand Down Expand Up @@ -534,6 +536,13 @@ JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetUserDi
return ToJString(env, File::GetUserPath(D_USER_IDX).c_str());
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetCacheDirectory(
JNIEnv* env, jobject obj, jstring jDirectory)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
File::SetUserPath(D_CACHE_IDX, GetJString(env, jDirectory) + DIR_SEP);
}

JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_DefaultCPUCore(JNIEnv* env,
jobject obj)
{
Expand Down