Skip to content

Commit

Permalink
Merge pull request #3849 from Tsunamical/codecleanup
Browse files Browse the repository at this point in the history
[Android] Reduce code redundancy and catch a leak
  • Loading branch information
Parlane committed May 19, 2016
2 parents a89e27b + 4a0a92e commit 2d41f12
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Source/Android/app/build.gradle
Expand Up @@ -60,14 +60,14 @@ android {
// of product flavors are paid vs. free, ARM vs. x86, etc.
productFlavors {
arm_64 {
flavorDimension "abi"
dimension "abi"
ndk {
abiFilter "arm64-v8a"
}
}

x86_64 {
flavorDimension "abi"
dimension "abi"
ndk {
abiFilter "x86_64"
}
Expand Down
Expand Up @@ -479,7 +479,7 @@ public boolean dispatchKeyEvent(KeyEvent event)
return super.dispatchKeyEvent(event);
}

int action = 0;
int action;

switch (event.getAction())
{
Expand All @@ -501,8 +501,7 @@ public boolean dispatchKeyEvent(KeyEvent event)
return false;
}
InputDevice input = event.getDevice();
boolean handled = NativeLibrary.onGamePadEvent(input.getDescriptor(), event.getKeyCode(), action);
return handled;
return NativeLibrary.onGamePadEvent(input.getDescriptor(), event.getKeyCode(), action);
}

@Override
Expand Down
Expand Up @@ -25,7 +25,7 @@ public final class MotionAlertDialog extends AlertDialog
private final Preference inputPref;

private boolean firstEvent = true;
private final ArrayList<Float> m_values = new ArrayList<Float>();
private final ArrayList<Float> m_values = new ArrayList<>();

/**
* Constructor
Expand Down Expand Up @@ -105,19 +105,13 @@ else if (m_values.get(a) < (event.getAxisValue(range.getAxis()) - 0.5f))
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (onKeyDown(event.getKeyCode(), event))
return true;

return super.dispatchKeyEvent(event);
return onKeyDown(event.getKeyCode(), event) || super.dispatchKeyEvent(event);
}

@Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
if (onMotionEvent(event))
return true;

return super.dispatchGenericMotionEvent(event);
return onMotionEvent(event) || super.dispatchGenericMotionEvent(event);
}

/**
Expand Down
Expand Up @@ -26,15 +26,12 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C

private SharedPreferences mPreferences;

private SurfaceView mSurfaceView;
private Surface mSurface;

private InputOverlay mInputOverlay;

private Thread mEmulationThread;

private String mPath;

private boolean mEmulationStarted;
private boolean mEmulationRunning;

Expand Down Expand Up @@ -70,15 +67,15 @@ public void onCreate(Bundle savedInstanceState)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mPath = getArguments().getString(ARGUMENT_GAME_PATH);
NativeLibrary.SetFilename(mPath);
String path = getArguments().getString(ARGUMENT_GAME_PATH);
NativeLibrary.SetFilename(path);

View contents = inflater.inflate(R.layout.fragment_emulation, container, false);

mSurfaceView = (SurfaceView) contents.findViewById(R.id.surface_emulation);
SurfaceView surfaceView = (SurfaceView) contents.findViewById(R.id.surface_emulation);
mInputOverlay = (InputOverlay) contents.findViewById(R.id.surface_input_overlay);

mSurfaceView.getHolder().addCallback(this);
surfaceView.getHolder().addCallback(this);

// If the input overlay was previously disabled, then don't show it.
if (mInputOverlay != null)
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Environment;

public final class Game
{
Expand All @@ -26,7 +27,7 @@ public final class Game
public static final int COUNTRY_WORLD = 12;
public static final int COUNTRY_UNKNOWN = 13;

private static final String PATH_SCREENSHOT_FOLDER = "file:///sdcard/dolphin-emu/ScreenShots/";
private static final String PATH_SCREENSHOT_FOLDER = Environment.getExternalStorageDirectory().getPath() + "/dolphin-emu/ScreenShots/";

private String mTitle;
private String mDescription;
Expand Down
Expand Up @@ -251,7 +251,7 @@ else if (!folder.exists())
}
}


fileCursor.close();
folderCursor.close();
database.close();
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;

import org.dolphinemu.dolphinemu.BuildConfig;
import org.dolphinemu.dolphinemu.utils.Log;
Expand Down Expand Up @@ -39,7 +40,7 @@ public boolean onCreate()
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
Log.info("[GameProvider] Querying URI: " + uri);

Expand All @@ -60,7 +61,7 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
}

@Override
public String getType(Uri uri)
public String getType(@NonNull Uri uri)
{
Log.verbose("[GameProvider] Getting MIME type for URI: " + uri);
String lastSegment = uri.getLastPathSegment();
Expand All @@ -85,7 +86,7 @@ else if (lastSegment.equals(GameDatabase.TABLE_NAME_GAMES))
}

@Override
public Uri insert(Uri uri, ContentValues values)
public Uri insert(@NonNull Uri uri, ContentValues values)
{
Log.info("[GameProvider] Inserting row at URI: " + uri);

Expand Down Expand Up @@ -134,14 +135,14 @@ public Uri insert(Uri uri, ContentValues values)
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs)
{
Log.error("[GameProvider] Delete operations unsupported. URI: " + uri);
return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
Log.error("[GameProvider] Update operations unsupported. URI: " + uri);
return 0;
Expand Down
Expand Up @@ -36,8 +36,8 @@
*/
public final class InputOverlay extends SurfaceView implements OnTouchListener
{
private final Set<InputOverlayDrawableButton> overlayButtons = new HashSet<InputOverlayDrawableButton>();
private final Set<InputOverlayDrawableJoystick> overlayJoysticks = new HashSet<InputOverlayDrawableJoystick>();
private final Set<InputOverlayDrawableButton> overlayButtons = new HashSet<>();
private final Set<InputOverlayDrawableJoystick> overlayJoysticks = new HashSet<>();

/**
* Resizes a {@link Bitmap} by a given scale factor
Expand All @@ -53,11 +53,10 @@ public static Bitmap resizeBitmap(Context context, Bitmap bitmap, float scale)
// Retrieve screen dimensions.
DisplayMetrics dm = context.getResources().getDisplayMetrics();

Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap,
return Bitmap.createScaledBitmap(bitmap,
(int)(dm.heightPixels * scale),
(int)(dm.heightPixels * scale),
true);
return bitmapResized;
}

/**
Expand Down
Expand Up @@ -54,14 +54,14 @@ protected void onHandleIntent(Intent intent)
SharedPreferences.Editor editor = preferences.edit();

editor.putBoolean("assetsCopied", true);
editor.commit();
editor.apply();
}

private void copyAsset(String asset, String output, Boolean overwrite)
{
Log.verbose("[AssetCopyService] Copying File " + asset + " to " + output);
InputStream in = null;
OutputStream out = null;
InputStream in;
OutputStream out;

try
{
Expand Down
Expand Up @@ -120,14 +120,12 @@ public void closeHelper()
*/
public String[] getEGLInfo()
{
String[] info = {
return new String[] {
mGL.glGetString(GL10.GL_VENDOR),
mGL.glGetString(GL10.GL_VERSION),
mGL.glGetString(GL10.GL_RENDERER),
mGL.glGetString(GL10.GL_EXTENSIONS),
};

return info;
}

/**
Expand Down Expand Up @@ -243,10 +241,10 @@ private boolean detect()
return false;
}

for (int i = 0; i < mEGLConfigs.length; i++)
for (EGLConfig mEGLConfig : mEGLConfigs)
{
int[] attribVal = new int[1];
boolean ret = mEGL.eglGetConfigAttrib(mDisplay, mEGLConfigs[i], EGL10.EGL_RENDERABLE_TYPE, attribVal);
boolean ret = mEGL.eglGetConfigAttrib(mDisplay, mEGLConfig, EGL10.EGL_RENDERABLE_TYPE, attribVal);
if (ret)
{
if ((attribVal[0] & EGL_OPENGL_BIT) != 0)
Expand Down
Expand Up @@ -32,7 +32,7 @@ private static void RequestPermission()
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
UsbDevice dev = (UsbDevice) pair.getValue();
UsbDevice dev = pair.getValue();
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
{
if (!manager.hasPermission(dev))
Expand All @@ -58,7 +58,7 @@ public static boolean QueryAdapter()
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
UsbDevice dev = (UsbDevice) pair.getValue();
UsbDevice dev = pair.getValue();
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
{
if (manager.hasPermission(dev))
Expand All @@ -77,24 +77,21 @@ public static void InitAdapter()
}

public static int Input() {
int read = usb_con.bulkTransfer(usb_in, controller_payload, controller_payload.length, 16);
return read;
return usb_con.bulkTransfer(usb_in, controller_payload, controller_payload.length, 16);
}

public static int Output(byte[] rumble) {
int size = usb_con.bulkTransfer(usb_out, rumble, 5, 16);
return size;
return usb_con.bulkTransfer(usb_out, rumble, 5, 16);
}

public static boolean OpenAdapter()
{
HashMap<String, UsbDevice> devices = manager.getDeviceList();
Iterator it = devices.entrySet().iterator();
while (it.hasNext())
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
HashMap.Entry pair = (HashMap.Entry)it.next();
UsbDevice dev = (UsbDevice)pair.getValue();
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e) {
UsbDevice dev = pair.getValue();
if (dev.getProductId() == 0x0337 && dev.getVendorId() == 0x057e)
{
if (manager.hasPermission(dev))
{
usb_con = manager.openDevice(dev);
Expand Down
Expand Up @@ -38,7 +38,7 @@ private static void RequestPermission()
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
UsbDevice dev = (UsbDevice) pair.getValue();
UsbDevice dev = pair.getValue();
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
{
if (!manager.hasPermission(dev))
Expand All @@ -59,7 +59,7 @@ public static boolean QueryAdapter()
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
UsbDevice dev = (UsbDevice) pair.getValue();
UsbDevice dev = pair.getValue();
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
{
if (manager.hasPermission(dev))
Expand Down Expand Up @@ -114,7 +114,7 @@ public static boolean OpenAdapter()
HashMap<String, UsbDevice> devices = manager.getDeviceList();
for (Map.Entry<String, UsbDevice> pair : devices.entrySet())
{
UsbDevice dev = (UsbDevice) pair.getValue();
UsbDevice dev = pair.getValue();
if (dev.getProductId() == NINTENDO_WIIMOTE_PRODUCT_ID && dev.getVendorId() == NINTENDO_VENDOR_ID)
{
if (manager.hasPermission(dev))
Expand Down
2 changes: 1 addition & 1 deletion Source/Android/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 2d41f12

Please sign in to comment.