Skip to content

Commit

Permalink
Merge pull request #1805 from simplepeng/master
Browse files Browse the repository at this point in the history
Fix VibrateUtils can not vibrate in background
  • Loading branch information
Blankj authored May 11, 2024
2 parents 51c8124 + c7ebceb commit 7b4caf9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/groovy/Config.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Config {
lib_base : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/base"),
lib_common : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/common"),
lib_subutil : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/subutil"),
lib_utilcode : new ModuleConfig(isApply: true , useLocal: false, localPath: "./lib/utilcode", remotePath: "com.blankj:utilcodex:$Config.versionName"),
lib_utilcode : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/utilcode", remotePath: "com.blankj:utilcodex:$Config.versionName"),
lib_utildebug : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/utildebug"),
lib_utildebug_no_op : new ModuleConfig(isApply: true , useLocal: true , localPath: "./lib/utildebug-no-op"),
/*Don't delete this line*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,35 @@ class VibrateActivity : CommonActivity() {

override fun bindItems(): MutableList<CommonItem<*>> {
return CollectionUtils.newArrayList(
CommonItemClick(R.string.vibrate_1000ms) { VibrateUtils.vibrate(1000) },
CommonItemClick(R.string.vibrate_1000ms) {
VibrateUtils.vibrate(1000)
},
CommonItemClick(R.string.vibrate_custom) {
VibrateUtils.vibrate(longArrayOf(0, 1000, 1000, 2000, 2000, 1000), 1)
},
CommonItemClick(R.string.vibrate_cancel) { VibrateUtils.cancel() }
CommonItemClick(R.string.vibrate_background) {
backHome()
mContentView.postDelayed({
// VibrateUtils.vibrate(1000) -- can not vibrate in background
VibrateUtils.vibrateCompat(longArrayOf(0, 1000, 1000, 2000, 2000, 1000), 1)
// VibrateUtils.vibrateCompat(1000)
}, 1000)
},
CommonItemClick(R.string.vibrate_cancel) {
VibrateUtils.cancel()
}
)
}

override fun onDestroy() {
super.onDestroy()
VibrateUtils.cancel()
}

private fun backHome() {
val intent = Intent(Intent.ACTION_MAIN).apply {
addCategory(Intent.CATEGORY_HOME)
}
startActivity(intent)
}
}
1 change: 1 addition & 0 deletions feature/utilcode/pkg/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,6 @@
<!--Vibrate 相关-->
<string name="vibrate_1000ms">Vibrate 1000ms</string>
<string name="vibrate_custom">Vibrate Custom</string>
<string name="vibrate_background">Vibrate Background</string>
<string name="vibrate_cancel">Cancel</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.blankj.utilcode.util;

import android.content.Context;
import android.media.AudioAttributes;
import android.os.Build;
import android.os.Vibrator;

import androidx.annotation.RequiresApi;
import androidx.annotation.RequiresPermission;

import static android.Manifest.permission.VIBRATE;
Expand Down Expand Up @@ -35,6 +39,39 @@ public static void vibrate(final long milliseconds) {
vibrator.vibrate(milliseconds);
}

/**
* Vibrate.
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
*
* @param milliseconds The number of milliseconds to vibrate.
* @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
* specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
* {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
* vibrations associated with incoming calls.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@RequiresPermission(VIBRATE)
public static void vibrate(final long milliseconds, AudioAttributes attributes) {
Vibrator vibrator = getVibrator();
if (vibrator == null) return;
vibrator.vibrate(milliseconds, attributes);
}

/**
* VibrateCompat - Can vibrate in background
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
*
* @param milliseconds he number of milliseconds to vibrate.
*/
@RequiresPermission(VIBRATE)
public static void vibrateCompat(final long milliseconds) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
vibrate(milliseconds, getAudioAttributes());
} else {
vibrate(milliseconds);
}
}

/**
* Vibrate.
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
Expand All @@ -49,6 +86,41 @@ public static void vibrate(final long[] pattern, final int repeat) {
vibrator.vibrate(pattern, repeat);
}

/**
* Vibrate.
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
*
* @param pattern An array of longs of times for which to turn the vibrator on or off.
* @param repeat The index into pattern at which to repeat, or -1 if you don't want to repeat.
* @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
* specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
* {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
* vibrations associated with incoming calls.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@RequiresPermission(VIBRATE)
public static void vibrate(final long[] pattern, final int repeat, AudioAttributes attributes) {
Vibrator vibrator = getVibrator();
if (vibrator == null) return;
vibrator.vibrate(pattern, repeat, attributes);
}

/**
* VibrateCompat - Can vibrate in background
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
*
* @param pattern An array of longs of times for which to turn the vibrator on or off.
* @param repeat The index into pattern at which to repeat, or -1 if you don't want to repeat.
*/
@RequiresPermission(VIBRATE)
public static void vibrateCompat(final long[] pattern, final int repeat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
vibrate(pattern, repeat, getAudioAttributes());
} else {
vibrate(pattern, repeat);
}
}

/**
* Cancel vibrate.
* <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
Expand All @@ -66,4 +138,11 @@ private static Vibrator getVibrator() {
}
return vibrator;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static AudioAttributes getAudioAttributes() {
return new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
}
}

0 comments on commit 7b4caf9

Please sign in to comment.