mFloatWindowMap;
30 |
31 | public static IFloatWindow get() {
32 | return get(mDefaultTag);
33 | }
34 |
35 | public static IFloatWindow get(@NonNull String tag) {
36 | return mFloatWindowMap == null ? null : mFloatWindowMap.get(tag);
37 | }
38 |
39 | private static B mBuilder = null;
40 |
41 | @MainThread
42 | public static B with(@NonNull Context applicationContext) {
43 | return mBuilder = new B(applicationContext);
44 | }
45 |
46 | public static void destroy() {
47 | destroy(mDefaultTag);
48 | }
49 |
50 | public static void destroy(String tag) {
51 | if (mFloatWindowMap == null || !mFloatWindowMap.containsKey(tag)) {
52 | return;
53 | }
54 | mFloatWindowMap.get(tag).dismiss();
55 | mFloatWindowMap.remove(tag);
56 | }
57 |
58 | public static class B {
59 | Context mApplicationContext;
60 | View mView;
61 | private int mLayoutId;
62 | int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
63 | int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
64 | int gravity = Gravity.TOP | Gravity.START;
65 | int xOffset;
66 | int yOffset;
67 | boolean mShow = true;
68 | Class[] mActivities;
69 | int mMoveType = MoveType.slide;
70 | int mSlideLeftMargin;
71 | int mSlideRightMargin;
72 | long mDuration = 300;
73 | TimeInterpolator mInterpolator;
74 | private String mTag = mDefaultTag;
75 | boolean mDesktopShow;
76 | PermissionListener mPermissionListener;
77 | ViewStateListener mViewStateListener;
78 |
79 | private B() {
80 |
81 | }
82 |
83 | B(Context applicationContext) {
84 | mApplicationContext = applicationContext;
85 | }
86 |
87 | public B setView(@NonNull View view) {
88 | mView = view;
89 | return this;
90 | }
91 |
92 | public B setView(@LayoutRes int layoutId) {
93 | mLayoutId = layoutId;
94 | return this;
95 | }
96 |
97 | public B setWidth(int width) {
98 | mWidth = width;
99 | return this;
100 | }
101 |
102 | public B setHeight(int height) {
103 | mHeight = height;
104 | return this;
105 | }
106 |
107 | public B setWidth(@Screen.screenType int screenType, float ratio) {
108 | mWidth = (int) ((screenType == Screen.width ?
109 | Util.getScreenWidth(mApplicationContext) :
110 | Util.getScreenHeight(mApplicationContext)) * ratio);
111 | return this;
112 | }
113 |
114 |
115 | public B setHeight(@Screen.screenType int screenType, float ratio) {
116 | mHeight = (int) ((screenType == Screen.width ?
117 | Util.getScreenWidth(mApplicationContext) :
118 | Util.getScreenHeight(mApplicationContext)) * ratio);
119 | return this;
120 | }
121 |
122 |
123 | public B setX(int x) {
124 | xOffset = x;
125 | return this;
126 | }
127 |
128 | public B setY(int y) {
129 | yOffset = y;
130 | return this;
131 | }
132 |
133 | public B setX(@Screen.screenType int screenType, float ratio) {
134 | xOffset = (int) ((screenType == Screen.width ?
135 | Util.getScreenWidth(mApplicationContext) :
136 | Util.getScreenHeight(mApplicationContext)) * ratio);
137 | return this;
138 | }
139 |
140 | public B setY(@Screen.screenType int screenType, float ratio) {
141 | yOffset = (int) ((screenType == Screen.width ?
142 | Util.getScreenWidth(mApplicationContext) :
143 | Util.getScreenHeight(mApplicationContext)) * ratio);
144 | return this;
145 | }
146 |
147 |
148 | /**
149 | * 设置 Activity 过滤器,用于指定在哪些界面显示悬浮窗,默认全部界面都显示
150 | *
151 | * @param show 过滤类型,子类类型也会生效
152 | * @param activities 过滤界面
153 | */
154 | public B setFilter(boolean show, @NonNull Class... activities) {
155 | mShow = show;
156 | mActivities = activities;
157 | return this;
158 | }
159 |
160 | public B setMoveType(@MoveType.MOVE_TYPE int moveType) {
161 | return setMoveType(moveType, 0, 0);
162 | }
163 |
164 |
165 | /**
166 | * 设置带边距的贴边动画,只有 moveType 为 MoveType.slide,设置边距才有意义,这个方法不标准,后面调整
167 | *
168 | * @param moveType 贴边动画 MoveType.slide
169 | * @param slideLeftMargin 贴边动画左边距,默认为 0
170 | * @param slideRightMargin 贴边动画右边距,默认为 0
171 | */
172 | public B setMoveType(@MoveType.MOVE_TYPE int moveType, int slideLeftMargin, int slideRightMargin) {
173 | mMoveType = moveType;
174 | mSlideLeftMargin = slideLeftMargin;
175 | mSlideRightMargin = slideRightMargin;
176 | return this;
177 | }
178 |
179 | public B setMoveStyle(long duration, @Nullable TimeInterpolator interpolator) {
180 | mDuration = duration;
181 | mInterpolator = interpolator;
182 | return this;
183 | }
184 |
185 | public B setTag(@NonNull String tag) {
186 | mTag = tag;
187 | return this;
188 | }
189 |
190 | public B setDesktopShow(boolean show) {
191 | mDesktopShow = show;
192 | return this;
193 | }
194 |
195 | public B setPermissionListener(PermissionListener listener) {
196 | mPermissionListener = listener;
197 | return this;
198 | }
199 |
200 | public B setViewStateListener(ViewStateListener listener) {
201 | mViewStateListener = listener;
202 | return this;
203 | }
204 |
205 | public void build() {
206 | if (mFloatWindowMap == null) {
207 | mFloatWindowMap = new HashMap<>();
208 | }
209 | if (mFloatWindowMap.containsKey(mTag)) {
210 | throw new IllegalArgumentException("FloatWindow of this tag has been added, Please set a new tag for the new FloatWindow");
211 | }
212 | if (mView == null && mLayoutId == 0) {
213 | throw new IllegalArgumentException("View has not been set!");
214 | }
215 | if (mView == null) {
216 | mView = Util.inflate(mApplicationContext, mLayoutId);
217 | }
218 | IFloatWindow floatWindowImpl = new IFloatWindowImpl(this);
219 | mFloatWindowMap.put(mTag, floatWindowImpl);
220 | }
221 |
222 | }
223 | }
224 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/IFloatWindow.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by yhao on 2017/12/22.
7 | * https://github.com/yhaolpz
8 | */
9 |
10 | public abstract class IFloatWindow {
11 | public abstract void show();
12 |
13 | public abstract void hide();
14 |
15 | public abstract boolean isShowing();
16 |
17 | public abstract int getX();
18 |
19 | public abstract int getY();
20 |
21 | public abstract void updateX(int x);
22 |
23 | public abstract void updateX(@Screen.screenType int screenType, float ratio);
24 |
25 | public abstract void updateY(int y);
26 |
27 | public abstract void updateY(@Screen.screenType int screenType, float ratio);
28 |
29 | public abstract View getView();
30 |
31 | abstract void dismiss();
32 |
33 | public abstract void addFocus();
34 |
35 | public abstract void clearFocus();
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/IFloatWindowImpl.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.animation.ObjectAnimator;
6 | import android.animation.PropertyValuesHolder;
7 | import android.animation.TimeInterpolator;
8 | import android.animation.ValueAnimator;
9 | import android.annotation.SuppressLint;
10 | import android.os.Build;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewConfiguration;
14 | import android.view.animation.DecelerateInterpolator;
15 |
16 | /**
17 | * Created by yhao on 2017/12/22.
18 | * https://github.com/yhaolpz
19 | */
20 |
21 | public class IFloatWindowImpl extends IFloatWindow {
22 |
23 |
24 | private FloatWindow.B mB;
25 | private FloatView mFloatView;
26 | private FloatLifecycle mFloatLifecycle;
27 | private boolean isShow;
28 | private boolean once = true;
29 | private ValueAnimator mAnimator;
30 | private TimeInterpolator mDecelerateInterpolator;
31 | private float downX;
32 | private float downY;
33 | private float upX;
34 | private float upY;
35 | private boolean mClick = false;
36 | private int mSlop;
37 |
38 |
39 | private IFloatWindowImpl() {
40 |
41 | }
42 |
43 | IFloatWindowImpl(FloatWindow.B b) {
44 | mB = b;
45 | if (mB.mMoveType == MoveType.fixed) {
46 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
47 | mFloatView = new FloatPhone(b.mApplicationContext, mB.mPermissionListener);
48 | } else {
49 | mFloatView = new FloatToast(b.mApplicationContext);
50 | }
51 | } else {
52 | mFloatView = new FloatPhone(b.mApplicationContext, mB.mPermissionListener);
53 | initTouchEvent();
54 | }
55 | mFloatView.setSize(mB.mWidth, mB.mHeight);
56 | mFloatView.setGravity(mB.gravity, mB.xOffset, mB.yOffset);
57 | mFloatView.setView(mB.mView);
58 | mFloatLifecycle = new FloatLifecycle(mB.mApplicationContext, mB.mShow, mB.mActivities, new LifecycleListener() {
59 | @Override
60 | public void onShow() {
61 | show();
62 | }
63 |
64 | @Override
65 | public void onHide() {
66 | hide();
67 | }
68 |
69 | @Override
70 | public void onBackToDesktop() {
71 | if (!mB.mDesktopShow) {
72 | hide();
73 | }
74 | if (mB.mViewStateListener != null) {
75 | mB.mViewStateListener.onBackToDesktop();
76 | }
77 | }
78 | });
79 | }
80 |
81 | @Override
82 | public void show() {
83 | if (once) {
84 | mFloatView.init();
85 | once = false;
86 | isShow = true;
87 | } else {
88 | if (isShow) {
89 | return;
90 | }
91 | getView().setVisibility(View.VISIBLE);
92 | isShow = true;
93 | }
94 | if (mB.mViewStateListener != null) {
95 | mB.mViewStateListener.onShow();
96 | }
97 | }
98 |
99 | @Override
100 | public void hide() {
101 | if (once || !isShow) {
102 | return;
103 | }
104 | getView().setVisibility(View.INVISIBLE);
105 | isShow = false;
106 | if (mB.mViewStateListener != null) {
107 | mB.mViewStateListener.onHide();
108 | }
109 | }
110 |
111 | @Override
112 | public boolean isShowing() {
113 | return isShow;
114 | }
115 |
116 | @Override
117 | void dismiss() {
118 | mFloatView.dismiss();
119 | isShow = false;
120 | if (mB.mViewStateListener != null) {
121 | mB.mViewStateListener.onDismiss();
122 | }
123 | }
124 |
125 | @Override
126 | public void addFocus() {
127 | mFloatView.addFocus();
128 | }
129 |
130 | @Override
131 | public void clearFocus() {
132 | mFloatView.clearFocus();
133 | }
134 |
135 |
136 | @Override
137 | public void updateX(int x) {
138 | checkMoveType();
139 | mB.xOffset = x;
140 | mFloatView.updateX(x);
141 | }
142 |
143 | @Override
144 | public void updateY(int y) {
145 | checkMoveType();
146 | mB.yOffset = y;
147 | mFloatView.updateY(y);
148 | }
149 |
150 | @Override
151 | public void updateX(int screenType, float ratio) {
152 | checkMoveType();
153 | mB.xOffset = (int) ((screenType == Screen.width ?
154 | Util.getScreenWidth(mB.mApplicationContext) :
155 | Util.getScreenHeight(mB.mApplicationContext)) * ratio);
156 | mFloatView.updateX(mB.xOffset);
157 |
158 | }
159 |
160 | @Override
161 | public void updateY(int screenType, float ratio) {
162 | checkMoveType();
163 | mB.yOffset = (int) ((screenType == Screen.width ?
164 | Util.getScreenWidth(mB.mApplicationContext) :
165 | Util.getScreenHeight(mB.mApplicationContext)) * ratio);
166 | mFloatView.updateY(mB.yOffset);
167 |
168 | }
169 |
170 | @Override
171 | public int getX() {
172 | return mFloatView.getX();
173 | }
174 |
175 | @Override
176 | public int getY() {
177 | return mFloatView.getY();
178 | }
179 |
180 |
181 | @Override
182 | public View getView() {
183 | mSlop = ViewConfiguration.get(mB.mApplicationContext).getScaledTouchSlop();
184 | return mB.mView;
185 | }
186 |
187 |
188 | private void checkMoveType() {
189 | if (mB.mMoveType == MoveType.fixed) {
190 | throw new IllegalArgumentException("FloatWindow of this tag is not allowed to move!");
191 | }
192 | }
193 |
194 |
195 | private void initTouchEvent() {
196 | switch (mB.mMoveType) {
197 | case MoveType.inactive:
198 | break;
199 | default:
200 | getView().setOnTouchListener(new View.OnTouchListener() {
201 | float lastX, lastY, changeX, changeY;
202 | int newX, newY;
203 |
204 | @SuppressLint("ClickableViewAccessibility")
205 | @Override
206 | public boolean onTouch(View v, MotionEvent event) {
207 |
208 | switch (event.getAction()) {
209 | case MotionEvent.ACTION_DOWN:
210 | downX = event.getRawX();
211 | downY = event.getRawY();
212 | lastX = event.getRawX();
213 | lastY = event.getRawY();
214 | cancelAnimator();
215 | break;
216 | case MotionEvent.ACTION_MOVE:
217 | changeX = event.getRawX() - lastX;
218 | changeY = event.getRawY() - lastY;
219 | newX = (int) (mFloatView.getX() + changeX);
220 | newY = (int) (mFloatView.getY() + changeY);
221 | mFloatView.updateXY(newX, newY);
222 | if (mB.mViewStateListener != null) {
223 | mB.mViewStateListener.onPositionUpdate(newX, newY);
224 | }
225 | lastX = event.getRawX();
226 | lastY = event.getRawY();
227 | break;
228 | case MotionEvent.ACTION_UP:
229 | upX = event.getRawX();
230 | upY = event.getRawY();
231 | mClick = (Math.abs(upX - downX) > mSlop) || (Math.abs(upY - downY) > mSlop);
232 | switch (mB.mMoveType) {
233 | case MoveType.slide:
234 | int startX = mFloatView.getX();
235 | int endX = (startX * 2 + v.getWidth() > Util.getScreenWidth(mB.mApplicationContext)) ?
236 | Util.getScreenWidth(mB.mApplicationContext) - v.getWidth() - mB.mSlideRightMargin :
237 | mB.mSlideLeftMargin;
238 | mAnimator = ObjectAnimator.ofInt(startX, endX);
239 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
240 | @Override
241 | public void onAnimationUpdate(ValueAnimator animation) {
242 | int x = (int) animation.getAnimatedValue();
243 | mFloatView.updateX(x);
244 | if (mB.mViewStateListener != null) {
245 | mB.mViewStateListener.onPositionUpdate(x, (int) upY);
246 | }
247 | }
248 | });
249 | startAnimator();
250 | break;
251 | case MoveType.back:
252 | PropertyValuesHolder pvhX = PropertyValuesHolder.ofInt("x", mFloatView.getX(), mB.xOffset);
253 | PropertyValuesHolder pvhY = PropertyValuesHolder.ofInt("y", mFloatView.getY(), mB.yOffset);
254 | mAnimator = ObjectAnimator.ofPropertyValuesHolder(pvhX, pvhY);
255 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
256 | @Override
257 | public void onAnimationUpdate(ValueAnimator animation) {
258 | int x = (int) animation.getAnimatedValue("x");
259 | int y = (int) animation.getAnimatedValue("y");
260 | mFloatView.updateXY(x, y);
261 | if (mB.mViewStateListener != null) {
262 | mB.mViewStateListener.onPositionUpdate(x, y);
263 | }
264 | }
265 | });
266 | startAnimator();
267 | break;
268 | default:
269 | break;
270 | }
271 | break;
272 | default:
273 | break;
274 | }
275 | return mClick;
276 | }
277 | });
278 | }
279 | }
280 |
281 |
282 | private void startAnimator() {
283 | if (mB.mInterpolator == null) {
284 | if (mDecelerateInterpolator == null) {
285 | mDecelerateInterpolator = new DecelerateInterpolator();
286 | }
287 | mB.mInterpolator = mDecelerateInterpolator;
288 | }
289 | mAnimator.setInterpolator(mB.mInterpolator);
290 | mAnimator.addListener(new AnimatorListenerAdapter() {
291 | @Override
292 | public void onAnimationEnd(Animator animation) {
293 | mAnimator.removeAllUpdateListeners();
294 | mAnimator.removeAllListeners();
295 | mAnimator = null;
296 | if (mB.mViewStateListener != null) {
297 | mB.mViewStateListener.onMoveAnimEnd();
298 | }
299 | }
300 | });
301 | mAnimator.setDuration(mB.mDuration).start();
302 | if (mB.mViewStateListener != null) {
303 | mB.mViewStateListener.onMoveAnimStart();
304 | }
305 | }
306 |
307 | private void cancelAnimator() {
308 | if (mAnimator != null && mAnimator.isRunning()) {
309 | mAnimator.cancel();
310 | }
311 | }
312 |
313 | }
314 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/LifecycleListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | /**
4 | * Created by yhao on 2017/12/22.
5 | * https://github.com/yhaolpz
6 | */
7 |
8 | interface LifecycleListener {
9 |
10 | void onShow();
11 |
12 | void onHide();
13 |
14 | void onBackToDesktop();
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/LogUtil.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.util.Log;
4 |
5 |
6 | /**
7 | * Created by yhao on 2017/12/29.
8 | * https://github.com/yhaolpz
9 | */
10 |
11 | class LogUtil {
12 |
13 | private static final String TAG = "FloatWindow";
14 |
15 |
16 | static void e(String message) {
17 |
18 | Log.e(TAG, message);
19 | }
20 |
21 |
22 | static void d(String message) {
23 |
24 | Log.d(TAG, message);
25 | }
26 |
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/Miui.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Build;
7 | import android.provider.Settings;
8 | import android.view.View;
9 | import android.view.WindowManager;
10 |
11 | import java.lang.reflect.Field;
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | import static com.yhao.floatwindow.Rom.isIntentAvailable;
16 |
17 | /**
18 | * Created by yhao on 2017/12/30.
19 | * https://github.com/yhaolpz
20 | *
21 | * 需要清楚:一个MIUI版本对应小米各种机型,基于不同的安卓版本,但是权限设置页跟MIUI版本有关
22 | * 测试TYPE_TOAST类型:
23 | * 7.0:
24 | * 小米 5 MIUI8 -------------------- 失败
25 | * 小米 Note2 MIUI9 -------------------- 失败
26 | * 6.0.1
27 | * 小米 5 -------------------- 失败
28 | * 小米 红米note3 -------------------- 失败
29 | * 6.0:
30 | * 小米 5 -------------------- 成功
31 | * 小米 红米4A MIUI8 -------------------- 成功
32 | * 小米 红米Pro MIUI7 -------------------- 成功
33 | * 小米 红米Note4 MIUI8 -------------------- 失败
34 | *
35 | * 经过各种横向纵向测试对比,得出一个结论,就是小米对TYPE_TOAST的处理机制毫无规律可言!
36 | * 跟Android版本无关,跟MIUI版本无关,addView方法也不报错
37 | * 所以最后对小米6.0以上的适配方法是:不使用 TYPE_TOAST 类型,统一申请权限
38 | */
39 |
40 | class Miui {
41 |
42 | private static final String miui = "ro.miui.ui.version.name";
43 | private static final String miui5 = "V5";
44 | private static final String miui6 = "V6";
45 | private static final String miui7 = "V7";
46 | private static final String miui8 = "V8";
47 | private static final String miui9 = "V9";
48 | private static List mPermissionListenerList;
49 | private static PermissionListener mPermissionListener;
50 |
51 |
52 | static boolean rom() {
53 | LogUtil.d(" Miui : " + Miui.getProp());
54 | return Build.MANUFACTURER.equals("Xiaomi");
55 | }
56 |
57 | private static String getProp() {
58 | return Rom.getProp(miui);
59 | }
60 |
61 | /**
62 | * Android6.0以下申请权限
63 | */
64 | static void req(final Context context, PermissionListener permissionListener) {
65 | if (PermissionUtil.hasPermission(context)) {
66 | permissionListener.onSuccess();
67 | return;
68 | }
69 | if (mPermissionListenerList == null) {
70 | mPermissionListenerList = new ArrayList<>();
71 | mPermissionListener = new PermissionListener() {
72 | @Override
73 | public void onSuccess() {
74 | for (PermissionListener listener : mPermissionListenerList) {
75 | listener.onSuccess();
76 | }
77 | mPermissionListenerList.clear();
78 | }
79 | @Override
80 | public void onFail() {
81 | for (PermissionListener listener : mPermissionListenerList) {
82 | listener.onFail();
83 | }
84 | mPermissionListenerList.clear();
85 | }
86 | };
87 | req_(context);
88 | }
89 | mPermissionListenerList.add(permissionListener);
90 | }
91 |
92 |
93 | private static void req_(final Context context) {
94 | switch (getProp()) {
95 | case miui5:
96 | reqForMiui5(context);
97 | break;
98 | case miui6:
99 | case miui7:
100 | reqForMiui67(context);
101 | break;
102 | case miui8:
103 | case miui9:
104 | reqForMiui89(context);
105 | break;
106 | }
107 | FloatLifecycle.setResumedListener(new ResumedListener() {
108 | @Override
109 | public void onResumed() {
110 | if (PermissionUtil.hasPermission(context)) {
111 | mPermissionListener.onSuccess();
112 | } else {
113 | mPermissionListener.onFail();
114 | }
115 | }
116 | });
117 | }
118 |
119 |
120 | private static void reqForMiui5(Context context) {
121 | String packageName = context.getPackageName();
122 | Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
123 | Uri uri = Uri.fromParts("package", packageName, null);
124 | intent.setData(uri);
125 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
126 | if (isIntentAvailable(intent, context)) {
127 | context.startActivity(intent);
128 | } else {
129 | LogUtil.e("intent is not available!");
130 | }
131 | }
132 |
133 | private static void reqForMiui67(Context context) {
134 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
135 | intent.setClassName("com.miui.securitycenter",
136 | "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
137 | intent.putExtra("extra_pkgname", context.getPackageName());
138 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
139 | if (isIntentAvailable(intent, context)) {
140 | context.startActivity(intent);
141 | } else {
142 | LogUtil.e("intent is not available!");
143 | }
144 | }
145 |
146 | private static void reqForMiui89(Context context) {
147 | Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
148 | intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
149 | intent.putExtra("extra_pkgname", context.getPackageName());
150 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
151 | if (isIntentAvailable(intent, context)) {
152 | context.startActivity(intent);
153 | } else {
154 | intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
155 | intent.setPackage("com.miui.securitycenter");
156 | intent.putExtra("extra_pkgname", context.getPackageName());
157 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
158 | if (isIntentAvailable(intent, context)) {
159 | context.startActivity(intent);
160 | } else {
161 | LogUtil.e("intent is not available!");
162 | }
163 | }
164 | }
165 |
166 |
167 | /**
168 | * 有些机型在添加TYPE-TOAST类型时会自动改为TYPE_SYSTEM_ALERT,通过此方法可以屏蔽修改
169 | * 但是...即使成功显示出悬浮窗,移动的话也会崩溃
170 | */
171 | private static void addViewToWindow(WindowManager wm, View view, WindowManager.LayoutParams params) {
172 | setMiUI_International(true);
173 | wm.addView(view, params);
174 | setMiUI_International(false);
175 | }
176 |
177 |
178 | private static void setMiUI_International(boolean flag) {
179 | try {
180 | Class BuildForMi = Class.forName("miui.os.Build");
181 | Field isInternational = BuildForMi.getDeclaredField("IS_INTERNATIONAL_BUILD");
182 | isInternational.setAccessible(true);
183 | isInternational.setBoolean(null, flag);
184 | } catch (Exception e) {
185 | e.printStackTrace();
186 | }
187 | }
188 |
189 |
190 | }
191 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/MoveType.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 |
4 | import androidx.annotation.IntDef;
5 |
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 |
9 | /**
10 | * Created by yhao on 2017/12/22.
11 | * https://github.com/yhaolpz
12 | */
13 |
14 | public class MoveType {
15 | static final int fixed = 0;
16 | public static final int inactive = 1;
17 | public static final int active = 2;
18 | public static final int slide = 3;
19 | public static final int back = 4;
20 |
21 | @IntDef({fixed, inactive, active, slide, back})
22 | @Retention(RetentionPolicy.SOURCE)
23 | @interface MOVE_TYPE {
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/PermissionListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | /**
4 | * Created by yhao on 2017/11/14.
5 | * https://github.com/yhaolpz
6 | */
7 | public interface PermissionListener {
8 | void onSuccess();
9 |
10 | void onFail();
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/PermissionUtil.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.app.AppOpsManager;
4 | import android.content.Context;
5 | import android.graphics.PixelFormat;
6 | import android.os.Binder;
7 | import android.os.Build;
8 | import android.provider.Settings;
9 | import android.view.View;
10 | import android.view.WindowManager;
11 |
12 | import androidx.annotation.RequiresApi;
13 |
14 | import java.lang.reflect.Method;
15 |
16 | /**
17 | * Created by yhao on 2017/12/29.
18 | * https://github.com/yhaolpz
19 | */
20 |
21 | class PermissionUtil {
22 |
23 | static boolean hasPermission(Context context) {
24 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
25 | return Settings.canDrawOverlays(context);
26 | } else {
27 | return hasPermissionBelowMarshmallow(context);
28 | }
29 | }
30 |
31 | static boolean hasPermissionOnActivityResult(Context context) {
32 | if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
33 | return hasPermissionForO(context);
34 | }
35 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
36 | return Settings.canDrawOverlays(context);
37 | } else {
38 | return hasPermissionBelowMarshmallow(context);
39 | }
40 | }
41 |
42 | /**
43 | * 6.0以下判断是否有权限
44 | * 理论上6.0以上才需处理权限,但有的国内rom在6.0以下就添加了权限
45 | * 其实此方式也可以用于判断6.0以上版本,只不过有更简单的canDrawOverlays代替
46 | */
47 | static boolean hasPermissionBelowMarshmallow(Context context) {
48 | try {
49 | AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
50 | Method dispatchMethod = AppOpsManager.class.getMethod("checkOp", int.class, int.class, String.class);
51 | //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
52 | return AppOpsManager.MODE_ALLOWED == (Integer) dispatchMethod.invoke(
53 | manager, 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName());
54 | } catch (Exception e) {
55 | return false;
56 | }
57 | }
58 |
59 |
60 | /**
61 | * 用于判断8.0时是否有权限,仅用于OnActivityResult
62 | * 针对8.0官方bug:在用户授予权限后Settings.canDrawOverlays或checkOp方法判断仍然返回false
63 | */
64 | @RequiresApi(api = Build.VERSION_CODES.M)
65 | private static boolean hasPermissionForO(Context context) {
66 | try {
67 | WindowManager mgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
68 | if (mgr == null) return false;
69 | View viewToAdd = new View(context);
70 | WindowManager.LayoutParams params = new WindowManager.LayoutParams(0, 0,
71 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
72 | WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
73 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
74 | PixelFormat.TRANSPARENT);
75 | viewToAdd.setLayoutParams(params);
76 | mgr.addView(viewToAdd, params);
77 | mgr.removeView(viewToAdd);
78 | return true;
79 | } catch (Exception e) {
80 | LogUtil.e("hasPermissionForO e:" + e.toString());
81 | }
82 | return false;
83 | }
84 |
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/ResumedListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | /**
4 | * Created by yhao on 2017/12/30.
5 | * https://github.com/yhaolpz
6 | */
7 |
8 | interface ResumedListener {
9 | void onResumed();
10 | }
11 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/Rom.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 |
7 | import java.io.BufferedReader;
8 | import java.io.IOException;
9 | import java.io.InputStreamReader;
10 |
11 | /**
12 | * Created by yhao on 2017/12/30.
13 | * https://github.com/yhaolpz
14 | */
15 |
16 | class Rom {
17 |
18 | static boolean isIntentAvailable(Intent intent, Context context) {
19 | return intent != null && context.getPackageManager().queryIntentActivities(
20 | intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
21 | }
22 |
23 |
24 | static String getProp(String name) {
25 | BufferedReader input = null;
26 | try {
27 | Process p = Runtime.getRuntime().exec("getprop " + name);
28 | input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
29 | String line = input.readLine();
30 | input.close();
31 | return line;
32 | } catch (IOException ex) {
33 | return null;
34 | } finally {
35 | if (input != null) {
36 | try {
37 | input.close();
38 | } catch (IOException e) {
39 | e.printStackTrace();
40 | }
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/Screen.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 |
4 | import androidx.annotation.IntDef;
5 |
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 |
9 | /**
10 | * Created by yhao on 2017/12/23.
11 | * https://github.com/yhaolpz
12 | */
13 |
14 | public class Screen {
15 | public static final int width = 0;
16 | public static final int height = 1;
17 |
18 | @IntDef({width, height})
19 | @Retention(RetentionPolicy.SOURCE)
20 | @interface screenType {
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/Util.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | import android.content.Context;
4 | import android.graphics.PixelFormat;
5 | import android.graphics.Point;
6 | import android.graphics.Rect;
7 | import android.os.Build;
8 | import android.provider.Settings;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.WindowManager;
12 |
13 | import java.lang.reflect.Method;
14 |
15 | /**
16 | * Created by yhao on 2017/12/22.
17 | * https://github.com/yhaolpz
18 | */
19 |
20 | class Util {
21 |
22 |
23 | static View inflate(Context applicationContext, int layoutId) {
24 | LayoutInflater inflate = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
25 | return inflate.inflate(layoutId, null);
26 | }
27 |
28 | private static Point sPoint;
29 |
30 | static int getScreenWidth(Context context) {
31 | if (sPoint == null) {
32 | sPoint = new Point();
33 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
34 | wm.getDefaultDisplay().getSize(sPoint);
35 | }
36 | return sPoint.x;
37 | }
38 |
39 | static int getScreenHeight(Context context) {
40 | if (sPoint == null) {
41 | sPoint = new Point();
42 | WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
43 | wm.getDefaultDisplay().getSize(sPoint);
44 | }
45 | return sPoint.y;
46 | }
47 |
48 | static boolean isViewVisible(View view) {
49 | return view.getGlobalVisibleRect(new Rect());
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/ViewStateListener.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | /**
4 | * Created by yhao on 2018/5/5
5 | * https://github.com/yhaolpz
6 | */
7 | public interface ViewStateListener {
8 | void onPositionUpdate(int x, int y);
9 |
10 | void onShow();
11 |
12 | void onHide();
13 |
14 | void onDismiss();
15 |
16 | void onMoveAnimStart();
17 |
18 | void onMoveAnimEnd();
19 |
20 | void onBackToDesktop();
21 | }
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/yhao/floatwindow/ViewStateListenerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.yhao.floatwindow;
2 |
3 | /**
4 | * Created by yhao on 2018/5/5.
5 | * https://github.com/yhaolpz
6 | */
7 | public class ViewStateListenerAdapter implements ViewStateListener{
8 | @Override
9 | public void onPositionUpdate(int x, int y) {
10 |
11 | }
12 |
13 | @Override
14 | public void onShow() {
15 |
16 | }
17 |
18 | @Override
19 | public void onHide() {
20 |
21 | }
22 |
23 | @Override
24 | public void onDismiss() {
25 |
26 | }
27 |
28 | @Override
29 | public void onMoveAnimStart() {
30 |
31 | }
32 |
33 | @Override
34 | public void onMoveAnimEnd() {
35 |
36 | }
37 |
38 | @Override
39 | public void onBackToDesktop() {
40 |
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/java/vip/mimiya/helper/MainActivity.java:
--------------------------------------------------------------------------------
1 | package vip.mimiya.helper;
2 |
3 | import androidx.annotation.NonNull;
4 | import androidx.appcompat.app.AppCompatActivity;
5 |
6 | import android.content.ClipData;
7 | import android.content.ClipboardManager;
8 | import android.content.ComponentName;
9 | import android.content.Context;
10 | import android.content.Intent;
11 | import android.content.ServiceConnection;
12 | import android.os.Build;
13 | import android.os.Bundle;
14 | import android.os.Handler;
15 | import android.os.IBinder;
16 | import android.os.Message;
17 | import android.os.Messenger;
18 | import android.os.RemoteException;
19 | import android.text.TextUtils;
20 | import android.util.Log;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.widget.Button;
24 | import android.widget.EditText;
25 | import android.widget.Toast;
26 |
27 | import com.yhao.floatwindow.FloatWindow;
28 | import com.yhao.floatwindow.PermissionListener;
29 | import com.yhao.floatwindow.Screen;
30 | import com.yhao.floatwindow.ViewStateListener;
31 |
32 | import java.util.regex.Matcher;
33 | import java.util.regex.Pattern;
34 |
35 | import okhttp3.MediaType;
36 |
37 | public class MainActivity extends AppCompatActivity {
38 | private static final String TAG = "MainActivity";
39 | private ClipboardManager clipboardManager;
40 | private String lastPasteString;
41 | private EditText postEditText;
42 |
43 |
44 | @Override
45 | protected void onCreate(Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 | setContentView(R.layout.activity_main);
48 | initClipboard();
49 | bindRemoteService();
50 | startTarget();
51 | }
52 |
53 | private void startTarget() {
54 | ComponentName componetName = new ComponentName("com.ss.android.ugc.aweme", "com.ss.android.ugc.aweme.main.MainActivity");
55 |
56 | try {
57 | Intent intent = new Intent();
58 | intent.setComponent(componetName);
59 | startActivity(intent);
60 | } catch (Exception e) {
61 | }
62 | }
63 |
64 | private void initClipboard() {
65 | clipboardManager = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
66 | LayoutInflater inflater = LayoutInflater.from(this);
67 | View floatLayout = inflater.inflate(R.layout.input_layout, null);
68 |
69 | postEditText = floatLayout.findViewById(R.id.post_editText);
70 | Button postButton = floatLayout.findViewById(R.id.post_button);
71 |
72 | postButton.setOnClickListener(new View.OnClickListener() {
73 | @Override
74 | public void onClick(View v) {
75 |
76 | FloatWindow.get().addFocus();
77 | postEditText.setEnabled(true);
78 | postEditText.setFocusable(true);
79 | postEditText.setFocusableInTouchMode(true);
80 | postEditText.requestFocus();
81 | postEditText.findFocus();
82 | postEditText.setText("");
83 | handler.sendEmptyMessage(101);
84 | }
85 | });
86 | postButton.setOnLongClickListener(new View.OnLongClickListener() {
87 | @Override
88 | public boolean onLongClick(View v) {
89 | lastPasteString = "";
90 | return false;
91 | }
92 | });
93 |
94 | FloatWindow.with(getApplicationContext()).setView(floatLayout)
95 | //.setWidth(150) //设置控件宽高
96 | .setHeight(Screen.width, 0.2f)
97 | .setX(0) //设置控件初始位置
98 | .setY(Screen.height, 0.1f)
99 | .setDesktopShow(true) //桌面显示
100 | .setViewStateListener(mViewStateListener) //监听悬浮控件状态改变
101 | .setPermissionListener(mPermissionListener) //监听权限申请结果
102 | .build();
103 | }
104 |
105 |
106 | private Handler handler = new Handler() {
107 | @Override
108 | public void handleMessage(@NonNull Message msg) {
109 | super.handleMessage(msg);
110 | switch (msg.what) {
111 | case 100:
112 | String result = (String) msg.obj;
113 | Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
114 | break;
115 | case 101:
116 | getClipData();
117 | break;
118 | default:
119 | break;
120 | }
121 | }
122 | };
123 |
124 | private void getClipData() {
125 | try {
126 | ClipData clipData = clipboardManager.getPrimaryClip();
127 |
128 | String pasteString = "";
129 | if (clipData != null && clipData.getItemCount() > 0) {
130 | CharSequence text = clipData.getItemAt(0).getText();
131 | pasteString = text.toString();
132 | }
133 | // if (!status) return;
134 | if (TextUtils.isEmpty(pasteString)) return;
135 |
136 | clipboardManager.setPrimaryClip(ClipData.newPlainText("", "" + System.currentTimeMillis()));
137 |
138 | if (!TextUtils.isEmpty(lastPasteString) && lastPasteString.equals(pasteString))
139 | return;
140 |
141 |
142 | lastPasteString = pasteString;
143 |
144 | String pattern = "(https://v.douyin.com).*/";
145 | Pattern r = Pattern.compile(pattern);
146 | Matcher m = r.matcher(pasteString);
147 | if (m.find()) {
148 | String shareUrl = m.group(0);
149 | try {
150 | String result = iMyAidlInterface.postMessage(shareUrl);
151 | if (result.equals("ok"))
152 | Toast.makeText(MainActivity.this, "收到任务:[" + pasteString + "]", Toast.LENGTH_LONG).show();
153 | else {
154 | Toast.makeText(MainActivity.this, "POST ERROR!", Toast.LENGTH_SHORT).show();
155 | }
156 | } catch (RemoteException e) {
157 | Toast.makeText(MainActivity.this, "SERVICE ERROR!", Toast.LENGTH_SHORT).show();
158 | }
159 | } else {
160 | Toast.makeText(MainActivity.this, "NO MATCH!", Toast.LENGTH_SHORT).show();
161 | }
162 | } finally {
163 | postEditText.setText("");
164 | postEditText.clearFocus();
165 | postEditText.setEnabled(false);
166 | FloatWindow.get().clearFocus();
167 | }
168 | }
169 |
170 |
171 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172 | private PermissionListener mPermissionListener = new PermissionListener() {
173 | @Override
174 | public void onSuccess() {
175 | Log.d(TAG, "mPermissionListener onSuccess");
176 | }
177 |
178 | @Override
179 | public void onFail() {
180 | Log.d(TAG, "mPermissionListener onFail");
181 | }
182 | };
183 |
184 |
185 | private ViewStateListener mViewStateListener = new ViewStateListener() {
186 | @Override
187 | public void onPositionUpdate(int x, int y) {
188 | Log.d(TAG, "onPositionUpdate: x=" + x + " y=" + y);
189 | }
190 |
191 | @Override
192 | public void onShow() {
193 | Log.d(TAG, "onShow");
194 | }
195 |
196 | @Override
197 | public void onHide() {
198 | Log.d(TAG, "onHide");
199 | }
200 |
201 | @Override
202 | public void onDismiss() {
203 | Log.d(TAG, "onDismiss");
204 | }
205 |
206 | @Override
207 | public void onMoveAnimStart() {
208 | Log.d(TAG, "onMoveAnimStart");
209 | }
210 |
211 | @Override
212 | public void onMoveAnimEnd() {
213 | Log.d(TAG, "onMoveAnimEnd");
214 | }
215 |
216 | @Override
217 | public void onBackToDesktop() {
218 | Log.d(TAG, "onBackToDesktop");
219 | }
220 | };
221 |
222 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
223 | private IMyAidlInterface iMyAidlInterface;// 定义接口变量
224 | private ServiceConnection connection;
225 |
226 | private void bindRemoteService() {
227 | Intent intentService = new Intent();
228 | intentService.setClassName(this, "vip.mimiya.helper.MyService");
229 |
230 | connection = new ServiceConnection() {
231 | @Override
232 | public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
233 | iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
234 | }
235 |
236 | @Override
237 | public void onServiceDisconnected(ComponentName componentName) {
238 | // 断开连接
239 | iMyAidlInterface = null;
240 | }
241 | };
242 |
243 | bindService(intentService, connection, Context.BIND_AUTO_CREATE);
244 | }
245 |
246 |
247 | @Override
248 | protected void onDestroy() {
249 | super.onDestroy();
250 | if (connection != null)
251 | unbindService(connection);
252 | }
253 | }
254 |
--------------------------------------------------------------------------------
/app/src/main/java/vip/mimiya/helper/MyService.java:
--------------------------------------------------------------------------------
1 | package vip.mimiya.helper;
2 |
3 | import android.app.Service;
4 | import android.content.BroadcastReceiver;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.os.Handler;
8 | import android.os.HandlerThread;
9 | import android.os.IBinder;
10 | import android.os.Looper;
11 | import android.os.Message;
12 | import android.os.RemoteException;
13 | import android.util.Log;
14 | import android.widget.Toast;
15 |
16 | import org.json.JSONException;
17 | import org.json.JSONObject;
18 |
19 | import java.io.IOException;
20 | import java.net.InetAddress;
21 | import java.net.UnknownHostException;
22 | import java.util.concurrent.TimeUnit;
23 |
24 | import okhttp3.MediaType;
25 | import okhttp3.OkHttpClient;
26 | import okhttp3.Request;
27 | import okhttp3.RequestBody;
28 | import okhttp3.Response;
29 |
30 | public class MyService extends Service {
31 | private static final String TAG = "MyService";
32 | private CustomHanlder ch;
33 |
34 | @Override
35 | public void onCreate() {
36 | super.onCreate();
37 | Log.d(TAG, "onCreate: MyService");
38 | HandlerThread ht = new HandlerThread("handler.thread.name");
39 | ht.start();
40 | ch = new CustomHanlder(ht.getLooper());
41 |
42 | }
43 |
44 | @Override
45 | public int onStartCommand(Intent intent, int flags, int startId) {
46 | Log.w("service started:", "flag");
47 | Log.w("main thread id:", "" + Thread.currentThread().getName() + Thread.currentThread().getId());
48 | return super.onStartCommand(intent, flags, startId);
49 | }
50 |
51 | @Override
52 | public IBinder onBind(Intent intent) {
53 | return stub;// 在客户端连接服务端时,Stub通过ServiceConnection传递到客户端
54 | }
55 |
56 | // 实现接口中暴露给客户端的Stub--Stub继承自Binder,它实现了IBinder接口
57 | private IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() {
58 |
59 | @Override
60 | public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
61 |
62 | }
63 |
64 | @Override
65 | public String postMessage(String context) throws RemoteException {
66 | Message msg = ch.obtainMessage();
67 | msg.what = 100;
68 | msg.obj = context;
69 | ch.sendMessage(msg);
70 | return "ok";
71 | }
72 | };
73 |
74 | private class CustomHanlder extends Handler {
75 | public CustomHanlder(Looper looper) {
76 | super(looper);
77 | }
78 |
79 | @Override
80 | public void handleMessage(Message msg) {
81 | Log.w("handler thread id:", "" + Thread.currentThread().getName() + Thread.currentThread().getId());
82 | switch (msg.what) {
83 | case 100:
84 | doTask((String) msg.obj);
85 | break;
86 | case 102:
87 | Toast.makeText(getApplicationContext(), "后台处理-提交完成!", Toast.LENGTH_SHORT).show();
88 | break;
89 | case 103:
90 | Toast.makeText(getApplicationContext(), "后台处理-视频原始地址处理出错!", Toast.LENGTH_SHORT).show();
91 | break;
92 | case 104:
93 | Toast.makeText(getApplicationContext(), "后台处理-提交出错!", Toast.LENGTH_SHORT).show();
94 | break;
95 | default:
96 | break;
97 | }
98 | super.handleMessage(msg);
99 | }
100 | }
101 |
102 |
103 | void doTask(String context) {
104 | String video = getVideoRawUrl(context);
105 | if (video != null) {
106 | postMyVideoRecord(video);
107 | }
108 | }
109 |
110 | private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
111 |
112 | private void postMyVideoRecord(String video) {
113 | OkHttpClient client = new OkHttpClient.Builder()
114 | .writeTimeout(30, TimeUnit.SECONDS)
115 | .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间
116 | .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
117 | .build();
118 |
119 | RequestBody body = RequestBody.create(video, JSON);
120 | Request request = new Request.Builder()
121 | .url("http://47.98.199.11:5008/do/video_upload")
122 | .header("token", Utils.token)
123 | .post(body)
124 | .build();
125 |
126 | try {
127 | Response response = client.newCall(request).execute();
128 | String result = response.body().string();
129 | ch.sendEmptyMessage(102);
130 | } catch (IOException e) {
131 | ch.sendEmptyMessage(104);
132 | }
133 | }
134 |
135 | private String getVideoRawUrl(String url) {
136 |
137 | OkHttpClient client = new OkHttpClient.Builder()
138 | .writeTimeout(30, TimeUnit.SECONDS)
139 | .connectTimeout(30, TimeUnit.SECONDS)//设置连接超时时间
140 | .readTimeout(30, TimeUnit.SECONDS)//设置读取超时时间
141 | .build();
142 | String json = String.format("{\"share_url\":\"%s\"}", url);
143 | RequestBody body = RequestBody.create(json, JSON);
144 | Request request = new Request.Builder()
145 | .url("http://47.98.199.11:5008/do/video_raw_url")
146 | .header("token", Utils.token)
147 | .post(body)
148 | .build();
149 |
150 | try {
151 | Response response = client.newCall(request).execute();
152 | String result = response.body().string();
153 | JSONObject resultObj = new JSONObject(result);
154 | JSONObject resultData = resultObj.getJSONObject("data");
155 | resultData.put("phone", Utils.phone);
156 | System.out.println(result);
157 | return resultData.toString();
158 | } catch (IOException | JSONException e) {
159 | ch.sendEmptyMessage(103);
160 | }
161 | return null;
162 |
163 | }
164 |
165 | class LocalReceiver extends BroadcastReceiver {
166 | @Override
167 | public void onReceive(Context context, Intent intent) {
168 | //逻辑代码
169 | }
170 | }
171 | }
--------------------------------------------------------------------------------
/app/src/main/java/vip/mimiya/helper/Utils.java:
--------------------------------------------------------------------------------
1 | package vip.mimiya.helper;
2 |
3 | public class Utils {
4 | //授权使用,联系satng@qq.com
5 | public static String token = "***";
6 | public static String phone = "180********";
7 | }
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/button_bg_draw.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/meway24/douyin-assistant/88b687d43a230db67edc0bb55c59d34da9d44e44/app/src/main/res/drawable/icon.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/icon1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/meway24/douyin-assistant/88b687d43a230db67edc0bb55c59d34da9d44e44/app/src/main/res/drawable/icon1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/meway24/douyin-assistant/88b687d43a230db67edc0bb55c59d34da9d44e44/app/src/main/res/drawable/logo.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/input_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
21 |
22 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #6200EE
4 | #3700B3
5 | #03DAC5
6 | #ffff0000
7 | #ff00ff00
8 | #ff0000ff
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MiMiYaHelper
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/network_security_config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/test/java/vip/mimiya/helper/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package vip.mimiya.helper;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.6.2'
12 |
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | maven { url 'https://jitpack.io' }
24 |
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
31 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/meway24/douyin-assistant/88b687d43a230db67edc0bb55c59d34da9d44e44/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Apr 21 21:14:35 CST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='MiMiYaHelper'
2 | include ':app'
3 |
--------------------------------------------------------------------------------
/snapshoot/WX20200517-173855@2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/meway24/douyin-assistant/88b687d43a230db67edc0bb55c59d34da9d44e44/snapshoot/WX20200517-173855@2x.png
--------------------------------------------------------------------------------