blob: 1480f44bdada5a27ad4f42bf6f423494347110cc [file] [log] [blame]
Cody Northropdb593ad2016-09-26 21:05:00 -06001/*
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
26extern "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 Lobodzinski729a8d32017-01-26 12:16:30 -070030char **get_args(struct android_app *app, const char *intent_extra_data_key, const char *appTag, int *count) {
Cody Northropdb593ad2016-09-26 21:05:00 -060031 std::vector<std::string> args;
32 JavaVM &vm = *app->activity->vm;
33 JNIEnv *p_env;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070034 if (vm.AttachCurrentThread(&p_env, nullptr) != JNI_OK) return nullptr;
Cody Northropdb593ad2016-09-26 21:05:00 -060035
36 JNIEnv &env = *p_env;
37 jobject activity = app->activity->clazz;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070038 jmethodID get_intent_method = env.GetMethodID(env.GetObjectClass(activity), "getIntent", "()Landroid/content/Intent;");
Cody Northropdb593ad2016-09-26 21:05:00 -060039 jobject intent = env.CallObjectMethod(activity, get_intent_method);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070040 jmethodID get_string_extra_method =
41 env.GetMethodID(env.GetObjectClass(intent), "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
Cody Northropdb593ad2016-09-26 21:05:00 -060042 jvalue get_string_extra_args;
43 get_string_extra_args.l = env.NewStringUTF(intent_extra_data_key);
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070044 jstring extra_str = static_cast<jstring>(env.CallObjectMethodA(intent, get_string_extra_method, &get_string_extra_args));
Cody Northropdb593ad2016-09-26 21:05:00 -060045
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 Lobodzinski64318ba2017-01-26 13:34:13 -070062 if (!arg.empty()) args.push_back(arg);
Cody Northropdb593ad2016-09-26 21:05:00 -060063 }
64
65 // Convert our STL results to C friendly constructs
66 assert(count != nullptr);
67 *count = args.size() + 1;
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070068 char **vector = (char **)malloc(*count * sizeof(char *));
69 const char *appName = appTag ? appTag : (const char *)"appTag";
Cody Northropdb593ad2016-09-26 21:05:00 -060070
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070071 vector[0] = (char *)malloc(strlen(appName) * sizeof(char));
72 strcpy(vector[0], appName);
Cody Northropdb593ad2016-09-26 21:05:00 -060073
74 for (uint32_t i = 0; i < args.size(); i++) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -070075 vector[i + 1] = (char *)malloc(strlen(args[i].c_str()) * sizeof(char));
Cody Northropdb593ad2016-09-26 21:05:00 -060076 strcpy(vector[i + 1], args[i].c_str());
77 }
78
79 return vector;
80}
81
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070082} // extern "C"