3030
3131import com .example .flutter_overlay_window .R ;
3232
33+ import java .util .HashMap ;
34+ import java .util .Map ;
3335import java .util .Timer ;
3436import java .util .TimerTask ;
3537
@@ -50,6 +52,8 @@ public class OverlayService extends Service implements View.OnTouchListener {
5052 private Resources mResources ;
5153
5254 public static final String INTENT_EXTRA_IS_CLOSE_WINDOW = "IsCloseWindow" ;
55+
56+ private static OverlayService instance ;
5357 public static boolean isRunning = false ;
5458 private WindowManager windowManager = null ;
5559 private FlutterView flutterView ;
@@ -86,12 +90,15 @@ public void onDestroy() {
8690 isRunning = false ;
8791 NotificationManager notificationManager = (NotificationManager ) getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
8892 notificationManager .cancel (OverlayConstants .NOTIFICATION_ID );
93+ instance = null ;
8994 }
9095
9196 @ RequiresApi (api = Build .VERSION_CODES .JELLY_BEAN_MR1 )
9297 @ Override
9398 public int onStartCommand (Intent intent , int flags , int startId ) {
9499 mResources = getApplicationContext ().getResources ();
100+ int startX = intent .getIntExtra ("startX" , OverlayConstants .DEFAULT_XY );
101+ int startY = intent .getIntExtra ("startY" , OverlayConstants .DEFAULT_XY );
95102 boolean isCloseWindow = intent .getBooleanExtra (INTENT_EXTRA_IS_CLOSE_WINDOW , false );
96103 if (isCloseWindow ) {
97104 if (windowManager != null ) {
@@ -123,6 +130,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
123130 if (call .method .equals ("updateFlag" )) {
124131 String flag = call .argument ("flag" ).toString ();
125132 updateOverlayFlag (result , flag );
133+ } else if (call .method .equals ("updateOverlayPosition" )) {
134+ int x = call .<Integer >argument ("x" );
135+ int y = call .<Integer >argument ("y" );
136+ moveOverlay (x , y , result );
126137 } else if (call .method .equals ("resizeOverlay" )) {
127138 int width = call .argument ("width" );
128139 int height = call .argument ("height" );
@@ -144,6 +155,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
144155 int h = displaymetrics .heightPixels ;
145156 szWindow .set (w , h );
146157 }
158+ int dx = startX == OverlayConstants .DEFAULT_XY ? 0 : startX ;
159+ int dy = startY == OverlayConstants .DEFAULT_XY ? -statusBarHeightPx () : startY ;
147160 WindowManager .LayoutParams params = new WindowManager .LayoutParams (
148161 WindowSetup .width == -1999 ? -1 : WindowSetup .width ,
149162 WindowSetup .height != -1999 ? WindowSetup .height : screenHeight (),
@@ -162,6 +175,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
162175 params .gravity = WindowSetup .gravity ;
163176 flutterView .setOnTouchListener (this );
164177 windowManager .addView (flutterView , params );
178+ moveOverlay (dx , dy , null );
165179 return START_STICKY ;
166180 }
167181
@@ -238,6 +252,48 @@ private void resizeOverlay(int width, int height, boolean enableDrag, MethodChan
238252 }
239253 }
240254
255+ private void moveOverlay (int x , int y , MethodChannel .Result result ) {
256+ if (windowManager != null ) {
257+ WindowManager .LayoutParams params = (WindowManager .LayoutParams ) flutterView .getLayoutParams ();
258+ params .x = (x == -1999 || x == -1 ) ? -1 : dpToPx (x );
259+ params .y = dpToPx (y );
260+ windowManager .updateViewLayout (flutterView , params );
261+ if (result != null )
262+ result .success (true );
263+ } else {
264+ if (result != null )
265+ result .success (false );
266+ }
267+ }
268+
269+
270+ public static Map <String , Double > getCurrentPosition () {
271+ if (instance != null && instance .flutterView != null ) {
272+ WindowManager .LayoutParams params = (WindowManager .LayoutParams ) instance .flutterView .getLayoutParams ();
273+ Map <String , Double > position = new HashMap <>();
274+ position .put ("x" , instance .pxToDp (params .x ));
275+ position .put ("y" , instance .pxToDp (params .y ));
276+ return position ;
277+ }
278+ return null ;
279+ }
280+
281+ public static boolean moveOverlay (int x , int y ) {
282+ if (instance != null && instance .flutterView != null ) {
283+ if (instance .windowManager != null ) {
284+ WindowManager .LayoutParams params = (WindowManager .LayoutParams ) instance .flutterView .getLayoutParams ();
285+ params .x = (x == -1999 || x == -1 ) ? -1 : instance .dpToPx (x );
286+ params .y = instance .dpToPx (y );
287+ instance .windowManager .updateViewLayout (instance .flutterView , params );
288+ return true ;
289+ } else {
290+ return false ;
291+ }
292+ } else {
293+ return false ;
294+ }
295+ }
296+
241297
242298 @ Override
243299 public void onCreate () {
@@ -260,6 +316,7 @@ public void onCreate() {
260316 .setVisibility (WindowSetup .notificationVisibility )
261317 .build ();
262318 startForeground (OverlayConstants .NOTIFICATION_ID , notification );
319+ instance = this ;
263320 }
264321
265322 private void createNotificationChannel () {
@@ -284,6 +341,10 @@ private int dpToPx(int dp) {
284341 Float .parseFloat (dp + "" ), mResources .getDisplayMetrics ());
285342 }
286343
344+ private double pxToDp (int px ) {
345+ return (double ) px / mResources .getDisplayMetrics ().density ;
346+ }
347+
287348 private boolean inPortrait () {
288349 return mResources .getConfiguration ().orientation == Configuration .ORIENTATION_PORTRAIT ;
289350 }
0 commit comments