Android: Add support to copy strings to clipboard

This commit is contained in:
Henrik Rydgård 2024-09-02 23:04:40 +02:00
parent d95ae30717
commit 9a193261f4
3 changed files with 23 additions and 0 deletions

View File

@ -30,6 +30,7 @@
#include "Common/File/FileUtil.h"
#include "Common/StringUtils.h"
#include "Common/System/System.h"
#include "Common/System/OSD.h"
#include "Common/System/Request.h"
#include "Common/System/NativeApp.h"
#include "Core/Config.h"
@ -161,6 +162,8 @@ void GameScreen::CreateViews() {
char buffer[16];
snprintf(buffer, sizeof(buffer), "%08X", crc);
System_CopyStringToClipboard(buffer);
// Success indication. Not worth a translatable string.
g_OSD.Show(OSDType::MESSAGE_SUCCESS, buffer, 1.0f);
return UI::EVENT_DONE;
});
tvCRCCopy_->SetVisibility(crcVisibility);

View File

@ -499,6 +499,8 @@ bool System_GetPropertyBool(SystemProperty prop) {
case SYSPROP_SUPPORTS_SUSTAINED_PERF_MODE:
return sustainedPerfSupported; // 7.0 introduced sustained performance mode as an optional feature.
case SYSPROP_HAS_TEXT_INPUT_DIALOG:
return androidVersion >= 11; // honeycomb
case SYSPROP_HAS_TEXT_CLIPBOARD:
return true;
case SYSPROP_HAS_OPEN_DIRECTORY:
return false; // We have this implemented but it may or may not work depending on if a file explorer is installed.
@ -1114,6 +1116,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
case SystemRequestType::RECREATE_ACTIVITY:
PushCommand("recreate", param1);
return true;
case SystemRequestType::COPY_TO_CLIPBOARD:
PushCommand("copy_to_clipboard", param1);
return true;
case SystemRequestType::INPUT_TEXT_MODAL:
{
std::string serialized = StringFromFormat("%d:@:%s:@:%s", requestId, param1.c_str(), param2.c_str());

View File

@ -7,6 +7,8 @@ import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.UiModeManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -1666,10 +1668,23 @@ public abstract class NativeActivity extends Activity {
NativeApp.reportException(e, params);
return false;
}
} else if (command.equals("copy_to_clipboard")) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
copyStringToClipboard(params);
}
} else {
Log.w(TAG, "Unknown string command " + command);
}
return false;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void copyStringToClipboard(String text) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied Text", text);
clipboard.setPrimaryClip(clip);
}
@SuppressLint("NewApi")
@Override
public void recreate() {