1 // Copyright 2013 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 "content/browser/vibration/vibration_provider_android.h" 6 7 #include <algorithm> 8 9 #include "base/safe_numerics.h" 10 #include "content/browser/vibration/vibration_message_filter.h" 11 #include "content/common/view_messages.h" 12 #include "jni/VibrationProvider_jni.h" 13 #include "third_party/WebKit/public/platform/WebVibration.h" 14 15 using base::android::AttachCurrentThread; 16 17 namespace content { 18 VibrationProviderAndroid()19VibrationProviderAndroid::VibrationProviderAndroid() { 20 } 21 ~VibrationProviderAndroid()22VibrationProviderAndroid::~VibrationProviderAndroid() { 23 } 24 25 // static Register(JNIEnv * env)26bool VibrationProviderAndroid::Register(JNIEnv* env) { 27 return RegisterNativesImpl(env); 28 } 29 Vibrate(int64 milliseconds)30void VibrationProviderAndroid::Vibrate(int64 milliseconds) { 31 if (j_vibration_provider_.is_null()) { 32 j_vibration_provider_.Reset( 33 Java_VibrationProvider_create( 34 AttachCurrentThread(), 35 base::android::GetApplicationContext())); 36 } 37 Java_VibrationProvider_vibrate(AttachCurrentThread(), 38 j_vibration_provider_.obj(), 39 milliseconds); 40 } 41 CancelVibration()42void VibrationProviderAndroid::CancelVibration() { 43 // If somehow a cancel message is received before this object was 44 // instantiated, it means there is no current vibration anyway. Just return. 45 if (j_vibration_provider_.is_null()) 46 return; 47 48 Java_VibrationProvider_cancelVibration(AttachCurrentThread(), 49 j_vibration_provider_.obj()); 50 } 51 52 // static CreateProvider()53VibrationProvider* VibrationMessageFilter::CreateProvider() { 54 return new VibrationProviderAndroid(); 55 } 56 57 } // namespace content 58