Example usage for android.util ArraySet clear

List of usage examples for android.util ArraySet clear

Introduction

In this page you can find the example usage for android.util ArraySet clear.

Prototype

@Override
public void clear() 

Source Link

Document

Make the array map empty.

Usage

From source file:com.sourceallies.android.zonebeacon.ZoneBeaconRobolectricSuite.java

@SuppressLint("NewApi")
@After/*from   www .  j  av  a2 s . c o m*/
public void resetWindowManager() throws Exception {
    // https://github.com/robolectric/robolectric/pull/1741
    final Class<?> btclass = Class.forName("com.android.internal.os.BackgroundThread");
    Object backgroundThreadSingleton = ReflectionHelpers.getStaticField(btclass, "sInstance");
    if (backgroundThreadSingleton != null) {
        btclass.getMethod("quit").invoke(backgroundThreadSingleton);
        ReflectionHelpers.setStaticField(btclass, "sInstance", null);
        ReflectionHelpers.setStaticField(btclass, "sHandler", null);
    }

    // https://github.com/robolectric/robolectric/issues/2068
    Class clazz = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.view.WindowManagerGlobal");
    Object instance = ReflectionHelpers.callStaticMethod(clazz, "getInstance");

    // We essentially duplicate what's in {@link WindowManagerGlobal#closeAll} with what's below.
    // The closeAll method has a bit of a bug where it's iterating through the "roots" but
    // bases the number of objects to iterate through by the number of "views." This can result in
    // an {@link java.lang.IndexOutOfBoundsException} being thrown.
    Object lock = ReflectionHelpers.getField(instance, "mLock");

    ArrayList<Object> roots = ReflectionHelpers.getField(instance, "mRoots");
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (lock) {
        for (int i = 0; i < roots.size(); i++) {
            ReflectionHelpers.callInstanceMethod(instance, "removeViewLocked",
                    ReflectionHelpers.ClassParameter.from(int.class, i),
                    ReflectionHelpers.ClassParameter.from(boolean.class, false));
        }
    }

    // Views will still be held by this array. We need to clear it out to ensure
    // everything is released.
    ArraySet<View> dyingViews = ReflectionHelpers.getField(instance, "mDyingViews");
    dyingViews.clear();
}