summaryrefslogtreecommitdiffstats
path: root/Android/src/com
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Android/src/com/mcserver/MCServerActivity.java302
-rw-r--r--Android/src/com/mcserver/MCServerInstaller.java432
2 files changed, 0 insertions, 734 deletions
diff --git a/Android/src/com/mcserver/MCServerActivity.java b/Android/src/com/mcserver/MCServerActivity.java
deleted file mode 100644
index 8a9846ca1..000000000
--- a/Android/src/com/mcserver/MCServerActivity.java
+++ /dev/null
@@ -1,302 +0,0 @@
-package com.mcserver;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.SocketException;
-import java.util.ArrayList;
-import java.util.Enumeration;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.graphics.Color;
-import android.net.Uri;
-import android.os.Bundle;
-import android.util.Log;
-import android.view.KeyEvent;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.view.View;
-import android.widget.ArrayAdapter;
-import android.widget.Button;
-import android.widget.ListView;
-import android.widget.TextView;
-
-public class MCServerActivity extends Activity {
- MainThread mThread = null;
- Thread ServerStatusThread = null;
- boolean mbExiting = false;
- boolean mbEnabledLogging = false;
-
- ArrayList<String> mLogList = new ArrayList<String>();
- ArrayAdapter<String> mAdapter;
-
- MCServerInstaller mInstaller = null;
-
- final private int MENU_REINSTALL = 0;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- //Log.e("MCServer", "p id: " + android.os.Process.myPid() );
-
-
- ((Button)findViewById(R.id.start_server)).setOnClickListener( new View.OnClickListener() {
- public void onClick(View v) {
- mbEnabledLogging = true;
- if( mThread == null || mThread.isAlive() == false ) {
- mThread = new MainThread( (MCServerActivity)v.getContext() );
- mThread.start();
- }
- }
- });
-
- ((Button)findViewById(R.id.stop_server)).setOnClickListener( new View.OnClickListener() {
- public void onClick(View v) {
- mbEnabledLogging = true;
- NativeCleanUp();
- }
- });
-
- ((Button)findViewById(R.id.configure_server)).setOnClickListener( new View.OnClickListener() {
- public void onClick(View v) {
- Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://localhost:" + NativeGetWebAdminPort() + "/webadmin/"));
- startActivity( myIntent );
- }
- });
-
-
-
- ListView lv = (ListView)this.findViewById(R.id.listView1);
- mAdapter = new ArrayAdapter<String>(this,
- R.layout.list_item,
- mLogList);
- lv.setAdapter(mAdapter);
-
-
- mLogList.add("---- LOG ----");
-
- ServerStatusThread = new Thread( new Runnable() {
- public void run() {
- for(;;)
- {
- try {
- runOnUiThread( new Runnable() {
- public void run() {
- UpdateServerStatus();
- }
- });
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- }
- }
- });
- ServerStatusThread.start();
-
-
-
-
-
-
-
- Thread loggerThread = new Thread( new Runnable() {
- public void run() {
- Process process = null;
-
- try {
- process = Runtime.getRuntime().exec("logcat -v raw *:s MCServer ");// Verbose filter
- } catch (IOException e) {
- }
-
- BufferedReader reader = null;
-
- try {
- InputStreamReader isr = new InputStreamReader(process.getInputStream());
- reader = new BufferedReader( isr );
-
- String line;
-
- while( mbExiting == false ) {
- line = reader.readLine();
- if( mbEnabledLogging == true && line != null )
- {
- AddToLog( line );
- }
- }
-
- Log.i("MCServer", "Prepping thread for termination");
- reader.close();
- process.destroy();
- process = null;
- reader = null;
- } catch (IOException e) {
- }
- }
- });
- loggerThread.start();
-
- ((TextView)findViewById(R.id.ip_address)).setText("Connect to: " + getLocalIpAddress());
-
-
- mInstaller = new MCServerInstaller(this);
- if( mInstaller.NeedsUpdate() )
- {
- mInstaller.ShowFirstRunDialog();
- }
- }
-
-
-
- public String getLocalIpAddress() {
- try {
- for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
- NetworkInterface intf = en.nextElement();
- for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
- InetAddress inetAddress = enumIpAddr.nextElement();
- if (!inetAddress.isLoopbackAddress()) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException ex) {
- Log.e("MCServer", ex.toString());
- }
- return null;
- }
-
-
-
- public void UpdateServerStatus()
- {
- if( NativeIsServerRunning() ) {
- ((TextView)findViewById(R.id.server_status_text)).setText(R.string.mcserver_is_running);
- ((TextView)findViewById(R.id.server_status_text)).setTextColor(Color.GREEN);
- ((Button)findViewById(R.id.stop_server)).setEnabled(true);
- ((Button)findViewById(R.id.start_server)).setEnabled(false);
- ((Button)findViewById(R.id.configure_server)).setEnabled(true);
- } else {
- ((TextView)findViewById(R.id.server_status_text)).setText(R.string.mcserver_is_not_running);
- ((TextView)findViewById(R.id.server_status_text)).setTextColor(Color.RED);
- ((Button)findViewById(R.id.stop_server)).setEnabled(false);
- ((Button)findViewById(R.id.start_server)).setEnabled(true);
- ((Button)findViewById(R.id.configure_server)).setEnabled(false);
- }
- }
-
-
-
-
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode==KeyEvent.KEYCODE_BACK)
- {
- //android.os.Process.killProcess(android.os.Process.myPid());
- NativeCleanUp();
- return super.onKeyDown(keyCode, event);
- }
- return false;
- }
-
-
-
-
- public void onDestroy() {
- mbExiting = true;
- super.onDestroy();
- }
-
-
-
-
-
- public void AddToLog( final String logMessage ) {
- final ListView lv = ((ListView)findViewById(R.id.listView1));
- lv.post(new Runnable() {
- public void run() {
- //final boolean bAutoscroll = lv.getLastVisiblePosition() >= mAdapter.getCount() - 1 ? true : false;
-
- mLogList.add(logMessage);
- while( mLogList.size() > 100 ) // only allow 100 messages in the list, otherwise it might slow the GUI down
- {
- mLogList.remove(0);
- }
- mAdapter.notifyDataSetChanged();
-
-
- // Autoscroll detection is dodgy
- //if( bAutoscroll )
- {
- lv.setSelection(mAdapter.getCount() - 1);
- }
- }
- });
- }
-
-
-
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, MENU_REINSTALL, 0, "Reinstall MCServer" );
- return super.onCreateOptionsMenu(menu);
- }
-
-
-
-
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
- switch( item.getItemId() )
- {
- case MENU_REINSTALL:
- mInstaller.ShowPluginInstallDialog(true);
- return true;
- }
- return false;
- }
-
-
-
-
-
- static {
- System.loadLibrary("mcserver");
- }
-
-
- public native void NativeOnCreate();
- public native void NativeCleanUp();
- public native boolean NativeIsServerRunning();
- public native int NativeGetWebAdminPort();
-
-}
-
-
-class MainThread extends Thread {
- MCServerActivity mContext = null;
- int numlogs = 0;
-
- MainThread( MCServerActivity aContext ) {
- mContext = aContext;
- }
-
- public void run() {
- mContext.NativeOnCreate();
- }
-
-}
-
-
-
-
-
-
diff --git a/Android/src/com/mcserver/MCServerInstaller.java b/Android/src/com/mcserver/MCServerInstaller.java
deleted file mode 100644
index 5a865a602..000000000
--- a/Android/src/com/mcserver/MCServerInstaller.java
+++ /dev/null
@@ -1,432 +0,0 @@
-package com.mcserver;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-
-import android.app.AlertDialog;
-import android.app.ProgressDialog;
-import android.content.DialogInterface;
-import android.content.SharedPreferences;
-import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.res.AssetManager;
-import android.os.AsyncTask;
-import android.os.Environment;
-import android.util.Log;
-
-public class MCServerInstaller {
- private MCServerActivity mContext;
- final private String BaseDirectory = "basedir";
- final private String PluginDirectory = "Plugins";
-
- final public String SHARED_PREFS_NAME = "MCSERVER_PREFS";
- final public String PREF_IS_INSTALLED = "IS_INSTALLED";
- final public String PREF_LAST_VERSION = "LAST_VERSION";
- private SharedPreferences mSettings = null;
-
- int thisVersion;
-
- MCServerInstaller( MCServerActivity activity )
- {
- mContext = activity;
-
- mSettings = mContext.getSharedPreferences( SHARED_PREFS_NAME, 0);
-
- try {
- this.thisVersion = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
- } catch (NameNotFoundException e) {
- Log.e("MCServer", "Could not read version code from manifest!");
- e.printStackTrace();
- this.thisVersion = -1;
- }
- }
-
-
- public boolean IsInstalled()
- {
- return mSettings.getBoolean(PREF_IS_INSTALLED, false);
- }
-
-
- public boolean NeedsUpdate()
- {
- Log.i("MCServer", "thisVersion: " + this.thisVersion + " pref: " + mSettings.getInt(PREF_LAST_VERSION, 0));
- return mSettings.getInt(PREF_LAST_VERSION, 0) != this.thisVersion;
- }
-
-
- public ArrayList<String> FindFoldersInPath(String path)
- {
- ArrayList<String> allFolders = new ArrayList<String>();
- AssetManager am = mContext.getAssets();
- try {
- String[] allPlugins = am.list(path);
- for(String pluginName : allPlugins)
- {
- InputStream istr = null;
- try
- {
- istr = am.open(path + "/" + pluginName);
- } catch( java.io.FileNotFoundException e ) {
- // It seems to be a folder :D
- allFolders.add(pluginName);
- continue;
- }
- istr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return allFolders;
- }
-
-
-
-
- public void ExpandAssets( String path )
- {
- AssetManager am = mContext.getAssets();
- try {
- String[] getAssets = am.list(path);
- for(String assetName : getAssets)
- {
- //Log.e("MCServer", path + "/" + imgName);
-
- InputStream istr = null;
- try
- {
- istr = am.open(path + "/" + assetName);
- } catch( java.io.FileNotFoundException e ) {
- //Log.e("MCServer", "Could not open" + path + "/" + imgName );
- ExpandAssets(path + "/" + assetName);
- continue;
- }
-
- String outPath = Environment.getExternalStorageDirectory().getPath() + "/mcserver/" + path + "/" + assetName;
- //Log.e("MCServer", "outPath: " + outPath );
- File f = new File( outPath );
-
- f.getParentFile().mkdirs();
- f.createNewFile();
- OutputStream ostr = new FileOutputStream(f);
-
- byte[] buffer = new byte[1024];
- int length;
- while ((length = istr.read(buffer))>0)
- {
- ostr.write(buffer, 0, length);
- }
- ostr.flush();
- ostr.close();
- istr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- void ShowFirstRunDialog()
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- //builder.setTitle("blaa");
- builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.setMessage("It seems this is the first time you are running MCServer on your Android device or it has been updated! This app comes with a couple of pre-packaged plugins, please take a moment to select the plugins you would like to install.");
- builder.setCancelable(false);
- AlertDialog dialog = builder.create();
- dialog.show();
-
- dialog.setOnDismissListener( new DialogInterface.OnDismissListener(){
- public void onDismiss(DialogInterface dialog) {
- ShowPluginInstallDialog(false);
- }
- });
- }
-
-
- public void ShowPluginInstallDialog(boolean bCancelable)
- {
- final ArrayList<String> allPlugins = FindFoldersInPath( BaseDirectory + "/" + PluginDirectory );
- final CharSequence[] items = allPlugins.toArray(new CharSequence[allPlugins.size()]);
- final boolean[] selected = new boolean[items.length];
- for( int i = 0; i < items.length; ++i )
- {
- if( items[i].toString().contains("Core") )
- { // Select the core plugin by default
- selected[i] = true;
- items[i] = items[i] + " (Recommended)";
- }
- }
-
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setTitle("Plugins to install");
- builder.setCancelable(bCancelable);
- builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- selected[which] = isChecked;
- }
- });
- builder.setPositiveButton("Install", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- ArrayList<String> toInstall = new ArrayList<String>();
- for( int i = 0; i < selected.length; ++i )
- {
- if( selected[i] )
- {
- toInstall.add(allPlugins.get(i));
- }
- }
- InstallPlugins(toInstall);
- }
- });
-
- AlertDialog dialog2 = builder.create();
- dialog2.show();
- }
-
-
- void InstallPlugins( final ArrayList<String> plugins )
- {
- new AsyncTask<Void, Integer, Boolean>()
- {
- ProgressDialog progressDialog;
-
- @Override
- protected void onPreExecute()
- {
- /*
- * This is executed on UI thread before doInBackground(). It is
- * the perfect place to show the progress dialog.
- */
- progressDialog = ProgressDialog.show(mContext, "", "Installing...");
-
- }
-
- @Override
- protected Boolean doInBackground(Void... params)
- {
- if (params == null)
- {
- return false;
- }
- try
- {
- /*
- * This is run on a background thread, so we can sleep here
- * or do whatever we want without blocking UI thread. A more
- * advanced use would download chunks of fixed size and call
- * publishProgress();
- */
- for( int i = 0; i < plugins.size(); ++i )
- {
- this.publishProgress((int)(i / (float)plugins.size() * 100), i);
- InstallSinglePlugin(PluginDirectory + "/" + plugins.get(i));
- }
-
- this.publishProgress( 100, -1 );
- InstallExampleSettings();
-
- this.publishProgress( 100, -2 );
- InstallWebAdmin();
-
- }
- catch (Exception e)
- {
- Log.e("tag", e.getMessage());
- /*
- * The task failed
- */
- return false;
- }
-
- /*
- * The task succeeded
- */
- return true;
- }
-
- protected void onProgressUpdate(Integer... progress)
- {
- progressDialog.setProgress(progress[0]);
- if( progress[1] > -1 )
- {
- progressDialog.setMessage("Installing " + plugins.get(progress[1]) + "..." );
- }
- else if( progress[1] == -1 )
- {
- progressDialog.setMessage("Installing default settings...");
- }
- else if( progress[1] == -2 )
- {
- progressDialog.setMessage("Installing WebAdmin...");
- }
- }
-
- @Override
- protected void onPostExecute(Boolean result)
- {
- progressDialog.dismiss();
- /*
- * Update here your view objects with content from download. It
- * is save to dismiss dialogs, update views, etc., since we are
- * working on UI thread.
- */
- AlertDialog.Builder b = new AlertDialog.Builder(mContext);
- b.setTitle(android.R.string.dialog_alert_title);
- if (result)
- {
- b.setMessage("Install succeeded");
-
- SharedPreferences.Editor editor = mSettings.edit();
- editor.putBoolean(PREF_IS_INSTALLED, true);
- editor.putInt(PREF_LAST_VERSION, thisVersion);
- editor.commit();
- }
- else
- {
- b.setMessage("Install failed");
- }
- b.setPositiveButton(android.R.string.ok,
- new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- dialog.dismiss();
- }
- });
- b.create().show();
- }
- }.execute();
- }
-
-
- void InstallExampleSettings()
- {
- AssetManager am = mContext.getAssets();
- try {
- String[] allFiles = am.list(BaseDirectory);
- for(String fileName : allFiles)
- {
- InputStream istr = null;
- try
- {
- istr = am.open(BaseDirectory + "/" + fileName);
- } catch( java.io.FileNotFoundException e ) {
- // Must be a folder :D
- continue;
- }
-
- String outPath = Environment.getExternalStorageDirectory().getPath() + "/mcserver/" + fileName;
- Log.i("MCServer", "outPath: " + outPath );
- File f = new File( outPath );
-
- f.getParentFile().mkdirs();
- f.createNewFile();
- OutputStream ostr = new FileOutputStream(f);
-
- byte[] buffer = new byte[1024];
- int length;
- while ((length = istr.read(buffer))>0)
- {
- ostr.write(buffer, 0, length);
- }
- ostr.flush();
- ostr.close();
- istr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- void InstallWebAdmin()
- {
- AssetManager am = mContext.getAssets();
- try {
- String[] allFiles = am.list(BaseDirectory + "/webadmin");
- for(String fileName : allFiles)
- {
- InputStream istr = null;
- try
- {
- istr = am.open(BaseDirectory + "/webadmin/" + fileName);
- } catch( java.io.FileNotFoundException e ) {
- // Must be a folder :D
- continue;
- }
-
- String outPath = Environment.getExternalStorageDirectory().getPath() + "/mcserver/webadmin/" + fileName;
- Log.i("MCServer", "outPath: " + outPath );
- File f = new File( outPath );
-
- f.getParentFile().mkdirs();
- f.createNewFile();
- OutputStream ostr = new FileOutputStream(f);
-
- byte[] buffer = new byte[1024];
- int length;
- while ((length = istr.read(buffer))>0)
- {
- ostr.write(buffer, 0, length);
- }
- ostr.flush();
- ostr.close();
- istr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- void InstallSinglePlugin( String path )
- {
- AssetManager am = mContext.getAssets();
- try {
- String[] getImages = am.list(BaseDirectory + "/" + path);
- for(String imgName : getImages)
- {
- Log.i("MCServer", path + "/" + imgName);
-
- InputStream istr = null;
- try
- {
- istr = am.open(BaseDirectory + "/" + path + "/" + imgName);
- } catch( java.io.FileNotFoundException e ) {
- Log.i("MCServer", "Could not open" + path + "/" + imgName );
- InstallSinglePlugin(path + "/" + imgName);
- continue;
- }
-
- String outPath = Environment.getExternalStorageDirectory().getPath() + "/mcserver/" + path + "/" + imgName;
- Log.i("MCServer", "outPath: " + outPath );
- File f = new File( outPath );
-
- f.getParentFile().mkdirs();
- f.createNewFile();
- OutputStream ostr = new FileOutputStream(f);
-
- byte[] buffer = new byte[1024];
- int length;
- while ((length = istr.read(buffer))>0)
- {
- ostr.write(buffer, 0, length);
- }
- ostr.flush();
- ostr.close();
- istr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-}