11/*
2- * Copyright 2013 The Android Open Source Project
2+ * Copyright 2015 The Android Open Source Project
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -25,12 +25,79 @@ extern "C" {
2525JNIEXPORT void Java_com_sample_helper_JUIHelper_JUICallbackHandler (
2626 JNIEnv *env, jobject thiz, int32_t id, int32_t message, int32_t param1,
2727 int32_t param2) {
28- JUIBase *p = (JUIBase *)id;
28+ /*
29+ * id from Java is generated token from native side, which would be mapped back
30+ * into an UI pointer; so id is opaque to Java -- Java receives and returns back
31+ * the same id to native side
32+ */
33+ JUIBase *p = JUIBase::id_factory_.getUIBase (id);
34+ if ( p )
35+ p->DispatchEvent (message, param1, param2);
36+ else {
37+ LOGE (" Failed to get JUIBase pointer for %d in %s" , id, __FILE__);
38+ }
39+ }
40+ }
2941
30- p->DispatchEvent (message, param1, param2);
42+ /*
43+ * IdFactory Implementation: our ID is just a simple integer starting from 1,and
44+ * keeps on counting up. There is no re-use -- even
45+ * one ID is removed from cache, we still counting up
46+ * It will take a long while to overflow a 32 bit integer
47+ * for UI ids. Even it does, it wrap back, at that time,
48+ * previous low values will long gone.
49+ */
50+ int32_t IdFactory::cur_id_ = 0 ;
51+ int32_t IdFactory::getId (const JUIBase* ui_object) {
52+ auto it = ids_.find (ui_object);
53+ if (it == ids_.end ()) {
54+ LOGE (" IDs not inside hash for %p, patching done (%d)" , ui_object, cur_id_);
55+ ids_[ui_object] = ++cur_id_;
56+ return cur_id_;
57+ }
58+ return it->second ;
3159}
60+ JUIBase* IdFactory::getUIBase (int32_t ui_id) {
61+ for (auto id: ids_) {
62+ if (id.second == ui_id) {
63+ return const_cast <JUIBase*>(id.first );
64+ }
65+ }
66+ LOGE (" Unable to find object point for ID(%d, %x)" , ui_id, ui_id);
67+ return nullptr ;
68+ }
69+ bool IdFactory::insert (const JUIBase* ui_object) {
70+ bool status = true ;
71+ auto it = ids_.find (ui_object);
72+ if (it != ids_.end ()) {
73+ LOGE (" Error: %p is already in = %d" , it->first , it->second );
74+ LOGE (" Overwriting %p with %p for it" , it->first , ui_object);
75+ status = false ;
76+ // return status; ---> let it fall through
77+ }
78+ ids_[ui_object] = ++cur_id_;
79+ return status;
80+ }
81+ bool IdFactory::remove (const JUIBase* ui_object) {
82+ auto it = ids_.find (ui_object);
83+ if (it == ids_.end ()) {
84+ LOGE (" IDs not inside hash for %p!!!" , ui_object);
85+ return false ;
86+ }
87+ ids_.erase (it);
88+ return true ;
89+ }
90+ void IdFactory::debugDumpCurrentHashTable (void ) {
91+ for (auto id: ids_) {
92+ LOGI (" Cached ID (%p = %d)" , id.first , id.second );
93+ }
3294}
3395
96+ /*
97+ * Our global instance for id factory
98+ */
99+ IdFactory JUIBase::id_factory_;
100+
34101/*
35102 * JUIView
36103 */
@@ -138,7 +205,11 @@ void JUIView::AddRule(const int32_t layoutParameterIndex,
138205
139206void JUIView::AddRule (const int32_t layoutParameterIndex,
140207 const JUIView *parameter) {
141- AddRule (layoutParameterIndex, (int32_t ) parameter);
208+
209+ /*
210+ * perform pointer to id xlation
211+ */
212+ AddRule (layoutParameterIndex, id_factory_.getId (parameter));
142213}
143214
144215void JUIView::SetLayoutParams (const int32_t width, const int32_t height) {
0 commit comments