浏览代码

Expand status bar on swipe down (#115)

Co-authored-by: Joshua Kuestersteffen <jkuester@kuester7.com>
klausweiss 3 年之前
父节点
当前提交
e1f3a23737
共有 2 个文件被更改,包括 45 次插入0 次删除
  1. 1 0
      app/src/main/AndroidManifest.xml
  2. 44 0
      app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt

+ 1 - 0
app/src/main/AndroidManifest.xml

@@ -6,6 +6,7 @@
     <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
     <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
         tools:ignore="QueryAllPackagesPermission" />
+    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
 
 
     <application

+ 44 - 0
app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt

@@ -1,13 +1,16 @@
 package com.sduduzog.slimlauncher
 
+import android.annotation.SuppressLint
 import android.content.SharedPreferences
 import android.content.res.Resources
 import android.os.Bundle
+import android.util.Log
 import android.view.GestureDetector
 import android.view.GestureDetector.SimpleOnGestureListener
 import android.view.MotionEvent
 import android.view.View
 import androidx.appcompat.app.AppCompatActivity
+import androidx.constraintlayout.motion.widget.MotionLayout
 import androidx.navigation.NavController
 import androidx.navigation.Navigation.findNavController
 import com.sduduzog.slimlauncher.di.MainFragmentFactoryEntryPoint
@@ -17,6 +20,8 @@ import com.sduduzog.slimlauncher.utils.IPublisher
 import com.sduduzog.slimlauncher.utils.ISubscriber
 import dagger.hilt.android.AndroidEntryPoint
 import dagger.hilt.android.EntryPointAccessors
+import java.lang.reflect.Method
+import kotlin.math.absoluteValue
 
 
 @AndroidEntryPoint
@@ -156,5 +161,44 @@ class MainActivity : AppCompatActivity(),
                 findNavController(homeView).navigate(R.id.action_homeFragment_to_optionsFragment, null)
             }
         }
+
+        override fun onFling(
+            e1: MotionEvent,
+            e2: MotionEvent,
+            velocityX: Float,
+            velocityY: Float
+        ): Boolean {
+            val homeView = findViewById<MotionLayout>(R.id.home_fragment)
+            if (homeView != null) {
+                val homeScreen = homeView.constraintSetIds[0]
+                val isFlingFromHomeScreen = homeView.currentState == homeScreen
+                val isFlingDown = velocityY > 0 && velocityY > velocityX.absoluteValue
+                if (isFlingDown && isFlingFromHomeScreen) {
+                    expandStatusBar()
+                }
+            }
+            return super.onFling(e1, e2, velocityX, velocityY)
+        }
     })
+
+    @SuppressLint("WrongConstant")  // statusbar is an internal API
+    private fun expandStatusBar() {
+        try {
+            getSystemService("statusbar")?.let { service ->
+                val statusbarManager = Class.forName("android.app.StatusBarManager")
+                val expand: Method = statusbarManager.getMethod("expandNotificationsPanel")
+                expand.invoke(service)
+            }
+        } catch (e: Exception) {
+            // Do nothing. There does not seem to be any official way with the Android SKD to open the status bar.
+            // https://stackoverflow.com/questions/5029354/how-can-i-programmatically-open-close-notifications-in-android
+            // This hack may break on future versions of Android (or even just not work for specific manufacturer variants).
+            // So, if anything goes wrong, we will just do nothing.
+            Log.e(
+                "MainActivity",
+                "Error trying to expand the notifications panel.",
+                e
+            )
+        }
+    }
 }