Skip to content

Commit 0d081d3

Browse files
committed
UI finally complete!!!
1 parent 9a44a62 commit 0d081d3

8 files changed

Lines changed: 121 additions & 96 deletions

File tree

activitytaskview/src/main/java/cc/rome753/activitytask/ActivityTask.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ public class ActivityTask {
3131
public static long interval = 100;
3232

3333
public static void start(Context context) {
34-
if(activityTaskView == null) {
34+
if (activityTaskView == null) {
3535
activityTaskView = new ActivityTaskView(context);
3636
addViewToWindow(context, activityTaskView);
3737
}
3838
}
3939

4040
public static void clear() {
41-
if(activityTaskView != null) {
41+
if (activityTaskView != null) {
4242
activityTaskView.clear();
4343
}
4444
}
4545

46-
private static void addViewToWindow(Context context, View view){
46+
private static void addViewToWindow(Context context, View view) {
4747
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
4848
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
4949
if (Build.VERSION.SDK_INT >= 26) {// Android 8.0
50-
params.type= WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
51-
}else {
50+
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
51+
} else {
5252
params.type = WindowManager.LayoutParams.TYPE_PHONE;
5353
}
5454
params.format = PixelFormat.RGBA_8888;
@@ -58,27 +58,27 @@ private static void addViewToWindow(Context context, View view){
5858
params.gravity = Gravity.START | Gravity.TOP;
5959
params.x = 0;
6060
params.y = context.getResources().getDisplayMetrics().heightPixels - 500;
61-
if(windowManager != null) {
61+
if (windowManager != null) {
6262
windowManager.addView(view, params);
6363
}
6464
}
6565

6666
private static QueueHandler queueHandler;
6767

6868
public static void add(String lifecycle, String task, String activity, List<String> fragments) {
69-
if(queueHandler == null) {
69+
if (queueHandler == null) {
7070
queueHandler = new QueueHandler();
7171
}
7272
LifecycleInfo info = new LifecycleInfo(lifecycle, task, activity, fragments);
7373
queueHandler.send(info);
7474
}
7575

76-
private static class QueueHandler extends Handler{
76+
private static class QueueHandler extends Handler {
7777

7878
private Queue<LifecycleInfo> queue;
7979
private long lastTime;
8080

81-
QueueHandler(){
81+
QueueHandler() {
8282
super(Looper.getMainLooper());
8383
lastTime = 0;
8484
queue = new LinkedList<>();
@@ -91,27 +91,21 @@ void send(LifecycleInfo info) {
9191

9292
@Override
9393
public void handleMessage(Message msg) {
94-
if(System.currentTimeMillis() - lastTime < interval){
94+
if (System.currentTimeMillis() - lastTime < interval) {
9595
sendEmptyMessageDelayed(0, interval / 5);
96-
}else {
96+
} else {
9797
lastTime = System.currentTimeMillis();
9898
LifecycleInfo info = queue.poll();
99-
if(info == null) {
100-
return;
101-
}
102-
if(info.fragments != null) {
103-
FragmentTaskView fragmentTaskView = activityTaskView.findFragmentTaskView(info.activity);
104-
if(fragmentTaskView != null) {
99+
if (info != null && activityTaskView != null) {
100+
if (info.fragments != null) {
105101
if (info.lifecycle.contains("PreAttach")) {
106-
fragmentTaskView.add(info);
102+
activityTaskView.addF(info);
107103
} else if (info.lifecycle.contains("Detach")) {
108-
fragmentTaskView.remove(info.fragments);
104+
activityTaskView.removeF(info);
109105
} else {
110-
fragmentTaskView.update(info);
106+
activityTaskView.updateF(info);
111107
}
112-
}
113-
} else {
114-
if(activityTaskView != null) {
108+
} else {
115109
if (info.lifecycle.contains("Create")) {
116110
activityTaskView.add(info);
117111
} else if (info.lifecycle.contains("Destroy")) {

activitytaskview/src/main/java/cc/rome753/activitytask/model/ViewPool.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
import android.view.View;
55
import android.view.ViewGroup;
66

7+
import java.util.HashMap;
78
import java.util.LinkedList;
89
import java.util.Observable;
910

1011
import cc.rome753.activitytask.view.ATextView;
12+
import cc.rome753.activitytask.view.FragmentTaskView;
1113

1214
public class ViewPool extends Observable {
1315

1416
LinkedList<ATextView> pool = new LinkedList<>();
17+
HashMap<String,FragmentTaskView> map = new HashMap<>();
18+
1519
private static ViewPool factory = new ViewPool();
1620
public static ViewPool get() {
1721
return factory;
@@ -25,6 +29,8 @@ public void recycle(ViewGroup viewGroup) {
2529
removeParent(view);
2630
view.setTag(null);
2731
pool.add((ATextView) view);
32+
} else if(view instanceof FragmentTaskView) {
33+
// don't recycle
2834
} else if(view instanceof ViewGroup) {
2935
recycle((ViewGroup) view);
3036
}
@@ -53,4 +59,23 @@ public void notifyLifecycleChange(LifecycleInfo info) {
5359
setChanged();
5460
notifyObservers(info);
5561
}
62+
63+
64+
public FragmentTaskView getF(String activity) {
65+
return map.get(activity);
66+
}
67+
68+
public FragmentTaskView addF(Context context, String activity) {
69+
FragmentTaskView view = new FragmentTaskView(context);
70+
map.put(activity, view);
71+
return view;
72+
}
73+
74+
public void removeF(String activity) {
75+
map.remove(activity);
76+
}
77+
78+
public void clearF() {
79+
map.clear();
80+
}
5681
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ public ATextView(Context context, AttributeSet attrs, int defStyleAttr) {
3939
}
4040

4141
public void setInfoText(String s, String lifecycle) {
42-
int i1 = s.indexOf("@");
43-
String tag = s.substring(i1);
44-
setTag(tag);
42+
String hash = s.substring(s.indexOf("@"));
43+
setTag(hash);
4544
s = s.replace("Activity", "A…");
4645
s = s.replace("Fragment", "F…");
47-
s = s.replace(tag, " ");
46+
s = s.replace(hash, " ");
4847

4948
addLifecycle(s, lifecycle);
5049
}
@@ -68,9 +67,8 @@ public void update(Observable o, Object arg) {
6867
if(arg instanceof LifecycleInfo) {
6968
LifecycleInfo info = (LifecycleInfo) arg;
7069
String s = info.fragments != null ? info.fragments.get(0) : info.activity;
71-
int i1 = s.indexOf("@");
72-
String tag = s.substring(i1);
73-
if(TextUtils.equals((CharSequence) getTag(), tag)) {
70+
String hash = s.substring(s.indexOf("@"));
71+
if(TextUtils.equals((CharSequence) getTag(), hash)) {
7472
s = getText().toString();
7573
s = s.substring(0, s.lastIndexOf(" ") + 1);
7674
addLifecycle(s, info.lifecycle);

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

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

33
import android.content.Context;
4-
import android.text.TextUtils;
54
import android.util.Log;
65
import android.view.MotionEvent;
76
import android.view.View;
@@ -10,8 +9,8 @@
109
import android.widget.LinearLayout;
1110

1211
import java.util.ArrayList;
12+
import java.util.HashSet;
1313
import java.util.Map;
14-
import java.util.Set;
1514

1615
import cc.rome753.activitytask.AUtils;
1716
import cc.rome753.activitytask.R;
@@ -27,11 +26,11 @@ public class ActivityTaskView extends LinearLayout {
2726

2827
public static final String TAG = ActivityTaskView.class.getSimpleName();
2928
private ViewGroup mLinearLayout;
30-
private ViewGroup mContainer;
3129
private View mTinyView;
3230
private View mTaskView;
3331
private View mEmptyView;
3432

33+
private HashSet<String> mPendingRemove; // Wait for fragments destroy
3534

3635
private int mStatusHeight;
3736
private int mScreenWidth;
@@ -41,21 +40,11 @@ public ActivityTaskView(Context context) {
4140
inflate(context, R.layout.view_activity_task, this);
4241
mStatusHeight = AUtils.getStatusBarHeight(context);
4342
mScreenWidth = AUtils.getScreenWidth(context);
44-
mLinearLayout = findViewById(R.id.ll);
45-
mContainer = findViewById(R.id.fl);
43+
mLinearLayout = findViewById(R.id.container);
4644
mTinyView = findViewById(R.id.tiny_view);
47-
mTaskView = findViewById(R.id.task_view);
45+
mTaskView = findViewById(R.id.main_view);
4846
mEmptyView = findViewById(R.id.view_empty);
49-
}
50-
51-
public FragmentTaskView findFragmentTaskView(String activity) {
52-
for(int i = 0; i < mContainer.getChildCount(); i++) {
53-
View view = mContainer.getChildAt(i);
54-
if (TextUtils.equals((CharSequence) view.getTag(), activity)) {
55-
return (FragmentTaskView) view;
56-
}
57-
}
58-
return null;
47+
mPendingRemove = new HashSet<>();
5948
}
6049

6150
float mInnerX;
@@ -119,50 +108,77 @@ private void moveToBorder() {
119108
private ATree aTree = new ATree();
120109

121110
public void add(LifecycleInfo info) {
122-
FragmentTaskView view = new FragmentTaskView(getContext());
123-
view.setTitle(info.activity);
124-
view.setTag(info.activity);
125-
mContainer.addView(view, 0);
126-
127111
aTree.add(info.task, info.activity, info.lifecycle);
128112
notifyData();
129113
}
130114

131115
public void remove(LifecycleInfo info) {
132-
FragmentTaskView view = findFragmentTaskView(info.activity);
133-
ViewPool.get().recycle(view);
134-
mContainer.removeView(view);
135-
136-
aTree.remove(info.task, info.activity);
137-
notifyData();
116+
if(ViewPool.get().getF(info.activity) != null) {
117+
mPendingRemove.add(info.activity);
118+
update(info);
119+
} else {
120+
aTree.remove(info.task, info.activity);
121+
notifyData();
122+
}
138123
}
139124

140125
public void update(LifecycleInfo info) {
141126
aTree.updateLifecycle(info.activity, info.lifecycle);
142127
ViewPool.get().notifyLifecycleChange(info);
143128
}
144129

130+
public void addF(LifecycleInfo info) {
131+
FragmentTaskView view = ViewPool.get().getF(info.activity);
132+
if(view == null) {
133+
view = ViewPool.get().addF(getContext(), info.activity);
134+
notifyData();
135+
}
136+
view.add(info);
137+
}
138+
139+
public void removeF(LifecycleInfo info) {
140+
FragmentTaskView view = ViewPool.get().getF(info.activity);
141+
if(view != null) {
142+
view.remove(info);
143+
if(view.getChildCount() == 0 && mPendingRemove.contains(info.activity)) {
144+
mPendingRemove.remove(info.activity);
145+
remove(info);
146+
}
147+
}
148+
}
149+
150+
public void updateF(LifecycleInfo info) {
151+
FragmentTaskView view = ViewPool.get().getF(info.activity);
152+
if(view != null) {
153+
view.update(info);
154+
}
155+
}
156+
145157
private void notifyData() {
146158
ViewPool.get().recycle(mLinearLayout);
147159
mLinearLayout.removeAllViews();
148-
Set<Map.Entry<String, ArrayList<String>>> set = aTree.entrySet();
149-
for(Map.Entry<String, ArrayList<String>> entry : set) {
150-
TaskLayout layout = new TaskLayout(getContext());
151-
layout.setTitle(entry.getKey());
152-
for (String value : entry.getValue()) {
160+
for(Map.Entry<String, ArrayList<String>> entry : aTree.entrySet()) {
161+
TaskLayout taskLayout = new TaskLayout(getContext());
162+
taskLayout.setTitle(entry.getKey());
163+
for (String activity : entry.getValue()) {
153164
ATextView textView = ViewPool.get().getOne(getContext());
154-
textView.setInfoText(value, aTree.getLifecycle(value));
155-
layout.addFirst(textView);
165+
textView.setInfoText(activity, aTree.getLifecycle(activity));
166+
taskLayout.addFirst(textView);
167+
168+
FragmentTaskView view = ViewPool.get().getF(activity);
169+
if(view != null) {
170+
taskLayout.addSecond(view);
171+
}
156172
}
157-
mLinearLayout.addView(layout, 0);
173+
mLinearLayout.addView(taskLayout, 0);
158174
}
159175
mEmptyView.setVisibility(mLinearLayout.getChildCount() == 0 ? VISIBLE : GONE);
160176

161177
}
162178

163179
public void clear() {
180+
ViewPool.get().clearF();
164181
aTree = new ATree();
165-
mContainer.removeAllViews();
166182
notifyData();
167183
}
168184

0 commit comments

Comments
 (0)