├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── vip │ │ └── mimiya │ │ └── helper │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── aidl │ │ └── vip │ │ │ └── mimiya │ │ │ └── helper │ │ │ └── IMyAidlInterface.aidl │ ├── java │ │ ├── com │ │ │ └── yhao │ │ │ │ └── floatwindow │ │ │ │ ├── FloatActivity.java │ │ │ │ ├── FloatLifecycle.java │ │ │ │ ├── FloatPhone.java │ │ │ │ ├── FloatToast.java │ │ │ │ ├── FloatView.java │ │ │ │ ├── FloatWindow.java │ │ │ │ ├── IFloatWindow.java │ │ │ │ ├── IFloatWindowImpl.java │ │ │ │ ├── LifecycleListener.java │ │ │ │ ├── LogUtil.java │ │ │ │ ├── Miui.java │ │ │ │ ├── MoveType.java │ │ │ │ ├── PermissionListener.java │ │ │ │ ├── PermissionUtil.java │ │ │ │ ├── ResumedListener.java │ │ │ │ ├── Rom.java │ │ │ │ ├── Screen.java │ │ │ │ ├── Util.java │ │ │ │ ├── ViewStateListener.java │ │ │ │ └── ViewStateListenerAdapter.java │ │ └── vip │ │ │ └── mimiya │ │ │ └── helper │ │ │ ├── MainActivity.java │ │ │ ├── MyService.java │ │ │ └── Utils.java │ └── res │ │ ├── drawable │ │ ├── button_bg_draw.xml │ │ ├── ic_launcher_background.xml │ │ ├── icon.png │ │ ├── icon1.png │ │ └── logo.png │ │ ├── layout │ │ ├── activity_main.xml │ │ └── input_layout.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── network_security_config.xml │ └── test │ └── java │ └── vip │ └── mimiya │ └── helper │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── snapshoot └── WX20200517-173855@2x.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # douyin-assistant 2 | 3 | 4 | If you want need contact with me: kkbbcc@foxmail.com 5 | 6 | Douyin sharp tool,floating window,copy sharp url to get raw video 7 | 8 | 9 | 10 | ## Fixed 11 | In Android 10 cannot listen to the clipboard,so we need a EditText on top Windows,hook global focus. 12 | 13 | Floating window button,click it to download raw video to self cloud. 14 | 15 | ## What to do?? 16 |
17 | One key operation copy other people's videos,and publish yourself at the same time. 18 |
19 | Copy the video of the personal main account with one click, and publish it to the collection of works of his auxiliary account at the same time. 20 |
21 | 22 | Please use correctly and pay attention to copyright. This tool is only for personal learning and communication. 23 | 24 | ## 25 |
26 |              27 |
28 | 29 | 30 | ## 服务端加解密调用接口文档 Gorgon Xlog TTEncrypt 31 | 32 | ```python 33 | 34 | import requests 35 | import time 36 | 37 | # 注册新设备 38 | new_device = requests.post("http://127.0.0.1:5016/device_register/", json={}).json() 39 | print(new_device) 40 | #{'status': 'ok', 'data': {'device_id': '***', 'uuid': '***', 'openudid': '***', 'cdid': '***', 'mc': '***', 'sim_serial_number': '***', 'clientudid': '***', 'req_id': '***', 'build_serial': '***', 'first_reg_time': ***, 'install_id': '***', 'new_user': ***, 'channel': '***', 'os_api': '***', 'device_type': '***'}, 'agent': '***'} 41 | 42 | 43 | device_id = new_device["data"]["device_id"] 44 | print(device_id) 45 | 46 | # 刷新设备注册信息 47 | refresh_device = requests.post("http://127.0.0.1:5016/device_register/", json={"device_id": device_id}).json() 48 | print(refresh_device) 49 | 50 | # 获取设备注册参数 51 | device_params = requests.get("http://127.0.0.1:5016/device_params/" + device_id).json() 52 | print(device_params) 53 | 54 | # 请求示例 55 | millis = int(time.time() * 1000) 56 | millis_short = int(millis / 1000) 57 | 58 | sec_user_id = '***************************************' 59 | 60 | params = dict([m.split("=") for m in device_params["data"].split("&")]) 61 | 62 | url = f"https://***.***.com/aweme/v1/**/favorite/?invalid_item_count=0&is_hiding_invalid_item=0&max_cursor=0&" \ 63 | f"sec_user_id={sec_user_id}&count=20&os_api={params['os_api']}&device_type={params['device_type']}&" \ 64 | f"ssmix=a&manifest_version_code=100601&dpi=480&uuid={params['uuid']}&app_name=***&version_name=11.1.0&" \ 65 | f"ts={str(millis_short)}&app_type=normal&ac=wifi&host_abi=armeabi-v7a&update_version_code==***&&channel=xiaomi&" \ 66 | f"_rticket={str(millis)}&device_platform=android&iid={params['iid']}&version_code=100600&" \ 67 | f"cdid={params['cdid']}&openudid={params['openudid']}&device_id={params['device_id']}&" \ 68 | f"resolution=1080*1920&os_version=6.0.1&language=zh&device_brand=Xiaomi&aid=1128" 69 | 70 | data = {"url": url, "millis": millis_short} 71 | gorgonInfo = requests.post("http://127.0.0.1:5016/gorgon", json=data).json() 72 | 73 | print(url, "\n", gorgonInfo) 74 | #{'khronos': 1590373639, 'gorgon': '040000800800ccfdf0b233263c9252381b50880d73efe2b7af06', 'status': 'ok'} 75 | 76 | headers = { 77 | "X-SS-REQ-TICKET": str(millis), 78 | "sdk-version": "1", 79 | "Host": "aweme.snssdk.com", 80 | "Connection": "Keep-Alive", 81 | "User-Agent": gorgonInfo["agent"], 82 | "X-Gorgon": gorgonInfo["gorgon"], 83 | "X-Khronos": str(gorgonInfo["khronos"]) 84 | } 85 | response = requests.get(url, headers=headers) 86 | 87 | print(response.text) 88 | 89 | # xlog 加密 90 | endata = requests.post("http://127.0.0.1:5016/xlog/en", data="加密字符串密密密密密密密密密密密密密密密密密".encode("utf-8")).json() 91 | print(endata) 92 | #{'message': '020ef5297ff661fc03a01acd7d0f37a7370e28599f87f81468e459c9947b0f5ffbf', 'status': 'ok'} 93 | 94 | # xlog 解密 95 | dedata = requests.post("http://127.0.0.1:5016/xlog/de", data=endata["message"].encode("utf-8")).json() 96 | print(dedata) 97 | #{'message': '加密字符串密密密密密密密密密密密密密密密密密', 'status': 'ok'} 98 | 99 | ``` 100 | 101 | ### 服务端功能调用接口文档 ~~Login SmsCode UserInfo ExtractShareUrl DownloadVideo SaveVideo PushVideo~~ 102 | ``` 103 | //todo 104 | 105 | # 发送短信验证码 106 | sms_result = requests.get("http://127.0.0.1:5016/send_sms/180********").json() 107 | print(sms_result) 108 | #{'status': 'ok'} 109 | 110 | # 登录 111 | login_result = requests.get("http://127.0.0.1:5016/login_sms/180********/7422").json() 112 | print(login_result) 113 | # {'status': 'ok', 'data': { 'x_tt_token': '********', 'session_key': '********'}} 114 | 115 | # getUserInfo token有效期很长 116 | login_result = requests.get("http://127.0.0.1:5016/user_info/180********").json() 117 | print(login_result) 118 | # {'status': 'ok', 'data': { 'x_tt_token': '********', 'session_key': '********'}} 119 | 120 | # video_raw_url 无水印视频 121 | headers={"token": "***********************"} 122 | json={"share_url":"https://v.douyin.com/Jdpsx7Q/"} 123 | video_info_result = requests.post("http://**************:5008/do/video_raw_url",headers=headers,json=json).json() 124 | print(video_info_result) 125 | #{"data":{"author":"**********","url":"https://****************/?video_id=v0200f070000bridurlp06vruuk1hqog","desc":"*********。"},"status":"ok"} 126 | ``` 127 | 128 | 129 | ## Agreement 130 |
131 | The project is based on GNU open source agreement and cannot be used for commercial purposes 132 |
133 | This project is for learning and exchange only, and I will not bear any legal responsibility 134 |
135 | In case of infringement, please contact to delete. 136 |
137 | 138 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.3" 6 | 7 | defaultConfig { 8 | applicationId "vip.mimiya.helper" 9 | minSdkVersion 21 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 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 | implementation 'androidx.appcompat:appcompat:1.1.0' 29 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 33 | implementation("com.squareup.okhttp3:okhttp:4.5.0") 34 | } 35 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/vip/mimiya/helper/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package vip.mimiya.helper; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("vip.mimiya.helper", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/aidl/vip/mimiya/helper/IMyAidlInterface.aidl: -------------------------------------------------------------------------------- 1 | // IMyAidlInterface.aidl 2 | package vip.mimiya.helper; 3 | 4 | // Declare any non-default types here with import statements 5 | 6 | interface IMyAidlInterface { 7 | /** 8 | * Demonstrates some basic types that you can use as parameters 9 | * and return values in AIDL. 10 | */ 11 | void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,double aDouble, String aString); 12 | String postMessage(String context); 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatActivity.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.net.Uri; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | import android.provider.Settings; 10 | 11 | import androidx.annotation.RequiresApi; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * 用于在内部自动申请权限 18 | * https://github.com/yhaolpz 19 | */ 20 | 21 | public class FloatActivity extends Activity { 22 | 23 | private static List mPermissionListenerList; 24 | private static PermissionListener mPermissionListener; 25 | 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 31 | requestAlertWindowPermission(); 32 | } 33 | } 34 | 35 | @RequiresApi(api = Build.VERSION_CODES.M) 36 | private void requestAlertWindowPermission() { 37 | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); 38 | intent.setData(Uri.parse("package:" + getPackageName())); 39 | startActivityForResult(intent, 756232212); 40 | } 41 | 42 | 43 | @Override 44 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 45 | super.onActivityResult(requestCode, resultCode, data); 46 | if (requestCode == 756232212) { 47 | if (PermissionUtil.hasPermissionOnActivityResult(this)) { 48 | mPermissionListener.onSuccess(); 49 | } else { 50 | mPermissionListener.onFail(); 51 | } 52 | } 53 | finish(); 54 | } 55 | 56 | static synchronized void request(Context context, PermissionListener permissionListener) { 57 | if (PermissionUtil.hasPermission(context)) { 58 | permissionListener.onSuccess(); 59 | return; 60 | } 61 | if (mPermissionListenerList == null) { 62 | mPermissionListenerList = new ArrayList<>(); 63 | mPermissionListener = new PermissionListener() { 64 | @Override 65 | public void onSuccess() { 66 | for (PermissionListener listener : mPermissionListenerList) { 67 | listener.onSuccess(); 68 | } 69 | mPermissionListenerList.clear(); 70 | } 71 | 72 | @Override 73 | public void onFail() { 74 | for (PermissionListener listener : mPermissionListenerList) { 75 | listener.onFail(); 76 | } 77 | mPermissionListenerList.clear(); 78 | } 79 | }; 80 | Intent intent = new Intent(context, FloatActivity.class); 81 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 82 | context.startActivity(intent); 83 | } 84 | mPermissionListenerList.add(permissionListener); 85 | } 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatLifecycle.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.BroadcastReceiver; 6 | import android.content.Context; 7 | import android.content.Intent; 8 | import android.content.IntentFilter; 9 | import android.os.Bundle; 10 | import android.os.Handler; 11 | import android.widget.Toast; 12 | 13 | /** 14 | * Created by yhao on 17-12-1. 15 | * 用于控制悬浮窗显示周期 16 | * 使用了三种方法针对返回桌面时隐藏悬浮按钮 17 | * 1.startCount计数,针对back到桌面可以及时隐藏 18 | * 2.监听home键,从而及时隐藏 19 | * 3.resumeCount计时,针对一些只执行onPause不执行onStop的奇葩情况 20 | */ 21 | 22 | class FloatLifecycle extends BroadcastReceiver implements Application.ActivityLifecycleCallbacks { 23 | 24 | private static final String SYSTEM_DIALOG_REASON_KEY = "reason"; 25 | private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; 26 | private static final long delay = 300; 27 | private Handler mHandler; 28 | private Class[] activities; 29 | private boolean showFlag; 30 | private int startCount; 31 | private int resumeCount; 32 | private boolean appBackground; 33 | private LifecycleListener mLifecycleListener; 34 | private static ResumedListener sResumedListener; 35 | private static int num = 0; 36 | 37 | 38 | FloatLifecycle(Context applicationContext, boolean showFlag, Class[] activities, LifecycleListener lifecycleListener) { 39 | this.showFlag = showFlag; 40 | this.activities = activities; 41 | num++; 42 | mLifecycleListener = lifecycleListener; 43 | mHandler = new Handler(); 44 | ((Application) applicationContext).registerActivityLifecycleCallbacks(this); 45 | applicationContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); 46 | } 47 | 48 | public static void setResumedListener(ResumedListener resumedListener) { 49 | sResumedListener = resumedListener; 50 | } 51 | 52 | private boolean needShow(Activity activity) { 53 | if (activities == null) { 54 | return true; 55 | } 56 | for (Class a : activities) { 57 | if (a.isInstance(activity)) { 58 | return showFlag; 59 | } 60 | } 61 | return !showFlag; 62 | } 63 | 64 | 65 | @Override 66 | public void onActivityResumed(Activity activity) { 67 | if (sResumedListener != null) { 68 | num--; 69 | if (num == 0) { 70 | sResumedListener.onResumed(); 71 | sResumedListener = null; 72 | } 73 | } 74 | resumeCount++; 75 | if (needShow(activity)) { 76 | mLifecycleListener.onShow(); 77 | } else { 78 | mLifecycleListener.onHide(); 79 | } 80 | if (appBackground) { 81 | appBackground = false; 82 | } 83 | } 84 | 85 | @Override 86 | public void onActivityPaused(final Activity activity) { 87 | resumeCount--; 88 | mHandler.postDelayed(new Runnable() { 89 | @Override 90 | public void run() { 91 | if (resumeCount == 0) { 92 | appBackground = true; 93 | mLifecycleListener.onBackToDesktop(); 94 | } 95 | } 96 | }, delay); 97 | 98 | } 99 | 100 | @Override 101 | public void onActivityStarted(Activity activity) { 102 | startCount++; 103 | } 104 | 105 | 106 | @Override 107 | public void onActivityStopped(Activity activity) { 108 | startCount--; 109 | if (startCount == 0) { 110 | mLifecycleListener.onBackToDesktop(); 111 | } 112 | } 113 | 114 | @Override 115 | public void onReceive(Context context, Intent intent) { 116 | String action = intent.getAction(); 117 | if (action != null && action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 118 | String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); 119 | if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) { 120 | mLifecycleListener.onBackToDesktop(); 121 | } 122 | } 123 | } 124 | 125 | 126 | @Override 127 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 128 | 129 | } 130 | 131 | 132 | @Override 133 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 134 | 135 | } 136 | 137 | @Override 138 | public void onActivityDestroyed(Activity activity) { 139 | 140 | } 141 | 142 | 143 | } 144 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatPhone.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.graphics.PixelFormat; 5 | import android.os.Build; 6 | import android.view.KeyEvent; 7 | import android.view.View; 8 | import android.view.Window; 9 | import android.view.WindowManager; 10 | import android.widget.LinearLayout; 11 | 12 | /** 13 | * Created by yhao on 17-11-14. 14 | * https://github.com/yhaolpz 15 | */ 16 | 17 | class FloatPhone extends FloatView { 18 | 19 | private final Context mContext; 20 | 21 | private final WindowManager mWindowManager; 22 | private final WindowManager.LayoutParams mLayoutParams; 23 | private View mView; 24 | private int mX, mY; 25 | private boolean isRemove = false; 26 | private PermissionListener mPermissionListener; 27 | 28 | FloatPhone(Context applicationContext, PermissionListener permissionListener) { 29 | mContext = applicationContext; 30 | mPermissionListener = permissionListener; 31 | mWindowManager = (WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE); 32 | mLayoutParams = new WindowManager.LayoutParams(); 33 | mLayoutParams.format = PixelFormat.RGBA_8888; 34 | mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 35 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 36 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 37 | mLayoutParams.windowAnimations = 0; 38 | 39 | 40 | } 41 | 42 | @Override 43 | public void setSize(int width, int height) { 44 | mLayoutParams.width = width; 45 | mLayoutParams.height = height; 46 | } 47 | 48 | @Override 49 | public void setView(View view) { 50 | mView = view; 51 | mView.setFocusableInTouchMode(true); 52 | } 53 | 54 | @Override 55 | public void setGravity(int gravity, int xOffset, int yOffset) { 56 | mLayoutParams.gravity = gravity; 57 | mLayoutParams.x = mX = xOffset; 58 | mLayoutParams.y = mY = yOffset; 59 | } 60 | 61 | public void addFocus() { 62 | WindowManager.LayoutParams lp = (WindowManager.LayoutParams) mView.getLayoutParams(); 63 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 64 | //| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 65 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 66 | //mView.setLayoutParams(lp); 67 | mView.requestFocus(); 68 | mWindowManager.updateViewLayout(mView, lp); 69 | } 70 | 71 | public void clearFocus() { 72 | WindowManager.LayoutParams lp = (WindowManager.LayoutParams) mView.getLayoutParams(); 73 | lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 74 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 75 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; 76 | //mView.setLayoutParams(lp); 77 | mView.requestFocus(); 78 | mWindowManager.updateViewLayout(mView, lp); 79 | } 80 | 81 | @Override 82 | public void init() { 83 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { 84 | req(); 85 | } else if (Miui.rom()) { 86 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 87 | req(); 88 | } else { 89 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; 90 | Miui.req(mContext, new PermissionListener() { 91 | @Override 92 | public void onSuccess() { 93 | mWindowManager.addView(mView, mLayoutParams); 94 | if (mPermissionListener != null) { 95 | mPermissionListener.onSuccess(); 96 | } 97 | } 98 | 99 | @Override 100 | public void onFail() { 101 | if (mPermissionListener != null) { 102 | mPermissionListener.onFail(); 103 | } 104 | } 105 | }); 106 | } 107 | } else { 108 | try { 109 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST; 110 | mWindowManager.addView(mView, mLayoutParams); 111 | } catch (Exception e) { 112 | mWindowManager.removeView(mView); 113 | LogUtil.e("TYPE_TOAST 失败"); 114 | req(); 115 | } 116 | } 117 | } 118 | 119 | private void req() { 120 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 121 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; 122 | } else { 123 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; 124 | } 125 | FloatActivity.request(mContext, new PermissionListener() { 126 | @Override 127 | public void onSuccess() { 128 | mWindowManager.addView(mView, mLayoutParams); 129 | if (mPermissionListener != null) { 130 | mPermissionListener.onSuccess(); 131 | } 132 | } 133 | 134 | @Override 135 | public void onFail() { 136 | if (mPermissionListener != null) { 137 | mPermissionListener.onFail(); 138 | } 139 | } 140 | }); 141 | } 142 | 143 | @Override 144 | public void dismiss() { 145 | isRemove = true; 146 | mWindowManager.removeView(mView); 147 | } 148 | 149 | @Override 150 | public void updateXY(int x, int y) { 151 | if (isRemove) return; 152 | mLayoutParams.x = mX = x; 153 | mLayoutParams.y = mY = y; 154 | mWindowManager.updateViewLayout(mView, mLayoutParams); 155 | } 156 | 157 | @Override 158 | void updateX(int x) { 159 | if (isRemove) return; 160 | mLayoutParams.x = mX = x; 161 | mWindowManager.updateViewLayout(mView, mLayoutParams); 162 | } 163 | 164 | @Override 165 | void updateY(int y) { 166 | if (isRemove) return; 167 | mLayoutParams.y = mY = y; 168 | mWindowManager.updateViewLayout(mView, mLayoutParams); 169 | } 170 | 171 | @Override 172 | int getX() { 173 | return mX; 174 | } 175 | 176 | @Override 177 | int getY() { 178 | return mY; 179 | } 180 | 181 | 182 | } 183 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatToast.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | import android.view.WindowManager; 6 | import android.widget.Toast; 7 | 8 | import java.lang.reflect.Field; 9 | import java.lang.reflect.Method; 10 | 11 | /** 12 | * 自定义 toast 方式,无需申请权限 13 | * 当前版本暂时用 TYPE_TOAST 代替,后续版本可能会再融入此方式 14 | */ 15 | 16 | class FloatToast extends FloatView { 17 | 18 | 19 | private Toast toast; 20 | 21 | private Object mTN; 22 | private Method show; 23 | private Method hide; 24 | 25 | private int mWidth; 26 | private int mHeight; 27 | 28 | 29 | FloatToast(Context applicationContext) { 30 | toast = new Toast(applicationContext); 31 | } 32 | 33 | 34 | @Override 35 | public void setSize(int width, int height) { 36 | mWidth = width; 37 | mHeight = height; 38 | } 39 | 40 | @Override 41 | public void setView(View view) { 42 | toast.setView(view); 43 | initTN(); 44 | } 45 | 46 | @Override 47 | public void setGravity(int gravity, int xOffset, int yOffset) { 48 | toast.setGravity(gravity, xOffset, yOffset); 49 | } 50 | 51 | @Override 52 | public void init() { 53 | try { 54 | show.invoke(mTN); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | } 59 | 60 | @Override 61 | public void dismiss() { 62 | try { 63 | hide.invoke(mTN); 64 | } catch (Exception e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | 69 | @Override 70 | void addFocus() { 71 | 72 | } 73 | 74 | @Override 75 | void clearFocus() { 76 | 77 | } 78 | 79 | 80 | private void initTN() { 81 | try { 82 | Field tnField = toast.getClass().getDeclaredField("mTN"); 83 | tnField.setAccessible(true); 84 | mTN = tnField.get(toast); 85 | show = mTN.getClass().getMethod("show"); 86 | hide = mTN.getClass().getMethod("hide"); 87 | Field tnParamsField = mTN.getClass().getDeclaredField("mParams"); 88 | tnParamsField.setAccessible(true); 89 | WindowManager.LayoutParams params = (WindowManager.LayoutParams) tnParamsField.get(mTN); 90 | params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 91 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 92 | params.width = mWidth; 93 | params.height = mHeight; 94 | params.windowAnimations = 0; 95 | Field tnNextViewField = mTN.getClass().getDeclaredField("mNextView"); 96 | tnNextViewField.setAccessible(true); 97 | tnNextViewField.set(mTN, toast.getView()); 98 | 99 | } catch (Exception e) { 100 | e.printStackTrace(); 101 | } 102 | } 103 | 104 | 105 | } 106 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatView.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.content.Context; 4 | import android.view.KeyEvent; 5 | import android.view.View; 6 | import android.widget.LinearLayout; 7 | 8 | /** 9 | * Created by yhao on 17-11-14. 10 | * https://github.com/yhaolpz 11 | */ 12 | 13 | abstract class FloatView { 14 | 15 | abstract void setSize(int width, int height); 16 | 17 | abstract void setView(View view); 18 | 19 | abstract void setGravity(int gravity, int xOffset, int yOffset); 20 | 21 | abstract void init(); 22 | 23 | abstract void dismiss(); 24 | 25 | void updateXY(int x, int y) { 26 | } 27 | 28 | void updateX(int x) { 29 | } 30 | 31 | void updateY(int y) { 32 | } 33 | 34 | int getX() { 35 | return 0; 36 | } 37 | 38 | int getY() { 39 | return 0; 40 | } 41 | 42 | abstract void addFocus(); 43 | 44 | abstract void clearFocus(); 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/java/com/yhao/floatwindow/FloatWindow.java: -------------------------------------------------------------------------------- 1 | package com.yhao.floatwindow; 2 | 3 | import android.animation.TimeInterpolator; 4 | import android.content.Context; 5 | import android.view.Gravity; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import androidx.annotation.LayoutRes; 10 | import androidx.annotation.MainThread; 11 | import androidx.annotation.NonNull; 12 | import androidx.annotation.Nullable; 13 | 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | /** 18 | * Created by yhao on 2017/12/22. 19 | * https://github.com/yhaolpz 20 | */ 21 | 22 | public class FloatWindow { 23 | 24 | private FloatWindow() { 25 | 26 | } 27 | 28 | private static final String mDefaultTag = "default_float_window_tag"; 29 | private static Map 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 |