Skip to content

Commit

Permalink
[Cloud]Native android fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Dec 16, 2023
1 parent ba77dcd commit 14af909
Show file tree
Hide file tree
Showing 5 changed files with 3,621 additions and 7 deletions.
56 changes: 52 additions & 4 deletions src/cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const char* se_get_pref_path();
#include <emscripten.h>
#endif

#ifdef SE_PLATFORM_ANDROID
#include <android/native_activity.h>
extern "C" const void* sapp_android_get_native_activity();
#endif

struct file_metadata_t
{
std::string id;
Expand Down Expand Up @@ -245,6 +250,32 @@ enum class http_request_e
PATCH,
};

#ifdef SE_PLATFORM_ANDROID
const char* get_ca_path()
{
static std::string ca_path;
if (ca_path.empty())
{
ANativeActivity* activity = (ANativeActivity*)sapp_android_get_native_activity();
JavaVM* pJavaVM = activity->vm;
JNIEnv* pJNIEnv = activity->env;
jint nResult = pJavaVM->AttachCurrentThread(&pJNIEnv, NULL);
if (nResult != JNI_ERR)
{
jobject nativeActivity = activity->clazz;
jclass ClassNativeActivity = pJNIEnv->GetObjectClass(nativeActivity);
jmethodID MethodGetCertificatePath = pJNIEnv->GetMethodID(ClassNativeActivity, "getCertificatePath", "()Ljava/lang/String;");
jstring jstrCertificatePath = (jstring)pJNIEnv->CallObjectMethod(nativeActivity, MethodGetCertificatePath);
const char* strCertificatePath = pJNIEnv->GetStringUTFChars(jstrCertificatePath, NULL);
ca_path = std::string(strCertificatePath);
pJNIEnv->ReleaseStringUTFChars(jstrCertificatePath, strCertificatePath);
pJavaVM->DetachCurrentThread();
}
}
return ca_path.c_str();
}
#endif

