package b4a.example.test;
import anywheresoftware.b4a.B4AMenuItem;
import android.app.Activity;
import android.os.Bundle;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.B4AActivity;
import anywheresoftware.b4a.ObjectWrapper;
import anywheresoftware.b4a.objects.ActivityWrapper;
import java.lang.reflect.InvocationTargetException;
import anywheresoftware.b4a.B4AUncaughtException;
import anywheresoftware.b4a.debug.*;
import java.lang.ref.WeakReference;
public class main extends Activity implements B4AActivity{
public static main mostCurrent;
static boolean afterFirstLayout;
static boolean isFirst = true;
private static boolean processGlobalsRun = false;
BALayout layout;
public static BA processBA;
BA activityBA;
ActivityWrapper _activity;
java.util.ArrayList<B4AMenuItem> menuItems;
public static final boolean fullScreen = false;
public static final boolean includeTitle = true;
public static WeakReference<Activity> previousOne;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirst) {
processBA = new BA(this.getApplicationContext(), null, null, "b4a.example.test", "b4a.example.test.main");
processBA.loadHtSubs(this.getClass());
float deviceScale = getApplicationContext().getResources().getDisplayMetrics().density;
BALayout.setDeviceScale(deviceScale);
}
else if (previousOne != null) {
Activity p = previousOne.get();
if (p != null && p != this) {
BA.LogInfo("Killing previous instance (main).");
p.finish();
}
}
processBA.runHook("oncreate", this, null);
if (!includeTitle) {
this.getWindow().requestFeature(android.view.Window.FEATURE_NO_TITLE);
}
if (fullScreen) {
getWindow().setFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,
android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mostCurrent = this;
processBA.sharedProcessBA.activityBA = null;
layout = new BALayout(this);
setContentView(layout);
afterFirstLayout = false;
WaitForLayout wl = new WaitForLayout();
if (anywheresoftware.b4a.objects.ServiceHelper.StarterHelper.startFromActivity(processBA, wl, false))
BA.handler.postDelayed(wl, 5);
}
static class WaitForLayout implements Runnable {
public void run() {
if (afterFirstLayout)
return;
if (mostCurrent == null)
return;
if (mostCurrent.layout.getWidth() == 0) {
BA.handler.postDelayed(this, 5);
return;
}
mostCurrent.layout.getLayoutParams().height = mostCurrent.layout.getHeight();
mostCurrent.layout.getLayoutParams().width = mostCurrent.layout.getWidth();
afterFirstLayout = true;
mostCurrent.afterFirstLayout();
}
}
private void afterFirstLayout() {
if (this != mostCurrent)
return;
activityBA = new BA(this, layout, processBA, "b4a.example.test", "b4a.example.test.main");
processBA.sharedProcessBA.activityBA = new java.lang.ref.WeakReference<BA>(activityBA);
anywheresoftware.b4a.objects.ViewWrapper.lastId = 0;
_activity = new ActivityWrapper(activityBA, "activity");
anywheresoftware.b4a.Msgbox.isDismissing = false;
if (BA.isShellModeRuntimeCheck(processBA)) {
if (isFirst)
processBA.raiseEvent2(null, true, "SHELL", false);
processBA.raiseEvent2(null, true, "CREATE", true, "b4a.example.test.main", processBA, activityBA, _activity, anywheresoftware.b4a.keywords.Common.Density, mostCurrent);
_activity.reinitializeForShell(activityBA, "activity");
}
initializeProcessGlobals();
initializeGlobals();
BA.LogInfo("** Activity (main) Create, isFirst = " + isFirst + " **");
processBA.raiseEvent2(null, true, "activity_create", false, isFirst);
isFirst = false;
if (this != mostCurrent)
return;
processBA.setActivityPaused(false);
BA.LogInfo("** Activity (main) Resume **");
processBA.raiseEvent(null, "activity_resume");
if (android.os.Build.VERSION.SDK_INT >= 11) {
try {
android.app.Activity.class.getMethod("invalidateOptionsMenu").invoke(this,(Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void addMenuItem(B4AMenuItem item) {
if (menuItems == null)
menuItems = new java.util.ArrayList<B4AMenuItem>();
menuItems.add(item);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
try {
if (processBA.subExists("activity_actionbarhomeclick")) {
Class.forName("android.app.ActionBar").getMethod("setHomeButtonEnabled", boolean.class).invoke(
getClass().getMethod("getActionBar").invoke(this), true);
}
} catch (Exception e) {
e.printStackTrace();
}
if (processBA.runHook("oncreateoptionsmenu", this, new Object[] {menu}))
return true;
if (menuItems == null)
return false;
for (B4AMenuItem bmi : menuItems) {
android.view.MenuItem mi = menu.add(bmi.title);
if (bmi.drawable != null)
mi.setIcon(bmi.drawable);
if (android.os.Build.VERSION.SDK_INT >= 11) {
try {
if (bmi.addToBar) {
android.view.MenuItem.class.getMethod("setShowAsAction", int.class).invoke(mi, 1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
mi.setOnMenuItemClickListener(new B4AMenuItemsClickListener(bmi.eventName.toLowerCase(BA.cul)));
}
return true;
}
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if (item.getItemId() == 16908332) {
processBA.raiseEvent(null, "activity_actionbarhomeclick");
return true;
}
else
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(android.view.Menu menu) {
super.onPrepareOptionsMenu(menu);
processBA.runHook("onprepareoptionsmenu", this, new Object[] {menu});
return true;
}
protected void onStart() {
super.onStart();
processBA.runHook("onstart", this, null);
}
protected void onStop() {
super.onStop();
processBA.runHook("onstop", this, null);
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (processBA.subExists("activity_windowfocuschanged"))
processBA.raiseEvent2(null, true, "activity_windowfocuschanged", false, hasFocus);
}
private class B4AMenuItemsClickListener implements android.view.MenuItem.OnMenuItemClickListener {
private final String eventName;
public B4AMenuItemsClickListener(String eventName) {
this.eventName = eventName;
}
public boolean onMenuItemClick(android.view.MenuItem item) {
processBA.raiseEvent(item.getTitle(), eventName + "_click");
return true;
}
}
public static Class<?> getObject() {
return main.class;
}
private Boolean onKeySubExist = null;
private Boolean onKeyUpSubExist = null;
@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if (processBA.runHook("onkeydown", this, new Object[] {keyCode, event}))
return true;
if (onKeySubExist == null)
onKeySubExist = processBA.subExists("activity_keypress");
if (onKeySubExist) {
if (keyCode == anywheresoftware.b4a.keywords.constants.KeyCodes.KEYCODE_BACK &&
android.os.Build.VERSION.SDK_INT >= 18) {
HandleKeyDelayed hk = new HandleKeyDelayed();
hk.kc = keyCode;
BA.handler.post(hk);
return true;
}
else {
boolean res = new HandleKeyDelayed().runDirectly(keyCode);
if (res)
return