OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/android/jni_android.h" |
| 6 #include "base/android/jni_string.h" |
| 7 #include "content/browser/android/gamepad_reader_impl.h" |
| 8 |
| 9 #include "jni/GamepadList_jni.h" |
| 10 |
| 11 using base::android::AttachCurrentThread; |
| 12 using base::android::CheckException; |
| 13 using base::android::ClearException; |
| 14 using base::android::ConvertJavaStringToUTF8; |
| 15 using base::android::ScopedJavaLocalRef; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 bool GamepadsReader::RegisterGamepadsReader(JNIEnv* env) { |
| 20 return RegisterNativesImpl(env); |
| 21 } |
| 22 |
| 23 GamepadsReader::GamepadsReader() { |
| 24 JNIEnv* env = AttachCurrentThread(); |
| 25 CreateJavaObject(env); |
| 26 } |
| 27 |
| 28 GamepadsReader::~GamepadsReader() { |
| 29 CHECK(java_gamepadList_object_.is_null()); |
| 30 } |
| 31 |
| 32 GamepadsReader* GamepadsReader::GetInstance() { |
| 33 return Singleton<GamepadsReader>::get(); |
| 34 } |
| 35 |
| 36 void GamepadsReader::NotifyForGamepadsAccess(bool isaccesspaused) { |
| 37 JNIEnv* env = AttachCurrentThread(); |
| 38 if (!env) |
| 39 return; |
| 40 |
| 41 CHECK(!java_gamepadList_object_.is_null()); |
| 42 Java_GamepadList_notifyForGamepadsAccess(env, |
| 43 java_gamepadList_object_.obj(), |
| 44 isaccesspaused); |
| 45 } |
| 46 |
| 47 int GamepadsReader::UpdateGamepadsCount() { |
| 48 JNIEnv* env = AttachCurrentThread(); |
| 49 if (!env) |
| 50 return 0; |
| 51 |
| 52 CHECK(!java_gamepadList_object_.is_null()); |
| 53 |
| 54 if (!(Java_GamepadList_haveDevicesBeenInteractedWith(env, |
| 55 java_gamepadList_object_.obj()))) |
| 56 return 0; |
| 57 |
| 58 return Java_GamepadList_getDeviceCount(env, |
| 59 java_gamepadList_object_.obj()); |
| 60 } |
| 61 |
| 62 bool GamepadsReader::IsDeviceConnected(int Index) { |
| 63 JNIEnv* env = AttachCurrentThread(); |
| 64 if (!env) |
| 65 return false; |
| 66 |
| 67 CHECK(!java_gamepadList_object_.is_null()); |
| 68 return Java_GamepadList_isDeviceConnected(env, |
| 69 java_gamepadList_object_.obj(), |
| 70 Index); |
| 71 } |
| 72 |
| 73 std::string GamepadsReader::GetDeviceName(int Index) { |
| 74 JNIEnv* env = AttachCurrentThread(); |
| 75 if (!env) |
| 76 return (std::string)0; |
| 77 |
| 78 CHECK(!java_gamepadList_object_.is_null()); |
| 79 |
| 80 ScopedJavaLocalRef<jstring> str = |
| 81 Java_GamepadList_getDeviceName(env, |
| 82 java_gamepadList_object_.obj(), |
| 83 Index); |
| 84 |
| 85 bool ua_overidden = str.obj() != NULL; |
| 86 std::string override; |
| 87 |
| 88 if (override.empty()) |
| 89 override += " "; |
| 90 |
| 91 if (ua_overidden) { |
| 92 override = base::android::ConvertJavaStringToUTF8(str); |
| 93 } |
| 94 |
| 95 return override; |
| 96 } |
| 97 |
| 98 int64 GamepadsReader::GetDeviceTimestamp(int Index) { |
| 99 JNIEnv* env = AttachCurrentThread(); |
| 100 if (!env) |
| 101 return 0; |
| 102 |
| 103 CHECK(!java_gamepadList_object_.is_null()); |
| 104 return Java_GamepadList_getDeviceTimestamp(env, |
| 105 java_gamepadList_object_.obj(), |
| 106 Index); |
| 107 } |
| 108 |
| 109 std::vector<float> GamepadsReader::GetDeviceAxes(int Index) { |
| 110 JNIEnv* env = AttachCurrentThread(); |
| 111 if (!env) |
| 112 return (std::vector<float>)0; |
| 113 |
| 114 CHECK(!java_gamepadList_object_.is_null()); |
| 115 std::vector<float> axes; |
| 116 base::android::JavaFloatArrayToFloatVector( |
| 117 env, |
| 118 Java_GamepadList_getDeviceAxes(env, |
| 119 java_gamepadList_object_.obj(), |
| 120 Index).obj(), |
| 121 &axes); |
| 122 return axes; |
| 123 } |
| 124 |
| 125 std::vector<float> GamepadsReader::GetDeviceButtons(int Index) { |
| 126 JNIEnv* env = AttachCurrentThread(); |
| 127 if (!env) |
| 128 return (std::vector<float>)0; |
| 129 |
| 130 CHECK(!java_gamepadList_object_.is_null()); |
| 131 std::vector<float> buttons; |
| 132 base::android::JavaFloatArrayToFloatVector( |
| 133 env, |
| 134 Java_GamepadList_getDeviceButtons(env, |
| 135 java_gamepadList_object_.obj(), |
| 136 Index).obj(), |
| 137 &buttons); |
| 138 |
| 139 return buttons; |
| 140 } |
| 141 |
| 142 bool GamepadsReader::IsGamepadLayoutKnown(int Index) { |
| 143 JNIEnv* env = AttachCurrentThread(); |
| 144 if (!env) |
| 145 return 0; |
| 146 |
| 147 CHECK(!java_gamepadList_object_.is_null()); |
| 148 return Java_GamepadList_isKnownGamepadLayout(env, |
| 149 java_gamepadList_object_.obj(), |
| 150 Index); |
| 151 } |
| 152 |
| 153 void GamepadsReader::CreateJavaObject(JNIEnv* env) { |
| 154 // Create the Java GamepadsReader object. |
| 155 java_gamepadList_object_.Reset( |
| 156 Java_GamepadList_getInstance(env)); |
| 157 CHECK(!java_gamepadList_object_.is_null()); |
| 158 } |
| 159 |
| 160 } // namespace content |
OLD | NEW |