// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content.browser;

import android.content.Context;
import android.media.AudioManager;
import android.os.Vibrator;

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;

/**
 * This is the implementation of the C++ counterpart VibrationProvider.
 */
@JNINamespace("content")
class VibrationProvider {

    private final AudioManager mAudioManager;
    private final Vibrator mVibrator;

    @CalledByNative
    private static VibrationProvider create(Context context) {
        return new VibrationProvider(context);
    }

    @CalledByNative
    private void vibrate(long milliseconds) {
        if (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT)
            mVibrator.vibrate(milliseconds);
    }

    @CalledByNative
    private void cancelVibration() {
        mVibrator.cancel();
    }

    private VibrationProvider(Context context) {
        mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }
}

@@ -230,16 +230,58 @@
 ontext;%0A
+import android.content.pm.PackageManager;%0A
 import a
@@ -299,32 +299,32 @@
 a.AudioManager;%0A
-
 import android.o
@@ -334,16 +334,41 @@
 ibrator;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -572,16 +572,75 @@
 ovider %7B
+%0A    private static final String TAG = %22VibrationProvider%22;
 %0A%0A    pr
@@ -716,16 +716,65 @@
 ibrator;
+%0A    private final boolean mHasVibratePermission;
 %0A%0A    @C
@@ -1046,17 +1046,60 @@
 E_SILENT
-)
+ &&%0A                mHasVibratePermission) %7B
 %0A       
@@ -1136,16 +1136,26 @@
 conds);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1209,32 +1209,59 @@
 tion() %7B%0A       
+ if (mHasVibratePermission)
  mVibrator.cance
@@ -1405,24 +1405,24 @@
 O_SERVICE);%0A
-
         mVib
@@ -1488,20 +1488,312 @@
 R_SERVICE);%0A
+        mHasVibratePermission = context.checkCallingOrSelfPermission(%0A                android.Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED;%0A        if (!mHasVibratePermission) %7B%0A            Log.w(TAG, %22Failed to use vibrate API, requires VIBRATE permission.%22);%0A        %7D%0A
     %7D%0A%7D%0A

Fix the crash issue caused by vibrate permission