Introduce FWGSLib: an utility library that contains userful shortcuts and hides Android and Java design flaws
This commit is contained in:
parent
5885a6ccd4
commit
1c02cfb443
2 changed files with 155 additions and 11 deletions
|
@ -32,13 +32,41 @@ public class CertCheck
|
||||||
if( !XashConfig.CHECK_SIGNATURES )
|
if( !XashConfig.CHECK_SIGNATURES )
|
||||||
return false; // disable checking for debug builds
|
return false; // disable checking for debug builds
|
||||||
|
|
||||||
|
final String sig;
|
||||||
|
|
||||||
|
if( XashConfig.PKG_TEST )
|
||||||
|
{
|
||||||
|
sig = SIG_TEST;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sig = SIG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( dumbCertificateCheck( context, context.getPackageName(), sig, false ) )
|
||||||
|
{
|
||||||
|
Log.e(TAG, "Please, don't resign our public release builds!");
|
||||||
|
Log.e(TAG, "If you want to insert some features, rebuild package with ANOTHER package name from git repository.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean dumbCertificateCheck( Context context, String pkgName, String sig, boolean failIfNoPkg )
|
||||||
|
{
|
||||||
|
if( sig == null )
|
||||||
|
sig = SIG;
|
||||||
|
|
||||||
|
Log.d( TAG, "pkgName = " + pkgName );
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
PackageInfo info = context.getPackageManager()
|
PackageInfo info = context.getPackageManager()
|
||||||
.getPackageInfo( context.getPackageName(), PackageManager.GET_SIGNATURES );
|
.getPackageInfo( pkgName, PackageManager.GET_SIGNATURES );
|
||||||
|
|
||||||
for( Signature signature: info.signatures )
|
for( Signature signature: info.signatures )
|
||||||
{
|
{
|
||||||
|
Log.d( TAG, "found signature" );
|
||||||
MessageDigest md = MessageDigest.getInstance( "SHA" );
|
MessageDigest md = MessageDigest.getInstance( "SHA" );
|
||||||
final byte[] signatureBytes = signature.toByteArray();
|
final byte[] signatureBytes = signature.toByteArray();
|
||||||
|
|
||||||
|
@ -46,25 +74,27 @@ public class CertCheck
|
||||||
|
|
||||||
final String curSIG = Base64.encodeToString( md.digest(), Base64.NO_WRAP );
|
final String curSIG = Base64.encodeToString( md.digest(), Base64.NO_WRAP );
|
||||||
|
|
||||||
if( XashConfig.PKG_TEST )
|
if( sig.equals(curSIG) )
|
||||||
{
|
{
|
||||||
if( SIG_TEST.equals(curSIG) )
|
Log.d( TAG, "Found valid cert" );
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( SIG.equals(curSIG) )
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch( PackageManager.NameNotFoundException e )
|
||||||
|
{
|
||||||
|
Log.d( TAG, "Package not found" );
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
if( !failIfNoPkg )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
catch( Exception e )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.e(TAG, "Please, don't resign our public release builds!");
|
|
||||||
Log.e(TAG, "If you want to insert some features, rebuild package with ANOTHER package name from git repository.");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
114
src/su/xash/fwgslib/FWGSLib.java
Normal file
114
src/su/xash/fwgslib/FWGSLib.java
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
package su.xash.fwgslib;
|
||||||
|
|
||||||
|
import android.app.*;
|
||||||
|
import android.content.*;
|
||||||
|
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.*;
|
||||||
|
import org.json.*;
|
||||||
|
import android.preference.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This utility class is intended to hide some Android and Java design-flaws
|
||||||
|
* also just shortcuts
|
||||||
|
*/
|
||||||
|
public class FWGSLib
|
||||||
|
{
|
||||||
|
private static final String TAG = "FWGSLib";
|
||||||
|
public static boolean FBitSet( final int bits, final int mask )
|
||||||
|
{
|
||||||
|
return ((bits & mask) == mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
// Log.d( TAG, " gamelibdir = " + gamelibdir + " allowed = " + allowed );
|
||||||
|
|
||||||
|
File f = new File( gamelibdir );
|
||||||
|
|
||||||
|
if( !f.isDirectory() )
|
||||||
|
{
|
||||||
|
// Log.d( TAG, "Not a directory" );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !f.exists() )
|
||||||
|
{
|
||||||
|
// Log.d( TAG, "Does not exist" );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String path = f.getCanonicalPath();
|
||||||
|
|
||||||
|
// Log.d( TAG, "path = " + path );
|
||||||
|
|
||||||
|
final String regex = ".+\\/" + allowed + "(\\/|(-\\d)?\\/).+";
|
||||||
|
|
||||||
|
return path.matches( regex );
|
||||||
|
}
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isLandscapeOrientation( Activity act )
|
||||||
|
{
|
||||||
|
DisplayMetrics metrics = new DisplayMetrics();
|
||||||
|
act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
||||||
|
return (metrics.widthPixels > metrics.heightPixels);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final int sdk = Integer.valueOf(Build.VERSION.SDK);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue