1
1
package com .codemx .effectivecard ;
2
2
3
3
import android .content .Context ;
4
- import android .content . pm . PackageManager ;
5
- import android .graphics . PixelFormat ;
4
+ import android .os . Handler ;
5
+ import android .os . Looper ;
6
6
import android .view .Gravity ;
7
7
import android .view .LayoutInflater ;
8
+ import android .view .MotionEvent ;
8
9
import android .view .View ;
9
10
import android .view .WindowManager ;
11
+ import android .widget .ImageView ;
10
12
11
- import com .codemx . effectivecard . launcherclient . Constant ;
13
+ import com .android . mxlibrary . util . XLog ;
12
14
import com .codemx .effectivecard .launcherclient .ILauncherOverlayCallback ;
13
15
import com .codemx .effectivecard .launcherclient .MxLayoutParams ;
14
16
import com .codemx .effectivecard .launcherclient .MxMessage ;
17
+ import com .codemx .floatwindow .FloatWindow ;
15
18
16
19
/**
17
20
* Created by yuchuan
18
21
* DATE 2020/4/17
19
22
* TIME 16:03
20
23
*/
21
- public class CardWindowManager implements IWindowCallback {
24
+ public class CardWindowManager implements IWindowCallback {
22
25
23
26
private Context mContext ;
24
- private Context mCardContext ;
25
- private WindowManager mWindowManager ;
26
- private WindowManager .LayoutParams mParams ;
27
27
private View mContentView ;
28
+ private Handler mHandler ;
29
+ private WindowManager mWindowManager ;
30
+ private WindowManager .LayoutParams mWindowParams ;
31
+ private final int mScreenWidth ;
32
+ private final int mScreenHeight ;
28
33
29
- public CardWindowManager (Context context ) {
34
+ CardWindowManager (Context context ) {
30
35
mContext = context ;
31
- initApplicationWindow ();
32
- }
33
-
34
- private void initApplicationWindow () {
35
- try {
36
- mCardContext = mContext .createPackageContext (Constant .GSA_PACKAGE , Context .CONTEXT_IGNORE_SECURITY );
37
- mWindowManager = (WindowManager ) mCardContext .getSystemService (Context .WINDOW_SERVICE );
38
- mParams = new WindowManager .LayoutParams ();
39
- mParams .packageName = Constant .GSA_PACKAGE ;
40
- mParams .type = WindowManager .LayoutParams .TYPE_APPLICATION_MEDIA ;
41
- mParams .format = PixelFormat .RGBA_8888 ;
42
- mParams .flags = WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE |
43
- WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS ;
44
- mParams .gravity = Gravity .TOP | Gravity .LEFT ;
45
- mParams .width = WindowManager .LayoutParams .MATCH_PARENT ;
46
- mParams .height = WindowManager .LayoutParams .MATCH_PARENT ;
47
- } catch (PackageManager .NameNotFoundException n ) {
48
- n .printStackTrace ();
49
- }
36
+ mHandler = new Handler (Looper .getMainLooper ());
37
+ mScreenWidth = context .getResources ().getDisplayMetrics ().widthPixels ;
38
+ mScreenHeight = context .getResources ().getDisplayMetrics ().heightPixels ;
50
39
}
51
40
52
- private void initView () {
53
- mContentView = LayoutInflater .from (mCardContext ).inflate (R .layout .layout_card , null );
54
- }
41
+ private boolean isShow = false ;
55
42
56
- public void showWindow () {
57
- initView ();
58
- mWindowManager .addView (mContentView , mParams );
59
- }
43
+ private void showWindow (MxLayoutParams layoutParams ) {
44
+ if (isShow ) {
45
+ return ;
46
+ }
47
+ isShow = true ;
48
+ mContentView = LayoutInflater .from (mContext ).inflate (R .layout .layout_card , null );
49
+ ImageView imageView = new ImageView (mContext );
50
+ imageView .setImageResource (R .drawable .ic_home_white_24dp );
51
+
52
+ mWindowManager = (WindowManager ) mContext .getSystemService (Context .WINDOW_SERVICE );
53
+ mWindowParams = new WindowManager .LayoutParams ();
54
+ // mWindowParams.width = layoutParams.width;
55
+ // mWindowParams.height = layoutParams.height;
56
+ mWindowParams .width = 600 ;
57
+ mWindowParams .height = 600 ;
58
+ XLog .d (XLog .getTag (), "width= " + layoutParams .width + " ,height= " + layoutParams .height );
59
+ mWindowParams .x = 0 ;
60
+ mWindowParams .y = 0 ;
61
+ mWindowParams .gravity = Gravity .TOP | Gravity .START ;
62
+ mWindowParams .flags = WindowManager .LayoutParams .FLAG_NOT_FOCUSABLE
63
+ | WindowManager .LayoutParams .FLAG_NOT_TOUCH_MODAL
64
+ | WindowManager .LayoutParams .FLAG_FULLSCREEN
65
+ | WindowManager .LayoutParams .FLAG_LAYOUT_INSET_DECOR
66
+ | WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER
67
+ | WindowManager .LayoutParams .FLAG_LAYOUT_NO_LIMITS ;
68
+ mWindowParams .type = WindowManager .LayoutParams .TYPE_APPLICATION_OVERLAY ;
69
+ mWindowManager .addView (mContentView , mWindowParams );
70
+ mContentView .setOnTouchListener (new View .OnTouchListener () {
71
+ @ Override
72
+ public boolean onTouch (View v , MotionEvent event ) {
73
+ switch (event .getAction ()) {
74
+ case MotionEvent .ACTION_DOWN :
75
+ mStartX = event .getRawX ();
76
+ XLog .d (XLog .getTag (), "ACTION_DOWN= " + event .getRawX ());
77
+ break ;
78
+ case MotionEvent .ACTION_MOVE :
79
+ // mWindowParams.x += (int) (event.getRawX() - mStartX);
80
+ // mStartX = event.getRawX();
81
+ // XLog.d(XLog.getTag(), "ACTION_MOVE= " + event.getRawX());
82
+ // mWindowManager.updateViewLayout(v, mWindowParams);
83
+ v .setTranslationX (-100 );
84
+ break ;
85
+ case MotionEvent .ACTION_UP :
86
+ break ;
87
+ default :
88
+ break ;
89
+ }
90
+ return false ;
91
+ }
92
+ });
93
+ }
94
+
95
+ private float mStartX ;
60
96
61
97
public void hideWindow () {
62
- mWindowManager . removeView ( mContentView );
98
+ FloatWindow . destroy ( );
63
99
}
64
100
65
101
// 向Launcher通信的回调函数,对应LauncherOverlayCallbacks
@@ -72,7 +108,9 @@ public void startScroll() {
72
108
73
109
@ Override
74
110
public void onScroll (float progress , boolean isRtl ) {
75
-
111
+ if (mWindowManager != null ) {
112
+ mWindowParams .x = -(int ) (mScreenWidth * (1 - progress ));
113
+ }
76
114
}
77
115
78
116
@ Override
@@ -81,14 +119,23 @@ public void endScroll() {
81
119
}
82
120
83
121
@ Override
84
- public void windowAttached (MxLayoutParams layoutParams , ILauncherOverlayCallback overlayCallback , int flags ) {
85
-
122
+ public void windowAttached (final MxLayoutParams layoutParams , ILauncherOverlayCallback overlayCallback , int flags ) {
86
123
mOverlayCallback = overlayCallback ;
124
+ mHandler .post (new Runnable () {
125
+ @ Override
126
+ public void run () {
127
+ try {
128
+ showWindow (layoutParams );
129
+ } catch (Exception e ) {
130
+ e .printStackTrace ();
131
+ }
132
+ }
133
+ });
87
134
}
88
135
89
136
@ Override
90
137
public void windowDetached (boolean isChangingConfigurations ) {
91
-
138
+ hideWindow ();
92
139
}
93
140
94
141
@ Override
0 commit comments