2017-09-30 02:25:08 +03:00
|
|
|
package su.xash.fwgslib;
|
|
|
|
|
2019-11-29 19:42:48 +03:00
|
|
|
import android.Manifest;
|
2017-09-30 02:25:08 +03:00
|
|
|
import android.app.*;
|
|
|
|
import android.content.*;
|
2019-11-29 19:42:48 +03:00
|
|
|
import android.content.pm.*;
|
2017-09-30 02:25:08 +03:00
|
|
|
import android.graphics.*;
|
|
|
|
import android.graphics.drawable.*;
|
|
|
|
import android.net.*;
|
|
|
|
import android.os.*;
|
|
|
|
import android.text.*;
|
|
|
|
import android.text.method.*;
|
|
|
|
import android.text.style.*;
|
|
|
|
import android.util.*;
|
|
|
|
import android.view.*;
|
|
|
|
import android.widget.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
2019-11-29 19:42:48 +03:00
|
|
|
import java.lang.*;
|
|
|
|
import java.util.*;
|
2017-09-30 02:25:08 +03:00
|
|
|
import org.json.*;
|
|
|
|
import android.preference.*;
|
|
|
|
|
|
|
|
/*
|
2018-01-01 20:18:12 +03:00
|
|
|
* This utility class is intended to hide some Android and Java design-flaws and
|
2017-09-30 02:25:08 +03:00
|
|
|
* also just shortcuts
|
|
|
|
*/
|
|
|
|
public class FWGSLib
|
|
|
|
{
|
|
|
|
private static final String TAG = "FWGSLib";
|
2018-04-02 03:55:23 +07:00
|
|
|
static String externalFilesDir;
|
2017-09-30 02:25:08 +03:00
|
|
|
public static boolean FBitSet( final int bits, final int mask )
|
|
|
|
{
|
2018-04-07 03:46:07 +03:00
|
|
|
return ((bits & mask) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean FExactBitSet( final int bits, final int mask )
|
|
|
|
{
|
|
|
|
return ((bits & mask) == mask );
|
2017-09-30 02:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static float atof( String str, float fallback )
|
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ret = Float.valueOf( str );
|
|
|
|
}
|
|
|
|
catch( Exception e )
|
|
|
|
{
|
|
|
|
ret = fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int atoi( String str, int fallback )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ret = Integer.valueOf( str );
|
|
|
|
}
|
|
|
|
catch( Exception e )
|
|
|
|
{
|
|
|
|
ret = fallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean checkGameLibDir( String gamelibdir, String allowed )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-01-01 20:18:12 +03:00
|
|
|
Log.d( TAG, " gamelibdir = " + gamelibdir + " allowed = " + allowed );
|
|
|
|
|
|
|
|
if( gamelibdir.contains( "/.." ))
|
|
|
|
return false;
|
2017-09-30 02:25:08 +03:00
|
|
|
|
|
|
|
File f = new File( gamelibdir );
|
|
|
|
|
|
|
|
if( !f.isDirectory() )
|
|
|
|
{
|
2018-01-01 20:18:12 +03:00
|
|
|
Log.d( TAG, "Not a directory" );
|
2017-09-30 02:25:08 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !f.exists() )
|
|
|
|
{
|
2018-01-01 20:18:12 +03:00
|
|
|
Log.d( TAG, "Does not exist" );
|
2017-09-30 02:25:08 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:18:12 +03:00
|
|
|
// add trailing / for simple regexp
|
|
|
|
if( gamelibdir.charAt(gamelibdir.length() - 1) != '/' )
|
|
|
|
gamelibdir = gamelibdir + "/";
|
|
|
|
|
|
|
|
final String regex = ".+\\/" + allowed.replace(".", "\\.") + "(|(-\\d))\\/(.+|)";
|
2017-09-30 02:25:08 +03:00
|
|
|
|
2018-01-01 20:18:12 +03:00
|
|
|
Log.d( TAG, regex );
|
2017-09-30 02:25:08 +03:00
|
|
|
|
2018-01-01 20:18:12 +03:00
|
|
|
final boolean ret = gamelibdir.matches( regex );
|
|
|
|
|
|
|
|
Log.d( TAG, "ret = " + ret );
|
|
|
|
|
|
|
|
return ret;
|
2017-09-30 02:25:08 +03:00
|
|
|
}
|
|
|
|
catch( Exception e )
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getDefaultXashPath()
|
|
|
|
{
|
|
|
|
File dir = Environment.getExternalStorageDirectory();
|
|
|
|
if( dir != null && dir.exists() )
|
|
|
|
return dir.getPath() + "/xash";
|
|
|
|
return "/sdcard/xash";
|
|
|
|
}
|
2018-04-02 03:55:23 +07:00
|
|
|
static class GetExternalFilesDir extends Thread
|
|
|
|
{
|
|
|
|
Context ctx;
|
|
|
|
GetExternalFilesDir( Context ctx1 )
|
|
|
|
{
|
|
|
|
ctx = ctx1;
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File f = ctx.getExternalFilesDir(null);
|
|
|
|
|
|
|
|
f.mkdirs();
|
|
|
|
|
|
|
|
externalFilesDir = f.getAbsolutePath();
|
|
|
|
Log.d(TAG, "getExternalFilesDir success");
|
|
|
|
}
|
|
|
|
catch( Exception e )
|
|
|
|
{
|
|
|
|
Log.e( TAG, e.toString(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 02:18:25 +03:00
|
|
|
public static String getExternalFilesDir( Context ctx )
|
|
|
|
{
|
2018-04-02 03:55:23 +07:00
|
|
|
if( externalFilesDir != null )
|
|
|
|
return externalFilesDir;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( sdk >= 8 )
|
|
|
|
{
|
|
|
|
Thread t = new GetExternalFilesDir(ctx);
|
|
|
|
t.start();
|
|
|
|
t.join(2000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
2018-04-01 18:29:57 +03:00
|
|
|
{
|
2018-04-02 03:55:23 +07:00
|
|
|
Log.e( TAG, e.toString(), e);
|
|
|
|
externalFilesDir = getDefaultXashPath();
|
2018-04-01 18:29:57 +03:00
|
|
|
}
|
2018-04-02 03:55:23 +07:00
|
|
|
if( externalFilesDir == null )
|
|
|
|
externalFilesDir = getDefaultXashPath();
|
|
|
|
return externalFilesDir;
|
2018-03-21 02:18:25 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 02:25:08 +03:00
|
|
|
public static boolean isLandscapeOrientation( Activity act )
|
|
|
|
{
|
|
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
|
|
|
act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
|
|
|
return (metrics.widthPixels > metrics.heightPixels);
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:54:45 +03:00
|
|
|
public static String getStringExtraFromIntent( Intent intent, String extraString, String ifNotFound )
|
|
|
|
{
|
|
|
|
String ret = intent.getStringExtra( extraString );
|
|
|
|
if( ret == null )
|
|
|
|
{
|
|
|
|
ret = ifNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:08:11 +07:00
|
|
|
public static void changeButtonsStyle( ViewGroup parent )
|
|
|
|
{
|
|
|
|
if( sdk >= 21 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for( int i = parent.getChildCount() - 1; i >= 0; i-- )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
final View child = parent.getChildAt(i);
|
|
|
|
|
|
|
|
if( child == null )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( child instanceof ViewGroup )
|
|
|
|
{
|
|
|
|
changeButtonsStyle((ViewGroup) child);
|
|
|
|
// DO SOMETHING WITH VIEWGROUP, AFTER CHILDREN HAS BEEN LOOPED
|
|
|
|
}
|
|
|
|
else if( child instanceof Button )
|
|
|
|
{
|
|
|
|
final Button b = (Button)child;
|
|
|
|
final Drawable bg = b.getBackground();
|
|
|
|
if(bg!= null)bg.setAlpha( 96 );
|
|
|
|
b.setTextColor( 0xFFFFFFFF );
|
|
|
|
b.setTextSize( 15f );
|
|
|
|
//b.setText(b.getText().toString().toUpperCase());
|
|
|
|
b.setTypeface( b.getTypeface(),Typeface.BOLD );
|
|
|
|
}
|
|
|
|
else if( child instanceof EditText )
|
|
|
|
{
|
|
|
|
final EditText b = ( EditText )child;
|
|
|
|
b.setBackgroundColor( 0xFF353535 );
|
|
|
|
b.setTextColor( 0xFFFFFFFF );
|
|
|
|
b.setTextSize( 15f );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( Exception e )
|
|
|
|
{
|
|
|
|
}
|
2019-11-29 19:42:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void applyPermissions( final Activity act, final String permissions[], final int code )
|
|
|
|
{
|
|
|
|
if( sdk >= 23 )
|
|
|
|
{
|
|
|
|
List<String> requestPermissions = new ArrayList<String>();
|
|
|
|
|
|
|
|
for( int i = 0; i < permissions.length; i++ )
|
|
|
|
{
|
|
|
|
if( act.checkSelfPermission(permissions[i]) != PackageManager.PERMISSION_GRANTED )
|
|
|
|
{
|
|
|
|
requestPermissions.add(permissions[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:08:11 +07:00
|
|
|
|
2019-11-29 19:42:48 +03:00
|
|
|
if( !requestPermissions.isEmpty() )
|
|
|
|
{
|
|
|
|
String[] requestPermissionsArray = new String[requestPermissions.size()];
|
|
|
|
for( int i = 0; i < requestPermissions.size(); i++ )
|
|
|
|
{
|
|
|
|
requestPermissionsArray[i] = requestPermissions.get(i);
|
|
|
|
}
|
|
|
|
act.requestPermissions(requestPermissionsArray, code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 02:25:08 +03:00
|
|
|
|
|
|
|
public static final int sdk = Integer.valueOf(Build.VERSION.SDK);
|
|
|
|
}
|