Skip to content

Commit b366439

Browse files
committed
添加测试悬浮窗
1 parent 35c26ea commit b366439

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3278
-40
lines changed

effectivecard/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ repositories {
3434
dependencies {
3535
implementation fileTree(dir: 'libs', include: ['*.jar'])
3636
implementation project(':mxlibrary')
37+
implementation project(':floatwindow')
3738
implementation 'androidx.appcompat:appcompat:1.1.0'
3839
testImplementation 'junit:junit:4.12'
3940
androidTestImplementation 'androidx.test.ext:junit:1.1.1'

effectivecard/src/main/java/com/codemx/effectivecard/CardWindowManager.java

Lines changed: 86 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,101 @@
11
package com.codemx.effectivecard;
22

33
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;
66
import android.view.Gravity;
77
import android.view.LayoutInflater;
8+
import android.view.MotionEvent;
89
import android.view.View;
910
import android.view.WindowManager;
11+
import android.widget.ImageView;
1012

11-
import com.codemx.effectivecard.launcherclient.Constant;
13+
import com.android.mxlibrary.util.XLog;
1214
import com.codemx.effectivecard.launcherclient.ILauncherOverlayCallback;
1315
import com.codemx.effectivecard.launcherclient.MxLayoutParams;
1416
import com.codemx.effectivecard.launcherclient.MxMessage;
17+
import com.codemx.floatwindow.FloatWindow;
1518

1619
/**
1720
* Created by yuchuan
1821
* DATE 2020/4/17
1922
* TIME 16:03
2023
*/
21-
public class CardWindowManager implements IWindowCallback{
24+
public class CardWindowManager implements IWindowCallback {
2225

2326
private Context mContext;
24-
private Context mCardContext;
25-
private WindowManager mWindowManager;
26-
private WindowManager.LayoutParams mParams;
2727
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;
2833

29-
public CardWindowManager(Context context) {
34+
CardWindowManager(Context context) {
3035
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;
5039
}
5140

52-
private void initView() {
53-
mContentView = LayoutInflater.from(mCardContext).inflate(R.layout.layout_card, null);
54-
}
41+
private boolean isShow = false;
5542

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;
6096

6197
public void hideWindow() {
62-
mWindowManager.removeView(mContentView);
98+
FloatWindow.destroy();
6399
}
64100

65101
// 向Launcher通信的回调函数,对应LauncherOverlayCallbacks
@@ -72,7 +108,9 @@ public void startScroll() {
72108

73109
@Override
74110
public void onScroll(float progress, boolean isRtl) {
75-
111+
if (mWindowManager != null) {
112+
mWindowParams.x = -(int) (mScreenWidth * (1 - progress));
113+
}
76114
}
77115

78116
@Override
@@ -81,14 +119,23 @@ public void endScroll() {
81119
}
82120

83121
@Override
84-
public void windowAttached(MxLayoutParams layoutParams, ILauncherOverlayCallback overlayCallback, int flags) {
85-
122+
public void windowAttached(final MxLayoutParams layoutParams, ILauncherOverlayCallback overlayCallback, int flags) {
86123
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+
});
87134
}
88135

89136
@Override
90137
public void windowDetached(boolean isChangingConfigurations) {
91-
138+
hideWindow();
92139
}
93140

94141
@Override

effectivecard/src/main/java/com/codemx/effectivecard/launcherclient/LauncherClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ private void applyWindowToken() {
258258
Log.d("LauncherClient", "applyWindowToken mServiceConnectionOptions=" + mServiceConnectionOptions);
259259
MxLayoutParams windowAttrs = new MxLayoutParams();
260260
windowAttrs.copyFrom(mWindowAttrs);
261+
// windowAttrs.setWidth(mWindowAttrs.width);
262+
// windowAttrs.setHeight(mWindowAttrs.height);
261263
mOverlay.windowAttached(windowAttrs, mLauncherOverlayCallbacks, mServiceConnectionOptions);
262264
if (mIsResumed) {
263265
mOverlay.onResume();

floatwindow/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 29
5+
buildToolsVersion "29.0.3"
6+
7+
defaultConfig {
8+
minSdkVersion 19
9+
targetSdkVersion 29
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14+
consumerProguardFiles 'consumer-rules.pro'
15+
}
16+
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
24+
}
25+
26+
dependencies {
27+
implementation fileTree(dir: 'libs', include: ['*.jar'])
28+
29+
implementation 'androidx.appcompat:appcompat:1.1.0'
30+
implementation project(path: ':mxlibrary')
31+
32+
testImplementation 'junit:junit:4.12'
33+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
34+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35+
}

floatwindow/consumer-rules.pro

Whitespace-only changes.

0 commit comments

Comments
 (0)