Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 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 <vector> |
| 23 | #include <string> |
| 24 | #include <sstream> |
| 25 | |
| 26 | extern "C" { |
| 27 | |
| 28 | // Convert Intents to arg list, returning argc and argv |
| 29 | // Note that this C routine mallocs memory that the caller must free |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 30 | char **get_args(struct android_app *app, const char *intent_extra_data_key, const char *appTag, int *count) { |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 31 | std::vector<std::string> args; |
| 32 | JavaVM &vm = *app->activity->vm; |
| 33 | JNIEnv *p_env; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 34 | if (vm.AttachCurrentThread(&p_env, nullptr) != JNI_OK) return nullptr; |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 35 | |
| 36 | JNIEnv &env = *p_env; |
| 37 | jobject activity = app->activity->clazz; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 38 | jmethodID get_intent_method = env.GetMethodID(env.GetObjectClass(activity), "getIntent", "()Landroid/content/Intent;"); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 39 | jobject intent = env.CallObjectMethod(activity, get_intent_method); |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 40 | jmethodID get_string_extra_method = |
| 41 | env.GetMethodID(env.GetObjectClass(intent), "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;"); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 42 | jvalue get_string_extra_args; |
| 43 | get_string_extra_args.l = env.NewStringUTF(intent_extra_data_key); |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 44 | jstring extra_str = static_cast<jstring>(env.CallObjectMethodA(intent, get_string_extra_method, &get_string_extra_args)); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 45 | |
| 46 | std::string args_str; |
| 47 | if (extra_str) { |
| 48 | const char *extra_utf = env.GetStringUTFChars(extra_str, nullptr); |
| 49 | args_str = extra_utf; |
| 50 | env.ReleaseStringUTFChars(extra_str, extra_utf); |
| 51 | env.DeleteLocalRef(extra_str); |
| 52 | } |
| 53 | |
| 54 | env.DeleteLocalRef(get_string_extra_args.l); |
| 55 | env.DeleteLocalRef(intent); |
| 56 | vm.DetachCurrentThread(); |
| 57 | |
| 58 | // split args_str |
| 59 | std::stringstream ss(args_str); |
| 60 | std::string arg; |
| 61 | while (std::getline(ss, arg, ' ')) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 62 | if (!arg.empty()) args.push_back(arg); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Convert our STL results to C friendly constructs |
| 66 | assert(count != nullptr); |
| 67 | *count = args.size() + 1; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 68 | char **vector = (char **)malloc(*count * sizeof(char *)); |
| 69 | const char *appName = appTag ? appTag : (const char *)"appTag"; |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 70 | |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 71 | vector[0] = (char *)malloc(strlen(appName) * sizeof(char)); |
| 72 | strcpy(vector[0], appName); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 73 | |
| 74 | for (uint32_t i = 0; i < args.size(); i++) { |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 75 | vector[i + 1] = (char *)malloc(strlen(args[i].c_str()) * sizeof(char)); |
Cody Northrop | db593ad | 2016-09-26 21:05:00 -0600 | [diff] [blame] | 76 | strcpy(vector[i + 1], args[i].c_str()); |
| 77 | } |
| 78 | |
| 79 | return vector; |
| 80 | } |
| 81 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 82 | } // extern "C" |