From 19349b9998224f04385c37f643fc96e69faf33bd Mon Sep 17 00:00:00 2001 From: simple <383559698@qq.com> Date: Wed, 24 Apr 2024 19:22:35 +0800 Subject: [PATCH 1/2] update VibrateUtils --- buildSrc/src/main/groovy/Config.groovy | 2 +- .../pkg/feature/vibrate/VibrateActivity.kt | 22 +++++- .../pkg/src/main/res/values/strings.xml | 1 + .../blankj/utilcode/util/VibrateUtils.java | 79 +++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/buildSrc/src/main/groovy/Config.groovy b/buildSrc/src/main/groovy/Config.groovy index a69b8907c7..9a8d69d659 100644 --- a/buildSrc/src/main/groovy/Config.groovy +++ b/buildSrc/src/main/groovy/Config.groovy @@ -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*/ diff --git a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt index d2cd9bc147..ee88dab0fa 100644 --- a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt +++ b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt @@ -32,11 +32,22 @@ class VibrateActivity : CommonActivity() { override fun bindItems(): MutableList> { 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.vibrateCompat(longArrayOf(0, 1000, 1000, 2000, 2000, 1000), 1) +// VibrateUtils.vibrateCompat(1000) + }, 1000) + }, + CommonItemClick(R.string.vibrate_cancel) { + VibrateUtils.cancel() + } ) } @@ -44,4 +55,11 @@ class VibrateActivity : CommonActivity() { super.onDestroy() VibrateUtils.cancel() } + + private fun backHome() { + val intent = Intent(Intent.ACTION_MAIN).apply { + addCategory(Intent.CATEGORY_HOME) + } + startActivity(intent) + } } diff --git a/feature/utilcode/pkg/src/main/res/values/strings.xml b/feature/utilcode/pkg/src/main/res/values/strings.xml index ccec698647..551f9adbaf 100644 --- a/feature/utilcode/pkg/src/main/res/values/strings.xml +++ b/feature/utilcode/pkg/src/main/res/values/strings.xml @@ -366,5 +366,6 @@ Vibrate 1000ms Vibrate Custom + Vibrate Background Cancel diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java index 622d657817..8d2ab055e4 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java @@ -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; @@ -35,6 +39,39 @@ public static void vibrate(final long milliseconds) { vibrator.vibrate(milliseconds); } + /** + * Vibrate. + *

Must hold {@code }

+ * + * @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 + *

Must hold {@code }

+ * + * @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. *

Must hold {@code }

@@ -49,6 +86,41 @@ public static void vibrate(final long[] pattern, final int repeat) { vibrator.vibrate(pattern, repeat); } + /** + * Vibrate. + *

Must hold {@code }

+ * + * @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 + *

Must hold {@code }

+ * + * @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. *

Must hold {@code }

@@ -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(); + } } From c7ebceb6675670080389472445d0bcb3c71f8bc9 Mon Sep 17 00:00:00 2001 From: simple <383559698@qq.com> Date: Thu, 25 Apr 2024 10:36:38 +0800 Subject: [PATCH 2/2] add VibrateUtils comment --- .../blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt | 1 + .../src/main/java/com/blankj/utilcode/util/VibrateUtils.java | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt index ee88dab0fa..5dc337bc5b 100644 --- a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt +++ b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/vibrate/VibrateActivity.kt @@ -41,6 +41,7 @@ class VibrateActivity : CommonActivity() { 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) diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java index 8d2ab055e4..164810381a 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/VibrateUtils.java @@ -58,7 +58,7 @@ public static void vibrate(final long milliseconds, AudioAttributes attributes) } /** - * VibrateCompat + * VibrateCompat - Can vibrate in background *

Must hold {@code }

* * @param milliseconds he number of milliseconds to vibrate. @@ -106,7 +106,7 @@ public static void vibrate(final long[] pattern, final int repeat, AudioAttribut } /** - * VibrateCompat + * VibrateCompat - Can vibrate in background *

Must hold {@code }

* * @param pattern An array of longs of times for which to turn the vibrator on or off.