@Override public void addLifecycleCallback(ActivityLifecycleCallback callback) { // there will never be too many callbacks, so iterating over a list will probably // be faster then the constant time costs of setting up and maintaining a map. checkNotNull(callback); synchronized (mCallbacks) { boolean needsAdd = true; Iterator<WeakReference<ActivityLifecycleCallback>> refIter = mCallbacks.iterator(); while (refIter.hasNext()) { ActivityLifecycleCallback storedCallback = refIter.next().get(); if (null == storedCallback) { refIter.remove(); } else if (storedCallback == callback) { needsAdd = false; } } if (needsAdd) { mCallbacks.add(new WeakReference<ActivityLifecycleCallback>(callback)); } } }
@Override public void removeLifecycleCallback(ActivityLifecycleCallback callback) { checkNotNull(callback); synchronized (mCallbacks) { Iterator<WeakReference<ActivityLifecycleCallback>> refIter = mCallbacks.iterator(); while (refIter.hasNext()) { ActivityLifecycleCallback storedCallback = refIter.next().get(); if (null == storedCallback) { refIter.remove(); } else if (storedCallback == callback) { refIter.remove(); } } } }
public void wakeUpDeviceScreen() throws RemoteException { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); uiDevice.wakeUp(); ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(new ActivityLifecycleCallback() { @Override public void onActivityLifecycleChanged(Activity activity, Stage stage) { //if (stage == Stage.PRE_ON_CREATE) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // } } }); }
public void finishCurrentActivity() { if (mActivity != null) { final Object lock = new Object(); final ActivityLifecycleCallback callback = new ActivityLifecycleCallback() { @Override public void onActivityLifecycleChanged(Activity activity, Stage stage) { if( activity == mActivity && stage == Stage.DESTROYED ) { mActivity = null; afterActivityFinished(); ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(this); synchronized (lock) { lock.notifyAll(); } } } }; ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(callback); mInstrumentation.runOnMainSync(new Runnable() { @Override public void run() { mActivity.finish(); } }); synchronized (lock) { try { lock.wait(10000); } catch (InterruptedException ignored) { throw new RuntimeException("finishCurrentActivity timed out after 10s"); } } } }
/** * Called by the runner after a particular onXXX lifecycle method has been called on a given * activity. */ public void signalLifecycleChange(Stage stage, Activity activity) { // there are never too many activities in existence in an application - so we keep // track of everything in a single list. Log.d(TAG, "Lifecycle status change: " + activity + " in: " + stage); boolean needsAdd = true; Iterator<ActivityStatus> statusIterator = mActivityStatuses.iterator(); while (statusIterator.hasNext()) { ActivityStatus status = statusIterator.next(); Activity statusActivity = status.mActivityRef.get(); if (null == statusActivity) { statusIterator.remove(); } else if (activity == statusActivity) { needsAdd = false; status.mLifecycleStage = stage; } } if (needsAdd) { mActivityStatuses.add(new ActivityStatus(activity, stage)); } synchronized (mCallbacks) { Iterator<WeakReference<ActivityLifecycleCallback>> refIter = mCallbacks.iterator(); while (refIter.hasNext()) { ActivityLifecycleCallback callback = refIter.next().get(); if (null == callback) { refIter.remove(); } else { try { Log.d(TAG, "running callback: " + callback); callback.onActivityLifecycleChanged(activity, stage); Log.d(TAG, "callback completes: " + callback); } catch (RuntimeException re) { Log.e(TAG, String.format( "Callback threw exception! (callback: %s activity: %s stage: %s)", callback, activity, stage), re); } } } } }