Merge changes from topic "KeyCharacterMapRefactor"

* changes:
  Move KeyCharacterMap from RefBase to shared_ptr.
  Move KeyLayoutMap from RefBase to shared_ptr.
This commit is contained in:
TreeHugger Robot
2020-08-31 16:45:54 +00:00
committed by Android (Google) Code Review
4 changed files with 43 additions and 44 deletions

View File

@@ -1,18 +1,18 @@
/*
* Copyright 2006, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
#include <android_runtime/AndroidRuntime.h>
@@ -47,9 +47,8 @@ static struct {
class NativeKeyCharacterMap {
public:
NativeKeyCharacterMap(int32_t deviceId, const sp<KeyCharacterMap>& map) :
mDeviceId(deviceId), mMap(map) {
}
NativeKeyCharacterMap(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map)
: mDeviceId(deviceId), mMap(std::move(map)) {}
~NativeKeyCharacterMap() {
}
@@ -58,26 +57,22 @@ public:
return mDeviceId;
}
inline const sp<KeyCharacterMap>& getMap() const {
return mMap;
}
inline const std::shared_ptr<KeyCharacterMap> getMap() const { return mMap; }
private:
int32_t mDeviceId;
sp<KeyCharacterMap> mMap;
std::shared_ptr<KeyCharacterMap> mMap;
};
jobject android_view_KeyCharacterMap_create(JNIEnv* env, int32_t deviceId,
const sp<KeyCharacterMap>& kcm) {
NativeKeyCharacterMap* map = new NativeKeyCharacterMap(deviceId,
kcm.get() ? kcm : KeyCharacterMap::empty());
if (!map) {
return NULL;
const std::shared_ptr<KeyCharacterMap> kcm) {
NativeKeyCharacterMap* nativeMap = new NativeKeyCharacterMap(deviceId, kcm);
if (!nativeMap) {
return nullptr;
}
return env->NewObject(gKeyCharacterMapClassInfo.clazz, gKeyCharacterMapClassInfo.ctor,
reinterpret_cast<jlong>(map));
reinterpret_cast<jlong>(nativeMap));
}
static jlong nativeReadFromParcel(JNIEnv *env, jobject clazz, jobject parcelObj) {
@@ -91,7 +86,7 @@ static jlong nativeReadFromParcel(JNIEnv *env, jobject clazz, jobject parcelObj)
return 0;
}
sp<KeyCharacterMap> kcm = KeyCharacterMap::readFromParcel(parcel);
std::shared_ptr<KeyCharacterMap> kcm = KeyCharacterMap::readFromParcel(parcel);
if (!kcm.get()) {
return 0;
}
@@ -102,6 +97,9 @@ static jlong nativeReadFromParcel(JNIEnv *env, jobject clazz, jobject parcelObj)
static void nativeWriteToParcel(JNIEnv* env, jobject clazz, jlong ptr, jobject parcelObj) {
NativeKeyCharacterMap* map = reinterpret_cast<NativeKeyCharacterMap*>(ptr);
if (!map->getMap()) {
return;
}
Parcel* parcel = parcelForJavaObject(env, parcelObj);
if (parcel) {
parcel->writeInt32(map->getDeviceId());
@@ -150,9 +148,8 @@ static jchar nativeGetMatch(JNIEnv *env, jobject clazz, jlong ptr, jint keyCode,
return 0;
}
char16_t result = map->getMap()->getMatch(
keyCode, reinterpret_cast<char16_t*>(chars), size_t(numChars),
metaState);
char16_t result = map->getMap()->getMatch(keyCode, reinterpret_cast<char16_t*>(chars),
size_t(numChars), metaState);
env->ReleasePrimitiveArrayCritical(charsArray, chars, JNI_ABORT);
return result;
@@ -180,8 +177,7 @@ static jobjectArray nativeGetEvents(JNIEnv *env, jobject clazz, jlong ptr,
Vector<KeyEvent> events;
jobjectArray result = NULL;
if (map->getMap()->getEvents(map->getDeviceId(),
reinterpret_cast<char16_t*>(chars),
if (map->getMap()->getEvents(map->getDeviceId(), reinterpret_cast<char16_t*>(chars),
size_t(numChars), events)) {
result = env->NewObjectArray(jsize(events.size()), gKeyEventClassInfo.clazz, NULL);
if (result) {

View File

@@ -25,7 +25,7 @@ namespace android {
/* Creates a KeyCharacterMap object from the given information. */
extern jobject android_view_KeyCharacterMap_create(JNIEnv* env, int32_t deviceId,
const sp<KeyCharacterMap>& map);
const std::shared_ptr<KeyCharacterMap> kcm);
} // namespace android

View File

@@ -233,7 +233,8 @@ public:
virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId);
virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices);
virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
const InputDeviceIdentifier& identifier);
virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
jfloatArray matrixArr);
@@ -622,12 +623,12 @@ void NativeInputManager::notifyInputDevicesChanged(const std::vector<InputDevice
checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
}
sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
std::shared_ptr<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
const InputDeviceIdentifier& identifier) {
ATRACE_CALL();
JNIEnv* env = jniEnv();
sp<KeyCharacterMap> result;
std::shared_ptr<KeyCharacterMap> result;
ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
gInputDeviceIdentifierInfo.constructor, descriptor.get(),
@@ -642,8 +643,12 @@ sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
ScopedUtfChars filenameChars(env, filenameObj.get());
ScopedUtfChars contentsChars(env, contentsObj.get());
KeyCharacterMap::loadContents(filenameChars.c_str(),
contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
base::Result<std::shared_ptr<KeyCharacterMap>> ret =
KeyCharacterMap::loadContents(filenameChars.c_str(), contentsChars.c_str(),
KeyCharacterMap::FORMAT_OVERLAY);
if (ret) {
result = *ret;
}
}
checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
return result;

View File

@@ -96,21 +96,19 @@ static bool validateFile(const char* filename) {
return false;
case FILETYPE_KEYLAYOUT: {
sp<KeyLayoutMap> map;
status_t status = KeyLayoutMap::load(filename, &map);
if (status) {
error("Error %d parsing key layout file.\n\n", status);
base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(filename);
if (!ret) {
error("Error %s parsing key layout file.\n\n", ret.error().message().c_str());
return false;
}
break;
}
case FILETYPE_KEYCHARACTERMAP: {
sp<KeyCharacterMap> map;
status_t status = KeyCharacterMap::load(filename,
KeyCharacterMap::FORMAT_ANY, &map);
if (status) {
error("Error %d parsing key character map file.\n\n", status);
base::Result<std::shared_ptr<KeyCharacterMap>> ret = KeyCharacterMap::load(filename,
KeyCharacterMap::FORMAT_ANY);
if (!ret) {
error("Error %s parsing key character map file.\n\n", ret.error().message().c_str());
return false;
}
break;