android: bug fixes

onInputDeviceAdded may be called with an invalid device id. Avoid NPE
onGenericMotionEvent: event may have a null device. Avoid NPE
getExternalFilesDir may return null if no external storage available.
Use getFilesDir in this case.
Catch exceptions in HomeMover and continue.
addStorage: catch SecurityException and alert user. Graceful failure.
This commit is contained in:
Flyinghead 2024-07-20 10:30:27 +02:00
parent 2b83df86cd
commit 8b2c1bc5dc
4 changed files with 44 additions and 8 deletions

View File

@ -19,9 +19,11 @@
package com.flycast.emulator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
@ -76,7 +78,27 @@ public class AndroidStorage {
}
else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
activity.getContentResolver().takePersistableUriPermission(uri, storageIntentPerms);
{
try {
activity.getContentResolver().takePersistableUriPermission(uri, storageIntentPerms);
} catch (SecurityException e) {
Log.w("Flycast", "takePersistableUriPermission failed", e);
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(activity);
dlgAlert.setMessage("Can't get permissions to access this folder.\nPlease select a different one.");
dlgAlert.setTitle("Storage Error");
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int id) {
addStorageCallback(null);
}
});
dlgAlert.setIcon(android.R.drawable.ic_dialog_alert);
dlgAlert.setCancelable(false);
dlgAlert.create().show();
return;
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
String realPath = getRealPath(uri);
if (realPath != null) {

View File

@ -270,7 +270,10 @@ public abstract class BaseGLActivity extends Activity implements ActivityCompat.
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK
&& event.getAction() == MotionEvent.ACTION_MOVE
&& event.getDevice() != null)
{
List<InputDevice.MotionRange> axes = event.getDevice().getMotionRanges();
boolean rc = false;
for (InputDevice.MotionRange range : axes)
@ -416,9 +419,11 @@ public abstract class BaseGLActivity extends Activity implements ActivityCompat.
}
private String getDefaultHomeDir()
{
return getExternalFilesDir(null).getAbsolutePath();
private String getDefaultHomeDir() {
File dir = getExternalFilesDir(null);
if (dir == null)
dir = getFilesDir();
return dir.getAbsolutePath();
}
private String checkHomeDirectory(String homeDir)

View File

@ -55,8 +55,13 @@ public class HomeMover {
File f = getFile(parent);
if (f != null)
return new File(f, kid).toURI().toString();
else
return storage.getSubPath(parent, kid);
else {
try {
return storage.getSubPath(parent, kid);
} catch (RuntimeException e) {
return null;
}
}
}
public FileInfo[] listContent(String folder)
@ -97,6 +102,8 @@ public class HomeMover {
}
public boolean exists(String path) {
if (path == null)
return false;
File file = getFile(path);
if (file != null)
return file.exists();
@ -139,6 +146,8 @@ public class HomeMover {
Thread thread = new Thread(new Runnable() {
private void copyFile(String path, String name, String toDir)
{
if (path == null)
return;
//Log.d("flycast", "Copying " + path + " to " + toDir);
try {
InputStream in = wrapper.openInputStream(path);

View File

@ -61,7 +61,7 @@ public final class InputDeviceManager implements InputManager.InputDeviceListene
@Override
public void onInputDeviceAdded(int i) {
InputDevice device = InputDevice.getDevice(i);
if ((device.getSources() & InputDevice.SOURCE_CLASS_BUTTON) == InputDevice.SOURCE_CLASS_BUTTON) {
if (device != null && (device.getSources() & InputDevice.SOURCE_CLASS_BUTTON) == InputDevice.SOURCE_CLASS_BUTTON) {
int port = 0;
if ((device.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) {
port = this.maple_port == 3 ? 3 : this.maple_port++;