Merge pull request #14488 from Simonx22/android/convert-permissionshandler-to-kotlin
Android: Convert PermissionsHandler to Kotlin
This commit is contained in:
2 files changed
+84
-81
No files matched your search
-81
@@ -1,81 +0,0 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
||||||
|
|
||||||
package org.dolphinemu.dolphinemu.utils;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Environment;
|
|
||||||
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
|
||||||
|
|
||||||
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
|
|
||||||
import static android.Manifest.permission.RECORD_AUDIO;
|
|
||||||
|
|
||||||
import org.dolphinemu.dolphinemu.R;
|
|
||||||
import org.dolphinemu.dolphinemu.DolphinApplication;
|
|
||||||
import org.dolphinemu.dolphinemu.NativeLibrary;
|
|
||||||
|
|
||||||
public class PermissionsHandler
|
|
||||||
{
|
|
||||||
public static final int REQUEST_CODE_WRITE_PERMISSION = 500;
|
|
||||||
public static final int REQUEST_CODE_RECORD_AUDIO = 501;
|
|
||||||
private static boolean sWritePermissionDenied = false;
|
|
||||||
|
|
||||||
public static void requestWritePermission(final FragmentActivity activity)
|
|
||||||
{
|
|
||||||
activity.requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE},
|
|
||||||
REQUEST_CODE_WRITE_PERMISSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasWriteAccess(Context context)
|
|
||||||
{
|
|
||||||
if (!isExternalStorageLegacy())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int hasWritePermission = ContextCompat.checkSelfPermission(context, WRITE_EXTERNAL_STORAGE);
|
|
||||||
return hasWritePermission == PackageManager.PERMISSION_GRANTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isExternalStorageLegacy()
|
|
||||||
{
|
|
||||||
return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || Environment.isExternalStorageLegacy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setWritePermissionDenied()
|
|
||||||
{
|
|
||||||
sWritePermissionDenied = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isWritePermissionDenied()
|
|
||||||
{
|
|
||||||
return sWritePermissionDenied;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasRecordAudioPermission(Context context)
|
|
||||||
{
|
|
||||||
if (context == null)
|
|
||||||
context = DolphinApplication.getAppContext();
|
|
||||||
int hasRecordPermission = ContextCompat.checkSelfPermission(context, RECORD_AUDIO);
|
|
||||||
return hasRecordPermission == PackageManager.PERMISSION_GRANTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void requestRecordAudioPermission(Activity activity)
|
|
||||||
{
|
|
||||||
if (activity == null)
|
|
||||||
{
|
|
||||||
// Calling from C++ code
|
|
||||||
activity = DolphinApplication.getAppActivity();
|
|
||||||
// Since the emulation (and cubeb) has already started, enabling the microphone permission
|
|
||||||
// now might require restarting the game to be effective. Warn the user about it.
|
|
||||||
NativeLibrary.displayAlertMsg(
|
|
||||||
activity.getString(R.string.wii_speak_permission_warning),
|
|
||||||
activity.getString(R.string.wii_speak_permission_warning_description),
|
|
||||||
false, true, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
activity.requestPermissions(new String[]{RECORD_AUDIO}, REQUEST_CODE_RECORD_AUDIO);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+84
@@ -0,0 +1,84 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
package org.dolphinemu.dolphinemu.utils
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
|
import android.os.Environment
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import org.dolphinemu.dolphinemu.DolphinApplication
|
||||||
|
import org.dolphinemu.dolphinemu.NativeLibrary
|
||||||
|
import org.dolphinemu.dolphinemu.R
|
||||||
|
|
||||||
|
object PermissionsHandler {
|
||||||
|
const val REQUEST_CODE_WRITE_PERMISSION = 500
|
||||||
|
const val REQUEST_CODE_RECORD_AUDIO = 501
|
||||||
|
|
||||||
|
private var writePermissionDenied = false
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun requestWritePermission(activity: FragmentActivity) {
|
||||||
|
activity.requestPermissions(
|
||||||
|
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE_WRITE_PERMISSION
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun hasWriteAccess(context: Context): Boolean {
|
||||||
|
if (!isExternalStorageLegacy()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
val hasWritePermission =
|
||||||
|
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
|
return hasWritePermission == PackageManager.PERMISSION_GRANTED
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun isExternalStorageLegacy(): Boolean {
|
||||||
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || Environment.isExternalStorageLegacy()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun setWritePermissionDenied() {
|
||||||
|
writePermissionDenied = true
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun isWritePermissionDenied(): Boolean {
|
||||||
|
return writePermissionDenied
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun hasRecordAudioPermission(context: Context?): Boolean {
|
||||||
|
val nonNullContext = context ?: DolphinApplication.getAppContext()
|
||||||
|
val hasRecordPermission =
|
||||||
|
ContextCompat.checkSelfPermission(nonNullContext, Manifest.permission.RECORD_AUDIO)
|
||||||
|
return hasRecordPermission == PackageManager.PERMISSION_GRANTED
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun requestRecordAudioPermission(activity: Activity?) {
|
||||||
|
val targetActivity = activity ?: DolphinApplication.getAppActivity()!!
|
||||||
|
if (activity == null) {
|
||||||
|
// Calling from C++ code
|
||||||
|
// Since the emulation (and cubeb) has already started, enabling the microphone permission
|
||||||
|
// now might require restarting the game to be effective. Warn the user about it.
|
||||||
|
NativeLibrary.displayAlertMsg(
|
||||||
|
targetActivity.getString(R.string.wii_speak_permission_warning),
|
||||||
|
targetActivity.getString(R.string.wii_speak_permission_warning_description),
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetActivity.requestPermissions(
|
||||||
|
arrayOf(Manifest.permission.RECORD_AUDIO), REQUEST_CODE_RECORD_AUDIO
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user