package tv.ouya.console.api;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class OuyaController
{
  private static int[] AVAILABLE_AXES = { 0, 1, 11, 14, 17, 18 };
  public static final int AXIS_L2 = 17;
  public static final int AXIS_LS_X = 0;
  public static final int AXIS_LS_Y = 1;
  public static final int AXIS_R2 = 18;
  public static final int AXIS_RS_X = 11;
  public static final int AXIS_RS_Y = 14;
  public static final int BUTTON_A = 97;
  public static final int BUTTON_DPAD_DOWN = 20;
  public static final int BUTTON_DPAD_LEFT = 21;
  public static final int BUTTON_DPAD_RIGHT = 22;
  public static final int BUTTON_DPAD_UP = 19;
  public static final int BUTTON_L1 = 102;
  @Deprecated
  public static final int BUTTON_L2 = 104;
  public static final int BUTTON_L3 = 106;
  public static final int BUTTON_MENU = 82;
  public static final int BUTTON_O = 96;
  public static final int BUTTON_R1 = 103;
  @Deprecated
  public static final int BUTTON_R2 = 105;
  public static final int BUTTON_R3 = 107;
  public static final int BUTTON_U = 99;
  public static final int BUTTON_Y = 100;
  private static final String EXTRA_OUYA_CONTROLLER_DEVICE_ID = "DEVICE_ID";
  private static final String EXTRA_OUYA_CONTROLLER_PLAYER_NUM = "PLAYER_NUM";
  private static final int INVALID_INPUT_DEVICE_ID = -1;
  public static final int MAX_CONTROLLERS = 4;
  private static final String OUYA_CONTROLLER_ADDED_ACTION = "tv.ouya.controller.added";
  private static final String OUYA_CONTROLLER_REMOVED_ACTION = "tv.ouya.controller.removed";
  private static final String OUYA_ID = "OUYA Game Controller";
  private static final int PRESSED_THIS_FRAME = 1;
  private static final int RELEASED_THIS_FRAME = 2;
  private static final String ROCK_CANDY_ID = "Generic X-Box pad";
  public static final float STICK_DEADZONE = 0.25F;
  private static Context appContext;
  private static final Map<String, SparseIntArray> s_axisByDeviceName;
  private static final Map<String, SparseIntArray> s_buttonByDeviceName;
  static final OuyaController[] s_controllers = new OuyaController[4];
  private SparseArray<Float> axisValues;
  private SparseBooleanArray buttonValues;
  private InputDevice device;
  private SparseIntArray deviceAxis;
  private int deviceId;
  private SparseIntArray deviceKeycodes;
  private SparseIntArray thisFrameButtonValues;
  
  static
  {
    SparseIntArray localSparseIntArray1 = new SparseIntArray();
    localSparseIntArray1.put(96, 96);
    localSparseIntArray1.put(99, 99);
    localSparseIntArray1.put(100, 100);
    localSparseIntArray1.put(97, 97);
    localSparseIntArray1.put(102, 102);
    localSparseIntArray1.put(104, 104);
    localSparseIntArray1.put(106, 106);
    localSparseIntArray1.put(103, 103);
    localSparseIntArray1.put(105, 105);
    localSparseIntArray1.put(107, 107);
    localSparseIntArray1.put(20, 20);
    localSparseIntArray1.put(19, 19);
    localSparseIntArray1.put(22, 22);
    localSparseIntArray1.put(21, 21);
    localSparseIntArray1.put(108, 82);
    SparseIntArray localSparseIntArray2 = new SparseIntArray();
    localSparseIntArray2.put(96, 96);
    localSparseIntArray2.put(99, 99);
    localSparseIntArray2.put(100, 100);
    localSparseIntArray2.put(97, 97);
    localSparseIntArray2.put(102, 102);
    localSparseIntArray2.put(104, 104);
    localSparseIntArray2.put(106, 106);
    localSparseIntArray2.put(103, 103);
    localSparseIntArray2.put(105, 105);
    localSparseIntArray2.put(107, 107);
    localSparseIntArray2.put(20, 20);
    localSparseIntArray2.put(19, 19);
    localSparseIntArray2.put(22, 22);
    localSparseIntArray2.put(21, 21);
    localSparseIntArray2.put(82, 82);
    HashMap localHashMap = new HashMap();
    localHashMap.put("Generic X-Box pad", localSparseIntArray1);
    localHashMap.put("OUYA Game Controller", localSparseIntArray2);
    s_buttonByDeviceName = Collections.unmodifiableMap(localHashMap);
    localSparseIntArray1 = new SparseIntArray();
    localSparseIntArray1.put(0, 0);
    localSparseIntArray1.put(1, 1);
    localSparseIntArray1.put(11, 11);
    localSparseIntArray1.put(14, 14);
    localSparseIntArray1.put(17, 17);
    localSparseIntArray1.put(18, 18);
    localSparseIntArray2 = new SparseIntArray();
    localSparseIntArray2.put(0, 0);
    localSparseIntArray2.put(1, 1);
    localSparseIntArray2.put(11, 11);
    localSparseIntArray2.put(14, 14);
    localSparseIntArray2.put(17, 17);
    localSparseIntArray2.put(18, 18);
    localHashMap = new HashMap();
    localHashMap.put("Generic X-Box pad", localSparseIntArray1);
    localHashMap.put("OUYA Game Controller", localSparseIntArray2);
    s_axisByDeviceName = Collections.unmodifiableMap(localHashMap);
  }
  
  OuyaController(int paramInt)
  {
    this.deviceId = paramInt;
    this.device = InputDevice.getDevice(paramInt);
    this.axisValues = new SparseArray();
    this.buttonValues = new SparseBooleanArray();
    this.thisFrameButtonValues = new SparseIntArray();
    this.deviceAxis = getAxisMap(this.device);
    this.deviceKeycodes = getButtonMap(this.device);
  }
  
  private static SparseIntArray getAxisMap(InputDevice paramInputDevice)
  {
    if (paramInputDevice != null) {}
    for (paramInputDevice = paramInputDevice.getName();; paramInputDevice = "OUYA Game Controller")
    {
      Object localObject = paramInputDevice;
      if (paramInputDevice.contains("OUYA Game Controller")) {
        localObject = "OUYA Game Controller";
      }
      localObject = (SparseIntArray)s_axisByDeviceName.get(localObject);
      paramInputDevice = (InputDevice)localObject;
      if (localObject == null) {
        paramInputDevice = (SparseIntArray)s_axisByDeviceName.get("OUYA Game Controller");
      }
      return paramInputDevice;
    }
  }
  
  private static SparseIntArray getButtonMap(InputDevice paramInputDevice)
  {
    if (paramInputDevice != null) {}
    for (paramInputDevice = paramInputDevice.getName();; paramInputDevice = "OUYA Game Controller")
    {
      Object localObject = paramInputDevice;
      if (paramInputDevice.contains("OUYA Game Controller")) {
        localObject = "OUYA Game Controller";
      }
      localObject = (SparseIntArray)s_buttonByDeviceName.get(localObject);
      paramInputDevice = (InputDevice)localObject;
      if (localObject == null) {
        paramInputDevice = (SparseIntArray)s_buttonByDeviceName.get("OUYA Game Controller");
      }
      return paramInputDevice;
    }
  }
  
  public static OuyaController getControllerByDeviceId(int paramInt)
  {
    OuyaController[] arrayOfOuyaController = s_controllers;
    int j = arrayOfOuyaController.length;
    int i = 0;
    while (i < j)
    {
      OuyaController localOuyaController = arrayOfOuyaController[i];
      if ((localOuyaController != null) && (localOuyaController.getDeviceId() == paramInt)) {
        return localOuyaController;
      }
      i += 1;
    }
    return null;
  }
  
  public static OuyaController getControllerByPlayer(int paramInt)
  {
    if ((paramInt >= 0) && (paramInt < 4)) {
      return s_controllers[paramInt];
    }
    return null;
  }
  
  private static int getDeviceIdFromPlayerNum(int paramInt)
  {
    Object localObject1 = Uri.parse("content://tv.ouya.controllerdata/");
    localObject1 = appContext.getContentResolver().query((Uri)localObject1, new String[] { "input_device_id" }, "player_num = ?", new String[] { String.valueOf(paramInt) }, null);
    if (localObject1 != null) {}
    try
    {
      if (((Cursor)localObject1).moveToNext())
      {
        paramInt = ((Cursor)localObject1).getInt(0);
        return paramInt;
      }
      return -1;
    }
    finally
    {
      if (localObject1 != null) {
        ((Cursor)localObject1).close();
      }
    }
  }
  
  private static OuyaController getOrCreateControllerByDeviceId(int paramInt)
  {
    OuyaController localOuyaController = getControllerByDeviceId(paramInt);
    if (localOuyaController != null) {
      return localOuyaController;
    }
    int i = getPlayerNumFromDeviceId(paramInt);
    if ((i < 0) || (i >= 4)) {
      return null;
    }
    if (s_controllers[i] != null)
    {
      Log.e("OuyaController", "Controller for player " + i + " already found, but doesn't match device id " + "(expected " + paramInt + " got " + s_controllers[i].getDeviceId() + ")");
      return s_controllers[i];
    }
    s_controllers[i] = new OuyaController(paramInt);
    return s_controllers[i];
  }
  
  public static int getPlayerNumByDeviceId(int paramInt)
  {
    OuyaController localOuyaController = getOrCreateControllerByDeviceId(paramInt);
    if (localOuyaController == null) {
      return -1;
    }
    return localOuyaController.getPlayerNum();
  }
  
  private static int getPlayerNumFromDeviceId(int paramInt)
  {
    int i = -1;
    if (appContext == null) {
      paramInt = i;
    }
    for (;;)
    {
      return paramInt;
      Object localObject1 = Uri.parse("content://tv.ouya.controllerdata/");
      localObject1 = appContext.getContentResolver().query((Uri)localObject1, new String[] { "player_num" }, "input_device_id = ?", new String[] { String.valueOf(paramInt) }, null);
      if (localObject1 != null) {}
      try
      {
        if (((Cursor)localObject1).moveToNext())
        {
          i = ((Cursor)localObject1).getInt(0);
          paramInt = i;
          return i;
        }
        paramInt = i;
        return -1;
      }
      finally
      {
        if (localObject1 != null) {
          ((Cursor)localObject1).close();
        }
      }
    }
  }
  
  public static void init(Context paramContext)
  {
    if (appContext != null) {
      return;
    }
    appContext = paramContext.getApplicationContext();
    int i = 0;
    while (i < 4)
    {
      int j = getDeviceIdFromPlayerNum(i);
      if (j != -1) {
        getOrCreateControllerByDeviceId(j);
      }
      i += 1;
    }
    paramContext = new BroadcastReceiver()
    {
      public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
      {
        int i;
        int j;
        if (paramAnonymousIntent.getAction().equals("tv.ouya.controller.added"))
        {
          i = paramAnonymousIntent.getIntExtra("DEVICE_ID", -1);
          j = paramAnonymousIntent.getIntExtra("PLAYER_NUM", -1);
          if ((j >= 0) && (j < 4)) {}
        }
        for (;;)
        {
          return;
          if (OuyaController.s_controllers[j] != null) {
            Log.e("OuyaController", "Controller already exists for player " + j + "(new device id " + i + ", prev device id " + OuyaController.s_controllers[j].getDeviceId() + ")");
          }
          OuyaController.s_controllers[j] = new OuyaController(i);
          return;
          if (paramAnonymousIntent.getAction().equals("tv.ouya.controller.removed"))
          {
            j = paramAnonymousIntent.getIntExtra("DEVICE_ID", -1);
            i = 0;
            while (i < 4)
            {
              paramAnonymousContext = OuyaController.s_controllers[i];
              if ((paramAnonymousContext != null) && (paramAnonymousContext.getDeviceId() == j))
              {
                OuyaController.s_controllers[i] = null;
                return;
              }
              i += 1;
            }
          }
        }
      }
    };
    IntentFilter localIntentFilter = new IntentFilter();
    localIntentFilter.addAction("tv.ouya.controller.added");
    localIntentFilter.addAction("tv.ouya.controller.removed");
    appContext.registerReceiver(paramContext, localIntentFilter);
  }
  
  private int mapAxis(int paramInt)
  {
    if (this.deviceAxis.indexOfKey(paramInt) < 0) {
      return -1;
    }
    return this.deviceAxis.get(paramInt);
  }
  
  private int mapKeyCode(KeyEvent paramKeyEvent)
  {
    if (this.deviceKeycodes.indexOfKey(paramKeyEvent.getKeyCode()) < 0) {
      return -1;
    }
    return this.deviceKeycodes.get(paramKeyEvent.getKeyCode());
  }
  
  public static boolean onGenericMotionEvent(MotionEvent paramMotionEvent)
  {
    if ((paramMotionEvent.getSource() & 0x10) == 0) {}
    OuyaController localOuyaController;
    do
    {
      do
      {
        return false;
      } while (paramMotionEvent.getAction() != 2);
      localOuyaController = getOrCreateControllerByDeviceId(paramMotionEvent.getDeviceId());
    } while (localOuyaController == null);
    int[] arrayOfInt = AVAILABLE_AXES;
    int j = arrayOfInt.length;
    int i = 0;
    while (i < j)
    {
      int k = arrayOfInt[i];
      int m = localOuyaController.mapAxis(k);
      if (m >= 0)
      {
        Float localFloat = (Float)localOuyaController.axisValues.get(k);
        float f = paramMotionEvent.getAxisValue(m);
        if ((localFloat == null) || (localFloat.floatValue() != f)) {
          localOuyaController.axisValues.put(k, Float.valueOf(f));
        }
      }
      i += 1;
    }
    return true;
  }
  
  public static boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
  {
    return onKeyEvent(paramInt, paramKeyEvent);
  }
  
  private static boolean onKeyEvent(int paramInt, KeyEvent paramKeyEvent)
  {
    OuyaController localOuyaController = getOrCreateControllerByDeviceId(paramKeyEvent.getDeviceId());
    if (localOuyaController == null) {
      return false;
    }
    int i = localOuyaController.mapKeyCode(paramKeyEvent);
    if (i < 0) {
      return false;
    }
    boolean bool = false;
    switch (paramKeyEvent.getAction())
    {
    default: 
      if ((localOuyaController.buttonValues.indexOfKey(i) < 0) || (localOuyaController.buttonValues.get(i) != bool)) {
        if (!bool) {
          break label145;
        }
      }
      break;
    }
    label145:
    for (paramInt = 1;; paramInt = 2)
    {
      int j = localOuyaController.thisFrameButtonValues.get(i, 0);
      localOuyaController.thisFrameButtonValues.put(i, paramInt | j);
      localOuyaController.buttonValues.put(i, bool);
      return true;
      bool = true;
      break;
      bool = false;
      break;
    }
  }
  
  public static boolean onKeyUp(int paramInt, KeyEvent paramKeyEvent)
  {
    return onKeyEvent(paramInt, paramKeyEvent);
  }
  
  public static void setCursorBitmap(Bitmap paramBitmap, float paramFloat1, float paramFloat2)
  {
    Intent localIntent = new Intent("tv.ouya.controller.action.SET_CURSOR_BITMAP");
    if (paramBitmap != null)
    {
      localIntent.putExtra("CURSOR_BITMAP", paramBitmap);
      localIntent.putExtra("HOTSPOT_X", paramFloat1);
      localIntent.putExtra("HOTSPOT_Y", paramFloat2);
    }
    appContext.sendBroadcast(localIntent);
  }
  
  public static void showCursor(boolean paramBoolean)
  {
    if (paramBoolean) {}
    for (Object localObject = "tv.ouya.controller.action.SHOW_CURSOR";; localObject = "tv.ouya.controller.action.HIDE_CURSOR")
    {
      localObject = new Intent((String)localObject);
      appContext.sendBroadcast((Intent)localObject);
      return;
    }
  }
  
  public static void startOfFrame()
  {
    OuyaController[] arrayOfOuyaController = s_controllers;
    int j = arrayOfOuyaController.length;
    int i = 0;
    while (i < j)
    {
      OuyaController localOuyaController = arrayOfOuyaController[i];
      if (localOuyaController != null) {
        localOuyaController.startOfFrameInternal();
      }
      i += 1;
    }
  }
  
  private void startOfFrameInternal()
  {
    if (this.thisFrameButtonValues.size() > 0) {
      this.thisFrameButtonValues.clear();
    }
  }
  
  public boolean buttonChangedThisFrame(int paramInt)
  {
    return this.thisFrameButtonValues.indexOfKey(paramInt) >= 0;
  }
  
  public boolean buttonPressedThisFrame(int paramInt)
  {
    return (buttonChangedThisFrame(paramInt)) && ((this.thisFrameButtonValues.get(paramInt) & 0x1) != 0);
  }
  
  public boolean buttonReleasedThisFrame(int paramInt)
  {
    return (buttonChangedThisFrame(paramInt)) && ((this.thisFrameButtonValues.get(paramInt) & 0x2) != 0);
  }
  
  public float getAxisValue(int paramInt)
  {
    if (this.axisValues.indexOfKey(paramInt) >= 0) {
      return ((Float)this.axisValues.get(paramInt)).floatValue();
    }
    return 0.0F;
  }
  
  public boolean getButton(int paramInt)
  {
    if (this.buttonValues.indexOfKey(paramInt) >= 0) {
      return this.buttonValues.get(paramInt);
    }
    return false;
  }
  
  public InputDevice getDevice()
  {
    return this.device;
  }
  
  public int getDeviceId()
  {
    return this.deviceId;
  }
  
  public int getPlayerNum()
  {
    int i = 0;
    while (i < 4)
    {
      if (s_controllers[i] == this) {
        return i;
      }
      i += 1;
    }
    return -1;
  }
}


/* Location:              /home/enzo/Documents/hacking/malwares/viking-horde/classes.jar!/tv/ouya/console/api/OuyaController.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */