Guest User

Untitled

a guest
May 24th, 2024
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | Software | 0 0
  1.     @SuppressWarnings({"MissingPermission"})
  2.     public static void DoHapticFeedback(Activity activity, Context ctx, int strength, boolean ignoreDeviceHapticSetting) {
  3.         if(ctx == null || activity == null || activity.isFinishing() || activity.isDestroyed()) return;
  4.  
  5.         View rootView = null;
  6.  
  7.         try {
  8.             Window activityWindow = activity.getWindow();
  9.             if (activityWindow != null) {
  10.                 View activityView = activityWindow.getDecorView();
  11.  
  12.                 if(activityView != null) {
  13.                     rootView = activityView.findViewById(android.R.id.content);
  14.                 } else {
  15.                     Log.e("PicklePKG", "Vibration.DoHapticFeedback(..) null activityView!");
  16.                     return;
  17.                 }
  18.             } else {
  19.                 Log.e("PicklePKG", "Vibration.DoHapticFeedback(..) null activityWindow!");
  20.                 return;
  21.             }
  22.         } catch (Exception e) {
  23.             Log.e("PicklePKG", "Vibration.DoHapticFeedback(..) failed to get rootView - " + e);
  24.             return;
  25.         }
  26.  
  27.         if (rootView == null) {
  28.             Log.e("PicklePKG", "Vibration.DoHapticFeedback(..) rootView was null!");
  29.             return;
  30.         }
  31.  
  32.         // If haptic feedback isn't already enabled, enable it now
  33.         if (!rootView.isHapticFeedbackEnabled())
  34.             rootView.setHapticFeedbackEnabled(true);
  35.  
  36.         int type = HapticFeedbackConstants.CLOCK_TICK;
  37.  
  38.         switch(strength){
  39.             /*case 1: type = HapticFeedbackConstants.VIRTUAL_KEY; break;*/
  40.             case 2: type = HapticFeedbackConstants.KEYBOARD_TAP; break;
  41.             case 3: type = HapticFeedbackConstants.VIRTUAL_KEY; break;
  42.             case 4: type = HapticFeedbackConstants.LONG_PRESS; break;
  43.         }
  44.  
  45.         if(ignoreDeviceHapticSetting) {
  46.             // Android API 33+ does not support the flag to ignore device haptic settings, if we already have the VIBRATE permission just use the vibrator for haptics
  47.             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
  48.                 if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.VIBRATE) == PackageManager.PERMISSION_GRANTED) {
  49.                     long vibrationMilliseconds = 1L;
  50.  
  51.                     // Convert strength to millisecond durations
  52.                     switch (strength) {
  53.                         /*case 1: vibrationMilliseconds = 1L; break;*/
  54.                         case 2: vibrationMilliseconds = 20L; break;
  55.                         case 3: vibrationMilliseconds = 50L; break;
  56.                         case 4: vibrationMilliseconds = 100L; break;
  57.                     }
  58.  
  59.                     DoVibrate(ctx, vibrationMilliseconds, 1);
  60.                 } else {
  61.                     rootView.performHapticFeedback(type);
  62.                 }
  63.             } else {
  64.                 rootView.performHapticFeedback(type, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
  65.             }
  66.         } else {
  67.             rootView.performHapticFeedback(type);
  68.         }
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment