Xrasher/src/in/celest/xash3d/LauncherActivity.java

369 lines
9.9 KiB
Java
Raw Normal View History

2015-05-26 00:11:50 +06:00
package in.celest.xash3d;
import android.app.Activity;
import android.app.Dialog;
2016-05-20 23:46:35 +03:00
import android.app.AlertDialog;
import android.os.AsyncTask;
2015-05-26 00:11:50 +06:00
import android.os.Bundle;
2015-11-08 01:09:57 +06:00
import android.os.Build;
2016-05-20 23:46:35 +03:00
import android.os.Environment;
2015-05-26 00:11:50 +06:00
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
2016-04-29 21:43:56 +03:00
import android.view.Window;
2016-05-08 08:44:32 +00:00
2016-05-20 23:46:35 +03:00
import android.content.Intent;
2015-07-24 06:56:03 +06:00
import android.content.ComponentName;
2015-11-08 01:09:57 +06:00
import android.content.Context;
2015-07-24 06:56:03 +06:00
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
2016-05-20 23:46:35 +03:00
2015-05-26 00:11:50 +06:00
import android.widget.EditText;
2016-05-31 12:23:09 +00:00
import android.widget.TextView;
2015-07-24 06:56:03 +06:00
import android.widget.CheckBox;
import android.widget.CompoundButton;
2015-11-08 01:09:57 +06:00
import android.widget.Button;
2016-05-08 08:44:32 +00:00
import android.widget.Spinner;
import android.widget.ArrayAdapter;
2016-05-20 23:46:35 +03:00
import android.widget.TabHost;
import android.widget.ToggleButton;
import android.widget.Toast;
2016-05-08 08:44:32 +00:00
2015-11-08 01:09:57 +06:00
import android.net.Uri;
2016-05-20 23:46:35 +03:00
import android.util.Log;
2015-11-08 01:09:57 +06:00
import java.lang.reflect.Method;
2016-05-20 23:46:35 +03:00
2015-11-08 01:09:57 +06:00
import java.util.List;
2016-05-20 23:46:35 +03:00
2015-11-08 01:09:57 +06:00
import java.io.File;
2016-05-20 23:46:35 +03:00
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.net.URLConnection;
import java.net.URL;
import org.json.*;
2015-05-26 00:11:50 +06:00
import in.celest.xash3d.hl.R;
public class LauncherActivity extends Activity {
2015-07-24 06:56:03 +06:00
// public final static String ARGV = "in.celest.xash3d.MESSAGE";
2016-05-06 08:58:01 +00:00
public final static int sdk = Integer.valueOf(Build.VERSION.SDK);
2016-07-26 18:01:17 +06:00
public final static String UPDATE_LINK = "https://api.github.com/repos/FWGS/xash3d-android-project/releases"; // releases/latest doesn't return prerelease and drafts
static EditText cmdArgs;
static EditText resPath;
static ToggleButton useVolume;
static ToggleButton resizeWorkaround;
static CheckBox checkUpdates;
static CheckBox updateToBeta;
2015-07-24 06:56:03 +06:00
static SharedPreferences mPref;
2016-05-08 08:44:32 +00:00
static Spinner pixelSpinner;
static TextView tvResPath;
2016-05-20 23:46:35 +03:00
2015-11-08 01:09:57 +06:00
String getDefaultPath()
{
File dir = Environment.getExternalStorageDirectory();
if( dir != null && dir.exists() )
return dir.getPath() + "/xash";
return "/sdcard/xash";
}
2016-05-20 23:46:35 +03:00
2015-05-26 00:11:50 +06:00
@Override
2016-05-20 23:46:35 +03:00
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//super.setTheme( 0x01030005 );
if ( sdk >= 21 )
2016-07-26 17:07:01 +06:00
super.setTheme( 0x01030224 );
2016-05-20 23:46:35 +03:00
setContentView(R.layout.activity_launcher);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tabSpec;
tabSpec = tabHost.newTabSpec("tabtag1");
tabSpec.setIndicator(getString(R.string.text_tab1));
tabSpec.setContent(R.id.tab1);
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("tabtag2");
tabSpec.setIndicator(getString(R.string.text_tab2));
tabSpec.setContent(R.id.tab2);
tabHost.addTab(tabSpec);
mPref = getSharedPreferences("engine", 0);
cmdArgs = (EditText)findViewById(R.id.cmdArgs);
useVolume = (ToggleButton) findViewById( R.id.useVolume );
resPath = (EditText) findViewById( R.id.cmdPath );
2016-05-20 23:46:35 +03:00
checkUpdates = (CheckBox)findViewById(R.id.check_updates);
updateToBeta = (CheckBox)findViewById(R.id.check_betas);
2016-05-20 23:46:35 +03:00
pixelSpinner = (Spinner) findViewById(R.id.pixelSpinner);
2016-07-26 17:07:01 +06:00
resizeWorkaround = (ToggleButton) findViewById( R.id.enableResizeWorkaround );
tvResPath = (TextView) findViewById( R.id.textView_path );
2016-05-20 23:46:35 +03:00
final String[] list = {
"32 bit (RGBA8888)",
"24 bit (RGB888)",
"16 bit (RGB565)",
"15 bit (RGBA5551)",
"12 bit (RGBA4444)",
"8 bit (RGB332)"
2016-05-20 23:46:35 +03:00
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
pixelSpinner.setAdapter(adapter);
useVolume.setChecked(mPref.getBoolean("usevolume",true));
checkUpdates.setChecked(mPref.getBoolean("check_updates",true));
2016-07-26 22:39:54 +06:00
updateToBeta.setChecked(mPref.getBoolean("check_betas", false));
updatePath(mPref.getString("basedir", getDefaultPath()));
2016-05-20 23:46:35 +03:00
cmdArgs.setText(mPref.getString("argv","-dev 3 -log"));
pixelSpinner.setSelection(mPref.getInt("pixelformat", 0));
resizeWorkaround.setChecked(mPref.getBoolean("enableResizeWorkaround", true));
resPath.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
updatePath( resPath.getText().toString() );
}
} );
2016-05-20 23:46:35 +03:00
if(mPref.getBoolean("check_updates", true))
2016-05-31 12:23:09 +00:00
{
new CheckUpdate(true, updateToBeta.isChecked()).execute(UPDATE_LINK);
2016-05-31 12:23:09 +00:00
}
2015-11-08 01:09:57 +06:00
}
2016-05-06 08:58:01 +00:00
2016-05-31 12:23:09 +00:00
void updatePath( String text )
{
tvResPath.setText(getResources().getString(R.string.text_res_path) + ":\n" + text );
resPath.setText(text);
}
2015-05-26 00:11:50 +06:00
public void startXash(View view)
{
2016-05-20 23:46:35 +03:00
Intent intent = new Intent(this, XashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SharedPreferences.Editor editor = mPref.edit();
editor.putString("argv", cmdArgs.getText().toString());
editor.putBoolean("usevolume",useVolume.isChecked());
editor.putString("basedir", resPath.getText().toString());
editor.putInt("pixelformat", pixelSpinner.getSelectedItemPosition());
2016-07-26 17:07:01 +06:00
editor.putBoolean("enableResizeWorkaround",resizeWorkaround.isChecked());
2016-05-20 23:46:35 +03:00
editor.putBoolean("check_updates", checkUpdates.isChecked());
editor.commit();
startActivity(intent);
2015-05-26 00:11:50 +06:00
}
public void aboutXash(View view)
{
final Activity a = this;
2016-05-20 23:46:35 +03:00
this.runOnUiThread(new Runnable()
{
public void run()
{
final Dialog dialog = new Dialog(a);
dialog.setContentView(R.layout.about);
dialog.setCancelable(true);
dialog.show();
}
});
}
2015-11-08 01:09:57 +06:00
public void selectFolder(View view)
{
2016-05-01 03:18:32 +03:00
Intent intent = new Intent(this, in.celest.xash3d.FPicker.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2016-05-20 23:46:35 +03:00
//Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT_TREE");
2015-11-08 01:09:57 +06:00
startActivityForResult(intent, 42);
resPath.setEnabled(false);
}
2016-05-20 23:46:35 +03:00
public void onActivityResult(int requestCode, int resultCode, Intent resultData)
{
if (resultCode == RESULT_OK)
2015-11-08 01:09:57 +06:00
{
2016-05-20 23:46:35 +03:00
try
{
2016-07-26 17:07:01 +06:00
if( resPath == null )
return;
updatePath(resultData.getStringExtra("GetPath"));
2016-05-20 23:46:35 +03:00
resPath.setEnabled(true);
}
catch(Exception e)
{
e.printStackTrace();
}
2015-11-08 01:09:57 +06:00
}
resPath.setEnabled(true);
}
public void createShortcut(View view)
{
Intent intent = new Intent(this, ShortcutActivity.class);
2015-12-20 18:52:09 +06:00
intent.putExtra( "basedir", resPath.getText().toString() );
intent.putExtra( "name", "Xash3D" );
intent.putExtra( "argv", cmdArgs.getText().toString() );
startActivity(intent);
}
2015-05-26 00:11:50 +06:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_launcher, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
/*if (id == R.id.action_settings) {
return true;
}*/
return super.onOptionsItemSelected(item);
}
2016-05-20 23:46:35 +03:00
2016-05-21 20:13:18 +03:00
private class CheckUpdate extends AsyncTask<String, Void, String> {
2016-05-20 23:46:35 +03:00
InputStream is = null;
ByteArrayOutputStream os = null;
boolean mSilent;
boolean mBeta;
2016-05-20 23:46:35 +03:00
public CheckUpdate( boolean silent, boolean beta )
2016-05-20 23:46:35 +03:00
{
mSilent = silent;
mBeta = beta;
2016-05-20 23:46:35 +03:00
}
protected String doInBackground(String... urls)
{
2016-05-20 23:46:35 +03:00
try
{
URL url = new URL(urls[0]);
is = url.openConnection().getInputStream();
os = new ByteArrayOutputStream();
byte[] buffer = new byte[8196];
int len;
while ((len = is.read(buffer)) > 0)
{
os.write(buffer, 0, len);
}
2016-07-26 22:39:54 +06:00
os.flush();
2016-05-20 23:46:35 +03:00
return os.toString();
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
protected void onPostExecute(String result)
{
JSONArray releases = null;
2016-05-20 23:46:35 +03:00
try
{
if (is != null)
{
is.close();
is = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
2016-05-21 20:13:18 +03:00
if (os != null)
{
releases = new JSONArray(os.toString());
2016-05-20 23:46:35 +03:00
os.close();
os = null;
}
}
catch(Exception e)
{
e.printStackTrace();
return;
2016-05-20 23:46:35 +03:00
}
if( releases == null )
return;
for( int i = 0; i < releases.length(); i++ )
2016-05-20 23:46:35 +03:00
{
final JSONObject obj;
try
{
obj = releases.getJSONObject(i);
2016-08-01 09:40:56 +00:00
final String version, url, name;
final boolean beta = obj.getBoolean("prerelease");
if( beta && !mBeta )
continue;
version = obj.getString("tag_name");
url = obj.getString("html_url");
name = obj.getString("name");
Log.d("Xash", "Found: " + version +
", I: " + getString(R.string.version_string));
// this is an update
if( getString(R.string.version_string).compareTo(version) < 0 )
{
String dialog_message = String.format(getString(R.string.update_message), name);
AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
builder.setMessage(dialog_message)
.setPositiveButton(R.string.update, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{ public void onClick(DialogInterface dialog, int id) {} } );
builder.create().show();
}
else if( !mSilent )
{
Toast.makeText(getBaseContext(), R.string.no_updates, Toast.LENGTH_SHORT).show();
}
// No need to check other releases, so we will stop here.
break;
}
catch(Exception e)
{
e.printStackTrace();
continue;
}
2016-05-20 23:46:35 +03:00
}
}
}
2015-05-26 00:11:50 +06:00
}