11package cc .rome753 .activitytask ;
22
3- import android .app .Activity ;
4- import android .app .Application ;
53import android .content .Context ;
6- import android .content .Intent ;
74import android .graphics .PixelFormat ;
85import android .os .Build ;
9- import android .os .Bundle ;
106import android .os .Handler ;
117import android .os .Looper ;
128import android .os .Message ;
13- import android .provider .Settings ;
14- import android .util .Log ;
159import android .view .Gravity ;
1610import android .view .View ;
1711import android .view .WindowManager ;
1812
19- import androidx .fragment .app .Fragment ;
20- import androidx .fragment .app .FragmentActivity ;
21- import androidx .fragment .app .FragmentManager ;
22-
2313import java .util .LinkedList ;
14+ import java .util .List ;
2415import java .util .Queue ;
2516
26- import cc .rome753 .activitytask .model .FragmentInfo ;
27- import cc .rome753 .activitytask .model .TaskInfo ;
17+ import cc .rome753 .activitytask .model .LifecycleInfo ;
2818import cc .rome753 .activitytask .view .ActivityTaskView ;
29-
30- import static android .view .View .GONE ;
31- import static android .view .View .VISIBLE ;
32- import static cc .rome753 .activitytask .AUtils .getSimpleName ;
19+ import cc .rome753 .activitytask .view .FragmentTaskView ;
3320
3421/**
3522 * Created by rome753@163.com on 2017/4/16.
@@ -40,67 +27,15 @@ public class ActivityTask {
4027 private static final String TAG = ActivityTask .class .getSimpleName ();
4128
4229 private static ActivityTaskView activityTaskView ;
43- private static boolean autoHide = true ;
44- private static long interval = 100 ;
45- private static int textSize = 12 ;
46-
47- /**
48- * Is current app front. If not, hide the activityTaskView.
49- * <p>
50- * front: Activity A onPause -> Activity B onResume -> Activity A onStop
51- * <p>
52- * not front: Activity A onPause -> Activity A onStop
53- */
54- private static boolean isFront ;
55-
56- /**
57- * auto hide when app is not in foreground, default true
58- */
59- public static void setAutoHide (boolean autoHide ) {
60- ActivityTask .autoHide = autoHide ;
61- }
62-
63- /**
64- * interval between lifecycle, default 100(ms)
65- */
66- public static void setInterval (long interval ) {
67- ActivityTask .interval = interval ;
68- }
6930
70- public static int getTextSize () {
71- return textSize ;
72- }
73-
74- public static void setTextSize (int textSize ) {
75- ActivityTask .textSize = textSize ;
76- }
77-
78- /**
79- * Init in your application's onCreate()
80- *
81- * @param app your application
82- * @param debug your app module BuildConfig.DEBUG
83- */
84- public static void init (Application app , boolean debug ) {
85- if (!debug ) {
86- return ;
87- }
88- if (Build .VERSION .SDK_INT >= 23 && !Settings .canDrawOverlays (app )) {
89- Intent intent = new Intent (app , RequestOverlayActivity .class );
90- intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
91- app .startActivity (intent );
92- } else {
93- start (app );
94- }
95- }
31+ private static long interval = 100 ;
9632
97- static void start (Application app ) {
98- addViewToWindow (app , activityTaskView = new ActivityTaskView (app ));
99- registerActivityLifecycleCallbacks (app );
33+ public static void start (Context context ) {
34+ addViewToWindow (context , activityTaskView = new ActivityTaskView (context ));
10035 }
10136
102- private static void addViewToWindow (Application app , View view ){
103- WindowManager windowManager = (WindowManager ) app .getSystemService (Context .WINDOW_SERVICE );
37+ private static void addViewToWindow (Context context , View view ){
38+ WindowManager windowManager = (WindowManager ) context .getSystemService (Context .WINDOW_SERVICE );
10439 WindowManager .LayoutParams params = new WindowManager .LayoutParams ();
10540 if (Build .VERSION .SDK_INT >= 26 ) {// Android 8.0
10641 params .type = WindowManager .LayoutParams .TYPE_APPLICATION_OVERLAY ;
@@ -113,151 +48,25 @@ private static void addViewToWindow(Application app, View view){
11348 params .height = WindowManager .LayoutParams .WRAP_CONTENT ;
11449 params .gravity = Gravity .START | Gravity .TOP ;
11550 params .x = 0 ;
116- params .y = app .getResources ().getDisplayMetrics ().heightPixels - 500 ;
51+ params .y = context .getResources ().getDisplayMetrics ().heightPixels - 500 ;
11752 if (windowManager != null ) {
11853 windowManager .addView (view , params );
11954 }
12055 }
12156
122- private static void registerActivityLifecycleCallbacks (Application app ){
123- final QueueHandler queueHandler = new QueueHandler ();
124- app .registerActivityLifecycleCallbacks (new Application .ActivityLifecycleCallbacks () {
125-
126- @ Override
127- public void onActivityCreated (Activity activity , Bundle savedInstanceState ) {
128- Log .w (TAG , activity .getClass ().getName () + "@" + activity .hashCode () + " " + activity .getTaskId () + " " + " onActivityCreated" );
129- queueHandler .add (new TaskInfo (0 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
130- registerFragmentLifecycleCallbacks (activity , queueHandler );
131- }
132-
133- @ Override
134- public void onActivityStarted (Activity activity ) {
135- Log .d (TAG , activity .getClass ().getSimpleName () + " onActivityStarted" );
136- queueHandler .add (new TaskInfo (1 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
137- }
138-
139- @ Override
140- public void onActivityResumed (Activity activity ) {
141- Log .d (TAG , activity .getClass ().getSimpleName () + " onActivityResumed" );
142- queueHandler .add (new TaskInfo (2 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
143- if (autoHide ) {
144- activityTaskView .setVisibility (VISIBLE );
145- isFront = true ;
146- }
147- }
148-
149- @ Override
150- public void onActivityPaused (Activity activity ) {
151- Log .d (TAG , activity .getClass ().getSimpleName () + " onActivityPaused" );
152- queueHandler .add (new TaskInfo (3 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
153- isFront = false ;
154- }
155-
156- @ Override
157- public void onActivityStopped (Activity activity ) {
158- Log .d (TAG , activity .getClass ().getSimpleName () + " onActivityStopped" );
159- queueHandler .add (new TaskInfo (4 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
160- if (autoHide ){
161- activityTaskView .setVisibility (isFront ? VISIBLE : GONE );
162- }
163-
164- }
165-
166- @ Override
167- public void onActivitySaveInstanceState (Activity activity , Bundle outState ) {
168- Log .d (TAG , activity .getClass ().getSimpleName () + " onActivitySaveInstanceState" );
169- }
170-
171- @ Override
172- public void onActivityDestroyed (Activity activity ) {
173- Log .w (TAG , activity .getClass ().getSimpleName () + " onActivityDestroyed" );
174- queueHandler .add (new TaskInfo (5 , String .valueOf (activity .getTaskId ()), getSimpleName (activity )));
175- }
176- });
177- }
178-
179- private static void registerFragmentLifecycleCallbacks (Activity activity , final QueueHandler queueHandler ){
180- if (activity instanceof FragmentActivity ){
181- ((FragmentActivity ) activity ).getSupportFragmentManager ().registerFragmentLifecycleCallbacks (new FragmentManager .FragmentLifecycleCallbacks () {
182- @ Override
183- public void onFragmentPreAttached (FragmentManager fm , Fragment f , Context context ) {
184- Log .d (TAG , getSimpleName (context ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
185- }
186-
187- @ Override
188- public void onFragmentAttached (FragmentManager fm , Fragment f , Context context ) {
189- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
190- }
191-
192- @ Override
193- public void onFragmentCreated (FragmentManager fm , Fragment f , Bundle savedInstanceState ) {
194- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
195- queueHandler .add (new FragmentInfo (0 , f ));
196-
197- }
198-
199- @ Override
200- public void onFragmentActivityCreated (FragmentManager fm , Fragment f , Bundle savedInstanceState ) {
201- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
202- }
203-
204- @ Override
205- public void onFragmentViewCreated (FragmentManager fm , Fragment f , View v , Bundle savedInstanceState ) {
206- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
207- }
208-
209- @ Override
210- public void onFragmentStarted (FragmentManager fm , Fragment f ) {
211- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
212- queueHandler .add (new FragmentInfo (1 , f ));
213- }
214-
215- @ Override
216- public void onFragmentResumed (FragmentManager fm , Fragment f ) {
217- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
218- queueHandler .add (new FragmentInfo (2 , f ));
219- }
220-
221- @ Override
222- public void onFragmentPaused (FragmentManager fm , Fragment f ) {
223- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
224- queueHandler .add (new FragmentInfo (3 , f ));
225- }
226-
227- @ Override
228- public void onFragmentStopped (FragmentManager fm , Fragment f ) {
229- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
230- queueHandler .add (new FragmentInfo (4 , f ));
231- }
232-
233- @ Override
234- public void onFragmentSaveInstanceState (FragmentManager fm , Fragment f , Bundle outState ) {
235- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
236- }
237-
238- @ Override
239- public void onFragmentViewDestroyed (FragmentManager fm , Fragment f ) {
240- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
241- }
242-
243- @ Override
244- public void onFragmentDestroyed (FragmentManager fm , Fragment f ) {
245- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
246- queueHandler .add (new FragmentInfo (5 , f ));
247- }
248-
249- @ Override
250- public void onFragmentDetached (FragmentManager fm , Fragment f ) {
251- Log .d (TAG , getSimpleName (f ) + ": " + Thread .currentThread ().getStackTrace ()[2 ].getMethodName ());
252- }
57+ private static QueueHandler queueHandler ;
25358
254- }, true );
59+ public static void add (String lifecycle , String task , String activity , List <String > fragments ) {
60+ if (queueHandler == null ) {
61+ queueHandler = new QueueHandler ();
25562 }
63+ LifecycleInfo info = new LifecycleInfo (lifecycle , task , activity , fragments );
64+ queueHandler .send (info );
25665 }
25766
25867 private static class QueueHandler extends Handler {
25968
260- private Queue <Object > queue ;
69+ private Queue <LifecycleInfo > queue ;
26170 private long lastTime ;
26271
26372 QueueHandler (){
@@ -266,8 +75,8 @@ private static class QueueHandler extends Handler{
26675 queue = new LinkedList <>();
26776 }
26877
269- void add ( Object obj ) {
270- queue .add (obj );
78+ void send ( LifecycleInfo info ) {
79+ queue .add (info );
27180 sendEmptyMessage (0 );
27281 }
27382
@@ -277,11 +86,33 @@ public void handleMessage(Message msg) {
27786 sendEmptyMessageDelayed (0 , interval / 5 );
27887 }else {
27988 lastTime = System .currentTimeMillis ();
280- Object obj = queue .poll ();
281- if (obj instanceof FragmentInfo ) {
282- activityTaskView .onFragmentChange ((FragmentInfo ) obj );
283- } else if (obj instanceof TaskInfo ) {
284- activityTaskView .onActivityChange ((TaskInfo ) obj );
89+ LifecycleInfo info = queue .poll ();
90+ if (info == null ) {
91+ return ;
92+ }
93+ if (info .fragments != null ) {
94+ info .lifecycle = info .lifecycle .replace ("onFragment" , "" );
95+
96+ FragmentTaskView fragmentTaskView = activityTaskView .findFragmentTaskView (info .activity );
97+ if (fragmentTaskView != null ) {
98+ if (info .lifecycle .contains ("PreAttach" )) {
99+ fragmentTaskView .add (info );
100+ } else if (info .lifecycle .contains ("Detach" )) {
101+ fragmentTaskView .remove (info .fragments );
102+ } else {
103+ fragmentTaskView .update (info );
104+ }
105+ }
106+ } else {
107+ info .lifecycle = info .lifecycle .replace ("onActivity" , "" );
108+
109+ if (info .lifecycle .contains ("Create" )) {
110+ activityTaskView .add (info );
111+ } else if (info .lifecycle .contains ("Destroy" )) {
112+ activityTaskView .remove (info );
113+ } else {
114+ activityTaskView .update (info );
115+ }
285116 }
286117 }
287118 }
0 commit comments