OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "surface_glue_android.h" | 8 #include "surface_glue_android.h" |
9 | 9 |
10 #include <jni.h> | 10 #include <jni.h> |
(...skipping 28 matching lines...) Expand all Loading... |
39 {AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState}, | 39 {AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState}, |
40 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState}, | 40 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState}, |
41 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState}, | 41 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState}, |
42 }); | 42 }); |
43 | 43 |
44 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { | 44 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { |
45 env->GetJavaVM(&fJavaVM); | 45 env->GetJavaVM(&fJavaVM); |
46 fAndroidApp = env->NewGlobalRef(androidApp); | 46 fAndroidApp = env->NewGlobalRef(androidApp); |
47 jclass cls = env->GetObjectClass(fAndroidApp); | 47 jclass cls = env->GetObjectClass(fAndroidApp); |
48 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); | 48 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); |
| 49 fSetStateMethodID = env->GetMethodID(cls, "setState", "(Ljava/lang/String;)V
"); |
49 fNativeWindow = nullptr; | 50 fNativeWindow = nullptr; |
50 pthread_create(&fThread, nullptr, pthread_main, this); | 51 pthread_create(&fThread, nullptr, pthread_main, this); |
51 } | 52 } |
52 | 53 |
53 SkiaAndroidApp::~SkiaAndroidApp() { | 54 SkiaAndroidApp::~SkiaAndroidApp() { |
54 fPThreadEnv->DeleteGlobalRef(fAndroidApp); | 55 fPThreadEnv->DeleteGlobalRef(fAndroidApp); |
55 if (fWindow) { | 56 if (fWindow) { |
56 fWindow->detach(); | 57 fWindow->detach(); |
57 } | 58 } |
58 if (fNativeWindow) { | 59 if (fNativeWindow) { |
59 ANativeWindow_release(fNativeWindow); | 60 ANativeWindow_release(fNativeWindow); |
60 fNativeWindow = nullptr; | 61 fNativeWindow = nullptr; |
61 } | 62 } |
62 if (fApp) { | 63 if (fApp) { |
63 delete fApp; | 64 delete fApp; |
64 } | 65 } |
65 } | 66 } |
66 | 67 |
67 void SkiaAndroidApp::setTitle(const char* title) const { | 68 void SkiaAndroidApp::setTitle(const char* title) const { |
68 jstring titleString = fPThreadEnv->NewStringUTF(title); | 69 jstring titleString = fPThreadEnv->NewStringUTF(title); |
69 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString); | 70 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString); |
70 fPThreadEnv->DeleteLocalRef(titleString); | 71 fPThreadEnv->DeleteLocalRef(titleString); |
71 } | 72 } |
72 | 73 |
| 74 void SkiaAndroidApp::setUIState(const Json::Value& state) const { |
| 75 jstring jstr = fPThreadEnv->NewStringUTF(state.toStyledString().c_str()); |
| 76 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetStateMethodID, jstr); |
| 77 fPThreadEnv->DeleteLocalRef(jstr); |
| 78 } |
| 79 |
73 void SkiaAndroidApp::postMessage(const Message& message) const { | 80 void SkiaAndroidApp::postMessage(const Message& message) const { |
74 SkDEBUGCODE(auto writeSize =) write(fPipes[1], &message, sizeof(message)); | 81 SkDEBUGCODE(auto writeSize =) write(fPipes[1], &message, sizeof(message)); |
75 SkASSERT(writeSize == sizeof(message)); | 82 SkASSERT(writeSize == sizeof(message)); |
76 } | 83 } |
77 | 84 |
78 void SkiaAndroidApp::readMessage(Message* message) const { | 85 void SkiaAndroidApp::readMessage(Message* message) const { |
79 SkDEBUGCODE(auto readSize =) read(fPipes[0], message, sizeof(Message)); | 86 SkDEBUGCODE(auto readSize =) read(fPipes[0], message, sizeof(Message)); |
80 SkASSERT(readSize == sizeof(Message)); | 87 SkASSERT(readSize == sizeof(Message)); |
81 } | 88 } |
82 | 89 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); | 139 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); |
133 break; | 140 break; |
134 } | 141 } |
135 case kTouched: { | 142 case kTouched: { |
136 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); | 143 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); |
137 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); | 144 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); |
138 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me
ssage.fTouchX, | 145 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me
ssage.fTouchX, |
139 message.fTouchY); | 146 message.fTouchY); |
140 break; | 147 break; |
141 } | 148 } |
| 149 case kUIStateChanged: { |
| 150 skiaAndroidApp->fWindow->onUIStateChanged(*message.stateName, *messa
ge.stateValue); |
| 151 delete message.stateName; |
| 152 delete message.stateValue; |
| 153 break; |
| 154 } |
142 default: { | 155 default: { |
143 // do nothing | 156 // do nothing |
144 } | 157 } |
145 } | 158 } |
146 | 159 |
147 return 1; // continue receiving callbacks | 160 return 1; // continue receiving callbacks |
148 } | 161 } |
149 | 162 |
150 void* SkiaAndroidApp::pthread_main(void* arg) { | 163 void* SkiaAndroidApp::pthread_main(void* arg) { |
151 SkDebugf("pthread_main begins"); | 164 SkDebugf("pthread_main begins"); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 JNIEnv* env, jobject activity, jlong handle, jint owner, jfloat x, jfloat y,
jint state) { | 235 JNIEnv* env, jobject activity, jlong handle, jint owner, jfloat x, jfloat y,
jint state) { |
223 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 236 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
224 Message message(kTouched); | 237 Message message(kTouched); |
225 message.fTouchOwner = owner; | 238 message.fTouchOwner = owner; |
226 message.fTouchState = state; | 239 message.fTouchState = state; |
227 message.fTouchX = x; | 240 message.fTouchX = x; |
228 message.fTouchY = y; | 241 message.fTouchY = y; |
229 skiaAndroidApp->postMessage(message); | 242 skiaAndroidApp->postMessage(message); |
230 } | 243 } |
231 | 244 |
| 245 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onUIStateC
hanged( |
| 246 JNIEnv* env, jobject activity, jlong handle, jstring stateName, jstring stat
eValue) { |
| 247 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 248 Message message(kUIStateChanged); |
| 249 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); |
| 250 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); |
| 251 message.stateName = new SkString(nameChars); |
| 252 message.stateValue = new SkString(valueChars); |
| 253 skiaAndroidApp->postMessage(message); |
| 254 env->ReleaseStringUTFChars(stateName, nameChars); |
| 255 env->ReleaseStringUTFChars(stateValue, valueChars); |
| 256 } |
| 257 |
232 } // namespace sk_app | 258 } // namespace sk_app |
OLD | NEW |