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

95 lines
3 KiB
Java
Raw Normal View History

package in.celest.xash3d;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;
import in.celest.xash3d.hl.R;
import android.widget.EditText;
2015-09-03 03:52:21 +07:00
import java.io.File;
import java.io.FilenameFilter;
import android.os.*;
public class ShortcutActivity extends Activity
{
static EditText name;
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.activity_shortcut);
name=(EditText)findViewById(R.id.shortcut_name);
2015-09-03 03:52:21 +07:00
//name.setText("Name");
}
public void saveShortcut(View view)
{
2015-08-15 04:38:05 +07:00
Intent intent = new Intent();
intent.setAction("in.celest.xash3d.START");
EditText argv = (EditText)findViewById(R.id.shortcut_cmdArgs);
if(argv.length() != 0) intent.putExtra("argv",argv.getText().toString());
EditText gamedir = (EditText)findViewById(R.id.shortcut_gamedir);
if(gamedir.length() != 0) intent.putExtra("gamedir",gamedir.getText().toString());
Intent wrapIntent = new Intent();
wrapIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
wrapIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name.getText().toString());
2015-09-04 19:38:05 +07:00
Bitmap icon = null;
2015-09-03 03:52:21 +07:00
// Try find icon
2015-09-04 19:38:05 +07:00
int size = (int) getResources().getDimension(android.R.dimen.app_icon_size);
String gamedirstring = getSharedPreferences("engine", 0).getString("basedir","/sdcard/xash/")+(gamedir.length()!=0?gamedir.getText().toString():"valve");
2015-09-03 03:52:21 +07:00
try
2015-09-04 19:38:05 +07:00
{
icon = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(gamedirstring+"/icon.png"), size, size, false);
}
catch(Exception e)
{
}
if(icon == null) try
2015-09-04 21:06:00 +07:00
{
icon = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(gamedirstring+"/game.ico"), size, size, false);
}
catch(Exception e)
{
}
if(icon == null) try
2015-09-03 03:52:21 +07:00
{
FilenameFilter icoFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if(name.endsWith(".ico") || name.endsWith(".ICO")) {
return true;
}
return false;
}
};
2015-09-04 19:38:05 +07:00
2015-09-03 03:52:21 +07:00
File gamedirfile = new File(gamedirstring);
String files[] = gamedirfile.list(icoFilter);
icon = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(gamedirstring+"/"+files[0]), size, size, false);
}
catch(Exception e)
{
// Android may not support ico loading, so fallback if something going wrong
icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
wrapIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
if(getIntent().getAction() == "android.intent.action.CREATE_SHORTCUT" ) // Called from launcher
{
setResult(RESULT_OK, wrapIntent);
finish();
}
else try
{
wrapIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(wrapIntent);
Toast.makeText(getApplicationContext(), "Shortcut created!", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Problem creating shortcut: " + e.toString() +
"\nTry create it manually from laucnher", Toast.LENGTH_LONG).show();
}
}
}