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: Remove external storage permission #16393

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions pkg/android/phoenix/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<uses-feature android:name="android.software.leanback" android:required="false" />
<uses-feature android:name="android.hardware.gamepad" android:required="false"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />

Expand All @@ -24,8 +22,10 @@
android:isGame="true"
android:banner="@drawable/banner"
android:extractNativeLibs="true"
android:requestLegacyExternalStorage="true"
tools:ignore="UnusedAttribute">
<activity
android:name=".browser.mainmenu.MigrateRetroarchFolderActivity"
android:exported="false"/>
<activity android:name="com.retroarch.browser.mainmenu.MainMenuActivity" android:exported="true" android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
31 changes: 31 additions & 0 deletions pkg/android/phoenix/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="migrate_retroarch_folder_dialog_title">
Migrate RetroArch Folder
</string>
<string name="migrate_retroarch_folder_dialog_message">
Because RetroArch was updated, the location of the RetroArch folder has changed. \n
Would you like to import data from an existing RetroArch folder?
</string>
<string name="migrate_retroarch_folder_dialog_positive">
Yes, select existing RetroArch folder
</string>
<string name="migrate_retroarch_folder_dialog_negative">
No, don\'t ask again
</string>
<string name="migrate_retroarch_folder_dialog_neutral">
No, ask next time
</string>
<string name="migrate_retroarch_folder_inprogress">
Copying RetroArch Files…
</string>
<string name="migrate_retroarch_folder_confirm">
Your RetroArch folder has been migrated. \n
You can find it in the files app under RetroArch > User Data.
</string>
<string name="migrate_retroarch_folder_confirm_witherror">
Your RetroArch folder has been migrated. \n
You can find it in the files app under RetroArch > User Data. \n
There were errors copying some files.
</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -12,106 +12,14 @@
import android.preference.PreferenceManager;
import android.provider.Settings;

import java.util.List;
import java.util.ArrayList;
import android.content.pm.PackageManager;
import android.Manifest;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.util.Log;

/**
* {@link PreferenceActivity} subclass that provides all of the
* functionality of the main menu screen.
*/
public final class MainMenuActivity extends PreferenceActivity
{
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
public static String PACKAGE_NAME;
boolean checkPermissions = false;

public void showMessageOKCancel(String message, DialogInterface.OnClickListener onClickListener)
{
new AlertDialog.Builder(this).setMessage(message)
.setPositiveButton("OK", onClickListener).setCancelable(false)
.setNegativeButton("Cancel", null).create().show();
}

private boolean addPermission(List<String> permissionsList, String permission)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
{
permissionsList.add(permission);

// Check for Rationale Option
if (!shouldShowRequestPermissionRationale(permission))
return false;
}
}

return true;
}

public void checkRuntimePermissions()
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
// Android 6.0+ needs runtime permission checks
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();

if (!addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE))
permissionsNeeded.add("Read External Storage");
if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
permissionsNeeded.add("Write External Storage");

if (permissionsList.size() > 0)
{
checkPermissions = true;

if (permissionsNeeded.size() > 0)
{
// Need Rationale
Log.i("MainMenuActivity", "Need to request external storage permissions.");

String message = "You need to grant access to " + permissionsNeeded.get(0);

for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);

showMessageOKCancel(message,
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (which == AlertDialog.BUTTON_POSITIVE)
{
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);

Log.i("MainMenuActivity", "User accepted request for external storage permissions.");
}
}
});
}
else
{
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);

Log.i("MainMenuActivity", "Requested external storage permissions.");
}
}
}

if (!checkPermissions)
{
finalStartup();
}
}
final int REQUEST_CODE_START = 120;

public void finalStartup()
{
Expand All @@ -132,33 +40,6 @@ public void finalStartup()
finish();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
switch (requestCode)
{
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
for (int i = 0; i < permissions.length; i++)
{
if(grantResults[i] == PackageManager.PERMISSION_GRANTED)
{
Log.i("MainMenuActivity", "Permission: " + permissions[i] + " was granted.");
}
else
{
Log.i("MainMenuActivity", "Permission: " + permissions[i] + " was not granted.");
}
}

break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}

finalStartup();
}

public static void startRetroActivity(Intent retro, String contentPath, String corePath,
String configFilePath, String imePath, String dataDirPath, String dataSourcePath)
{
Expand All @@ -170,11 +51,19 @@ public static void startRetroActivity(Intent retro, String contentPath, String c
retro.putExtra("IME", imePath);
retro.putExtra("DATADIR", dataDirPath);
retro.putExtra("APK", dataSourcePath);
retro.putExtra("SDCARD", Environment.getExternalStorageDirectory().getAbsolutePath());
String external = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + PACKAGE_NAME + "/files";
retro.putExtra("SDCARD", external);
retro.putExtra("EXTERNAL", external);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_CODE_START) {
finalStartup();
}
}

@Override
public void onCreate(Bundle savedInstanceState)
{
Expand All @@ -187,6 +76,7 @@ public void onCreate(Bundle savedInstanceState)

UserPreferences.updateConfigFile(this);

checkRuntimePermissions();
Intent i = new Intent(this, MigrateRetroarchFolderActivity.class);
startActivityForResult(i, REQUEST_CODE_START);
}
}