gt: 		if (mainActivity != null) {
context: /********************************************************************** 
   Android-Freeciv - Copyright (C) 2010 - C Vaughn
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2, or (at your option)
     any later version.
  
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
  ***********************************************************************/
  
  package net.hackcasual.freeciv;
  
  import java.nio.Buffer;
  import java.nio.ByteBuffer;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.concurrent.Semaphore;
  import java.util.concurrent.TimeUnit;
  
  import net.hackcasual.freeciv.models.Advance;
  import net.hackcasual.freeciv.models.AdvanceExpense;
  import net.hackcasual.freeciv.models.City;
  import net.hackcasual.freeciv.models.Government;
  import net.hackcasual.freeciv.models.Improvement;
  import net.hackcasual.freeciv.models.Player;
  import net.hackcasual.freeciv.models.Unit;
  import net.hackcasual.freeciv.models.UnitType;
  import net.hackcasual.freeciv.views.Freeciv;
  
  import com.google.common.collect.BiMap;
  import com.google.common.collect.HashBiMap;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.graphics.Typeface;
  import android.text.Editable;
  import android.util.Log;
  import android.widget.EditText;
  import android.widget.ImageView;
  
  public class NativeHarness {
  	private DialogManager dm;
  	private Freeciv mainActivity;
  	private final static NativeHarness nh = new NativeHarness();
  	private Semaphore displayLock = new Semaphore(1);
  	private String lastTilesetProgress = null;
  	private boolean lastConnectionStatus = false;
  	
  	
  	private NativeEventListener nativeListener = null;
  	
  	public static NativeHarness getHarness() {
  		return nh;
  	}
  	
  	private NativeHarness() {
  		System.loadLibrary("server");
  		System.loadLibrary("client");
  		serverRunning = false;
  		clientRunning = false;
  		this.mainActivity = null;
  		this.dm = new DialogManager(this);
  	}
  	
  	public void setMainActivity(Freeciv act) {
  		this.mainActivity = act;
  	}
  	
  	public DialogManager getDialogManager() {
  		//this.dm = dm;
  		return dm;
  	}
  
  	static Bitmap overview = Bitmap.createBitmap(156, 104, Bitmap.Config.RGB_565);
  	
  	public native void startServer();
  	public native void startClient();
  	public static native void init(int width, int height);
  
  	public native void registerDialogManager(DialogManager dm);
  	public native void reloadMap();
  	
  	public native void cityNameResponse(String name, int unit_id);
  	
  	public native void clickCityMap(int cityPtr, int x, int y);	
  	
  	private native int[] getUnitsInCity(int cityId);
  	public static native int[] getUnitsOnTile(int x, int y);
  	public static native Unit getUnitById(int unitId);
  	
  	public List<Unit> getUnitsInCity(City city) {
  		List<Unit> units = new ArrayList<Unit>();
  		
  		for (int id: getUnitsInCity(city.getCityId())) {
  			units.add(getUnitById(id));
  		}
  		
  		return units;
  	}
  	
  	private native byte[] getAvailableCommandsForActiveUnit();
  	private native static void sendNativeCommand(int command);
  	
  	public native Improvement[] loadImprovements();
  	public native UnitType[] loadUnitTypes();
  	public native Advance[] loadAdvances();
  	public native Government[] loadGovernments();
  	public native AdvanceExpense[] getAdvanceExpenses();
  	public native City getCity(int id);
  	public static native int[] getBuildableImprovementsForCity(int id);
  	public static native int[] getBuildableUnitsForCity(int id);
  	public static native int[] getAvailableGovernments();
  	public static native Player getPlayerInfo();
  	public static native void setAdvanceGoal(int goalId);
  	public native static void focusOnUnit(int unitId);
  	
  	public native void positionFromOverview(int x, int y);
  	
  	public native void setGovernmentType(int governmentId);
  	
  	public static native void setCityProductionToImprovement(int cityId, int imprId);
  	public static native void setCityProductionToUnit(int cityId, int unitId);
  	
  	public static native void actionSelectPopup(int x, int y);
  	public static native void moveMapView(int deltax, int deltay);	
  	public static native int getCityOnTile(int x, int y);
  	public static native void showCursorAt(int x, int y);	
  	public static native void cancelAction();
  	
  	public static native void tellServer(String cmd);
  	
  	
  	public static void save(String name) {
  		tellServer("/save " + name);
  	}
  	
  	public static native void exercise1();
  	
  	public native int getFocusedUnitType();
  	
  	private static native void pullOverview();
  	
  	private boolean serverRunning, clientRunning;
  	
  	static private ByteBuffer nativeBuffer;
  	static private ByteBuffer javaBuffer;
  	
  	public void registerNativeBuffers(ByteBuffer nativeToJavaBuffer, ByteBuffer javaToNativeBuffer) {
  		//Log.i("Freeciv","Got a bitmap buffer");
  		nativeBuffer = nativeToJavaBuffer;
  		javaBuffer = javaToNativeBuffer;
  	}
  	
  	public static Bitmap getOverview() {
  		pullOverview();		
  		overview.copyPixelsFromBuffer(getIncomingBuffer());
  		return overview;
  	}
  	
  	public int getTextSize(String s) {
  		Paint fontPaint = new Paint();
  		
  		fontPaint.setColor(0xFFFFFFFF);
  		
  		fontPaint.setTypeface(Typeface.SANS_SERIF);
  		fontPaint.setTextSize(18.0f);
  		fontPaint.setAntiAlias(true);
  		Rect bounds = new Rect();
  		
  		fontPaint.getTextBounds(s, 0, s.length(), bounds);
  				
  		int packedReturn = ((bounds.width() << 16) | (int)(fontPaint.descent() - fontPaint.ascent())); 
  		
  		Log.i("Freeciv", String.format("Measured size of %s [%dx%d] %x", s, bounds.width(), (int)(fontPaint.descent() - fontPaint.ascent()), packedReturn));
  		
  		return packedReturn;
  	}
  
  	public int renderString(final String s) {
  		Paint fontPaint = new Paint();
  
  		fontPaint.setColor(0xFFFFFFFF);
  		
  		fontPaint.setTypeface(Typeface.SANS_SERIF);
  		fontPaint.setTextSize(18.0f);
  		
  		
  		fontPaint.setAntiAlias(true);
  		
  		Rect bounds = new Rect();
  		
  		fontPaint.getTextBounds(s, 0, s.length(), bounds);
  				
  		int packedReturn = ((bounds.width() << 16) | (int)(fontPaint.descent() - fontPaint.ascent())); 
  		
  		final Bitmap textAsSprite = Bitmap.createBitmap(bounds.width(), (int)(fontPaint.descent() - fontPaint.ascent()), Bitmap.Config.ARGB_4444);
  		Canvas cvs = new Canvas(textAsSprite);
  		cvs.drawARGB(0, 0, 0, 0);
  		cvs.drawText(s, 0f, -fontPaint.ascent(), fontPaint);
  		
  		javaBuffer.rewind();
  		textAsSprite.copyPixelsToBuffer(javaBuffer);
  		//packedRetu
  		
  		
  		return packedReturn;
  	}
  	
  	public void updateDisplay() {
  		Log.i("Freeciv", String.format("Updating display, acquiring display lock: Avail: %d: TID: %d", displayLock.availablePermits(), Thread.currentThread().getId()));
  		try {
  			displayLock.acquire();
  		} catch (InterruptedException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  