Skip to content

Commit 57c3c35

Browse files
committed
perf: Performance improvement
1 parent 8210ca1 commit 57c3c35

6 files changed

Lines changed: 90 additions & 41 deletions

File tree

.idea/deploymentTargetDropDown.xml

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ android {
1010
applicationId = "dev.docas.magictrapgo"
1111
minSdk = 29
1212
targetSdk = 34
13-
versionCode = 2
14-
versionName = "1.1.0"
13+
versionCode = 3
14+
versionName = "1.3.0"
1515

1616
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
17-
versionNameSuffix = "1.1.0"
1817
}
1918

2019
buildTypes {

app/src/main/java/dev/docas/magictrapgo/CustomImageView.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import androidx.annotation.Nullable;
99

1010
public class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
11+
public boolean isMoving = false;
12+
1113
public CustomImageView(@NonNull Context context) {
1214
super(context);
1315
}
@@ -29,11 +31,10 @@ public boolean onTouchEvent(MotionEvent event) {
2931
return true;
3032

3133
case MotionEvent.ACTION_UP:
32-
setClickable(true);
34+
performClick();
3335
return true;
3436

3537
case MotionEvent.ACTION_MOVE:
36-
setClickable(false);
3738
return true;
3839
}
3940
return false;

app/src/main/java/dev/docas/magictrapgo/FloatingWindowButton.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.json.JSONArray;
2828
import org.json.JSONObject;
2929

30+
import java.io.Console;
3031
import java.io.InputStream;
3132
import java.text.DecimalFormat;
3233
import 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

app/src/main/java/dev/docas/magictrapgo/FloatingWindowIVCalculator.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class FloatingWindowIVCalculator extends Service {
5353
private int LAYOUT_TYPE;
5454
private WindowManager.LayoutParams floatWindowLayoutParam;
5555
private WindowManager windowManager;
56-
private boolean keyboardOpened = false;
5756

5857
// As FloatingWindowGFG inherits Service class,
5958
// it actually overrides the onBind method
@@ -83,6 +82,7 @@ public void onCreate() {
8382

8483
// inflate a new view hierarchy from the floating_layout xml
8584
floatView = (ViewGroup) inflater.inflate(R.layout.floating_layout, null);
85+
floatView.setVisibility(View.INVISIBLE);
8686

8787
pokemonInput = floatView.findViewById(R.id.pokemonList);
8888
cpInput = floatView.findViewById(R.id.pokemonCP);
@@ -126,6 +126,19 @@ public void onCreate() {
126126
touchListener();
127127
}
128128

129+
@Override
130+
public int onStartCommand(Intent intent, int flags, int startId) {
131+
if(intent.getBooleanExtra("Destroy", false)) {
132+
destroy();
133+
return START_REDELIVER_INTENT;
134+
}
135+
136+
int visibility = intent.getIntExtra("Visibility", View.INVISIBLE);
137+
floatView.setVisibility(visibility);
138+
139+
return super.onStartCommand(intent, flags, startId);
140+
}
141+
129142
private void searchListener(){
130143
searchBtn.setOnClickListener(new View.OnClickListener(){
131144
@Override
@@ -248,17 +261,25 @@ private void createDropDownList(){
248261
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
249262
}
250263

264+
private void destroy(){
265+
// stopSelf() method is used to stop the service if
266+
// it was previously started
267+
stopSelf();
268+
269+
// The window is removed from the screen
270+
windowManager.removeView(floatView);
271+
}
272+
251273
private void closeListener(){
252274
// The button that helps to maximize the app
253275
closeBtn.setOnClickListener(new View.OnClickListener() {
254276
@Override
255277
public void onClick(View v) {
256-
// stopSelf() method is used to stop the service if
257-
// it was previously started
258-
stopSelf();
278+
Intent intent = new Intent(FloatingWindowIVCalculator.this, FloatingWindowButton.class);
279+
intent.putExtra("Destroy", true);
280+
startService(intent);
259281

260-
// The window is removed from the screen
261-
windowManager.removeView(floatView);
282+
destroy();
262283
}
263284
});
264285
}
@@ -268,14 +289,11 @@ private void toggleListener(){
268289
toggleBtn.setOnClickListener(new View.OnClickListener() {
269290
@Override
270291
public void onClick(View v) {
271-
// stopSelf() method is used to stop the service if
272-
// it was previously started
273-
stopSelf();
274-
275-
// The window is removed from the screen
276-
windowManager.removeView(floatView);
292+
floatView.setVisibility(View.INVISIBLE);
277293

278-
startService(new Intent(FloatingWindowIVCalculator.this, FloatingWindowButton.class));
294+
Intent intent = new Intent(FloatingWindowIVCalculator.this, FloatingWindowButton.class);
295+
intent.putExtra("Visibility", View.VISIBLE);
296+
startService(intent);
279297
}
280298
});
281299
}

app/src/main/java/dev/docas/magictrapgo/MainActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.net.Uri;
1111
import android.os.Bundle;
1212
import android.provider.Settings;
13+
import android.view.View;
1314

1415
public class MainActivity extends AppCompatActivity {
1516
private AlertDialog dialog;
@@ -56,7 +57,14 @@ private boolean isMyServiceRunning() {
5657
private void openFloatingScreen(){
5758
if (checkOverlayDisplayPermission()) {
5859
// FloatingWindowGFG service is started
59-
startService(new Intent(MainActivity.this, FloatingWindowButton.class));
60+
Intent btnIntent = new Intent(MainActivity.this, FloatingWindowButton.class);
61+
btnIntent.putExtra("Visibility", View.VISIBLE);
62+
startService(btnIntent);
63+
64+
Intent formIntent = new Intent(MainActivity.this, FloatingWindowIVCalculator.class);
65+
btnIntent.putExtra("Visibility", View.INVISIBLE);
66+
startService(formIntent);
67+
6068
// The MainActivity closes here
6169
finish();
6270
return;

0 commit comments

Comments
 (0)