|
@@ -1,6 +1,7 @@
|
|
|
package com.sunwin.visitorapp.utils;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
+import android.app.AlertDialog;
|
|
|
import android.app.Presentation;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
@@ -12,40 +13,95 @@ import android.provider.Settings;
|
|
|
import android.view.Display;
|
|
|
import android.view.WindowManager;
|
|
|
|
|
|
-import androidx.appcompat.app.AlertDialog;
|
|
|
+import com.sunwin.visitorapp.view.DisplayWelcome;
|
|
|
|
|
|
+import java.lang.reflect.Constructor;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
+import timber.log.Timber;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 多屏显示类
|
|
|
*/
|
|
|
public class PresentationManager {
|
|
|
|
|
|
+ private static volatile Presentation instance;
|
|
|
+
|
|
|
+
|
|
|
+ public static boolean hasInit(){
|
|
|
+ return instance != null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void checkAndShow(Context context){
|
|
|
+ Timber.e("checkAndShow");
|
|
|
+ if(PresentationManager.canMuteScreen(context)){
|
|
|
+ Timber.e("canMuteScreen");
|
|
|
+ if(PresentationManager.permissionsMuteScreen(context)){
|
|
|
+ Timber.e("permissionsMuteScreen");
|
|
|
+ if(!PresentationManager.hasInit())PresentationManager.instanceInit(DisplayWelcome.class,context);
|
|
|
+ PresentationManager.show();
|
|
|
+ }else{
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
|
+ new AlertDialog.Builder(context).setTitle("开启副屏权限")
|
|
|
+ .setMessage("需要打开在其他应用上层显示,请去设置中开启此权限")
|
|
|
+ .setNegativeButton("取消",null)
|
|
|
+ .setPositiveButton("开启", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
|
|
|
+ intent.setData(Uri.parse("package:" + context.getPackageName()));
|
|
|
+ context.startActivity(intent);
|
|
|
+ }
|
|
|
+ }).create().show();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static boolean canMuteScreen(Context context){
|
|
|
DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
|
|
|
Display[] presentationDisplays = displayManager.getDisplays();
|
|
|
return presentationDisplays.length > 1;
|
|
|
}
|
|
|
|
|
|
- public static Display[] getMuteScreenDisplays(Context context){
|
|
|
- DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
|
|
|
- return displayManager.getDisplays();
|
|
|
- }
|
|
|
+
|
|
|
public static boolean permissionsMuteScreen(Context context){
|
|
|
boolean needApply = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context);
|
|
|
return !needApply;
|
|
|
}
|
|
|
- public static void presentationShow(Presentation presentation) {
|
|
|
+ public static synchronized boolean instanceInit(Class<? extends Presentation> presentationClass,Context context) {
|
|
|
+ if(instance!=null){
|
|
|
+ instance.dismiss();
|
|
|
+ }
|
|
|
try {
|
|
|
+ DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
|
|
|
+ Constructor<? extends Presentation> constructor = presentationClass.getConstructor(Context.class,Display.class);
|
|
|
+ Presentation presentation = constructor.newInstance(context,displayManager.getDisplays()[1]);
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
Objects.requireNonNull(presentation.getWindow()).setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
|
|
|
} else {
|
|
|
Objects.requireNonNull(presentation.getWindow()).setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
|
|
}
|
|
|
- presentation.show();
|
|
|
+ instance = presentation;
|
|
|
+ return true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static synchronized void show() {
|
|
|
+ if(instance!=null)instance.show();
|
|
|
+ }
|
|
|
+ public static synchronized void hide() {
|
|
|
+ if(instance!=null)instance.hide();
|
|
|
+ }
|
|
|
+ public static synchronized void dismiss() {
|
|
|
+ if(instance!=null){
|
|
|
+ instance.dismiss();
|
|
|
+ instance = null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|