Skip to content

Commit c305040

Browse files
committed
Long press float window to open MainActivity
1 parent 0d081d3 commit c305040

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

activitytaskview/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
android:label="@string/app_name"
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme">
13-
<activity android:name=".MainActivity">
13+
<activity android:name=".MainActivity"
14+
android:launchMode="singleTask">
1415
<intent-filter>
1516
<action android:name="android.intent.action.MAIN" />
1617

activitytaskview/src/main/java/cc/rome753/activitytask/view/ActivityTaskView.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.rome753.activitytask.view;
22

33
import android.content.Context;
4+
import android.content.Intent;
45
import android.util.Log;
56
import android.view.MotionEvent;
67
import android.view.View;
@@ -13,6 +14,7 @@
1314
import java.util.Map;
1415

1516
import cc.rome753.activitytask.AUtils;
17+
import cc.rome753.activitytask.MainActivity;
1618
import cc.rome753.activitytask.R;
1719
import cc.rome753.activitytask.model.ATree;
1820
import cc.rome753.activitytask.model.LifecycleInfo;
@@ -22,7 +24,7 @@
2224
* Created by rome753 on 2017/3/31.
2325
*/
2426

25-
public class ActivityTaskView extends LinearLayout {
27+
public class ActivityTaskView extends LinearLayout implements Runnable {
2628

2729
public static final String TAG = ActivityTaskView.class.getSimpleName();
2830
private ViewGroup mLinearLayout;
@@ -52,12 +54,13 @@ public ActivityTaskView(Context context) {
5254
long downTime;
5355

5456
@Override
55-
public boolean onTouchEvent(MotionEvent event) {
57+
public boolean onTouchEvent(MotionEvent event) {
5658
switch (event.getAction()) {
5759
case MotionEvent.ACTION_DOWN:
5860
downTime = System.currentTimeMillis();
5961
mInnerX = event.getX();
6062
mInnerY = event.getY();
63+
postDelayed(this, 300);
6164
break;
6265
case MotionEvent.ACTION_MOVE:
6366
float x = event.getRawX();
@@ -66,11 +69,17 @@ public boolean onTouchEvent(MotionEvent event) {
6669
params.x = (int) (x - mInnerX);
6770
params.y = (int) (y - mInnerY - mStatusHeight);
6871
updateLayout(params);
72+
73+
if(Math.abs(event.getX() - mInnerX) > 20
74+
|| Math.abs(event.getY() - mInnerY) > 20) {
75+
removeCallbacks(this);
76+
}
6977
break;
7078
case MotionEvent.ACTION_UP:
79+
removeCallbacks(this);
7180
if(System.currentTimeMillis() - downTime < 100
72-
&& Math.abs(event.getX() - mInnerX) < 10
73-
&& Math.abs(event.getY() - mInnerY) < 10) {
81+
&& Math.abs(event.getX() - mInnerX) < 20
82+
&& Math.abs(event.getY() - mInnerY) < 20) {
7483
doClick();
7584
}
7685
moveToBorder();
@@ -86,6 +95,11 @@ private void doClick() {
8695
mTinyView.setVisibility(!visible ? GONE : VISIBLE);
8796
}
8897

98+
private void doLongClick() {
99+
Intent intent = new Intent(getContext(), MainActivity.class);
100+
getContext().startActivity(intent);
101+
}
102+
89103
private void updateLayout(WindowManager.LayoutParams params){
90104
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
91105
if(windowManager != null) {
@@ -182,4 +196,8 @@ public void clear() {
182196
notifyData();
183197
}
184198

199+
@Override
200+
public void run() {
201+
doLongClick();
202+
}
185203
}

activitytaskview/src/main/res/layout/activity_main.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
android:id="@+id/btn_clear"
3333
android:text="Clear"
3434
android:textAllCaps="false"
35-
app:layout_constraintTop_toBottomOf="@id/btn_permission"
35+
app:layout_constraintBottom_toTopOf="@id/btn_exit"
3636
app:layout_constraintStart_toStartOf="parent"
3737
app:layout_constraintEnd_toEndOf="parent"
3838
android:layout_width="wrap_content"
@@ -42,7 +42,19 @@
4242
android:id="@+id/btn_exit"
4343
android:text="Exit"
4444
android:textAllCaps="false"
45-
app:layout_constraintTop_toBottomOf="@id/btn_clear"
45+
android:textColor="#FF0000"
46+
android:layout_marginBottom="50dp"
47+
app:layout_constraintBottom_toBottomOf="parent"
48+
app:layout_constraintStart_toStartOf="parent"
49+
app:layout_constraintEnd_toEndOf="parent"
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content" />
52+
53+
<TextView
54+
android:id="@+id/tv_set_duration"
55+
android:text="Set duration (ms)"
56+
app:layout_constraintTop_toBottomOf="@id/btn_permission"
57+
android:layout_marginTop="50dp"
4658
app:layout_constraintStart_toStartOf="parent"
4759
app:layout_constraintEnd_toEndOf="parent"
4860
android:layout_width="wrap_content"
@@ -51,8 +63,10 @@
5163
<NumberPicker
5264
android:id="@+id/np"
5365
android:orientation="horizontal"
66+
android:layout_marginTop="10dp"
67+
app:layout_constraintTop_toBottomOf="@id/tv_set_duration"
5468
app:layout_constraintStart_toStartOf="parent"
55-
app:layout_constraintTop_toTopOf="parent"
69+
app:layout_constraintEnd_toEndOf="parent"
5670
android:layout_width="wrap_content"
5771
android:layout_height="wrap_content">
5872

0 commit comments

Comments
 (0)