// Abstraction layer for http requests
void https_request(http_request_e type, const std::string& url, const std::string& body,
const std::vector<std::pair<std::string, std::string>>& headers,
Expand All @@ -263,6 +294,9 @@ void https_request(http_request_e type, const std::string& url, const std::strin
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&result);
#ifdef SE_PLATFORM_ANDROID
curl_easy_setopt(curl, CURLOPT_CAINFO, get_ca_path());
#endif

switch (type)
{
Expand Down Expand Up @@ -616,7 +650,7 @@ void cloud_drive_authenticate(cloud_drive_t* drive)
{
#ifdef EMSCRIPTEN
return em_oath_sign_in(drive, GOOGLE_CLIENT_ID_WEB);
#elif !defined(SE_PLATFORM_ANDROID) && !defined(SE_PLATFORM_IOS)
#elif !defined(SE_PLATFORM_IOS)
srand(time(NULL));
const char* charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::string code_verifier;
Expand All @@ -634,15 +668,29 @@ void cloud_drive_authenticate(cloud_drive_t* drive)
"&code_challenge=" +
code_verifier + "&code_challenge_method=plain";

#ifdef __linux__
#ifdef SE_PLATFORM_LINUX
std::string command = "xdg-open \"" + request + "\"";
system(command.c_str());
#elif _WIN32 || _WIN64
#elif SE_PLATFORM_WINDOWS
std::string command = "start \"\" \"" + request + "\"";
system(command.c_str());
#elif __APPLE__
#elif SE_PLATFORM_MACOS
std::string command = "open \"" + request + "\"";
system(command.c_str());
#elif SE_PLATFORM_ANDROID
ANativeActivity* activity = (ANativeActivity*)sapp_android_get_native_activity();
JavaVM* pJavaVM = activity->vm;
JNIEnv* pJNIEnv = activity->env;
jint nResult = pJavaVM->AttachCurrentThread(&pJNIEnv, NULL);
if (nResult != JNI_ERR)
{
jobject nativeActivity = activity->clazz;
jclass ClassNativeActivity = pJNIEnv->GetObjectClass(nativeActivity);
jmethodID MethodOpenURL = pJNIEnv->GetMethodID(ClassNativeActivity, "openURL", "(Ljava/lang/String;)V");
jstring jstrURL = pJNIEnv->NewStringUTF(request.c_str());
pJNIEnv->CallVoidMethod(nativeActivity, MethodOpenURL, jstrURL);
pJavaVM->DetachCurrentThread();
}
#else
printf("Navigate to the following URL to authorize the application:\n%s\n", request.c_str());
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4269,7 +4269,7 @@ void se_android_get_language(char* language_buffer, size_t buffer_size){
// Retrieves NativeActivity.
jobject nativeActivity = activity->clazz;
jclass ClassNativeActivity = (*pJNIEnv)->GetObjectClass(pJNIEnv, nativeActivity );
jmethodID getLanguageMethod= (*pJNIEnv)->GetMethodID(pJNIEnv, ClassNativeActivity, "getLanguage", "()Ljava/lang/String;" );
jmethodID getLanguageMethod= (*pJNIEnv)->GetStaticMethodID(pJNIEnv, ClassNativeActivity, "getLanguage", "()Ljava/lang/String;" );
if(getLanguageMethod) {
jstring joStringPropVal = (jstring) (*pJNIEnv)->CallStaticObjectMethod(pJNIEnv,ClassNativeActivity,getLanguageMethod);
const jchar *jcVal = (*pJNIEnv)->GetStringUTFChars(pJNIEnv, joStringPropVal, JNI_FALSE);
Expand Down Expand Up @@ -4338,7 +4338,7 @@ void se_android_poll_events(bool visible){
jclass ClassNativeActivity = (*pJNIEnv)->GetObjectClass(pJNIEnv, nativeActivity);
jmethodID getEvent= (*pJNIEnv)->GetMethodID(pJNIEnv, ClassNativeActivity, "getEvent", "()I" );
jmethodID pollKeyboard= (*pJNIEnv)->GetMethodID(pJNIEnv, ClassNativeActivity, "pollKeyboard", "()V" );
(*pJNIEnv)->CallIntMethod(pJNIEnv, nativeActivity, pollKeyboard );
(*pJNIEnv)->CallVoidMethod(pJNIEnv, nativeActivity, pollKeyboard );
ImGuiIO* io= igGetIO();

io->KeysDown[io->KeyMap[ImGuiKey_Backspace]]=false;
Expand Down
1 change: 1 addition & 0 deletions tools/android_project/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.sky.SkyEmu">
<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-feature android:glEsVersion="0x00030000" android:required="true" />
<application
android:requestLegacyExternalStorage="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class EnhancedNativeActivity extends NativeActivity {
public View mRootView;
private Vector<Integer> keyboardEvents;
private boolean first_event;
private String certificatePath;

static {
System.loadLibrary("SkyEmu");
Expand All @@ -60,7 +61,7 @@ public float getDPIScale(){
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
return metrics.xdpi/120.0f;
}
public String getLanguage() {
public static String getLanguage() {
return Locale.getDefault().toString();
}
/*Handle permission request results*/
Expand Down Expand Up @@ -375,6 +376,36 @@ public void loadURI(Uri selectedFileUri, boolean is_rom){
}
}
}
public void openURL(String url){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
public String getCertificatePath(){
if (certificatePath != null) {
return certificatePath;
}
File file = new File(getFilesDir(), "cacert.pem");
if (file.exists()) {
certificatePath = file.getAbsolutePath();
return certificatePath;
}
InputStream caInput = getResources().openRawResource(R.raw.cacert);
try {
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = caInput.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
caInput.close();
} catch (Exception e) {
e.printStackTrace();
}
certificatePath = file.getAbsolutePath();
return certificatePath;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// If the selection didn't work
Expand Down
Loading

0 comments on commit 14af909

Please sign in to comment.