package net.cachapa.libra.util;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import android.view.View;
import android.view.ViewGroup;
/**
* android 1.5 compatible zoom controller, works like android.widget.ZoomButtonsController in 1.5 and up,
* the class exists on 1.5 devices you just can't compile with it against a 1.5 sdk. fun.
*
* Public Domain license, feel free to use wherever. I'd appreciate credit given.
* @author bobzilla at wigle.net
*
*/
public class ZoomButtonsController {
private static Class<?> ZOOM_CLASS;
private static Class<?> LISTENER_CLASS;
private static Method setOnZoomListener;
private static Method setVisible;
private static Method setZoomInEnabled;
private static Method setZoomOutEnabled;
private static Method getContainer;
private Object controller;
static {
try {
ZOOM_CLASS = Class.forName("android.widget.ZoomButtonsController");
for ( Class<?> clazz : ZOOM_CLASS.getDeclaredClasses() ) {
// info( "clazz: " + clazz.getSimpleName() );
if ( "OnZoomListener".equals( clazz.getSimpleName() ) ) {
// info( "listener_class set" );
LISTENER_CLASS = clazz;
}
}
setOnZoomListener = ZOOM_CLASS.getMethod("setOnZoomListener", LISTENER_CLASS );
setVisible = ZOOM_CLASS.getMethod("setVisible", Boolean.TYPE );
setZoomInEnabled = ZOOM_CLASS.getMethod("setZoomInEnabled", Boolean.TYPE );
setZoomOutEnabled = ZOOM_CLASS.getMethod("setZoomOutEnabled", Boolean.TYPE );
getContainer = ZOOM_CLASS.getMethod("getContainer");
}
catch ( Exception ex ) {
// don't have it
// logger.info( "no zoom buttons: " + ex );
}
}
public interface OnZoomListener {
public void onZoom(boolean zoomIn);
public void onVisibilityChanged(boolean visible);
}
public ZoomButtonsController ( final View view ) {
if ( ZOOM_CLASS != null ) {
try {
controller = ZOOM_CLASS.getConstructor( View.class ).newInstance( view );
}
catch ( Exception ex ) {
// logger.error( "exception instantiating: " + ex );
}
}
}
public void setOnZoomListener( final OnZoomListener listener ) {
if ( controller != null ) {
try {
final InvocationHandler handler = new InvocationHandler() {
public Object invoke( Object object, Method method, Object[] args ) {
if ( "onZoom".equals( method.getName() ) ) {
listener.onZoom( (Boolean) args[0] );
}
else if ( "onVisibilityChanged".equals( method.getName() ) ) {
listener.onVisibilityChanged( (Boolean) args[0] );
}
else {
// logger.info( "unhandled listener method: " + method );
}
return null;
}
};
Object proxy = Proxy.newProxyInstance( LISTENER_CLASS.getClassLoader(),
new Class[]{ LISTENER_CLASS }, handler );
setOnZoomListener.invoke( controller, proxy );
}
catch ( Exception ex ) {
// logger.error( "setOnZoomListener exception: " + ex );
}
}
}
public void setVisible( final boolean visible ) {
if ( controller != null ) {
try {
setVisible.invoke( controller, visible );
}
catch ( Exception ex ) {
// logger.error( "setVisible exception: " + ex );
}
}
}
public void setZoomInEnabled( final boolean enabled ) {
if ( controller != null ) {
try {
setZoomInEnabled.invoke( controller, enabled );
}
catch ( Exception ex ) {
// logger.error( "setZoomInEnabled exception: " + ex );
}
}
}
public void setZoomOutEnabled( final boolean enabled ) {
if ( controller != null ) {
try {
setZoomOutEnabled.invoke( controller, enabled );
}
catch ( Exception ex ) {
// logger.error( "setZoomOutEnabled exception: " + ex );
}
}
}
public ViewGroup getContainer() {
if ( controller != null ) {
try {
return (ViewGroup) getContainer.invoke( controller );
}
catch ( Exception ex ) {
ex.printStackTrace();
// logger.error( "setZoomOutEnabled exception: " + ex );
}
}
return null;
}
}
|