This commit is contained in:
wangcongtao
2020-05-22 10:49:58 +08:00
parent 2c3db70ef3
commit ded0e5b493
16 changed files with 163 additions and 94 deletions

View File

@@ -2,19 +2,12 @@ package com.mogo.module.common.dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.StringRes;
import com.mogo.module.common.R;
import com.mogo.module.common.utils.CarSeries;
import com.mogo.utils.WindowUtils;
import com.mogo.module.common.wm.WindowManagerView;
/**
* @author congtaowang
@@ -25,54 +18,22 @@ import com.mogo.utils.WindowUtils;
public class WMDialog implements DialogInterface {
private WMDialogParams mParams;
private WindowManager mWindowManager;
private boolean mIsShowing = false;
private View mContentView;
private WindowManager.LayoutParams mLayoutParams;
private WindowManagerView mWindowManagerView;
private WMDialog( WMDialogParams params ) {
this.mParams = params;
mWindowManagerView = new WindowManagerView.Builder( mParams.mContext ).contentView( R.layout.module_commons_layout_wm_dialog ).build();
initViews();
}
public void show() {
if ( mIsShowing ) {
return;
}
mIsShowing = true;
if ( mWindowManager == null ) {
mWindowManager = ( WindowManager ) mParams.mContext.getApplicationContext().getSystemService( Context.WINDOW_SERVICE );
}
if ( mContentView == null ) {
mLayoutParams = new WindowManager.LayoutParams();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
mLayoutParams.format = PixelFormat.TRANSLUCENT;
mLayoutParams.gravity = Gravity.CENTER;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
if ( CarSeries.getSeries() == CarSeries.CAR_SERIES_F80X ) {
mLayoutParams.width = 1920;
mLayoutParams.height = 1080;
} else {
mLayoutParams.width = WindowUtils.getScreenWidth( mParams.mContext );
mLayoutParams.height = WindowUtils.getScreenHeight( mParams.mContext );
}
mLayoutParams.dimAmount = 0.5f;
mLayoutParams.x = 0;
mLayoutParams.y = 0;
mContentView = initViews();
}
mWindowManager.addView( mContentView, mLayoutParams );
mWindowManagerView.show();
}
private View initViews() {
View contentView = LayoutInflater.from( mParams.mContext ).inflate( R.layout.module_commons_layout_wm_dialog, null );
TextView ok = contentView.findViewById( R.id.module_commons_wm_dialog_button_ok );
TextView cancel = contentView.findViewById( R.id.module_commons_wm_dialog_button_cancel );
TextView content = contentView.findViewById( R.id.module_commons_wm_dialog_content );
private void initViews() {
TextView ok = mWindowManagerView.findViewById( R.id.module_commons_wm_dialog_button_ok );
TextView cancel = mWindowManagerView.findViewById( R.id.module_commons_wm_dialog_button_cancel );
TextView content = mWindowManagerView.findViewById( R.id.module_commons_wm_dialog_content );
ok.setText( mParams.mOkButtonText );
if ( mParams.mOnOkButtonClickListener != null ) {
@@ -92,7 +53,6 @@ public class WMDialog implements DialogInterface {
}
content.setText( mParams.mContent );
return contentView;
}
@Override
@@ -102,20 +62,11 @@ public class WMDialog implements DialogInterface {
@Override
public void dismiss() {
if ( !mIsShowing ) {
return;
}
if ( mContentView != null ) {
mWindowManager.removeViewImmediate( mContentView );
}
if ( mParams.mOnDialogDismissListener != null ) {
mParams.mOnDialogDismissListener.onDismiss( this );
}
mIsShowing = false;
mWindowManagerView.dismiss();
}
public boolean isShowing() {
return mIsShowing;
return mWindowManagerView.isShowing();
}
public static class Builder {
@@ -129,16 +80,6 @@ public class WMDialog implements DialogInterface {
mParams.mContext = context;
}
// public Builder setTitle( CharSequence title ) {
// mParams.mTitle = title;
// return this;
// }
//
// public Builder setTitle( @StringRes int title ) {
// mParams.mTitle = mContext.getString( title );
// return this;
// }
public Builder setContent( CharSequence content ) {
mParams.mContent = content;
return this;

View File

@@ -0,0 +1,118 @@
package com.mogo.module.common.wm;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import com.mogo.module.common.utils.CarSeries;
import com.mogo.utils.WindowUtils;
/**
* @author congtaowang
* @since 2020-05-21
* <p>
* 往 window manager 添加view
*/
public class WindowManagerView {
private WMViewParams mParams;
private boolean mIsShowing;
private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
private WindowManagerView( WMViewParams params ) {
this.mParams = params;
init();
}
private void init() {
mWindowManager = ( WindowManager ) mParams.mContext.getApplicationContext().getSystemService( Context.WINDOW_SERVICE );
mLayoutParams = new WindowManager.LayoutParams();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
mLayoutParams.format = PixelFormat.TRANSLUCENT;
mLayoutParams.gravity = Gravity.CENTER;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
if ( CarSeries.getSeries() == CarSeries.CAR_SERIES_F80X ) {
mLayoutParams.width = 1920;
mLayoutParams.height = 1080;
} else {
mLayoutParams.width = WindowUtils.getScreenWidth( mParams.mContext );
mLayoutParams.height = WindowUtils.getScreenHeight( mParams.mContext );
}
mLayoutParams.dimAmount = 0.5f;
mLayoutParams.x = 0;
mLayoutParams.y = 0;
}
public boolean isShowing() {
return mIsShowing;
}
public < T extends View > T findViewById( @IdRes int id ) {
return mParams.mContentView.findViewById( id );
}
public void show() {
if ( mIsShowing ) {
return;
}
mIsShowing = true;
mWindowManager.addView( mParams.mContentView, mLayoutParams );
}
public void dismiss() {
if ( !mIsShowing ) {
return;
}
if ( mParams != null ) {
mWindowManager.removeViewImmediate( mParams.mContentView );
}
mIsShowing = false;
}
public static class Builder {
private WMViewParams mParams = null;
public Builder( Context context ) {
mParams = new WMViewParams();
mParams.mContext = context;
}
public Builder contentView( View contentView ) {
mParams.mContentView = contentView;
return this;
}
public Builder contentView( @LayoutRes int contentViewId ) {
mParams.mContentView = LayoutInflater.from( mParams.mContext ).inflate( contentViewId, null );
return this;
}
public WindowManagerView build() {
if ( mParams.mContentView == null ) {
throw new NullPointerException( "WMViewParams#mContentView must not be null." );
}
return new WindowManagerView( mParams );
}
}
public static class WMViewParams {
public View mContentView;
public Context mContext;
}
}