2727import org .json .JSONArray ;
2828import org .json .JSONObject ;
2929
30+ import java .io .Console ;
3031import java .io .InputStream ;
3132import java .text .DecimalFormat ;
3233import java .util .ArrayList ;
@@ -76,6 +77,7 @@ public void onCreate() {
7677
7778 // inflate a new view hierarchy from the floating_layout xml
7879 floatView = (ViewGroup ) inflater .inflate (R .layout .floating_layout_button , null );
80+ floatView .setVisibility (View .INVISIBLE );
7981
8082 openBtn = floatView .findViewById (R .id .openBtn );
8183
@@ -113,6 +115,28 @@ public void onCreate() {
113115 touchListener ();
114116 }
115117
118+ @ Override
119+ public int onStartCommand (Intent intent , int flags , int startId ) {
120+ if (intent .getBooleanExtra ("Destroy" , false )) {
121+ destroy ();
122+ return START_REDELIVER_INTENT ;
123+ }
124+
125+ int visibility = intent .getIntExtra ("Visibility" , View .INVISIBLE );
126+ floatView .setVisibility (visibility );
127+
128+ return super .onStartCommand (intent , flags , startId );
129+ }
130+
131+ private void destroy (){
132+ // stopSelf() method is used to stop the service if
133+ // it was previously started
134+ stopSelf ();
135+
136+ // The window is removed from the screen
137+ windowManager .removeView (floatView );
138+ }
139+
116140 private void touchListener (){
117141 // Another feature of the floating window is, the window is movable.
118142 // The window can be moved at any position on the screen.
@@ -125,11 +149,7 @@ private void touchListener(){
125149
126150 @ Override
127151 public boolean onTouch (View v , MotionEvent event ) {
128-
129152 switch (event .getAction ()) {
130- case MotionEvent .ACTION_UP :
131- break ;
132-
133153 // When the window will be touched,
134154 // the x and y position of that position
135155 // will be retrieved
@@ -151,6 +171,12 @@ public boolean onTouch(View v, MotionEvent event) {
151171 floatWindowLayoutUpdateParam .x = (int ) ((x + event .getRawX ()) - px );
152172 floatWindowLayoutUpdateParam .y = (int ) ((y + event .getRawY ()) - py );
153173
174+ int difX = (int )(floatWindowLayoutUpdateParam .x - x );
175+ int difY = (int )(floatWindowLayoutUpdateParam .y - y );
176+ //Log.e("TEST", "::" + difX);
177+ //Log.e("TEST", "::" + difY);
178+ ((CustomImageView )v ).isMoving = difX != 0 || difY != 0 ;
179+
154180 windowsPosition .registerWindowPosition (FloatingWindowButton .class .getName (), new WindowPosition (
155181 floatWindowLayoutUpdateParam .x ,
156182 floatWindowLayoutUpdateParam .y
@@ -170,18 +196,24 @@ private void openListener(){
170196 openBtn .setOnClickListener (new View .OnClickListener () {
171197 @ Override
172198 public void onClick (View v ) {
173- // stopSelf() method is used to stop the service if
174- // it was previously started
175- stopSelf ();
176-
177- // The window is removed from the screen
178- windowManager .removeView (floatView );
199+ //Log.e("TEST", "::" + ((CustomImageView)v).isMoving);
200+ if (((CustomImageView )v ).isMoving ){
201+ return ;
202+ }
179203
180- startService ( new Intent ( FloatingWindowButton . this , FloatingWindowIVCalculator . class ) );
204+ toggleWindow ( );
181205 }
182206 });
183207 }
184208
209+ private void toggleWindow (){
210+ floatView .setVisibility (View .INVISIBLE );
211+
212+ Intent intent = new Intent (FloatingWindowButton .this , FloatingWindowIVCalculator .class );
213+ intent .putExtra ("Visibility" , View .VISIBLE );
214+ startService (intent );
215+ }
216+
185217 // It is called when stopService()
186218 // method is called in MainActivity
187219 @ Override
0 commit comments