Skip to content

Commit

Permalink
适配不同机型悬浮窗权限判断
Browse files Browse the repository at this point in the history
  • Loading branch information
caiyonglong committed Sep 8, 2018
1 parent 94444ce commit 20e43b9
Show file tree
Hide file tree
Showing 19 changed files with 774 additions and 112 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
applicationId "com.cyl.musiclake"
minSdkVersion 21
targetSdkVersion 27
versionCode 16
versionName "4.1.4"
versionCode 17
versionName "4.1.5"
multiDexEnabled true
android.compileOptions.sourceCompatibility 1.8
android.compileOptions.targetCompatibility 1.8
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/cyl/musiclake/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public class Constants {

public static final String FORMAT = "json";


/**
* 悬浮窗权限requestCode
*/
public static final int REQUEST_CODE_FLOAT_WINDOW = 0x123;

//在线音乐
public static final String FILENAME_MP3 = ".mp3";
public static final String FILENAME_LRC = ".lrc";
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/cyl/musiclake/common/Extras.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cyl.musiclake.common;

import org.jetbrains.annotations.Nullable;

/**
* 作者:yonglong on 2016/9/24 10:57
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private boolean isHome() {
*/
private void createFloatLyricView(Context context) {
try {
WindowManager windowManager = getWindowManager(context);
WindowManager windowManager = getWindowManager();
Point size = new Point();
//获取屏幕宽高
windowManager.getDefaultDisplay().getSize(size);
Expand Down Expand Up @@ -208,10 +208,14 @@ private void createFloatLyricView(Context context) {
* @param context 必须为应用程序的Context.
*/
public void removeFloatLyricView(Context context) {
if (mFloatLyricView != null) {
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(mFloatLyricView);
mFloatLyricView = null;
try {
if (mFloatLyricView != null) {
WindowManager windowManager = getWindowManager();
windowManager.removeView(mFloatLyricView);
mFloatLyricView = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}

Expand Down Expand Up @@ -253,12 +257,11 @@ private static boolean isWindowShowing() {
/**
* 如果WindowManager还未创建,则创建一个新的WindowManager返回。否则返回当前已创建的WindowManager。
*
* @param context 必须为应用程序的Context.
* @return WindowManager的实例,用于控制在屏幕上添加或移除悬浮窗。
*/
private static WindowManager getWindowManager(Context context) {
private static WindowManager getWindowManager() {
if (mWindowManager == null) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mWindowManager = (WindowManager) MusicApp.getAppContext().getSystemService(Context.WINDOW_SERVICE);
}
return mWindowManager;
}
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/com/cyl/musiclake/player/MusicPlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1307,22 +1307,11 @@ private void handleCommandIntent(Intent intent) {
* 开启歌词
*/
private void startFloatLyric() {
if (SystemUtils.isOpenSystemWindow()) {
if (SystemUtils.isOpenFloatWindow()) {
showLyric = !showLyric;
showDesktopLyric(showLyric);
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + MusicApp.getAppContext().getPackageName()));
MusicApp.getAppContext().startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
ToastUtils.show(getString(R.string.float_settings));
}
} else {
ToastUtils.show(getString(R.string.float_settings));
}
SystemUtils.applySystemWindow();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import java.util.*
*/

object OnlinePlaylistUtils {

/**
* 保存当前歌单列表
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ protected void initInjector() {

private void setupViewPager(ViewPager viewPager) {
PageAdapter adapter = new PageAdapter(getChildFragmentManager());
adapter.addFragment(SongsFragment.Companion.newInstance(), "本地");
adapter.addFragment(AlbumFragment.newInstance(), "专辑");
adapter.addFragment(ArtistFragment.newInstance(), "艺术家");
adapter.addFragment(FoldersFragment.Companion.newInstance(), "文件夹");
adapter.addFragment(SongsFragment.Companion.newInstance(), getString(R.string.local_title));
adapter.addFragment(AlbumFragment.newInstance(), getString(R.string.album_title));
adapter.addFragment(ArtistFragment.newInstance(), getString(R.string.artist_title));
adapter.addFragment(FoldersFragment.Companion.newInstance(), getString(R.string.folder_title));
viewPager.setAdapter(adapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ class MyMusicFragment : BaseFragment<MyMusicPresenter>(), MyMusicContract.View {

}


override fun initInjector() {
mFragmentComponent.inject(this)
}

override fun listener() {
mAdapter?.setOnItemClickListener { _, _, position -> }

playlistAddIv.setOnClickListener {
if (UserStatus.getLoginStatus()) {
val dialog = CreatePlaylistDialog.newInstance()
dialog.show(childFragmentManager, TAG_CREATE)
} else {
ToastUtils.show("请登录")
ToastUtils.show(getString(R.string.prompt_login))
}
}
playlistManagerIv.setOnClickListener {
Expand Down Expand Up @@ -96,6 +93,7 @@ class MyMusicFragment : BaseFragment<MyMusicPresenter>(), MyMusicContract.View {
mAdapter?.setNewData(playlists)
if (playlists.size == 0) {
showEmptyState()
mAdapter?.setEmptyView(R.layout.view_playlist_empty)
}
hideLoading()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import javax.inject.Inject

class MyMusicPresenter @Inject
constructor() : BasePresenter<MyMusicContract.View>(), MyMusicContract.Presenter {
private var playlists = mutableListOf<Playlist>()

/**
* 更新播放历史
*/
Expand Down Expand Up @@ -81,15 +79,14 @@ constructor() : BasePresenter<MyMusicContract.View>(), MyMusicContract.Presenter
val mIsLogin = UserStatus.getLoginStatus()
if (mIsLogin) {
OnlinePlaylistUtils.getOnlinePlaylist(success = {
playlists = OnlinePlaylistUtils.playlists
mView?.showPlaylist(playlists)
mView?.showPlaylist(OnlinePlaylistUtils.playlists)
}, fail = {
ToastUtils.show(it)
mView?.showPlaylist(playlists)
mView?.showPlaylist(OnlinePlaylistUtils.playlists)
})
} else {
playlists.clear()
mView?.showPlaylist(playlists)
OnlinePlaylistUtils.playlists.clear()
mView?.showPlaylist(OnlinePlaylistUtils.playlists)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected void onDestroy() {

@Override
protected void initData() {
mVersion.setText(String.format("版本号v%s", BuildConfig.VERSION_NAME));
mVersion.setText(String.format("版本号 v%s", BuildConfig.VERSION_NAME));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.provider.Settings;
import android.text.InputType;

import com.cyl.musiclake.common.Constants;
import com.cyl.musiclake.utils.LogUtil;

import com.afollestad.materialdialogs.DialogAction;
Expand All @@ -26,6 +27,7 @@
import com.cyl.musiclake.utils.SPUtils;
import com.cyl.musiclake.utils.SystemUtils;
import com.cyl.musiclake.utils.ToastUtils;
import com.cyl.musiclake.utils.rom.FloatUtil;
import com.tencent.bugly.beta.Beta;

import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -225,14 +227,13 @@ public boolean onPreferenceClick(Preference preference) {
*/
private void checkLyricPermission() {
try {
if (!SystemUtils.isOpenSystemWindow() && SystemUtils.isMarshmallow()) {
ToastUtils.show(getActivity(), "请手动打开显示悬浮窗权限");
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
startActivityForResult(intent, 100);
if (!SystemUtils.isOpenFloatWindow()) {
ToastUtils.show(getString(R.string.float_window_manual_open));
SystemUtils.applySystemWindow();
mLyricCheckBox.setChecked(true);
} else {
mLyricCheckBox.setChecked(true);
ToastUtils.show(getActivity(), "显示悬浮窗权限已开通");
ToastUtils.show(getString(R.string.float_window_is_ready));
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -252,11 +253,11 @@ public void onResume() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (SystemUtils.isOpenSystemWindow()) {
if (requestCode == Constants.REQUEST_CODE_FLOAT_WINDOW) {
if (SystemUtils.isOpenFloatWindow()) {
checkLyricPermission();
} else {
ToastUtils.show(MusicApp.getAppContext(), "悬浮窗权限已被拒绝!");
ToastUtils.show(MusicApp.getAppContext(), getString(R.string.float_window_is_refused));
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/java/com/cyl/musiclake/utils/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.support.annotation.RequiresApi;

import com.cyl.musiclake.MusicApp;
import com.cyl.musiclake.utils.rom.FloatUtil;

import java.util.List;

Expand Down Expand Up @@ -48,26 +49,23 @@ public static boolean isKITKAT() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}


/**
* 判断是否打开悬浮窗权限&&“有权查看使用权限的应用”这个选项
* 判断是否打开“悬浮窗权限”
*
* @return
*/
public static boolean isOpenFloatWindow() {
return isOpenSystemWindow();
return FloatUtil.INSTANCE.checkPermission(MusicApp.getAppContext());
}

/**
* 判断是否打开“悬浮窗权限”
* 检查申请打开“悬浮窗权限”
*
* @return
*/
public static boolean isOpenSystemWindow() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(MusicApp.getAppContext());
} else {
return true;
}
public static void applySystemWindow() {
FloatUtil.INSTANCE.applyOrShowFloatWindow(MusicApp.getAppContext());
}


Expand Down
110 changes: 110 additions & 0 deletions app/src/main/java/com/cyl/musiclake/utils/rom/FloatUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.cyl.musiclake.utils.rom

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.Log

/**
* Created by cyl on 2018/6/29.
*/
object FloatUtil {
fun applyOrShowFloatWindow(context: Context) {
if (checkPermission(context)) {
} else {
applyPermission(context)
}
}

fun checkPermission(context: Context): Boolean {
//6.0 版本之后由于 google 增加了对悬浮窗权限的管理,所以方式就统一了
if (Build.VERSION.SDK_INT < 23) {
when {
RomUtils.checkIsMiuiRom() -> return miuiPermissionCheck(context)
RomUtils.checkIsMeizuRom() -> return meizuPermissionCheck(context)
RomUtils.checkIsHuaweiRom() -> return huaweiPermissionCheck(context)
RomUtils.checkIs360Rom() -> return qikuPermissionCheck(context)
RomUtils.checkIsOppoRom() -> return oppoROMPermissionCheck(context)
else -> {
}
}
}
return commonROMPermissionCheck(context)
}

private fun huaweiPermissionCheck(context: Context): Boolean {
return HuaweiUtils.checkFloatWindowPermission(context)
}

private fun miuiPermissionCheck(context: Context): Boolean {
return MiuiUtils.checkFloatWindowPermission(context)
}

private fun meizuPermissionCheck(context: Context): Boolean {
return MeizuUtils.checkFloatWindowPermission(context)
}

private fun qikuPermissionCheck(context: Context): Boolean {
return QikuUtils.checkFloatWindowPermission(context)
}

private fun oppoROMPermissionCheck(context: Context): Boolean {
return OppoUtils.checkFloatWindowPermission(context)
}

private fun commonROMPermissionCheck(context: Context): Boolean {
//最新发现魅族6.0的系统这种方式不好用,天杀的,只有你是奇葩,没办法,单独适配一下
return if (RomUtils.checkIsMeizuRom()) {
meizuPermissionCheck(context)
} else {
var result: Boolean? = true
if (Build.VERSION.SDK_INT >= 23) {
try {
val clazz = Settings::class.java
val canDrawOverlays = clazz!!.getDeclaredMethod("canDrawOverlays", Context::class.java)
result = canDrawOverlays.invoke(null, context) as Boolean
} catch (e: Exception) {
Log.e("FloatUtil", Log.getStackTraceString(e))
}
}
result!!
}
}

fun applyPermission(context: Context) {
if (Build.VERSION.SDK_INT < 23) {
when {
RomUtils.checkIsMiuiRom() -> MiuiUtils.applyMiuiPermission(context)
RomUtils.checkIsMeizuRom() -> MeizuUtils.applyPermission(context)
RomUtils.checkIsHuaweiRom() -> HuaweiUtils.applyPermission(context)
RomUtils.checkIs360Rom() -> QikuUtils.applyPermission(context)
RomUtils.checkIsOppoRom() -> OppoUtils.applyOppoPermission(context)
}
} else {
if (RomUtils.checkIsMeizuRom()) {
MeizuUtils.applyPermission(context)
} else {
commonROMPermissionApplyInternal(context)
}
}
}

private fun dp2px(context: Context, dp: Float): Int {
val scale = context.resources.displayMetrics.density
return (dp * scale + 0.5f).toInt()
}


@Throws(NoSuchFieldException::class, IllegalAccessException::class)
fun commonROMPermissionApplyInternal(context: Context) {
val clazz = Settings::class.java
val field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION")

val intent = Intent(field.get(null).toString())
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.data = Uri.parse("package:" + context.packageName)
context.startActivity(intent)
}
}
Loading

0 comments on commit 20e43b9

Please sign in to comment.