Created
January 9, 2015 10:29
-
-
Save jbruchanov/bf6c040b422aa4575381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.scurab.android.anuitor.reflect; | |
import android.annotation.TargetApi; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Build; | |
import android.view.View; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashSet; | |
import java.util.List; | |
/** | |
* @author jbruchanov | |
* @since 2014-05-28 16:34 | |
*/ | |
@TargetApi(value = Build.VERSION_CODES.JELLY_BEAN_MR2) | |
public class WindowManagerGlobalReflector extends Reflector<Object> implements WindowManager { | |
public WindowManagerGlobalReflector() { | |
super(getRealInstance()); | |
} | |
public String[] getViewRootNames() { | |
return callByReflection(); | |
} | |
public View getRootView(String name) { | |
return callByReflection(name); | |
} | |
private static Object getRealInstance() { | |
try { | |
Class<?> clz = Class.forName("android.view.WindowManagerGlobal"); | |
Method m = clz.getDeclaredMethod("getInstance"); | |
m.setAccessible(true); | |
return m.invoke(null, null); | |
} catch (Exception e) { | |
throw new RuntimeException("Problem with calling android.view.WindowManagerGlobal#getInstance()", e); | |
} | |
} | |
public Activity[] getActivites() { | |
HashSet<Activity> result = new HashSet<Activity>(); | |
for (String s : getViewRootNames()) { | |
View v = getRootView(s); | |
if (v != null) { | |
Context c = v.getContext(); | |
if (c instanceof Activity) { | |
result.add((Activity) c); | |
} | |
} | |
} | |
return result.toArray(new Activity[result.size()]); | |
} | |
@Override | |
public Activity getCurrentActivity() { | |
String[] viewRootNames = getViewRootNames(); | |
for (int i = viewRootNames.length - 1; i >= 0; i--) { | |
String name = viewRootNames[i]; | |
View v = getRootView(name); | |
Context c; | |
if (v != null && (c = v.getContext()) instanceof Activity) { | |
return (Activity) c; | |
} | |
} | |
return null; | |
} | |
@Override | |
public View getCurrentRootView() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
return getCurrentRootViewWithZCheck(); | |
} else { | |
return getCurrentRootViewByLastIndex(); | |
} | |
} | |
private View getCurrentRootViewByLastIndex() { | |
String[] viewRootNames = getViewRootNames(); | |
List<View> attachedViews = new ArrayList<>(); | |
for (int i = viewRootNames.length - 1; i >= 0; i--) { | |
String name = viewRootNames[i]; | |
View v = getRootView(name); | |
if (v != null && v.isAttachedToWindow()) { | |
return v; | |
} | |
} | |
return null; | |
} | |
@TargetApi(value = Build.VERSION_CODES.LOLLIPOP) | |
private View getCurrentRootViewWithZCheck() { | |
String[] viewRootNames = getViewRootNames(); | |
List<View> attachedViews = new ArrayList<>(); | |
for (int i = viewRootNames.length - 1; i >= 0; i--) { | |
String name = viewRootNames[i]; | |
View v = getRootView(name); | |
if (v != null && v.isAttachedToWindow()) { | |
attachedViews.add(v); | |
} | |
} | |
int size = attachedViews.size(); | |
if (size > 1) { | |
Collections.sort(attachedViews, new Comparator<View>() { | |
@Override | |
public int compare(View lhs, View rhs) { | |
return -Float.compare(lhs.getZ(), rhs.getZ()); | |
} | |
}); | |
return attachedViews.get(0); | |
} | |
return size == 1 ? attachedViews.get(0) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment