1/* 2 * Copyright (c) 2016 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * Relicensed from the WTFPL (http://www.wtfpl.net/faq/). 17 */ 18 19#include "android_util.h" 20#include <android_native_app_glue.h> 21#include <cassert> 22#include <cstring> 23#include <vector> 24#include <string> 25#include <sstream> 26#include <stdlib.h> 27 28extern "C" { 29 30// Convert Intents to arg list, returning argc and argv 31// Note that this C routine mallocs memory that the caller must free 32char **get_args(struct android_app *app, const char *intent_extra_data_key, const char *appTag, int *count) { 33 std::vector<std::string> args; 34 JavaVM &vm = *app->activity->vm; 35 JNIEnv *p_env; 36 if (vm.AttachCurrentThread(&p_env, nullptr) != JNI_OK) return nullptr; 37 38 JNIEnv &env = *p_env; 39 jobject activity = app->activity->clazz; 40 jmethodID get_intent_method = env.GetMethodID(env.GetObjectClass(activity), "getIntent", "()Landroid/content/Intent;"); 41 jobject intent = env.CallObjectMethod(activity, get_intent_method); 42 jmethodID get_string_extra_method = 43 env.GetMethodID(env.GetObjectClass(intent), "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;"); 44 jvalue get_string_extra_args; 45 get_string_extra_args.l = env.NewStringUTF(intent_extra_data_key); 46 jstring extra_str = static_cast<jstring>(env.CallObjectMethodA(intent, get_string_extra_method, &get_string_extra_args)); 47 48 std::string args_str; 49 if (extra_str) { 50 const char *extra_utf = env.GetStringUTFChars(extra_str, nullptr); 51 args_str = extra_utf; 52 env.ReleaseStringUTFChars(extra_str, extra_utf); 53 env.DeleteLocalRef(extra_str); 54 } 55 56 env.DeleteLocalRef(get_string_extra_args.l); 57 env.DeleteLocalRef(intent); 58 vm.DetachCurrentThread(); 59 60 // split args_str 61 std::stringstream ss(args_str); 62 std::string arg; 63 while (std::getline(ss, arg, ' ')) { 64 if (!arg.empty()) args.push_back(arg); 65 } 66 67 // Convert our STL results to C friendly constructs 68 assert(count != nullptr); 69 *count = args.size() + 1; 70 char **vector = (char **)malloc(*count * sizeof(char *)); 71 const char *appName = appTag ? appTag : (const char *)"appTag"; 72 73 vector[0] = (char *)malloc(strlen(appName) * sizeof(char)); 74 strcpy(vector[0], appName); 75 76 for (uint32_t i = 0; i < args.size(); i++) { 77 vector[i + 1] = (char *)malloc(strlen(args[i].c_str()) * sizeof(char)); 78 strcpy(vector[i + 1], args[i].c_str()); 79 } 80 81 return vector; 82} 83 84} // extern "C" 85