package com.lloydm.javakogengine_chess; import android.annotation.SuppressLint; import android.app.Activity; import android.content.SharedPreferences; import android.content.res.AssetManager; import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.Gravity; import android.view.SurfaceHolder; import android.view.View; import android.view.WindowManager; import android.widget.FrameLayout; import android.widget.FrameLayout.LayoutParams; import android.widget.Toast; public class StandardGameMenuActivity extends Activity implements MyActivityLoader, SurfaceHolder.Callback { // 22-Apr-2015 // menu music is worked out in here too..... public final static String TAG = "com.lloydm.javakogengine_chess.StandardGameMenuActivity"; public MyHandler handler = null; public MyRunnable runnable = null; public MyRunnable toastrunnable = null; public boolean mPause = false; public volatile boolean surfaceready = false; public static SharedPreferences prefs = null; public static SharedPreferences.Editor prefsedit = null; public volatile boolean isrunnablerunning = false; public volatile boolean istoastrunnablerunning = false; public boolean instantkill = true; public static AssetManager assetmgr = null; public Typeface font = null; public String txt = ""; public View mainview = null; public boolean mvisflag = false; // default screen resolution metric - not necessarily actual dimensions but // rather what we are basing our positioning calculations on.... // aspect ratio 16:9; public int screenwidth = 1280; public int screenheight = 720; public long uid = 0; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Log.i(TAG, "onCreate"); if (Build.VERSION.SDK_INT >= 18) { try { setImmersive(true); } catch (Exception e) { Log.w(TAG, "Failed to set Immersive Mode"); } } prefs = getSharedPreferences("com.lloydm.javakogengine_chess.stdgameprefs", MODE_PRIVATE); // start the menu music service.....or try to.... String audiostatus = getprefs("com.lloydm.javakogengine_chess.toggleaudio", "1"); if (audiostatus.equals("1")) { MenuMusicService.audiomuted = false; } else { MenuMusicService.audiomuted = true; } Log.i(TAG, "onCreate - music would normally be created here"); assetmgr = getAssets(); //font = Typeface.createFromAsset(assetmgr, "font/gamefont.ttf"); //must exist or will fail. toastrunnable = new MyRunnable() { @Override public void run() { if (istoastrunnablerunning) { return; } istoastrunnablerunning = true; Toast.makeText(getApplicationContext(), txt, Toast.LENGTH_SHORT).show(); istoastrunnablerunning = false; } }; setup(); if (mainview != null) { Log.d(TAG, "mainview not null setting layout nav stuff"); mainview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } } public static void saveprefs(String name, String value) { prefsedit = prefs.edit(); prefsedit.putString(name, value); prefsedit.commit(); } public static String getprefs(String name, String defvalue) { return prefs.getString(name, defvalue); } public void newtoast() { if (toastrunnable == null) { return; } if (!txt.equals("")) { runOnUiThread(toastrunnable); } } public void newtoast(String txt) { this.txt = txt; newtoast(); } public void setup() { } @Override public void onChangeActivity(int id) { instantkill = false; } public static long getmemoryusage() { try { long totalmemory = Runtime.getRuntime().totalMemory(); long freememory = Runtime.getRuntime().freeMemory(); Log.i(TAG, "Memory Usage:"+(totalmemory - freememory)); return totalmemory - freememory; } catch (Exception e) { Log.e(TAG, "Error getting memory usage"); return 0; } } public static void logmemoryusage() { try { long totalmemory = Runtime.getRuntime().totalMemory(); long freememory = Runtime.getRuntime().freeMemory(); Log.d(TAG, "Memory usage:" + (totalmemory - freememory)); } catch (Exception e) { Log.e(TAG, "Error getting memory usage"); } } public static void logmemoryusage(String txt) { try { long totalmemory = Runtime.getRuntime().totalMemory(); long freememory = Runtime.getRuntime().freeMemory(); Log.d(TAG, txt + ".Memory usage:" + (totalmemory - freememory)); } catch (Exception e) { Log.e(TAG, "Error getting memory usage"); } } @Override protected void onStop() { super.onStop(); if (!isFinishing()) { finish(); } } @SuppressLint("InlinedApi") //@Override //protected void onResume() //{ // //super.onResume(); //Log.i(TAG, "onResume"); //mPause = false; // //try and start the music again if we can.... //Log.i(TAG, "onResume starting music"); //Intent serviceintent = new Intent(this, MenuMusicService.class); //if (Build.VERSION.SDK_INT >= 12) //{ //serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //} // //serviceintent.setAction("com.lloydm.javakogengine_chess.MenuMusicService.start"); //startService(serviceintent); //Message msg = new Message(); //msg.what = 0; //gamehandler.sendEmptyMessageDelayed(msg.what, 1); //} // @Override //protected void onPause() //{ //super.onPause(); //Log.i(TAG, "onPause"); //mPause = true; // //Program.quit = true; //Log.i(Config.TAG,"System.exit(0)"); //Intent serviceintent = new Intent(this, MenuMusicService.class); //if (Build.VERSION.SDK_INT >= 12) //{ //serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //} //if (instantkill) //{ //serviceintent.setAction("com.lloydm.javakogengine_chess.MenuMusicService.instantkill"); //} //else //{ //serviceintent.setAction("com.lloydm.javakogengine_chess.MenuMusicService.finish"); //} //startService(serviceintent); // //6oct2021 removed this for now temporarily // //if (Audios.sndpool != null) ////{ ////try ////{ // // Audios.sndpool.release(); // //Audios.sndpool = null; ////} ////catch (Exception e) ////{ ////Log.w(TAG, "could not release sound pool"); ////} //} // //System.exit(0);//20sep2021 // //we want to signal a 'finish command' to the music which is delayed..... // //it waits approximately 2750ms before actually finishing..... // //if there is a resume then it will cancel the finish command..... // //} @Override public void surfaceCreated(SurfaceHolder holder) { surfaceready = true; onSurfaceReady(); Log.i(TAG, "surf created"); } //Override this in other classes..... public void onSurfaceReady() { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { surfaceready = true; onSurfaceReady(); Log.i(TAG, "surf changed"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { surfaceready = false; Log.i(TAG, "surf destroyed"); } public void setkillmusicflag() { instantkill = true; } public FrameLayout.LayoutParams setwidgetposition(final int sx,final int sy, final int sw,final int sh) { //sx,sy,sw,sh are based on a screen 1600x900 to determine position....... //or 1600x1200 for 4:3 screen ratios... final DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); final int actualWidth = (int) (metrics.widthPixels); final int actualHeight = (int) (metrics.heightPixels); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,Gravity.TOP|Gravity.START); params.leftMargin = (int)(sx * actualWidth / Game.RESOLUTIONX); params.topMargin = (int)(sy * actualHeight / Game.RESOLUTIONY); params.width = (int)(sw * actualWidth / Game.RESOLUTIONX); params.height = (int)(sh * actualHeight / Game.RESOLUTIONY); return params; } }