├── .gitignore
├── .idea
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── fenggit
│ │ └── floatwindow
│ │ ├── FloatManager.kt
│ │ ├── FloatWindowService.kt
│ │ ├── MainActivity.kt
│ │ └── NextActivity.kt
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ ├── float_view_bg.xml
│ ├── fm_float_play.xml
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_main.xml
│ ├── activity_next.xml
│ └── float_view.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-xhdpi
│ ├── default_record_album.png
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ ├── ic_launcher_round.png
│ ├── icon_float_close.png
│ ├── icon_float_next.png
│ ├── icon_float_pause.png
│ └── icon_float_play.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── float_window.gif
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── fenggit
│ │ └── floatwindow
│ │ ├── FLog.java
│ │ ├── FloatListener.java
│ │ ├── FloatWindow.java
│ │ ├── permission
│ │ └── FloatPermission.java
│ │ └── rom
│ │ ├── HuaweiUtils.java
│ │ ├── MeizuUtils.java
│ │ ├── MiuiUtils.java
│ │ ├── OppoUtils.java
│ │ ├── QikuUtils.java
│ │ └── RomUtils.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FloatWindow 安卓任意界面悬浮窗
2 |
3 | 
4 |
5 |
6 | ```java
7 | minSdkVersion 19
8 | targetSdkVersion 29
9 | ```
10 |
11 | 特性:
12 | ===
13 |
14 | 1.支持桌面和App内悬浮,需要权限
15 |
16 | 2.支持仅仅在App内悬浮,不需要权限
17 |
18 | 3.支持自定义悬浮窗样式
19 |
20 | 4.支持拖动,提供自动贴边等动画
21 |
22 | 5.支持权限申请操作
23 |
24 |
25 | 集成:
26 | ===
27 |
28 | 第 1 步、在工程的 build.gradle 中添加:
29 |
30 | ```
31 | allprojects {
32 | repositories {
33 | ...
34 | maven { url 'https://jitpack.io' }
35 | }
36 | }
37 | ```
38 | 第 2 步、在应用的 build.gradle 中添加:
39 |
40 | ```
41 | dependencies {
42 |
43 | }
44 | ```
45 |
46 | 使用:
47 | ===
48 |
49 | **0.声明权限**
50 |
51 | ```java
52 |
53 | ```
54 |
55 |
56 | **1.基础使用**
57 |
58 | ```kotlin
59 | // 自定义的View
60 | var view = LayoutInflater.from(context).inflate(R.layout.float_view, null)
61 |
62 | var float = FloatWindow.With(context, view)
63 | .setAutoAlign(true) //是否自动贴边
64 | .setModality(false)
65 | .setMoveAble(true) // 是否可拖动
66 | .setStartLocation(0, (getScreenHeight(context) * 0.7).toInt())
67 | .create()
68 |
69 | ```
70 |
71 |
72 | **2.TODO 指定界面显示**
73 |
74 | ```java
75 | .setFilter(true, A_Activity.class, C_Activity.class)
76 |
77 | ```
78 | 此方法表示 A_Activity、C_Activity 显示悬浮窗,其他界面隐藏。
79 |
80 | ```java
81 | .setFilter(false, B_Activity.class)
82 | ```
83 |
84 | **1.知识点**
85 | - app内悬浮窗使用的是:WindowManager.LayoutParams.TYPE_APPLICATION
86 |
87 | - 桌面悬浮使用的是:WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY 和 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
88 |
89 | 为什么app内悬浮窗使用的没有使用WindowManager.LayoutParams.TYPE_TOAST?
90 | 因为高版本不支持
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android-extensions'
3 | apply plugin: 'kotlin-android'
4 |
5 | android {
6 | compileSdkVersion 29
7 | defaultConfig {
8 | applicationId "com.fenggit.floatwindow"
9 | minSdkVersion 19
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'androidx.appcompat:appcompat:1.2.0'
25 | implementation "androidx.core:core-ktx:1.3.1"
26 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
27 |
28 | implementation project(':library')
29 | implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
30 | }
31 | repositories {
32 | mavenCentral()
33 | }
34 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/fenggit/floatwindow/FloatManager.kt:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow
2 |
3 | import android.content.Context
4 | import android.graphics.Point
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import android.view.WindowManager
8 | import android.widget.ImageView
9 | import android.widget.Toast
10 |
11 | /**
12 | * Author: felixhe
13 | * Date: 2019-12-06 14:53
14 | * Description:
15 | */
16 | class FloatManager {
17 | var float: FloatWindow? = null
18 |
19 | fun initView(context: Context) {
20 | val view = LayoutInflater.from(context).inflate(R.layout.float_view, null)
21 | val close = view!!.findViewById(R.id.fm_float_close)
22 | val next = view.findViewById(R.id.fm_float_next)
23 | val play = view.findViewById(R.id.fm_float_play)
24 |
25 | close.setOnClickListener {
26 | float?.hidden()
27 | }
28 |
29 | next.setOnClickListener {
30 | Toast.makeText(context, "next", Toast.LENGTH_SHORT).show()
31 | }
32 |
33 | play.setOnClickListener {
34 | Toast.makeText(context, "play", Toast.LENGTH_SHORT).show()
35 | }
36 |
37 | float = FloatWindow.With(context, view)
38 | .setAutoAlign(true) // 是否自动贴边
39 | .setModality(false) // 是否模态窗口(事件是否可穿透当前窗口)
40 | .setMoveAble(true) // 是否可拖动
41 | .setStartLocation(0, (getScreenHeight(context) * 0.7).toInt()) //设置起始位置
42 | .create()
43 | }
44 |
45 | fun getFloatWindow(): FloatWindow? {
46 | return float
47 | }
48 |
49 | fun getScreenHeight(context: Context): Int {
50 | val sPoint = Point()
51 | val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
52 | wm.defaultDisplay.getSize(sPoint)
53 | return sPoint.y
54 | }
55 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/fenggit/floatwindow/FloatWindowService.kt:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow
2 |
3 | import android.app.Service
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.graphics.Point
7 | import android.os.IBinder
8 | import android.util.Log
9 | import android.view.LayoutInflater
10 | import android.view.View
11 | import android.view.WindowManager
12 | import android.widget.ImageView
13 | import android.widget.Toast
14 |
15 | /**
16 | * Author: felixhe
17 | * Date: 2020/11/5 15:20
18 | * Description:
19 | */
20 | class FloatWindowService : Service() {
21 | var mFloatWindow: FloatWindow? = null
22 |
23 | override fun onCreate() {
24 | super.onCreate()
25 | initView()
26 | Log.e("FloatWindowService", "onCreate:$this")
27 | }
28 |
29 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
30 | Log.e("FloatWindowService", "onStartCommand:$this")
31 | initView()
32 | return super.onStartCommand(intent, flags, startId)
33 | }
34 |
35 | override fun onBind(intent: Intent?): IBinder? {
36 | return null
37 | }
38 |
39 | fun initView() {
40 | if (mFloatWindow == null) {
41 | val view = LayoutInflater.from(this).inflate(R.layout.float_view, null)
42 | val close = view!!.findViewById(R.id.fm_float_close)
43 | val next = view.findViewById(R.id.fm_float_next)
44 | val play = view.findViewById(R.id.fm_float_play)
45 |
46 | close.setOnClickListener {
47 | mFloatWindow?.hidden()
48 | }
49 |
50 | close.setOnClickListener {
51 | mFloatWindow?.hidden()
52 | }
53 |
54 | next.setOnClickListener {
55 | Toast.makeText(this, "next", Toast.LENGTH_SHORT).show()
56 | }
57 |
58 | play.setOnClickListener {
59 | Toast.makeText(this, "play", Toast.LENGTH_SHORT).show()
60 | }
61 |
62 | mFloatWindow = FloatWindow.With(this, view)
63 | .setAutoAlign(true) // 是否自动贴边
64 | .setModality(false) // 是否模态窗口(事件是否可穿透当前窗口)
65 | .setMoveAble(true) // 是否可拖动
66 | .setStartLocation(0, (getScreenHeight(this) * 0.7).toInt()) //设置起始位置
67 | .create()
68 | }
69 |
70 | if (mFloatWindow?.isShowing == false) {
71 | mFloatWindow?.show()
72 | }
73 | }
74 |
75 | fun getScreenHeight(context: Context): Int {
76 | val sPoint = Point()
77 | val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
78 | wm.defaultDisplay.getSize(sPoint)
79 | return sPoint.y
80 | }
81 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/fenggit/floatwindow/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import android.widget.Toast
6 | import androidx.appcompat.app.AlertDialog
7 | import androidx.appcompat.app.AppCompatActivity
8 | import com.fenggit.floatwindow.permission.FloatPermission
9 | import kotlinx.android.synthetic.main.activity_main.*
10 |
11 | class MainActivity : AppCompatActivity() {
12 | lateinit var mFloatManager: FloatManager
13 | lateinit var mFloatPermission: FloatPermission
14 |
15 | var mFloatWindow: FloatWindow? = null
16 |
17 | override fun onCreate(savedInstanceState: Bundle?) {
18 | super.onCreate(savedInstanceState)
19 | setContentView(R.layout.activity_main)
20 |
21 | mFloatManager = FloatManager()
22 | mFloatManager.initView(this)
23 | mFloatWindow = mFloatManager.getFloatWindow()
24 |
25 | mFloatPermission = FloatPermission()
26 |
27 | mFloatWindow?.setFloatListener {
28 | Toast.makeText(this, "click outside", Toast.LENGTH_SHORT).show()
29 | }
30 |
31 | btn_show.setOnClickListener {
32 | // mFloatWindow?.show()
33 |
34 | startService(Intent(this@MainActivity, FloatWindowService::class.java))
35 |
36 | // if (mFloatPermission.isHavePermission(this)) {
37 | // mFloatWindow?.show()
38 | // } else {
39 | // AlertDialog.Builder(this)
40 | // .setTitle("授权")
41 | // .setMessage("显示悬浮窗,需要开启悬浮窗权限")
42 | // .setPositiveButton("去授权") { dialog, which ->
43 | // mFloatPermission.gotoPermission(this@MainActivity)
44 | //
45 | // }.setNegativeButton("取消") { dialog, which ->
46 | // dialog.dismiss()
47 | // }.show()
48 | // }
49 | }
50 |
51 | btn_hidden.setOnClickListener {
52 | mFloatWindow?.hidden()
53 | }
54 |
55 | btn_next.setOnClickListener {
56 | startActivity(Intent(this, NextActivity::class.java))
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/fenggit/floatwindow/NextActivity.kt:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class NextActivity : AppCompatActivity() {
7 | override fun onCreate(savedInstanceState: Bundle?) {
8 | super.onCreate(savedInstanceState)
9 | setContentView(R.layout.activity_next)
10 | }
11 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/float_view_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/fm_float_play.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
24 |
25 |
26 |
32 |
33 |
39 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_next.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/float_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
23 |
24 |
30 |
31 |
38 |
39 |
46 |
47 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/default_record_album.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xhdpi/default_record_album.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/icon_float_close.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/icon_float_close.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/icon_float_next.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/icon_float_next.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/icon_float_pause.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/icon_float_pause.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/icon_float_play.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxhdpi/icon_float_play.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 35dp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FloatWindow
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.3.61'
5 | repositories {
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:4.0.1'
12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 |
24 | }
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
30 |
--------------------------------------------------------------------------------
/float_window.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/float_window.gif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Sep 29 14:49:37 CST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 29
5 |
6 | defaultConfig {
7 | minSdkVersion 19
8 | targetSdkVersion 29
9 | versionCode 1
10 | versionName "1.0"
11 |
12 | consumerProguardFiles 'consumer-rules.pro'
13 | }
14 |
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(dir: 'libs', include: ['*.jar'])
25 | }
26 |
--------------------------------------------------------------------------------
/library/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fenggit/FloatWindow/a79eaae01fb12b9995c841a6f074ad06fb01533b/library/consumer-rules.pro
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/FLog.java:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow;
2 |
3 | import android.util.Log;
4 |
5 | /**
6 | * Author: felixhe
7 | * Date: 2019-12-06 14:36
8 | * Description:
9 | */
10 | public class FLog {
11 | public static final String TAG = "FloatWindow";
12 |
13 | public static boolean isDebug = true;
14 |
15 | public static void e(String message) {
16 | if (isDebug) {
17 | Log.e(TAG, message);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/FloatListener.java:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow;
2 |
3 | import android.view.MotionEvent;
4 |
5 | /**
6 | * Author: felixhe
7 | * Date: 2019-12-06
8 | * Description:
9 | */
10 | public interface FloatListener {
11 | void actionOutSide(MotionEvent event);
12 | }
13 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/FloatWindow.java:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.annotation.SuppressLint;
5 | import android.app.AppOpsManager;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.content.res.Resources;
9 | import android.graphics.PixelFormat;
10 | import android.net.Uri;
11 | import android.os.Binder;
12 | import android.os.Build;
13 | import android.provider.Settings;
14 | import android.util.DisplayMetrics;
15 | import android.view.Gravity;
16 | import android.view.MotionEvent;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 | import android.view.WindowManager;
20 | import android.view.animation.BounceInterpolator;
21 | import android.widget.FrameLayout;
22 |
23 |
24 | import com.fenggit.floatwindow.permission.FloatPermission;
25 |
26 | import java.lang.reflect.Field;
27 | import java.lang.reflect.Method;
28 |
29 | /**
30 | * Author: felixhe
31 | * Date: 2019-12-06
32 | * Description:
33 | */
34 | public class FloatWindow {
35 | private WindowManager.LayoutParams mLayoutParams;
36 | private WindowManager mWindowManager;
37 | private DisplayMetrics mDisplayMetrics;
38 |
39 | /**
40 | * 触摸点相对于view左上角的坐标
41 | */
42 | private float downX;
43 | private float downY;
44 | /**
45 | * 触摸点相对于屏幕左上角的坐标
46 | */
47 | private float rowX;
48 | private float rowY;
49 | /**
50 | * 悬浮窗显示标记
51 | */
52 | private boolean isShowing;
53 | /**
54 | * 拖动最小偏移量
55 | */
56 | private static final int MINIMUM_OFFSET = 5;
57 |
58 | private Context mContext;
59 | /**
60 | * 是否自动贴边
61 | */
62 | private boolean autoAlign;
63 | /**
64 | * 是否模态窗口
65 | */
66 | private boolean modality;
67 | /**
68 | * 是否可拖动
69 | */
70 | private boolean moveAble;
71 |
72 |
73 | /**
74 | * 透明度
75 | */
76 | private float alpha;
77 |
78 | /**
79 | * 初始位置
80 | */
81 | private int startX;
82 | private int startY;
83 |
84 | /**
85 | * View 高度
86 | */
87 | private int height;
88 | /**
89 | * View 宽度
90 | */
91 | private int width;
92 |
93 |
94 | /**
95 | * 内部定义的View,专门处理事件拦截的父View
96 | */
97 | private FloatView floatView;
98 | /**
99 | * 外部传进来的需要悬浮的View
100 | */
101 | private View contentView;
102 |
103 | private boolean isAddView;
104 |
105 | private boolean isDesktopWindow;
106 |
107 | private FloatListener mFloatListener;
108 |
109 | private FloatWindow(With with) {
110 | this.mContext = with.context;
111 | this.autoAlign = with.autoAlign;
112 | this.modality = with.modality;
113 | this.contentView = with.contentView;
114 | this.moveAble = with.moveAble;
115 | this.startX = with.startX;
116 | this.startY = with.startY;
117 | this.alpha = with.alpha;
118 | this.height = with.height;
119 | this.width = with.width;
120 | this.isDesktopWindow = with.isDesktopWindow;
121 |
122 | initWindowManager();
123 | initLayoutParams();
124 | initFloatView();
125 | }
126 |
127 | private void initWindowManager() {
128 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
129 | //获取一个DisplayMetrics对象,该对象用来描述关于显示器的一些信息,例如其大小,密度和字体缩放。
130 | mDisplayMetrics = new DisplayMetrics();
131 | mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics);
132 | }
133 |
134 | @SuppressLint({"ClickableViewAccessibility"})
135 | private void initFloatView() {
136 | floatView = new FloatView(mContext);
137 | if (moveAble) {
138 | floatView.setOnTouchListener(new WindowTouchListener());
139 | }
140 | }
141 |
142 | private void initLayoutParams() {
143 | mLayoutParams = new WindowManager.LayoutParams();
144 | mLayoutParams.flags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
145 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
146 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
147 | if (modality) {
148 | mLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
149 | mLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
150 | }
151 | mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
152 | mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
153 | if (height != WindowManager.LayoutParams.WRAP_CONTENT) {
154 | mLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
155 | }
156 | if (width != WindowManager.LayoutParams.WRAP_CONTENT) {
157 | mLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
158 | }
159 | mLayoutParams.gravity = Gravity.START | Gravity.TOP;
160 | mLayoutParams.format = PixelFormat.RGBA_8888;
161 |
162 |
163 | // 该类型不需要申请权限
164 | if (isDesktopWindow) {
165 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
166 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
167 | } else {
168 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
169 | }
170 | } else {
171 | //mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
172 | ///mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
173 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
174 | //此处mLayoutParams.type不建议使用TYPE_TOAST,因为在一些版本较低的系统中会出现拖动异常的问题,虽然它不需要权限
175 | // 高版本不支持
176 | //mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;
177 | }
178 |
179 | //悬浮窗背景明暗度0~1,数值越大背景越暗,只有在flags设置了WindowManager.LayoutParams.FLAG_DIM_BEHIND 这个属性才会生效
180 | mLayoutParams.dimAmount = 0.0f;
181 | //悬浮窗透明度0~1,数值越大越不透明
182 | mLayoutParams.alpha = alpha;
183 | //悬浮窗起始位置
184 | mLayoutParams.x = startX;
185 | mLayoutParams.y = startY;
186 | }
187 |
188 | public void setFloatListener(FloatListener mFloatListener) {
189 | this.mFloatListener = mFloatListener;
190 | }
191 |
192 | public View getContentView() {
193 | return contentView;
194 | }
195 |
196 | public boolean isAddView() {
197 | return isAddView;
198 | }
199 |
200 | private FloatPermission mFloatPermission = new FloatPermission();
201 |
202 | /**
203 | * 将窗体添加到屏幕上
204 | */
205 | @SuppressLint("NewApi")
206 | public void show() {
207 | if (!isShowing()) {
208 | floatView.setVisibility(View.VISIBLE);
209 |
210 | if (!isAddView) {
211 | mWindowManager.addView(floatView, mLayoutParams);
212 | isAddView = true;
213 | }
214 | isShowing = true;
215 | }
216 | }
217 |
218 | public void hidden() {
219 | isShowing = false;
220 | if (floatView != null) {
221 | floatView.setVisibility(View.GONE);
222 | }
223 | }
224 |
225 | /**
226 | * 悬浮窗是否正在显示
227 | *
228 | * @return true if it's showing.
229 | */
230 | public boolean isShowing() {
231 | if (floatView != null && floatView.getVisibility() == View.VISIBLE) {
232 | return isShowing;
233 | }
234 | return false;
235 | }
236 |
237 | /**
238 | * 打开悬浮窗设置页
239 | * 部分第三方ROM无法直接跳转可使用{@link #openAppSettings(Context)}跳到应用详情页
240 | *
241 | * @param context
242 | * @return true if it's open successful.
243 | */
244 | public static boolean openOpsSettings(Context context) {
245 | try {
246 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
247 | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
248 | context.startActivity(intent);
249 | } else {
250 | return openAppSettings(context);
251 | }
252 | } catch (Exception e) {
253 | e.printStackTrace();
254 | return false;
255 | }
256 | return true;
257 | }
258 |
259 | /**
260 | * 打开应用详情页
261 | *
262 | * @param context
263 | * @return true if it's open success.
264 | */
265 | public static boolean openAppSettings(Context context) {
266 | try {
267 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
268 | Uri uri = Uri.fromParts("package", context.getPackageName(), null);
269 | intent.setData(uri);
270 | context.startActivity(intent);
271 | } catch (Exception e) {
272 | e.printStackTrace();
273 | return false;
274 | }
275 | return true;
276 | }
277 |
278 | /**
279 | * 判断 悬浮窗口权限是否打开
280 | * 由于android未提供直接跳转到悬浮窗设置页的api,此方法使用反射去查找相关函数进行跳转
281 | * 部分第三方ROM可能不适用
282 | *
283 | * @param context
284 | * @return true 允许 false禁止
285 | */
286 | public static boolean isAppOpsOn(Context context) {
287 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
288 | return Settings.canDrawOverlays(context);
289 | }
290 | try {
291 | Object object = context.getSystemService(Context.APP_OPS_SERVICE);
292 | if (object == null) {
293 | return false;
294 | }
295 | Class localClass = object.getClass();
296 | Class[] arrayOfClass = new Class[3];
297 | arrayOfClass[0] = Integer.TYPE;
298 | arrayOfClass[1] = Integer.TYPE;
299 | arrayOfClass[2] = String.class;
300 | Method method = localClass.getMethod("checkOp", arrayOfClass);
301 | if (method == null) {
302 | return false;
303 | }
304 | Object[] arrayOfObject1 = new Object[3];
305 | arrayOfObject1[0] = 24;
306 | arrayOfObject1[1] = Binder.getCallingUid();
307 | arrayOfObject1[2] = context.getPackageName();
308 | int m = (Integer) method.invoke(object, arrayOfObject1);
309 | return m == AppOpsManager.MODE_ALLOWED;
310 | } catch (Exception ex) {
311 | ex.getStackTrace();
312 | }
313 | return false;
314 | }
315 |
316 | public static boolean checkFloatPermission(Context context) {
317 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
318 | return true;
319 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
320 | try {
321 | Class cls = Class.forName("android.content.Context");
322 | Field declaredField = cls.getDeclaredField("APP_OPS_SERVICE");
323 | declaredField.setAccessible(true);
324 | Object obj = declaredField.get(cls);
325 | if (!(obj instanceof String)) {
326 | return false;
327 | }
328 | String str2 = (String) obj;
329 | obj = cls.getMethod("getSystemService", String.class).invoke(context, str2);
330 | cls = Class.forName("android.app.AppOpsManager");
331 | Field declaredField2 = cls.getDeclaredField("MODE_ALLOWED");
332 | declaredField2.setAccessible(true);
333 | Method checkOp = cls.getMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class);
334 | int result = (Integer) checkOp.invoke(obj, 24, Binder.getCallingUid(), context.getPackageName());
335 | return result == declaredField2.getInt(cls);
336 | } catch (Exception e) {
337 | return false;
338 | }
339 | } else {
340 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
341 | AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
342 | if (appOpsMgr == null)
343 | return false;
344 | int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), context
345 | .getPackageName());
346 | return mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED;
347 | } else {
348 | return Settings.canDrawOverlays(context);
349 | }
350 | }
351 | }
352 |
353 | /**
354 | * 移除悬浮窗
355 | */
356 | public void remove() {
357 | if (isShowing()) {
358 | floatView.removeView(contentView);
359 | mWindowManager.removeView(floatView);
360 | isShowing = false;
361 | isAddView = false;
362 | }
363 | }
364 |
365 | /**
366 | * 用于获取系统状态栏的高度。
367 | *
368 | * @return 返回状态栏高度的像素值。
369 | */
370 | private int getStatusBarHeight() {
371 | int identifier = Resources.getSystem().getIdentifier("status_bar_height",
372 | "dimen", "android");
373 | if (identifier > 0) {
374 | return Resources.getSystem().getDimensionPixelSize(identifier);
375 | }
376 | return 0;
377 | }
378 |
379 | class FloatView extends FrameLayout {
380 |
381 | /**
382 | * 记录按下位置
383 | */
384 | int interceptX = 0;
385 | int interceptY = 0;
386 |
387 | public FloatView(Context context) {
388 | super(context);
389 | //这里由于一个ViewGroup不能add一个已经有Parent的contentView,所以需要先判断contentView是否有Parent
390 | //如果有则需要将contentView先移除
391 | if (contentView.getParent() != null && contentView.getParent() instanceof ViewGroup) {
392 | ((ViewGroup) contentView.getParent()).removeView(contentView);
393 | }
394 |
395 | addView(contentView);
396 | }
397 |
398 | /**
399 | * 解决点击与拖动冲突的关键代码
400 | *
401 | * @param ev
402 | * @return
403 | */
404 | @Override
405 | public boolean onInterceptTouchEvent(MotionEvent ev) {
406 | //此回调如果返回true则表示拦截TouchEvent由自己处理,false表示不拦截TouchEvent分发出去由子view处理
407 | //解决方案:如果是拖动父View则返回true调用自己的onTouch改变位置,是点击则返回false去响应子view的点击事件
408 | boolean isIntercept = false;
409 | switch (ev.getAction()) {
410 | case MotionEvent.ACTION_DOWN:
411 | interceptX = (int) ev.getX();
412 | interceptY = (int) ev.getY();
413 | downX = ev.getX();
414 | downY = ev.getY();
415 | isIntercept = false;
416 | break;
417 | case MotionEvent.ACTION_MOVE:
418 | //在一些dpi较高的设备上点击view很容易触发 ACTION_MOVE,所以此处做一个过滤
419 | isIntercept = Math.abs(ev.getX() - interceptX) > MINIMUM_OFFSET && Math.abs(ev.getY() - interceptY) > MINIMUM_OFFSET;
420 | break;
421 | case MotionEvent.ACTION_UP:
422 | break;
423 | default:
424 | break;
425 | }
426 | return isIntercept;
427 | }
428 | }
429 |
430 | class WindowTouchListener implements View.OnTouchListener {
431 |
432 | @SuppressLint("ClickableViewAccessibility")
433 | @Override
434 | public boolean onTouch(View v, MotionEvent event) {
435 |
436 | //获取触摸点相对于屏幕左上角的坐标
437 | rowX = event.getRawX();
438 | rowY = event.getRawY() - getStatusBarHeight();
439 |
440 | switch (event.getAction()) {
441 | case MotionEvent.ACTION_DOWN:
442 | actionDown(event);
443 | break;
444 | case MotionEvent.ACTION_MOVE:
445 | actionMove(event);
446 | break;
447 | case MotionEvent.ACTION_UP:
448 | actionUp(event);
449 | break;
450 | case MotionEvent.ACTION_OUTSIDE:
451 | actionOutSide(event);
452 | break;
453 | default:
454 | break;
455 | }
456 | return false;
457 | }
458 |
459 | /**
460 | * 手指点击窗口外的事件
461 | *
462 | * @param event
463 | */
464 | private void actionOutSide(MotionEvent event) {
465 | //由于我们在layoutParams中添加了FLAG_WATCH_OUTSIDE_TOUCH标记,那么点击悬浮窗之外时此事件就会被响应
466 | //这里可以用来扩展点击悬浮窗外部响应事件
467 | if (mFloatListener != null) {
468 | mFloatListener.actionOutSide(event);
469 | }
470 | }
471 |
472 | /**
473 | * 手指抬起事件
474 | *
475 | * @param event
476 | */
477 | private void actionUp(MotionEvent event) {
478 | if (autoAlign) {
479 | autoAlign();
480 | }
481 | }
482 |
483 | /**
484 | * 拖动事件
485 | *
486 | * @param event
487 | */
488 | private void actionMove(MotionEvent event) {
489 | //拖动事件下一直计算坐标 然后更新悬浮窗位置
490 | updateLocation((rowX - downX), (rowY - downY));
491 | }
492 |
493 | /**
494 | * 更新位置
495 | */
496 | private void updateLocation(float x, float y) {
497 | mLayoutParams.x = (int) x;
498 | mLayoutParams.y = (int) y;
499 | mWindowManager.updateViewLayout(floatView, mLayoutParams);
500 | }
501 |
502 | /**
503 | * 手指按下事件
504 | *
505 | * @param event
506 | */
507 | private void actionDown(MotionEvent event) {
508 | // downX = event.getX();
509 | // downY = event.getY();
510 | }
511 |
512 | /**
513 | * 自动贴边
514 | */
515 | private void autoAlign() {
516 | float fromX = mLayoutParams.x;
517 |
518 | if (rowX <= mDisplayMetrics.widthPixels / 2) {
519 | mLayoutParams.x = 0;
520 | } else {
521 | mLayoutParams.x = mDisplayMetrics.widthPixels;
522 | }
523 | FLog.e("fromX:" + fromX + " || toX : " + mLayoutParams.x);
524 | //这里使用ValueAnimator来平滑计算起始X坐标到结束X坐标之间的值,并更新悬浮窗位置
525 | ValueAnimator animator = ValueAnimator.ofFloat(fromX, mLayoutParams.x);
526 | animator.setDuration(500);
527 | animator.setInterpolator(new BounceInterpolator());
528 | animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
529 | @Override
530 | public void onAnimationUpdate(ValueAnimator animation) {
531 | //这里会返回fromX ~ mLayoutParams.x之间经过计算的过渡值
532 | float toX = (float) animation.getAnimatedValue();
533 | FLog.e("toX : " + toX);
534 | //我们直接使用这个值来更新悬浮窗位置
535 | updateLocation(toX, mLayoutParams.y);
536 | }
537 | });
538 | animator.start();
539 | }
540 | }
541 |
542 | public static class With {
543 | private Context context;
544 | private boolean autoAlign;
545 | private boolean modality;
546 | private View contentView;
547 | private boolean moveAble;
548 | private float alpha = 1f;
549 | private boolean isDesktopWindow;
550 |
551 | /**
552 | * View 高度
553 | */
554 | private int height = WindowManager.LayoutParams.WRAP_CONTENT;
555 | /**
556 | * View 宽度
557 | */
558 | private int width = WindowManager.LayoutParams.WRAP_CONTENT;
559 |
560 | /**
561 | * 初始位置
562 | */
563 | private int startX;
564 | private int startY;
565 |
566 | /**
567 | * @param context 上下文环境
568 | * @param contentView 需要悬浮的视图
569 | */
570 | public With(Context context, View contentView) {
571 | this.context = context;
572 | this.contentView = contentView;
573 | }
574 |
575 | /**
576 | * 是否自动贴边
577 | *
578 | * @param autoAlign
579 | * @return
580 | */
581 | public With setAutoAlign(boolean autoAlign) {
582 | this.autoAlign = autoAlign;
583 | return this;
584 | }
585 |
586 | /**
587 | * 是否模态窗口(事件是否可穿透当前窗口)
588 | *
589 | * @param modality
590 | * @return
591 | */
592 | public With setModality(boolean modality) {
593 | this.modality = modality;
594 | return this;
595 | }
596 |
597 | /**
598 | * 是否可拖动
599 | *
600 | * @param moveAble
601 | * @return
602 | */
603 | public With setMoveAble(boolean moveAble) {
604 | this.moveAble = moveAble;
605 | return this;
606 | }
607 |
608 | /**
609 | * 设置起始位置
610 | *
611 | * @param startX
612 | * @param startY
613 | * @return
614 | */
615 | public With setStartLocation(int startX, int startY) {
616 | this.startX = startX;
617 | this.startY = startY;
618 | return this;
619 | }
620 |
621 | public With setDeskTopWindow(boolean isDesktopWindow) {
622 | this.isDesktopWindow = isDesktopWindow;
623 | return this;
624 | }
625 |
626 | public With setAlpha(float alpha) {
627 | this.alpha = alpha;
628 | return this;
629 | }
630 |
631 | public With setHeight(int height) {
632 | this.height = height;
633 | return this;
634 | }
635 |
636 | public With setWidth(int width) {
637 | this.width = width;
638 | return this;
639 | }
640 |
641 | public FloatWindow create() {
642 | return new FloatWindow(this);
643 | }
644 | }
645 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/permission/FloatPermission.java:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow.permission;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Build;
7 | import android.provider.Settings;
8 | import android.util.Log;
9 |
10 | import com.fenggit.floatwindow.FLog;
11 | import com.fenggit.floatwindow.rom.HuaweiUtils;
12 | import com.fenggit.floatwindow.rom.MeizuUtils;
13 | import com.fenggit.floatwindow.rom.MiuiUtils;
14 | import com.fenggit.floatwindow.rom.OppoUtils;
15 | import com.fenggit.floatwindow.rom.QikuUtils;
16 | import com.fenggit.floatwindow.rom.RomUtils;
17 |
18 | import java.lang.reflect.Field;
19 | import java.lang.reflect.Method;
20 |
21 | /**
22 | * Author: felixhe
23 | * Date: 2019-12-06
24 | * Description:
25 | */
26 | public class FloatPermission {
27 | public static final String TAG = "FloatPermission";
28 |
29 | public boolean isHavePermission(Context context) {
30 | //6.0 版本之后由于 google 增加了对悬浮窗权限的管理,所以方式就统一了
31 | if (Build.VERSION.SDK_INT < 23) {
32 | if (RomUtils.checkIsMiuiRom()) {
33 | return miuiPermissionCheck(context);
34 | } else if (RomUtils.checkIsMeizuRom()) {
35 | return meizuPermissionCheck(context);
36 | } else if (RomUtils.checkIsHuaweiRom()) {
37 | return huaweiPermissionCheck(context);
38 | } else if (RomUtils.checkIs360Rom()) {
39 | return qikuPermissionCheck(context);
40 | } else if (RomUtils.checkIsOppoRom()) {
41 | return oppoROMPermissionCheck(context);
42 | }
43 | }
44 | return commonROMPermissionCheck(context);
45 | }
46 |
47 | public void gotoPermission(Context context) {
48 | if (Build.VERSION.SDK_INT < 23) {
49 | if (RomUtils.checkIsMiuiRom()) {
50 | MiuiUtils.applyMiuiPermission(context);
51 | } else if (RomUtils.checkIsMeizuRom()) {
52 | MeizuUtils.applyPermission(context);
53 | } else if (RomUtils.checkIsHuaweiRom()) {
54 | HuaweiUtils.applyPermission(context);
55 | } else if (RomUtils.checkIs360Rom()) {
56 | QikuUtils.applyPermission(context);
57 | } else if (RomUtils.checkIsOppoRom()) {
58 | OppoUtils.applyOppoPermission(context);
59 | }
60 | } else {
61 | commonROMPermissionApply(context);
62 | }
63 | }
64 |
65 |
66 | /**
67 | * 通用 rom 权限申请
68 | */
69 | private void commonROMPermissionApply(final Context context) {
70 | //这里也一样,魅族系统需要单独适配
71 | if (RomUtils.checkIsMeizuRom()) {
72 | MeizuUtils.applyPermission(context);
73 | } else {
74 | if (Build.VERSION.SDK_INT >= 23) {
75 | try {
76 | commonROMPermissionApplyInternal(context);
77 | } catch (Exception e) {
78 | Log.e(TAG, Log.getStackTraceString(e));
79 | }
80 | }
81 | }
82 | }
83 |
84 | public static void commonROMPermissionApplyInternal(Context context) throws NoSuchFieldException, IllegalAccessException {
85 | Class clazz = Settings.class;
86 | Field field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
87 |
88 | Intent intent = new Intent(field.get(null).toString());
89 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
90 | intent.setData(Uri.parse("package:" + context.getPackageName()));
91 | context.startActivity(intent);
92 | }
93 |
94 | private boolean huaweiPermissionCheck(Context context) {
95 | return HuaweiUtils.checkFloatWindowPermission(context);
96 | }
97 |
98 | private boolean miuiPermissionCheck(Context context) {
99 | return MiuiUtils.checkFloatWindowPermission(context);
100 | }
101 |
102 | private boolean meizuPermissionCheck(Context context) {
103 | return MeizuUtils.checkFloatWindowPermission(context);
104 | }
105 |
106 | private boolean qikuPermissionCheck(Context context) {
107 | return QikuUtils.checkFloatWindowPermission(context);
108 | }
109 |
110 | private boolean oppoROMPermissionCheck(Context context) {
111 | return OppoUtils.checkFloatWindowPermission(context);
112 | }
113 |
114 | private boolean commonROMPermissionCheck(Context context) {
115 | //最新发现魅族6.0的系统这种方式不好用,天杀的,只有你是奇葩,没办法,单独适配一下
116 | if (RomUtils.checkIsMeizuRom()) {
117 | return meizuPermissionCheck(context);
118 | } else {
119 | Boolean result = true;
120 | if (Build.VERSION.SDK_INT >= 23) {
121 | try {
122 | Class clazz = Settings.class;
123 | Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
124 | result = (Boolean) canDrawOverlays.invoke(null, context);
125 | } catch (Exception e) {
126 | FLog.e(Log.getStackTraceString(e));
127 | }
128 | }
129 | return result;
130 | }
131 | }
132 |
133 | }
134 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/HuaweiUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
3 | */
4 | package com.fenggit.floatwindow.rom;
5 |
6 | import android.annotation.TargetApi;
7 | import android.app.AppOpsManager;
8 | import android.content.ActivityNotFoundException;
9 | import android.content.ComponentName;
10 | import android.content.Context;
11 | import android.content.Intent;
12 | import android.os.Binder;
13 | import android.os.Build;
14 | import android.util.Log;
15 | import android.widget.Toast;
16 |
17 | import java.lang.reflect.Method;
18 |
19 | public class HuaweiUtils {
20 | private static final String TAG = "HuaweiUtils";
21 |
22 | /**
23 | * 检测 Huawei 悬浮窗权限
24 | */
25 | public static boolean checkFloatWindowPermission(Context context) {
26 | final int version = Build.VERSION.SDK_INT;
27 | if (version >= 19) {
28 | return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
29 | }
30 | return true;
31 | }
32 |
33 | /**
34 | * 去华为权限申请页面
35 | */
36 | public static void applyPermission(Context context) {
37 | try {
38 | Intent intent = new Intent();
39 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
40 | // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
41 | // ComponentName comp = new ComponentName("com.huawei.systemmanager",
42 | // "com.huawei.permissionmanager.ui.SingleAppActivity");//华为权限管理,跳转到指定app的权限管理位置需要华为接口权限,未解决
43 | ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//悬浮窗管理页面
44 | intent.setComponent(comp);
45 | if (RomUtils.getEmuiVersion() == 3.1) {
46 | //emui 3.1 的适配
47 | context.startActivity(intent);
48 | } else {
49 | //emui 3.0 的适配
50 | comp = new ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity");//悬浮窗管理页面
51 | intent.setComponent(comp);
52 | context.startActivity(intent);
53 | }
54 | } catch (SecurityException e) {
55 | Intent intent = new Intent();
56 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
57 | // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
58 | ComponentName comp = new ComponentName("com.huawei.systemmanager",
59 | "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理,跳转到本app的权限管理页面,这个需要华为接口权限,未解决
60 | // ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//悬浮窗管理页面
61 | intent.setComponent(comp);
62 | context.startActivity(intent);
63 | Log.e(TAG, Log.getStackTraceString(e));
64 | } catch (ActivityNotFoundException e) {
65 | /**
66 | * 手机管家版本较低 HUAWEI SC-UL10
67 | */
68 | // Toast.makeText(MainActivity.this, "act找不到", Toast.LENGTH_LONG).show();
69 | Intent intent = new Intent();
70 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71 | ComponentName comp = new ComponentName("com.Android.settings", "com.android.settings.permission.TabItem");//权限管理页面 android4.4
72 | // ComponentName comp = new ComponentName("com.android.settings","com.android.settings.permission.single_app_activity");//此处可跳转到指定app对应的权限管理页面,但是需要相关权限,未解决
73 | intent.setComponent(comp);
74 | context.startActivity(intent);
75 | e.printStackTrace();
76 | Log.e(TAG, Log.getStackTraceString(e));
77 | } catch (Exception e) {
78 | //抛出异常时提示信息
79 | Toast.makeText(context, "进入设置页面失败,请手动设置", Toast.LENGTH_LONG).show();
80 | Log.e(TAG, Log.getStackTraceString(e));
81 | }
82 | }
83 |
84 | @TargetApi(Build.VERSION_CODES.KITKAT)
85 | private static boolean checkOp(Context context, int op) {
86 | final int version = Build.VERSION.SDK_INT;
87 | if (version >= 19) {
88 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
89 | try {
90 | Class clazz = AppOpsManager.class;
91 | Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
92 | return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
93 | } catch (Exception e) {
94 | Log.e(TAG, Log.getStackTraceString(e));
95 | }
96 | } else {
97 | Log.e(TAG, "Below API 19 cannot invoke!");
98 | }
99 | return false;
100 | }
101 | }
102 |
103 |
104 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/MeizuUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
3 | */
4 | package com.fenggit.floatwindow.rom;
5 |
6 | import android.annotation.TargetApi;
7 | import android.app.AppOpsManager;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.net.Uri;
11 | import android.os.Binder;
12 | import android.os.Build;
13 | import android.provider.Settings;
14 | import android.util.Log;
15 |
16 | import java.lang.reflect.Field;
17 | import java.lang.reflect.Method;
18 |
19 | public class MeizuUtils {
20 | private static final String TAG = "MeizuUtils";
21 |
22 | /**
23 | * 检测 meizu 悬浮窗权限
24 | */
25 | public static boolean checkFloatWindowPermission(Context context) {
26 | final int version = Build.VERSION.SDK_INT;
27 | if (version >= 19) {
28 | return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
29 | }
30 | return true;
31 | }
32 |
33 | /**
34 | * 去魅族权限申请页面
35 | */
36 | public static void applyPermission(Context context) {
37 | try {
38 | Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
39 | // intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");//remove this line code for fix flyme6.3
40 | intent.putExtra("packageName", context.getPackageName());
41 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
42 | context.startActivity(intent);
43 | } catch (Exception e) {
44 | try {
45 | Log.e(TAG, "获取悬浮窗权限, 打开AppSecActivity失败, " + Log.getStackTraceString(e));
46 | // 最新的魅族flyme 6.2.5 用上述方法获取权限失败, 不过又可以用下述方法获取权限了
47 | commonROMPermissionApplyInternal(context);
48 | } catch (Exception eFinal) {
49 | Log.e(TAG, "获取悬浮窗权限失败, 通用获取方法失败, " + Log.getStackTraceString(eFinal));
50 | }
51 | }
52 | }
53 |
54 | public static void commonROMPermissionApplyInternal(Context context) throws NoSuchFieldException, IllegalAccessException {
55 | Class clazz = Settings.class;
56 | Field field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
57 |
58 | Intent intent = new Intent(field.get(null).toString());
59 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60 | intent.setData(Uri.parse("package:" + context.getPackageName()));
61 | context.startActivity(intent);
62 | }
63 |
64 | @TargetApi(Build.VERSION_CODES.KITKAT)
65 | private static boolean checkOp(Context context, int op) {
66 | final int version = Build.VERSION.SDK_INT;
67 | if (version >= 19) {
68 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
69 | try {
70 | Class clazz = AppOpsManager.class;
71 | Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
72 | return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
73 | } catch (Exception e) {
74 | Log.e(TAG, Log.getStackTraceString(e));
75 | }
76 | } else {
77 | Log.e(TAG, "Below API 19 cannot invoke!");
78 | }
79 | return false;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/MiuiUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
3 | */
4 | package com.fenggit.floatwindow.rom;
5 |
6 | import android.annotation.TargetApi;
7 | import android.app.AppOpsManager;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.content.pm.PackageManager;
11 | import android.net.Uri;
12 | import android.os.Binder;
13 | import android.os.Build;
14 | import android.provider.Settings;
15 | import android.util.Log;
16 |
17 | import java.lang.reflect.Method;
18 |
19 | public class MiuiUtils {
20 | private static final String TAG = "MiuiUtils";
21 |
22 | /**
23 | * 获取小米 rom 版本号,获取失败返回 -1
24 | *
25 | * @return miui rom version code, if fail , return -1
26 | */
27 | public static int getMiuiVersion() {
28 | String version = RomUtils.getSystemProperty("ro.miui.ui.version.name");
29 | if (version != null) {
30 | try {
31 | return Integer.parseInt(version.substring(1));
32 | } catch (Exception e) {
33 | Log.e(TAG, "get miui version code error, version : " + version);
34 | Log.e(TAG, Log.getStackTraceString(e));
35 | }
36 | }
37 | return -1;
38 | }
39 |
40 | /**
41 | * 检测 miui 悬浮窗权限
42 | */
43 | public static boolean checkFloatWindowPermission(Context context) {
44 | final int version = Build.VERSION.SDK_INT;
45 |
46 | if (version >= 19) {
47 | return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
48 | } else {
49 | // if ((context.getApplicationInfo().flags & 1 << 27) == 1) {
50 | // return true;
51 | // } else {
52 | // return false;
53 | // }
54 | return true;
55 | }
56 | }
57 |
58 | @TargetApi(Build.VERSION_CODES.KITKAT)
59 | private static boolean checkOp(Context context, int op) {
60 | final int version = Build.VERSION.SDK_INT;
61 | if (version >= 19) {
62 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
63 | try {
64 | Class clazz = AppOpsManager.class;
65 | Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
66 | return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
67 | } catch (Exception e) {
68 | Log.e(TAG, Log.getStackTraceString(e));
69 | }
70 | } else {
71 | Log.e(TAG, "Below API 19 cannot invoke!");
72 | }
73 | return false;
74 | }
75 |
76 | /**
77 | * 小米 ROM 权限申请
78 | */
79 | public static void applyMiuiPermission(Context context) {
80 | int versionCode = getMiuiVersion();
81 | if (versionCode == 5) {
82 | goToMiuiPermissionActivity_V5(context);
83 | } else if (versionCode == 6) {
84 | goToMiuiPermissionActivity_V6(context);
85 | } else if (versionCode == 7) {
86 | goToMiuiPermissionActivity_V7(context);
87 | } else if (versionCode == 8) {
88 | goToMiuiPermissionActivity_V8(context);
89 | } else {
90 | Log.e(TAG, "this is a special MIUI rom version, its version code " + versionCode);
91 | }
92 | }
93 |
94 | private static boolean isIntentAvailable(Intent intent, Context context) {
95 | if (intent == null) {
96 | return false;
97 | }
98 | return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
99 | }
100 |
101 | /**
102 | * 小米 V5 版本 ROM权限申请
103 | */
104 | public static void goToMiuiPermissionActivity_V5(Context context) {
105 | Intent intent = null;
106 | String packageName = context.getPackageName();
107 | intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
108 | Uri uri = Uri.fromParts("package", packageName, null);
109 | intent.setData(uri);
110 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
111 | if (isIntentAvailable(intent, context)) {
112 | context.startActivity(intent);
113 | } else {
114 | Log.e(TAG, "intent is not available!");
115 | }
116 |
117 | //设置页面在应用详情页面
118 | // Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
119 | // PackageInfo pInfo = null;
120 | // try {
121 | // pInfo = context.getPackageManager().getPackageInfo
122 | // (HostInterfaceManager.getHostInterface().getApp().getPackageName(), 0);
123 | // } catch (PackageManager.NameNotFoundException e) {
124 | // AVLogUtils.e(TAG, e.getMessage());
125 | // }
126 | // intent.setClassName("com.android.settings", "com.miui.securitycenter.permission.AppPermissionsEditor");
127 | // intent.putExtra("extra_package_uid", pInfo.applicationInfo.uid);
128 | // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
129 | // if (isIntentAvailable(intent, context)) {
130 | // context.startActivity(intent);
131 | // } else {
132 | // AVLogUtils.e(TAG, "Intent is not available!");
133 | // }
134 | }
135 |
136 | /**
137 | * 小米 V6 版本 ROM权限申请
138 | */
139 | public static void goToMiuiPermissionActivity_V6(Context context) {
140 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
141 | intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
142 | intent.putExtra("extra_pkgname", context.getPackageName());
143 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
144 |
145 | if (isIntentAvailable(intent, context)) {
146 | context.startActivity(intent);
147 | } else {
148 | Log.e(TAG, "Intent is not available!");
149 | }
150 | }
151 |
152 | /**
153 | * 小米 V7 版本 ROM权限申请
154 | */
155 | public static void goToMiuiPermissionActivity_V7(Context context) {
156 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
157 | intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
158 | intent.putExtra("extra_pkgname", context.getPackageName());
159 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
160 |
161 | if (isIntentAvailable(intent, context)) {
162 | context.startActivity(intent);
163 | } else {
164 | Log.e(TAG, "Intent is not available!");
165 | }
166 | }
167 |
168 | /**
169 | * 小米 V8 版本 ROM权限申请
170 | */
171 | public static void goToMiuiPermissionActivity_V8(Context context) {
172 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
173 | intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
174 | // intent.setPackage("com.miui.securitycenter");
175 | intent.putExtra("extra_pkgname", context.getPackageName());
176 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
177 |
178 | if (isIntentAvailable(intent, context)) {
179 | context.startActivity(intent);
180 | } else {
181 | intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
182 | intent.setPackage("com.miui.securitycenter");
183 | intent.putExtra("extra_pkgname", context.getPackageName());
184 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
185 |
186 | if (isIntentAvailable(intent, context)) {
187 | context.startActivity(intent);
188 | } else {
189 | Log.e(TAG, "Intent is not available!");
190 | }
191 | }
192 | }
193 | }
194 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/OppoUtils.java:
--------------------------------------------------------------------------------
1 | package com.fenggit.floatwindow.rom;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.AppOpsManager;
5 | import android.content.ComponentName;
6 | import android.content.Context;
7 | import android.content.Intent;
8 | import android.os.Binder;
9 | import android.os.Build;
10 | import android.util.Log;
11 |
12 | import java.lang.reflect.Method;
13 |
14 | /**
15 | * Description:
16 | *
17 | * @author Shawn_Dut
18 | * @since 2018-02-01
19 | */
20 | public class OppoUtils {
21 |
22 | private static final String TAG = "OppoUtils";
23 |
24 | /**
25 | * 检测 360 悬浮窗权限
26 | */
27 | public static boolean checkFloatWindowPermission(Context context) {
28 | final int version = Build.VERSION.SDK_INT;
29 | if (version >= 19) {
30 | return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
31 | }
32 | return true;
33 | }
34 |
35 | @TargetApi(Build.VERSION_CODES.KITKAT)
36 | private static boolean checkOp(Context context, int op) {
37 | final int version = Build.VERSION.SDK_INT;
38 | if (version >= 19) {
39 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
40 | try {
41 | Class clazz = AppOpsManager.class;
42 | Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
43 | return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
44 | } catch (Exception e) {
45 | Log.e(TAG, Log.getStackTraceString(e));
46 | }
47 | } else {
48 | Log.e(TAG, "Below API 19 cannot invoke!");
49 | }
50 | return false;
51 | }
52 |
53 | /**
54 | * oppo ROM 权限申请
55 | */
56 | public static void applyOppoPermission(Context context) {
57 | //merge request from https://github.com/zhaozepeng/FloatWindowPermission/pull/26
58 | try {
59 | Intent intent = new Intent();
60 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
61 | //com.coloros.safecenter/.sysfloatwindow.FloatWindowListActivity
62 | ComponentName comp = new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity");//悬浮窗管理页面
63 | intent.setComponent(comp);
64 | context.startActivity(intent);
65 | }
66 | catch(Exception e){
67 | e.printStackTrace();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/QikuUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
3 | */
4 | package com.fenggit.floatwindow.rom;
5 |
6 | import android.annotation.TargetApi;
7 | import android.app.AppOpsManager;
8 | import android.content.Context;
9 | import android.content.Intent;
10 | import android.content.pm.PackageManager;
11 | import android.os.Binder;
12 | import android.os.Build;
13 | import android.util.Log;
14 |
15 | import java.lang.reflect.Method;
16 |
17 | public class QikuUtils {
18 | private static final String TAG = "QikuUtils";
19 |
20 | /**
21 | * 检测 360 悬浮窗权限
22 | */
23 | public static boolean checkFloatWindowPermission(Context context) {
24 | final int version = Build.VERSION.SDK_INT;
25 | if (version >= 19) {
26 | return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
27 | }
28 | return true;
29 | }
30 |
31 | @TargetApi(Build.VERSION_CODES.KITKAT)
32 | private static boolean checkOp(Context context, int op) {
33 | final int version = Build.VERSION.SDK_INT;
34 | if (version >= 19) {
35 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
36 | try {
37 | Class clazz = AppOpsManager.class;
38 | Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
39 | return AppOpsManager.MODE_ALLOWED == (int)method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
40 | } catch (Exception e) {
41 | Log.e(TAG, Log.getStackTraceString(e));
42 | }
43 | } else {
44 | Log.e("", "Below API 19 cannot invoke!");
45 | }
46 | return false;
47 | }
48 |
49 | /**
50 | * 去360权限申请页面
51 | */
52 | public static void applyPermission(Context context) {
53 | Intent intent = new Intent();
54 | intent.setClassName("com.android.settings", "com.android.settings.Settings$OverlaySettingsActivity");
55 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
56 | if (isIntentAvailable(intent, context)) {
57 | context.startActivity(intent);
58 | } else {
59 | intent.setClassName("com.qihoo360.mobilesafe", "com.qihoo360.mobilesafe.ui.index.AppEnterActivity");
60 | if (isIntentAvailable(intent, context)) {
61 | context.startActivity(intent);
62 | } else {
63 | Log.e(TAG, "can't open permission page with particular name, please use " +
64 | "\"adb shell dumpsys activity\" command and tell me the name of the float window permission page");
65 | }
66 | }
67 | }
68 |
69 | private static boolean isIntentAvailable(Intent intent, Context context) {
70 | if (intent == null) {
71 | return false;
72 | }
73 | return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/library/src/main/java/com/fenggit/floatwindow/rom/RomUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
3 | */
4 | package com.fenggit.floatwindow.rom;
5 |
6 | import android.os.Build;
7 | import android.text.TextUtils;
8 | import android.util.Log;
9 |
10 | import java.io.BufferedReader;
11 | import java.io.IOException;
12 | import java.io.InputStreamReader;
13 |
14 | /**
15 | * Description:
16 | *
17 | * @author zhaozp
18 | * @since 2016-05-23
19 | */
20 | public class RomUtils {
21 | private static final String TAG = "RomUtils";
22 |
23 | /**
24 | * 获取 emui 版本号
25 | * @return
26 | */
27 | public static double getEmuiVersion() {
28 | try {
29 | String emuiVersion = getSystemProperty("ro.build.version.emui");
30 | String version = emuiVersion.substring(emuiVersion.indexOf("_") + 1);
31 | return Double.parseDouble(version);
32 | } catch (Exception e) {
33 | e.printStackTrace();
34 | }
35 | return 4.0;
36 | }
37 |
38 | /**
39 | * 获取小米 rom 版本号,获取失败返回 -1
40 | *
41 | * @return miui rom version code, if fail , return -1
42 | */
43 | public static int getMiuiVersion() {
44 | String version = getSystemProperty("ro.miui.ui.version.name");
45 | if (version != null) {
46 | try {
47 | return Integer.parseInt(version.substring(1));
48 | } catch (Exception e) {
49 | Log.e(TAG, "get miui version code error, version : " + version);
50 | }
51 | }
52 | return -1;
53 | }
54 |
55 | public static String getSystemProperty(String propName) {
56 | String line;
57 | BufferedReader input = null;
58 | try {
59 | Process p = Runtime.getRuntime().exec("getprop " + propName);
60 | input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
61 | line = input.readLine();
62 | input.close();
63 | } catch (IOException ex) {
64 | Log.e(TAG, "Unable to read sysprop " + propName, ex);
65 | return null;
66 | } finally {
67 | if (input != null) {
68 | try {
69 | input.close();
70 | } catch (IOException e) {
71 | Log.e(TAG, "Exception while closing InputStream", e);
72 | }
73 | }
74 | }
75 | return line;
76 | }
77 | public static boolean checkIsHuaweiRom() {
78 | return Build.MANUFACTURER.contains("HUAWEI");
79 | }
80 |
81 | /**
82 | * check if is miui ROM
83 | */
84 | public static boolean checkIsMiuiRom() {
85 | return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"));
86 | }
87 |
88 | public static boolean checkIsMeizuRom() {
89 | //return Build.MANUFACTURER.contains("Meizu");
90 | String meizuFlymeOSFlag = getSystemProperty("ro.build.display.id");
91 | if (TextUtils.isEmpty(meizuFlymeOSFlag)){
92 | return false;
93 | }else if (meizuFlymeOSFlag.contains("flyme") || meizuFlymeOSFlag.toLowerCase().contains("flyme")){
94 | return true;
95 | }else {
96 | return false;
97 | }
98 | }
99 |
100 | public static boolean checkIs360Rom() {
101 | //fix issue https://github.com/zhaozepeng/FloatWindowPermission/issues/9
102 | return Build.MANUFACTURER.contains("QiKU")
103 | || Build.MANUFACTURER.contains("360");
104 | }
105 |
106 | public static boolean checkIsOppoRom() {
107 | //https://github.com/zhaozepeng/FloatWindowPermission/pull/26
108 | return Build.MANUFACTURER.contains("OPPO") || Build.MANUFACTURER.contains("oppo");
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | floatwindow
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 | rootProject.name='Floatwindow'
3 |
--------------------------------------------------------------------------------