Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: examples/device_name/device_name.cc

Issue 772393003: Android handler: inject the application loading part from the shell .so. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/shell/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is an example of an Android Mojo app that is composed of native code and 5 // This is an example of an Android Mojo app that is composed of native code and
6 // Java code that the native code calls into. In particular, we call to Java 6 // Java code that the native code calls into. In particular, we call to Java
7 // upon entering MojoMain to retrieve the name of the device that the 7 // upon entering MojoMain to retrieve the name of the device that the
8 // application is running on and print it out to the log. 8 // application is running on and print it out to the log.
9 // 9 //
10 // To run the example: 10 // To run the example:
11 // - build and install the MojoShell on the device: 11 // - build and install the MojoShell on the device:
12 // ninja -C some_place mojo_shell_apk 12 // ninja -C some_place mojo_shell_apk
13 // adb install -r some_place/apks/MojoShell.apk 13 // adb install -r some_place/apks/MojoShell.apk
14 // - install forwarder on the device: 14 // - install forwarder on the device:
15 // ninja -C some_place forwarder 15 // ninja -C some_place forwarder
16 // adb push some_place/forwarder /data/tmp/forwarder 16 // adb push some_place/forwarder /data/tmp/forwarder
17 // - run the forwarder on the device: 17 // - run the forwarder on the device:
18 // adb shell /data/tmp/forwarder 4444:4444 18 // adb shell /data/tmp/forwarder 4444:4444
19 // - build the example: 19 // - build the example:
20 // ninja -C some_place examples/device_name 20 // ninja -C some_place examples/device_name
21 // - set up the http server in your output directory: 21 // - set up the http server in your output directory:
22 // python -m SimpleHTTPServer 4444 22 // python -m SimpleHTTPServer 4444
23 // - build/android/adb_run_mojo_shell 23 // - build/android/adb_run_mojo_shell
24 // http://127.0.0.1:4444/obj/examples/device_name/device_name.mojo 24 // http://127.0.0.1:4444/obj/examples/device_name/device_name.mojo
25 25
26 #include "base/android/jni_string.h" 26 #include "base/android/jni_string.h"
27 #include "jni/DeviceName_jni.h" 27 #include "jni/DeviceName_jni.h"
28 #include "mojo/public/c/system/main.h" 28 #include "mojo/public/c/system/main.h"
29 #include "mojo/public/cpp/system/core.h"
29 30
30 namespace mojo { 31 namespace mojo {
31 32
32 namespace examples { 33 namespace examples {
33 34
34 bool RegisterDeviceJni(JNIEnv* env) { 35 bool RegisterDeviceJni(JNIEnv* env) {
35 return RegisterNativesImpl(env); 36 return RegisterNativesImpl(env);
36 } 37 }
37 38
38 // Simplest example of a function that needs to call into Java. 39 // Simplest example of a function that needs to call into Java.
39 std::string GetDeviceName() { 40 std::string GetDeviceName() {
40 JNIEnv* env = base::android::AttachCurrentThread(); 41 JNIEnv* env = base::android::AttachCurrentThread();
41 return base::android::ConvertJavaStringToUTF8( 42 return base::android::ConvertJavaStringToUTF8(
42 env, Java_DeviceName_getName(env).obj()); 43 env, Java_DeviceName_getName(env).obj());
43 } 44 }
44 45
45 // Example of a function that need to access the application context - see 46 // Example of a function that need to access the application context - see
46 // InitApplicationContext() below. 47 // InitApplicationContext() below.
47 std::string GetApplicationClassName() { 48 std::string GetApplicationClassName() {
48 JNIEnv* env = base::android::AttachCurrentThread(); 49 JNIEnv* env = base::android::AttachCurrentThread();
49 jobject context = base::android::GetApplicationContext(); 50 jobject context = base::android::GetApplicationContext();
50 return base::android::ConvertJavaStringToUTF8( 51 return base::android::ConvertJavaStringToUTF8(
51 env, Java_DeviceName_getApplicationClassName(env, context).obj()); 52 env, Java_DeviceName_getApplicationClassName(env, context).obj());
52 } 53 }
53 54
54 } // namespace examples 55 } // namespace examples
55 } // namespace mojo 56 } // namespace mojo
56 57
57 MojoResult MojoMain(MojoHandle shell_handle) { 58 MojoResult MojoMain(MojoHandle shell_handle) {
59 // Call a Java function to demonstrate that the JNI was correctly set up.
58 LOG(INFO) << "Device name: " << mojo::examples::GetDeviceName(); 60 LOG(INFO) << "Device name: " << mojo::examples::GetDeviceName();
61
62 // Call a function that uses the application context to demonstrate that it
63 // was properly set.
59 LOG(INFO) << "Application class: " 64 LOG(INFO) << "Application class: "
60 << mojo::examples::GetApplicationClassName(); 65 << mojo::examples::GetApplicationClassName();
66
67 // Call a Mojo core function to demonstrate that the thunks were properly set.
68 MojoTimeTicks ticks = MojoGetTimeTicksNow();
69 LOG(INFO) << "Timeticks: " << ticks;
70
61 return MOJO_RESULT_OK; 71 return MOJO_RESULT_OK;
62 } 72 }
63 73
64 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 74 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
65 base::android::InitVM(vm); 75 base::android::InitVM(vm);
66 JNIEnv* env = base::android::AttachCurrentThread(); 76 JNIEnv* env = base::android::AttachCurrentThread();
67 77
68 if (!mojo::examples::RegisterDeviceJni(env)) 78 if (!mojo::examples::RegisterDeviceJni(env))
69 return -1; 79 return -1;
70 80
71 return JNI_VERSION_1_4; 81 return JNI_VERSION_1_4;
72 } 82 }
73 83
74 // This is needed only if the application actually needs to access the 84 // This is needed only if the application actually needs to access the
75 // application context. For instance, GetDeviceName() itself doesn't need it, 85 // application context. For instance, GetDeviceName() itself doesn't need it,
76 // but we use it for demonstrative purposes in GetApplicationClassName(). 86 // but we use it for demonstrative purposes in GetApplicationClassName().
77 extern "C" JNI_EXPORT void InitApplicationContext( 87 extern "C" JNI_EXPORT void InitApplicationContext(
78 const base::android::JavaRef<jobject>& context) { 88 const base::android::JavaRef<jobject>& context) {
79 JNIEnv* env = base::android::AttachCurrentThread(); 89 JNIEnv* env = base::android::AttachCurrentThread();
80 base::android::InitApplicationContext(env, context); 90 base::android::InitApplicationContext(env, context);
81 } 91 }
OLDNEW
« no previous file with comments | « no previous file | mojo/shell/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698