|
14 | 14 | import android.os.Message; |
15 | 15 | import com.retroarch.browser.preferences.util.ConfigFile; |
16 | 16 | import com.retroarch.browser.preferences.util.UserPreferences; |
| 17 | +import com.retroarch.browser.mainmenu.MainMenuActivity; |
17 | 18 | import java.lang.reflect.InvocationTargetException; |
18 | 19 | import java.lang.reflect.Method; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.FileOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.List; |
| 25 | +import java.util.ArrayList; |
| 26 | +import android.content.pm.PackageManager; |
| 27 | +import android.Manifest; |
19 | 28 |
|
20 | 29 | public final class RetroActivityFuture extends RetroActivityCamera { |
21 | 30 |
|
@@ -62,10 +71,111 @@ public void onCreate(Bundle savedInstanceState) { |
62 | 71 | isRunning = true; |
63 | 72 | mDecorView = getWindow().getDecorView(); |
64 | 73 |
|
| 74 | + // Check if we need permissions before proceeding |
| 75 | + if (!checkPermissions()) { |
| 76 | + // Redirect to MainMenuActivity to get permissions |
| 77 | + Intent mainMenuIntent = new Intent(this, MainMenuActivity.class); |
| 78 | + mainMenuIntent.putExtras(getIntent()); |
| 79 | + startActivity(mainMenuIntent); |
| 80 | + finish(); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Handle core sideloading if external core path provided |
| 85 | + handleCoreSideloading(); |
| 86 | + |
65 | 87 | // If QUITFOCUS parameter is provided then enable that Retroarch quits when focus is lost |
66 | 88 | quitfocus = getIntent().hasExtra("QUITFOCUS"); |
67 | 89 | } |
68 | 90 |
|
| 91 | + private boolean checkPermissions() { |
| 92 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 93 | + List<String> permissionsNeeded = new ArrayList<String>(); |
| 94 | + |
| 95 | + if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { |
| 96 | + permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE); |
| 97 | + } |
| 98 | + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { |
| 99 | + permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 100 | + } |
| 101 | + |
| 102 | + return permissionsNeeded.isEmpty(); |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + private void handleCoreSideloading() { |
| 108 | + String corePath = getIntent().getStringExtra("LIBRETRO"); |
| 109 | + if (corePath == null || corePath.isEmpty()) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + File coreFile = new File(corePath); |
| 114 | + if (!coreFile.exists()) { |
| 115 | + Log.w("RetroActivityFuture", "Core file does not exist: " + corePath); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Get the cores directory |
| 120 | + String coresDir = getApplicationInfo().dataDir + "/cores/"; |
| 121 | + File coresDirFile = new File(coresDir); |
| 122 | + |
| 123 | + // Create cores directory if it doesn't exist |
| 124 | + if (!coresDirFile.exists()) { |
| 125 | + if (!coresDirFile.mkdirs()) { |
| 126 | + Log.e("RetroActivityFuture", "Failed to create cores directory: " + coresDir); |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Check if core is outside RetroArch cores folder - if so, sideload it |
| 132 | + if (!corePath.startsWith(coresDir)) { |
| 133 | + String coreFileName = coreFile.getName(); |
| 134 | + File destinationFile = new File(coresDir, coreFileName); |
| 135 | + |
| 136 | + try { |
| 137 | + copyFile(coreFile, destinationFile); |
| 138 | + Log.i("RetroActivityFuture", "Sideloaded core from " + corePath + " to " + destinationFile.getAbsolutePath()); |
| 139 | + |
| 140 | + // Update intent to point to the new location |
| 141 | + getIntent().putExtra("LIBRETRO", destinationFile.getAbsolutePath()); |
| 142 | + } catch (IOException e) { |
| 143 | + Log.e("RetroActivityFuture", "Failed to sideload core: " + e.getMessage()); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void copyFile(File source, File destination) throws IOException { |
| 149 | + FileInputStream inStream = null; |
| 150 | + FileOutputStream outStream = null; |
| 151 | + |
| 152 | + try { |
| 153 | + inStream = new FileInputStream(source); |
| 154 | + outStream = new FileOutputStream(destination); |
| 155 | + |
| 156 | + byte[] buffer = new byte[8192]; |
| 157 | + int length; |
| 158 | + while ((length = inStream.read(buffer)) > 0) { |
| 159 | + outStream.write(buffer, 0, length); |
| 160 | + } |
| 161 | + } finally { |
| 162 | + if (inStream != null) { |
| 163 | + try { |
| 164 | + inStream.close(); |
| 165 | + } catch (IOException e) { |
| 166 | + // Ignore |
| 167 | + } |
| 168 | + } |
| 169 | + if (outStream != null) { |
| 170 | + try { |
| 171 | + outStream.close(); |
| 172 | + } catch (IOException e) { |
| 173 | + // Ignore |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
69 | 179 | @Override |
70 | 180 | public void onNewIntent(Intent intent) { |
71 | 181 | super.onNewIntent(intent); |
|
0 commit comments