-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove listening for screen lock and unlock events and add listening …
…for onResume. On some Android devices, the screen lock and unlock events are not paired, we can use the activity's onResume callback.
- Loading branch information
Showing
6 changed files
with
116 additions
and
212 deletions.
There are no files selected for viewing
117 changes: 0 additions & 117 deletions
117
android/libpag/src/main/java/org/extra/tools/BroadcastUtil.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
android/libpag/src/main/java/org/extra/tools/Lifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.extra.tools; | ||
|
||
import android.app.Activity; | ||
import android.app.FragmentManager; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.os.Message; | ||
import android.util.Log; | ||
|
||
import org.libpag.PAGView; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Lifecycle implements Handler.Callback { | ||
private static final String FRAGMENT_TAG = "io.pag.manager"; | ||
private static final String TAG = "Lifecycle"; | ||
|
||
private static final int ID_REMOVE_FRAGMENT_MANAGER = 1; | ||
private static final Lifecycle lifecycle = new Lifecycle(); | ||
private final Handler handler; | ||
private final Map<FragmentManager, LifecycleFragment> pendingRequestManagerFragments = | ||
new HashMap<>(); | ||
|
||
private Lifecycle() { | ||
handler = new Handler(Looper.getMainLooper(), this); | ||
} | ||
|
||
public static Lifecycle getInstance() { | ||
return lifecycle; | ||
} | ||
|
||
public void addListener(final PAGView pagView) { | ||
if (pagView.getContext() instanceof Activity) { | ||
Activity activity = (Activity) pagView.getContext(); | ||
FragmentManager fm = activity.getFragmentManager(); | ||
LifecycleFragment current = pendingRequestManagerFragments.get(fm); | ||
if (current == null) { | ||
current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG); | ||
if (current == null) { | ||
current = new LifecycleFragment(); | ||
pendingRequestManagerFragments.put(fm, current); | ||
fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss(); | ||
handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget(); | ||
} | ||
} | ||
current.addListener(pagView); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean handleMessage(Message message) { | ||
boolean handled = true; | ||
if (message.what == ID_REMOVE_FRAGMENT_MANAGER) { | ||
FragmentManager fm = (FragmentManager) message.obj; | ||
LifecycleFragment current = (LifecycleFragment) fm.findFragmentByTag(FRAGMENT_TAG); | ||
if (fm.isDestroyed()) { | ||
Log.w(TAG, "Parent was destroyed before our Fragment could be added."); | ||
} else if (current != pendingRequestManagerFragments.get(fm)) { | ||
Log.w(TAG, "adding Fragment failed."); | ||
} | ||
pendingRequestManagerFragments.remove(fm); | ||
} else { | ||
handled = false; | ||
} | ||
return handled; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
android/libpag/src/main/java/org/extra/tools/LifecycleFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.extra.tools; | ||
|
||
import android.app.Fragment; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
|
||
public class LifecycleFragment extends Fragment { | ||
private final Set<LifecycleListener> lifecycleListeners = | ||
Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>()); | ||
|
||
public LifecycleFragment() { | ||
} | ||
|
||
public void addListener(LifecycleListener listener) { | ||
lifecycleListeners.add(listener); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
for (LifecycleListener lifecycleListener : lifecycleListeners) { | ||
if (lifecycleListener != null) { | ||
lifecycleListener.onResume(); | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
android/libpag/src/main/java/org/extra/tools/LifecycleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.extra.tools; | ||
|
||
public interface LifecycleListener { | ||
void onResume(); | ||
} |
68 changes: 0 additions & 68 deletions
68
android/libpag/src/main/java/org/extra/tools/ScreenBroadcastReceiver.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters