diff --git a/cocos/platform/android/ControllerAutoAdapter/.classpath b/cocos/platform/android/ControllerAutoAdapter/.classpath deleted file mode 100644 index 51769745b2..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/.classpath +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/cocos/platform/android/ControllerAutoAdapter/.project b/cocos/platform/android/ControllerAutoAdapter/.project deleted file mode 100644 index c49a037939..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/.project +++ /dev/null @@ -1,33 +0,0 @@ - - - libControllerAutoAdapter - - - - - - com.android.ide.eclipse.adt.ResourceManagerBuilder - - - - - com.android.ide.eclipse.adt.PreCompilerBuilder - - - - - org.eclipse.jdt.core.javabuilder - - - - - com.android.ide.eclipse.adt.ApkBuilder - - - - - - com.android.ide.eclipse.adt.AndroidNature - org.eclipse.jdt.core.javanature - - diff --git a/cocos/platform/android/ControllerAutoAdapter/.settings/org.eclipse.jdt.core.prefs b/cocos/platform/android/ControllerAutoAdapter/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b080d2ddc8..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 -org.eclipse.jdt.core.compiler.compliance=1.6 -org.eclipse.jdt.core.compiler.source=1.6 diff --git a/cocos/platform/android/ControllerAutoAdapter/AndroidManifest.xml b/cocos/platform/android/ControllerAutoAdapter/AndroidManifest.xml deleted file mode 100644 index d1f4a837d8..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/AndroidManifest.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - diff --git a/cocos/platform/android/ControllerAutoAdapter/ant.properties b/cocos/platform/android/ControllerAutoAdapter/ant.properties deleted file mode 100644 index b0971e891e..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/ant.properties +++ /dev/null @@ -1,17 +0,0 @@ -# This file is used to override default values used by the Ant build system. -# -# This file must be checked into Version Control Systems, as it is -# integral to the build system of your project. - -# This file is only used by the Ant script. - -# You can use this to override default values such as -# 'source.dir' for the location of your java source folder and -# 'out.dir' for the location of your output folder. - -# You can also use it define how the release builds are signed by declaring -# the following properties: -# 'key.store' for the location of your keystore and -# 'key.alias' for the name of the key to use. -# The password will be asked during the build when you use the 'release' target. - diff --git a/cocos/platform/android/ControllerAutoAdapter/build.xml b/cocos/platform/android/ControllerAutoAdapter/build.xml deleted file mode 100644 index 413effd90c..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/build.xml +++ /dev/null @@ -1,83 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/cocos/platform/android/ControllerAutoAdapter/libs/android-async-http-1.4.4.jar b/cocos/platform/android/ControllerAutoAdapter/libs/android-async-http-1.4.4.jar deleted file mode 100644 index 75af7015db..0000000000 Binary files a/cocos/platform/android/ControllerAutoAdapter/libs/android-async-http-1.4.4.jar and /dev/null differ diff --git a/cocos/platform/android/ControllerAutoAdapter/lint.xml b/cocos/platform/android/ControllerAutoAdapter/lint.xml deleted file mode 100644 index 55e8455701..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/lint.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/cocos/platform/android/ControllerAutoAdapter/proguard-project.txt b/cocos/platform/android/ControllerAutoAdapter/proguard-project.txt deleted file mode 100644 index f2fe1559a2..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/proguard-project.txt +++ /dev/null @@ -1,20 +0,0 @@ -# To enable ProGuard in your project, edit project.properties -# to define the proguard.config property as described in that file. -# -# Add project specific ProGuard rules here. -# By default, the flags in this file are appended to flags specified -# in ${sdk.dir}/tools/proguard/proguard-android.txt -# You can edit the include path and order by changing the ProGuard -# include property in project.properties. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# Add any project specific keep options here: - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} diff --git a/cocos/platform/android/ControllerAutoAdapter/project.properties b/cocos/platform/android/ControllerAutoAdapter/project.properties deleted file mode 100644 index 9990d6c114..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/project.properties +++ /dev/null @@ -1,16 +0,0 @@ -# This file is automatically generated by Android Tools. -# Do not modify this file -- YOUR CHANGES WILL BE ERASED! -# -# This file must be checked in Version Control Systems. -# -# To customize properties used by the Ant build system edit -# "ant.properties", and override values to adapt the script to your -# project structure. -# -# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): -#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt - -android.library=true -# Project target. -target=android-16 -android.library.reference.1=../java diff --git a/cocos/platform/android/ControllerAutoAdapter/res/.gitignore b/cocos/platform/android/ControllerAutoAdapter/res/.gitignore deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerActivity.java b/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerActivity.java deleted file mode 100644 index c38c34665c..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerActivity.java +++ /dev/null @@ -1,284 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010-2013 cocos2d-x.org - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - ****************************************************************************/ -package org.cocos2dx.lib; - -import org.cocos2dx.lib.GameControllerDelegate.ControllerEventListener; -import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat; -import org.cocos2dx.lib.inputmanagercompat.InputManagerCompat.InputDeviceListener; -import org.cocos2dx.lib.Cocos2dxActivity; - -import android.os.Bundle; -import android.view.InputDevice; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.util.Log; - -public abstract class GameControllerActivity extends Cocos2dxActivity implements InputDeviceListener { - // =========================================================== - // Constants - // =========================================================== - - private final static String TAG = GameControllerActivity.class.getSimpleName(); - - public static final int DRIVERTYPE_NIBIRU = 0; - public static final int DRIVERTYPE_MOGA = 1; - public static final int DRIVERTYPE_OUYA = 2; - public static final int DRIVERTYPE_STANDARD = 3; - public static final int DRIVERTYPE_UNKNOWN = 4; - - // =========================================================== - // Fields - // =========================================================== - private static GameControllerActivity sGameControllerActivity; - private InputManagerCompat mInputManager = null; - - protected GameControllerHelper mControllerHelper = null; - - protected GameControllerDelegate mControllerNibiru = null; - protected GameControllerDelegate mControllerMoga = null; - protected GameControllerDelegate mControllerOuya = null; - - public void connectController(){ - mControllerHelper.connectController(); - } - - public void setGameControllerInstance(GameControllerDelegate controllerDelegate, int driveType) { - if (driveType == DRIVERTYPE_NIBIRU) { - mControllerNibiru = controllerDelegate; - }else if (driveType == DRIVERTYPE_MOGA) { - mControllerMoga = controllerDelegate; - } - else if (driveType == DRIVERTYPE_OUYA) { - mControllerOuya = controllerDelegate; - } - controllerDelegate.setControllerEventListener(mControllerEventListener); - controllerDelegate.onCreate(sGameControllerActivity); - } - - public GameControllerDelegate getGameControllerDelegate(int driveType){ - if (driveType == DRIVERTYPE_NIBIRU) { - return mControllerNibiru; - }else if (driveType == DRIVERTYPE_MOGA) { - return mControllerMoga; - } - else if (driveType == DRIVERTYPE_OUYA) { - return mControllerOuya; - } - - return null; - } - - ControllerEventListener mControllerEventListener = new ControllerEventListener() { - - @Override - public void onButtonEvent(String vendorName, int controller, int button, - boolean isPressed, float value, boolean isAnalog) { - GameControllerAdapter.onButtonEvent(vendorName, controller, button, isPressed, value, isAnalog); - } - - @Override - public void onAxisEvent(String vendorName, int controller, int axisID, - float value, boolean isAnalog) { - GameControllerAdapter.onAxisEvent(vendorName, controller, axisID, value, isAnalog); - } - - @Override - public void onConnected(String vendorName, int controller) { - GameControllerAdapter.onConnected(vendorName, controller); - } - - @Override - public void onDisconnected(String vendorName, int controller) { - GameControllerAdapter.onDisconnected(vendorName, controller); - } - }; - - // =========================================================== - // Constructors - // =========================================================== - - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - sGameControllerActivity = this; - mInputManager = InputManagerCompat.Factory.getInputManager(this); - mInputManager.registerInputDeviceListener(this, null); - - if (mControllerNibiru != null) { - mControllerNibiru.onCreate(this); - } - if (mControllerMoga != null) { - mControllerMoga.onCreate(this); - } - if (mControllerOuya != null) { - mControllerOuya.onCreate(this); - } - if (mControllerHelper == null) { - mControllerHelper = new GameControllerHelper(this); - } - } - - // =========================================================== - // Getter & Setter - // =========================================================== - - // =========================================================== - // Methods for/from SuperClass/Interfaces - // =========================================================== - - @Override - public boolean dispatchKeyEvent(KeyEvent event) { - boolean handled = false; - if (mControllerNibiru != null) { - handled |= mControllerNibiru.dispatchKeyEvent(event); - } - if (mControllerMoga != null) { - handled |= mControllerMoga.dispatchKeyEvent(event); - } - if (mControllerOuya != null) { - handled |= mControllerOuya.dispatchKeyEvent(event); - } - - handled |= mControllerHelper.dispatchKeyEvent(event); - Log.d(TAG, "dispatchKeyEvent:" + handled); - - return handled || super.dispatchKeyEvent(event); - } - - @Override - public boolean dispatchGenericMotionEvent(MotionEvent event) { - boolean handled = false; - if (mControllerNibiru != null) { - handled |= mControllerNibiru.dispatchGenericMotionEvent(event); - } - if (mControllerMoga != null) { - handled |= mControllerMoga.dispatchGenericMotionEvent(event); - } - if (mControllerOuya != null) { - handled |= mControllerOuya.dispatchGenericMotionEvent(event); - } - - handled |= mControllerHelper.dispatchGenericMotionEvent(event); - - return handled || super.dispatchGenericMotionEvent(event); - } - - @Override - public void onInputDeviceAdded(int deviceId) { - - Log.d(TAG,"onInputDeviceAdded:" + deviceId); - - InputDevice device = InputDevice.getDevice(deviceId); - int deviceSource = device.getSources(); - - if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) - || ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ) - { - GameControllerAdapter.onConnected("Standard", deviceId); - } - } - /* - * This is an unusual case. Input devices don't typically change, but they - * certainly can --- for example a device may have different modes. We use - * this to make sure that the ship has an up-to-date InputDevice. - * - * @see - * com.example.inputmanagercompat.InputManagerCompat.InputDeviceListener - * #onInputDeviceChanged(int) - */ - @Override - public void onInputDeviceChanged(int deviceId) { - Log.d(TAG,"onInputDeviceChanged:" + deviceId); - } - - /* - * Remove any ship associated with the ID. - * - * @see - * com.example.inputmanagercompat.InputManagerCompat.InputDeviceListener - * #onInputDeviceRemoved(int) - */ - @Override - public void onInputDeviceRemoved(int deviceId) { - Log.d(TAG,"onInputDeviceRemoved:" + deviceId); - - InputDevice device = InputDevice.getDevice(deviceId); - int deviceSource = device.getSources(); - - if ( ((deviceSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) - || ((deviceSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ) - { - GameControllerAdapter.onDisconnected("Standard", deviceId); - } - } - - @Override - protected void onResume() { - super.onResume(); - - if (mControllerNibiru != null) { - mControllerNibiru.onResume(); - } - if (mControllerMoga != null) { - mControllerMoga.onResume(); - } - if (mControllerOuya != null) { - mControllerOuya.onResume(); - } - } - - @Override - protected void onPause() { - if (mControllerNibiru != null) { - mControllerNibiru.onPause(); - } - if (mControllerMoga != null) { - mControllerMoga.onPause(); - } - if (mControllerOuya != null) { - mControllerOuya.onPause(); - } - - super.onPause(); - } - - @Override - protected void onDestroy() { - if (mControllerNibiru != null) { - mControllerNibiru.onDestroy(); - } - if (mControllerMoga != null) { - mControllerMoga.onDestroy(); - } - if (mControllerOuya != null) { - mControllerOuya.onDestroy(); - } - - mControllerHelper.destrory(); - - super.onDestroy(); - } - -} \ No newline at end of file diff --git a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerHelper.java b/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerHelper.java deleted file mode 100644 index 52480b7524..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/GameControllerHelper.java +++ /dev/null @@ -1,856 +0,0 @@ -package org.cocos2dx.lib; - -import java.io.File; -import java.io.FileOutputStream; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.apache.http.Header; - -import com.loopj.android.http.AsyncHttpClient; -import com.loopj.android.http.FileAsyncHttpResponseHandler; -import com.loopj.android.http.JsonHttpResponseHandler; - -import dalvik.system.DexClassLoader; -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.net.Uri; -import android.os.Environment; -import android.util.Log; -import android.util.SparseIntArray; -import android.view.InputDevice; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.widget.Toast; - -public class GameControllerHelper { - - private final static String TAG = GameControllerHelper.class.getSimpleName(); - - public static final String StandardControllerName = "Standard"; - public static final String[] DRIVERS_NAME = {"nibiru","moga","ouya",StandardControllerName}; - - public static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; - - SparseIntArray ControllerKeyMap; - - private static final String COCOS_CONTROLLER_URL = "http://115.28.134.83:9000/download/cocoscontroller/"; - private static final String COCOS_CONTROLLER_CONFIG = "controller.json"; - - private static final String NIBIRU_DEP_PACKAGE = "com.nibiru"; - private static final String MOGA__DEP_PACKAGE = "com.bda.pivot.mogapgp"; - - private static GameControllerActivity sGameControllerActivity; - private static GameControllerHelper sControllerHelper; - - private String mLibAdapterFileName; - private int mLibAdapterFileSize; - - private List mNibiruSupportedDrives; - private String mNibiruDepFileName; - private int mNibiruDepFileSize; - - private List mMogaSupportedDrives; - private String mMogaDepFileName; - private int mMogaDepFileSize; - - private List mOuyaSupportedDrives; - - private AsyncHttpClient mDownDepsHttpClient = null; - - private BluetoothAdapter mBluetoothAdapter = null; - private ArrayList mBluetoothDevices = null; - private SparseIntArray mDevicesDriver; - private int mClearDevices = 0; - - private String mConfigFilePath; - private String mLocalSavePath = null; - - private boolean mLazyInit = true; - private boolean mLazyConfigInit = true; - private static ControllerListener mControllerListener = null; - - public static interface ControllerListener{ - void onDownloadConfigStarted(); - void onDownloadConfigFinished(boolean isSuccess); - - void onControllerDiscoveryStarted(); - // - void onControllerDiscoveryFinish(ArrayList devices); - - void onDownloadDepsStarted(); - void onDownloadDepsProgress(int bytesWritten, int totalSize); - void onDownloadDepsFinished(boolean isSuccess); - - void onInstallDriver(String filePath); - void onConnectController(); - } - - public void setControllerListener(ControllerListener listener){ - mControllerListener = listener; - } - - private static final int AXIS_X = 0; - private static final int AXIS_Y = 1; - private static final int AXIS_Z = 11; - private static final int AXIS_RZ = 14; - public static final int AXIS_LTRIGGER = 17; - public static final int AXIS_RTRIGGER = 18; - public static final int AXIS_BRAKE = 23; - public static final int AXIS_THROTTLE = 19; - - public GameControllerHelper(GameControllerActivity activity){ - sGameControllerActivity = activity; - sControllerHelper = this; - - ControllerKeyMap = new SparseIntArray(25); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_A, GameControllerDelegate.BUTTON_A); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_B, GameControllerDelegate.BUTTON_B); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_C, GameControllerDelegate.BUTTON_C); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_X, GameControllerDelegate.BUTTON_X); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_Y, GameControllerDelegate.BUTTON_Y); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_Z, GameControllerDelegate.BUTTON_Z); - - ControllerKeyMap.put(KeyEvent.KEYCODE_DPAD_UP, GameControllerDelegate.BUTTON_DPAD_UP); - ControllerKeyMap.put(KeyEvent.KEYCODE_DPAD_DOWN, GameControllerDelegate.BUTTON_DPAD_DOWN); - ControllerKeyMap.put(KeyEvent.KEYCODE_DPAD_LEFT, GameControllerDelegate.BUTTON_DPAD_LEFT); - ControllerKeyMap.put(KeyEvent.KEYCODE_DPAD_RIGHT, GameControllerDelegate.BUTTON_DPAD_RIGHT); - ControllerKeyMap.put(KeyEvent.KEYCODE_DPAD_CENTER, GameControllerDelegate.BUTTON_DPAD_CENTER); - - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_THUMBL, GameControllerDelegate.BUTTON_LEFT_THUMBSTICK); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_THUMBR, GameControllerDelegate.BUTTON_RIGHT_THUMBSTICK); - - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_L1, GameControllerDelegate.BUTTON_LEFT_SHOULDER); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_R1, GameControllerDelegate.BUTTON_RIGHT_SHOULDER); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_L2, GameControllerDelegate.BUTTON_LEFT_TRIGGER); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_R2, GameControllerDelegate.BUTTON_RIGHT_TRIGGER); - - ControllerKeyMap.put(AXIS_X, GameControllerDelegate.THUMBSTICK_LEFT_X); - ControllerKeyMap.put(AXIS_Y, GameControllerDelegate.THUMBSTICK_LEFT_Y); - ControllerKeyMap.put(AXIS_Z, GameControllerDelegate.THUMBSTICK_RIGHT_X); - ControllerKeyMap.put(AXIS_RZ, GameControllerDelegate.THUMBSTICK_RIGHT_Y); - - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_START, GameControllerDelegate.BUTTON_START); - ControllerKeyMap.put(KeyEvent.KEYCODE_BUTTON_SELECT, GameControllerDelegate.BUTTON_SELECT); - //KEYCODE_BUTTON_MODE - mDownDepsHttpClient = new AsyncHttpClient(); - mDownDepsHttpClient.setTimeout(360 * 1000); - } - - public void connectController(){ - if (mLazyInit) { - mLazyInit = false; - mNibiruSupportedDrives = new ArrayList(30); - mMogaSupportedDrives = new ArrayList(5); - mOuyaSupportedDrives = new ArrayList(5); - - mLocalSavePath = Environment.getExternalStorageDirectory() + File.separator + "CocosGameController" + File.separator; - Log.i(TAG, "mLocalSavePath:" + mLocalSavePath); - mConfigFilePath = sGameControllerActivity.getFilesDir().getAbsolutePath() + File.separator + COCOS_CONTROLLER_CONFIG; - Log.i(TAG, "mConfigFilePath:" + mConfigFilePath); - - } - - if (mControllerListener != null) { - mControllerListener.onDownloadConfigStarted(); - } - if (mLazyConfigInit) { - //if (mDownDepsHttpClient != null) { - // mDownDepsHttpClient.cancelRequests(sGameControllerActivity, true); - //} - requestControllerConfig(); - } - else { - scanBluetoothDrive(); - } - } - - public Set getBondedDevices(){ - if (mBluetoothAdapter == null) { - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (mBluetoothAdapter == null) { - return null; - } - } - - return mBluetoothAdapter.getBondedDevices(); - } - - public void destrory(){ - if (mDownDepsHttpClient != null) { - mDownDepsHttpClient.cancelRequests(sGameControllerActivity, true); - } - } - - private boolean scanBluetoothDrive(){ - if (mControllerListener != null) { - mControllerListener.onDownloadConfigFinished(true); - } - - if (mBluetoothAdapter == null) { - mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - if (mBluetoothAdapter == null) { - return false; - } - - if (mBluetoothDevices == null) { - mBluetoothDevices = new ArrayList(5); - mDevicesDriver = new SparseIntArray(); - } - - IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); - //filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); - //filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); - //filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); - filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); - sGameControllerActivity.registerReceiver(mBluetoothReceiver, filter); - - IntentFilter appFilter = new IntentFilter(); - appFilter.addAction("android.intent.action.PACKAGE_ADDED"); - appFilter.addDataScheme("package"); - sGameControllerActivity.registerReceiver(mAppReceiver, appFilter); - } - - if (!mBluetoothAdapter.isEnabled()) { - mBluetoothAdapter.enable(); - } - - if (mBluetoothAdapter.isDiscovering()) { - mBluetoothAdapter.cancelDiscovery(); - } - - mBluetoothAdapter.startDiscovery(); - - return true; - } - - public int checkDriverType(BluetoothDevice device){ - String deviceName = device.getName(); - - if (mNibiruSupportedDrives.contains(deviceName)) { - return GameControllerActivity.DRIVERTYPE_NIBIRU; - } - else if (mMogaSupportedDrives.contains(deviceName)) { - return GameControllerActivity.DRIVERTYPE_MOGA; - } - else if (mOuyaSupportedDrives.contains(deviceName)) { - return GameControllerActivity.DRIVERTYPE_OUYA; - } - else { - - } - - return GameControllerActivity.DRIVERTYPE_UNKNOWN; - } - - public static void installApplication(String filePath){ - if (sGameControllerActivity != null) { - Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive"); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - sGameControllerActivity.startActivity(intent); - } - } - - public static boolean checkApplication(String packName){ - try { - ApplicationInfo applicationInfo = sGameControllerActivity.getPackageManager().getApplicationInfo(packName, PackageManager.GET_UNINSTALLED_PACKAGES); - Log.d(TAG, applicationInfo.toString()); - return true; - } catch (NameNotFoundException e) { - return false; - } - } - - private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - - if (BluetoothDevice.ACTION_FOUND.equals(action)) { - BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - if (mBluetoothDevices.contains(device)) { - return; - } - Log.d(TAG, "Remote device discovered :" + device.getName()); - //We can't ensure non-controller can be filtered out.Some game controller marked with computer class. - /*String deviceName = device.getName(); - - if(device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.COMPUTER - || device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.PHONE) - { - Log.w(TAG, "Remote device discovered :" + deviceName + " is computer or phone." + device.getBluetoothClass().getMajorDeviceClass()); - return; - }*/ - - mBluetoothDevices.add(device); - int type = checkDriverType(device); - if (type != GameControllerActivity.DRIVERTYPE_UNKNOWN) { - mTargetDriverType = type; - mClearDevices += 1; - } - mDevicesDriver.append(mBluetoothDevices.size() - 1, type); - } - else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { - Log.d(TAG, "The local Bluetooth adapter has finished the device discovery process."); - - if (mControllerListener != null) { - mControllerListener.onControllerDiscoveryFinish(mBluetoothDevices); - } - else { - if (mBluetoothDevices.size() == 0) { - if (checkApplication(NIBIRU_DEP_PACKAGE)) { - downControllerDeps(GameControllerActivity.DRIVERTYPE_NIBIRU); - } - Log.w(TAG, "Not found any supported bluetooth game controller!"); - }else { - if (mClearDevices == 1 ) { - downControllerDeps(mTargetDriverType); - } - else { - Log.i(TAG, "Not clear target!"); - if (checkApplication(NIBIRU_DEP_PACKAGE)) { - downControllerDeps(GameControllerActivity.DRIVERTYPE_NIBIRU); - } - //todo:show sel - } - } - } - } - else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { - Log.d(TAG, "The local Bluetooth adapter has started the remote device discovery process."); - if(mControllerListener != null){ - mControllerListener.onControllerDiscoveryStarted(); - } - - mBluetoothDevices.clear(); - mDevicesDriver.clear(); - mClearDevices = 0; - mTargetDriverType = GameControllerActivity.DRIVERTYPE_UNKNOWN; - - //check moga controller - Iterator it = mBluetoothAdapter.getBondedDevices().iterator(); - while (it.hasNext()) { - BluetoothDevice device = it.next(); - //if(device.getBluetoothClass().getMajorDeviceClass() != BluetoothClass.Device.Major.COMPUTER - // && device.getBluetoothClass().getMajorDeviceClass() != BluetoothClass.Device.Major.PHONE) - { - if (!mBluetoothDevices.contains(device)) { - mBluetoothDevices.add(device); - int type = checkDriverType(device); - Log.d(TAG, "BluetoothDevice objects that are bonded (paired) to the local adapter :" + device.getName()); - if (type != GameControllerActivity.DRIVERTYPE_UNKNOWN) { - mClearDevices += 1; - mTargetDriverType = type; - } - mDevicesDriver.append(mBluetoothDevices.size() - 1, type); - } - } - } - } - } - }; - - private static int depsCount = 0; - private static int mTargetDriverType = GameControllerActivity.DRIVERTYPE_UNKNOWN; - - private static int mDownloadTotalSize; - private static int mDownloadLibSize; - private static int mLibDownloadCompletedSize; - private static int mDepDownloadCompletedSize; - - public void downControllerDeps(int driverType){ - if (driverType != GameControllerActivity.DRIVERTYPE_NIBIRU - && driverType != GameControllerActivity.DRIVERTYPE_MOGA - && driverType != GameControllerActivity.DRIVERTYPE_OUYA) { - return; - } - - if (mControllerListener != null) { - mControllerListener.onDownloadDepsStarted(); - } - - mDownloadTotalSize = 0; - mLibDownloadCompletedSize = 0; - mDepDownloadCompletedSize = 0; - - mTargetDriverType = driverType; - depsCount = 1; - - GameControllerUtils.ensureDirectoryExist(mLocalSavePath); - - String remoteDir = COCOS_CONTROLLER_URL + DRIVERS_NAME[driverType] + File.separator; - - if (driverType == GameControllerActivity.DRIVERTYPE_NIBIRU) { - if (!checkApplication(NIBIRU_DEP_PACKAGE)) { - depsCount += 1; - mDownloadTotalSize += mNibiruDepFileSize; - mDownDepsHttpClient.get(remoteDir + mNibiruDepFileName, new MyDepsAsyncHandler( - new File(mLocalSavePath + mNibiruDepFileName), MyDepsAsyncHandler.FILETYPE_DEP_APK)); - } - } - else if (driverType == GameControllerActivity.DRIVERTYPE_MOGA) { - if (!checkApplication(MOGA__DEP_PACKAGE)) { - mDownloadTotalSize += mMogaDepFileSize; - depsCount += 1; - mDownDepsHttpClient.get(remoteDir + mMogaDepFileName, new MyDepsAsyncHandler( - new File(mLocalSavePath + mMogaDepFileName), MyDepsAsyncHandler.FILETYPE_DEP_APK)); - } - } - else if(driverType == GameControllerActivity.DRIVERTYPE_OUYA){ - - } - - File libFile = new File(mLocalSavePath + mLibAdapterFileName); - if (libFile.exists() && libFile.length() == mLibAdapterFileSize) { - depsCount -= 1; - if (depsCount == 0) { - onDepsReady(); - } - }else { - mDownloadLibSize = mLibAdapterFileSize; - mDownloadTotalSize += mDownloadLibSize; - mDownDepsHttpClient.get(COCOS_CONTROLLER_URL + mLibAdapterFileName, new MyDepsAsyncHandler( - new File(mLocalSavePath + mLibAdapterFileName), MyDepsAsyncHandler.FILETYPE_JAR)); - } - } - - static class MyDepsAsyncHandler extends FileAsyncHttpResponseHandler{ - - public static final int FILETYPE_DEP_APK = 0; - public static final int FILETYPE_JAR = 1; - - private int mFileType = FILETYPE_DEP_APK; - - public MyDepsAsyncHandler(File file, int fileType) { - super(file); - mFileType = fileType; - } - - @Override - public void onFailure(int statusCode, Header[] headers, - Throwable e, File file) { - if (mFileType == FILETYPE_JAR) { - if (file.exists() && file.length() == mDownloadLibSize) { - depsCount -= 1; - if (depsCount == 0) { - if (mControllerListener != null) { - mControllerListener.onDownloadDepsFinished(true); - } - sControllerHelper.onDepsReady(); - } - } - } - else if (mFileType == FILETYPE_DEP_APK) { - if (mControllerListener != null) { - mControllerListener.onDownloadDepsFinished(false); - } - } - - Log.e(TAG,"Failed to download:" + file.getName()); - } - - @Override - public void onProgress(int bytesWritten, int totalSize) { - if (mFileType == FILETYPE_JAR) { - mLibDownloadCompletedSize = bytesWritten; - } else { - mDepDownloadCompletedSize = bytesWritten; - } - if (mControllerListener != null) { - - mControllerListener.onDownloadDepsProgress(mLibDownloadCompletedSize + mDepDownloadCompletedSize, mDownloadTotalSize); - } - Log.d(TAG, "totalSize:" + totalSize + ", bytesWritten:" + bytesWritten); - } - - - public void onSuccess(File file) { - Log.d(TAG, "11Down file success:" + file.getName()); - - depsCount -= 1; - if (depsCount == 0) { - if (mControllerListener != null) { - mControllerListener.onDownloadDepsFinished(true); - } - sControllerHelper.onDepsReady(); - } - super.onSuccess(file); - } - - /*@Override - public void onSuccess(int arg0, Header[] arg1, File file) { - Log.d(TAG, "22Down file success:" + file.getName()); - - depsCount -= 1; - if (depsCount == 0) { - if (mControllerListener != null) { - mControllerListener.onDownloadDepsFinished(true); - } - sControllerHelper.onDepsReady(); - } - }*/ - } - - private void onDepsReady(){ - Log.d(TAG, "onDepsReady:" + mTargetDriverType); - - if (mTargetDriverType == GameControllerActivity.DRIVERTYPE_NIBIRU) { - if (checkApplication(NIBIRU_DEP_PACKAGE)) { - createControllerInstance(mLocalSavePath + mLibAdapterFileName, mTargetDriverType); - } - else { - if (mControllerListener != null) { - mControllerListener.onInstallDriver(mLocalSavePath + mMogaDepFileName); - } - installApplication(mLocalSavePath + mNibiruDepFileName); - } - } - else if (mTargetDriverType == GameControllerActivity.DRIVERTYPE_MOGA) { - if (checkApplication(MOGA__DEP_PACKAGE)) { - createControllerInstance(mLocalSavePath + mLibAdapterFileName, mTargetDriverType); - } - else { - if (mControllerListener != null) { - mControllerListener.onInstallDriver(mLocalSavePath + mMogaDepFileName); - } - installApplication(mLocalSavePath + mMogaDepFileName); - } - } - else if (mTargetDriverType == GameControllerActivity.DRIVERTYPE_OUYA) { - createControllerInstance(mLocalSavePath + mLibAdapterFileName, mTargetDriverType); - } - } - - private static final String CONFIGKEY_DRIVES = "drives"; - private static final String CONFIGKEY_LIBADAPTER_FILENAME = "adapter-file"; - private static final String CONFIGKEY_LIBADAPTER_FILESIZE = "adapter-filesize"; - private static final String CONFIGKEY_DEP_FILENAME = "dep-apk"; - private static final String CONFIGKEY_DEP_FILESIZE = "dep-size"; - - private boolean parseConfig(String jsonString){ - mMogaSupportedDrives.clear(); - mNibiruSupportedDrives.clear(); - mOuyaSupportedDrives.clear(); - - try { - int tint = 9879; - JSONObject jsonObject = new JSONObject(); - jsonObject.put("intvalue",tint); - Log.w(TAG, "intJson:" + jsonObject); - - JSONObject configObject = new JSONObject(jsonString); - mLibAdapterFileName = configObject.getString(CONFIGKEY_LIBADAPTER_FILENAME); - mLibAdapterFileSize = configObject.getInt(CONFIGKEY_LIBADAPTER_FILESIZE); - - JSONObject nibiruObject = configObject.getJSONObject("nibiru"); - JSONArray drives = nibiruObject.getJSONArray(CONFIGKEY_DRIVES); - int count = drives.length(); - for (int i = 0; i < count; i++) { - mNibiruSupportedDrives.add(drives.getString(i)); - } - mNibiruDepFileName = nibiruObject.getString(CONFIGKEY_DEP_FILENAME); - mNibiruDepFileSize = nibiruObject.getInt(CONFIGKEY_DEP_FILESIZE); - - JSONObject mogaObject = configObject.getJSONObject("moga"); - drives = mogaObject.getJSONArray(CONFIGKEY_DRIVES); - count = drives.length(); - for (int i = 0; i < count; i++) { - mMogaSupportedDrives.add(drives.getString(i)); - } - mMogaDepFileName = mogaObject.getString(CONFIGKEY_DEP_FILENAME); - mMogaDepFileSize = mogaObject.getInt(CONFIGKEY_DEP_FILESIZE); - - JSONObject ouyaObject = configObject.getJSONObject("ouya"); - drives = ouyaObject.getJSONArray(CONFIGKEY_DRIVES); - count = drives.length(); - for (int i = 0; i < count; i++) { - mOuyaSupportedDrives.add(drives.getString(i)); - } - - mLazyConfigInit = false; - return true; - } catch (Exception e1) { - e1.printStackTrace(); - return false; - } - } - - private void requestControllerConfig() { - - final JsonHttpResponseHandler configResponseHandler = new JsonHttpResponseHandler() { - - @Override - public void onFailure(int statusCode, Header[] headers, - String responseBody, Throwable e) { - - Log.e(TAG, "Failed to download game controller config!"); - - String configJSON = GameControllerUtils.readJsonFile(mConfigFilePath); - if (configJSON != null) { - if (parseConfig(configJSON)) { - scanBluetoothDrive(); - return; - } - } - if (mControllerListener != null) { - mControllerListener.onDownloadConfigFinished(false); - } - /*new AlertDialog.Builder(sCocos2dxActivity) - .setTitle("Loading controller config failed!") - .setMessage( - "Please make sure internet connection works ok!") - .setPositiveButton("Retry", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, - int which) { - dialog.dismiss(); - requestControllerConfig(); - } - }).setCancelable(false).show();*/ - } - - @Override - public void onSuccess(int statusCode, Header[] headers, - String responseBody) { - - String jsonString = responseBody.trim(); - - try { - if (parseConfig(jsonString)) { - scanBluetoothDrive(); - showToast("Get controller config succeed!"); - - File configFile = new File(mConfigFilePath); - FileOutputStream outputStream = new FileOutputStream(configFile); - byte[] contentString = jsonString.getBytes(); - outputStream.write(contentString, 0, contentString.length); - outputStream.flush(); - outputStream.close(); - return; - } - else { - String jsonStr = GameControllerUtils.readJsonFile(mConfigFilePath); - if (jsonStr != null) { - if (parseConfig(jsonStr)) { - scanBluetoothDrive(); - showToast("Get controller config succeed!"); - return; - } - } - } - } catch (Exception e1) { - e1.printStackTrace(); - } - - if (mControllerListener != null) { - mControllerListener.onDownloadConfigFinished(false); - } - } - }; - - mDownDepsHttpClient.get(COCOS_CONTROLLER_URL + COCOS_CONTROLLER_CONFIG, configResponseHandler); - } - - private static void showToast(String message){ - Toast.makeText(sGameControllerActivity, message, Toast.LENGTH_SHORT).show(); - } - - private static void createControllerInstance(String libFilePath,int driveType) { - //if (sGameControllerActivity.getGameControllerDelegate(driveType) != null) { - // return; - //} - - File libFile = new File(libFilePath); - if (! libFile.exists()) { - Log.w(TAG, libFile.toString() + "not exist!"); - return; - } - - DexClassLoader classLoader = null; - - try { - File dexOutputDir = sGameControllerActivity.getDir("dex", Context.MODE_PRIVATE); - classLoader = new DexClassLoader(libFile.getCanonicalPath(), dexOutputDir.getCanonicalPath(), - null, sGameControllerActivity.getClassLoader()); - } catch (Exception e1) { - e1.printStackTrace(); - } - - try { - Class controllerDelegate = null; - if (driveType == GameControllerActivity.DRIVERTYPE_MOGA) { - controllerDelegate = classLoader.loadClass("org.cocos2dx.lib.GameControllerMoga"); - } else if (driveType == GameControllerActivity.DRIVERTYPE_NIBIRU) { - controllerDelegate = classLoader.loadClass("org.cocos2dx.lib.GameControllerNibiru"); - } else if (driveType == GameControllerActivity.DRIVERTYPE_OUYA) { - controllerDelegate = classLoader.loadClass("org.cocos2dx.lib.GameControllerOuya"); - } - - GameControllerDelegate instance = (GameControllerDelegate)controllerDelegate.newInstance(); - - if (mControllerListener != null) { - mControllerListener.onConnectController(); - } - sGameControllerActivity.setGameControllerInstance(instance, driveType); - if (driveType == GameControllerActivity.DRIVERTYPE_NIBIRU) { - Method method = controllerDelegate.getDeclaredMethod("onResume"); - method.invoke(instance); - } - } - catch (Exception e) { - e.printStackTrace(); - } - } - - private BroadcastReceiver mAppReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - String packageName = intent.getDataString(); - Log.d(TAG, "mAppReceiver:" + intent); - if (packageName.contains(NIBIRU_DEP_PACKAGE)) { - createControllerInstance(mLocalSavePath + mLibAdapterFileName, GameControllerActivity.DRIVERTYPE_NIBIRU); - } - else if (packageName.contains(MOGA__DEP_PACKAGE)) { - createControllerInstance(mLocalSavePath + mLibAdapterFileName, GameControllerActivity.DRIVERTYPE_MOGA); - } - } - }; - - private float mOldLeftThumbstickX = 0.0f; - private float mOldLeftThumbstickY = 0.0f; - private float mOldRightThumbstickX = 0.0f; - private float mOldRightThumbstickY = 0.0f; - - private float mOldLeftTrigger = 0.0f; - private float mOldRightTrigger = 0.0f; - private float mOldThrottle = 0.0f; - private float mOldBrake = 0.0f; - - public boolean dispatchGenericMotionEvent(MotionEvent event) { - boolean handled = false; - - int eventSource = event.getSource(); - - if ( ((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) - || ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) ) - { - if (event.getAction() == MotionEvent.ACTION_MOVE) { - int devicedId = event.getDeviceId(); - - float newAXIS_LX = event.getAxisValue(AXIS_X); - if (Float.compare(newAXIS_LX , mOldLeftThumbstickX) != 0) { - GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_X, newAXIS_LX, true); - mOldLeftThumbstickX = newAXIS_LX; - handled = true; - } - - float newAXIS_LY = event.getAxisValue(AXIS_Y); - if (Float.compare(newAXIS_LY , mOldLeftThumbstickY) != 0) { - GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_LEFT_Y, newAXIS_LY, true); - mOldLeftThumbstickY = newAXIS_LY; - handled = true; - } - - float newAXIS_RX = event.getAxisValue(AXIS_Z); - if (Float.compare(newAXIS_RX , mOldRightThumbstickX) != 0) { - GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_X, newAXIS_RX, true); - mOldRightThumbstickX = newAXIS_RX; - handled = true; - } - - float newAXIS_RY = event.getAxisValue(AXIS_RZ); - if (Float.compare(newAXIS_RY , mOldRightThumbstickY) != 0) { - GameControllerAdapter.onAxisEvent(StandardControllerName, devicedId, GameControllerDelegate.THUMBSTICK_RIGHT_Y, newAXIS_RY, true); - mOldRightThumbstickY = newAXIS_RY; - handled = true; - } - - float newAXIS_LTRIGGER = event.getAxisValue(AXIS_LTRIGGER); - if (Float.compare(newAXIS_LTRIGGER , mOldLeftTrigger) != 0) { - if (Float.compare(newAXIS_LTRIGGER, 0.0f) == 0) { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, false, 0.0f, true); - }else { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, true, newAXIS_LTRIGGER, true); - } - mOldLeftTrigger = newAXIS_LTRIGGER; - handled = true; - } - - float newAXIS_RTRIGGER = event.getAxisValue(AXIS_RTRIGGER); - if (Float.compare(newAXIS_RTRIGGER , mOldRightTrigger) != 0) { - if (Float.compare(newAXIS_RTRIGGER, 0.0f) == 0) { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true); - }else { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_RTRIGGER, true); - } - mOldRightTrigger = newAXIS_RTRIGGER; - handled = true; - } - - float newAXIS_BRAKE = event.getAxisValue(AXIS_BRAKE); - if (Float.compare(newAXIS_BRAKE , mOldBrake) != 0) { - if (Float.compare(newAXIS_BRAKE, 0.0f) == 0) { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, false, 0.0f, true); - }else { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_LEFT_TRIGGER, true, newAXIS_BRAKE, true); - } - mOldBrake = newAXIS_BRAKE; - handled = true; - } - - float newAXIS_THROTTLE = event.getAxisValue(AXIS_THROTTLE); - if (Float.compare(newAXIS_THROTTLE , mOldThrottle) != 0) { - if (Float.compare(newAXIS_THROTTLE, 0.0f) == 0) { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, false, 0.0f, true); - }else { - GameControllerAdapter.onButtonEvent(StandardControllerName, devicedId, GameControllerDelegate.BUTTON_RIGHT_TRIGGER, true, newAXIS_THROTTLE, true); - } - mOldThrottle = newAXIS_THROTTLE; - handled = true; - } - } - } - - return handled; - } - - public boolean dispatchKeyEvent(KeyEvent event) { - boolean handled = false; - - int eventSource = event.getSource(); - int controllerKey = ControllerKeyMap.get(event.getKeyCode()); - - if (controllerKey != 0 && (((eventSource & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) - || ((eventSource & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) - || ((eventSource & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD))) - { - int action = event.getAction(); - if (action == KeyEvent.ACTION_DOWN) { - handled = true; - GameControllerAdapter.onButtonEvent(StandardControllerName,event.getDeviceId(), controllerKey,true, 1.0f, false); - }else if (action == KeyEvent.ACTION_UP) { - handled = true; - GameControllerAdapter.onButtonEvent(StandardControllerName,event.getDeviceId(), controllerKey,false, 0.0f, false); - } - } - - return handled; - } -} diff --git a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerCompat.java b/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerCompat.java deleted file mode 100644 index 65b919bdae..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerCompat.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.cocos2dx.lib.inputmanagercompat; - -import android.content.Context; -import android.os.Build; -import android.os.Handler; -import android.view.InputDevice; -import android.view.MotionEvent; - -public interface InputManagerCompat { - /** - * Gets information about the input device with the specified id. - * - * @param id The device id - * @return The input device or null if not found - */ - public InputDevice getInputDevice(int id); - - /** - * Gets the ids of all input devices in the system. - * - * @return The input device ids. - */ - public int[] getInputDeviceIds(); - - /** - * Registers an input device listener to receive notifications about when - * input devices are added, removed or changed. - * - * @param listener The listener to register. - * @param handler The handler on which the listener should be invoked, or - * null if the listener should be invoked on the calling thread's - * looper. - */ - public void registerInputDeviceListener(InputManagerCompat.InputDeviceListener listener, - Handler handler); - - /** - * Unregisters an input device listener. - * - * @param listener The listener to unregister. - */ - public void unregisterInputDeviceListener(InputManagerCompat.InputDeviceListener listener); - - /* - * The following three calls are to simulate V16 behavior on pre-Jellybean - * devices. If you don't call them, your callback will never be called - * pre-API 16. - */ - - /** - * Pass the motion events to the InputManagerCompat. This is used to - * optimize for polling for controllers. If you do not pass these events in, - * polling will cause regular object creation. - * - * @param event the motion event from the app - */ - public void onGenericMotionEvent(MotionEvent event); - - /** - * Tell the V9 input manager that it should stop polling for disconnected - * devices. You can call this during onPause in your activity, although you - * might want to call it whenever your game is not active (or whenever you - * don't care about being notified of new input devices) - */ - public void onPause(); - - /** - * Tell the V9 input manager that it should start polling for disconnected - * devices. You can call this during onResume in your activity, although you - * might want to call it less often (only when the gameplay is actually - * active) - */ - public void onResume(); - - public interface InputDeviceListener { - /** - * Called whenever the input manager detects that a device has been - * added. This will only be called in the V9 version when a motion event - * is detected. - * - * @param deviceId The id of the input device that was added. - */ - void onInputDeviceAdded(int deviceId); - - /** - * Called whenever the properties of an input device have changed since - * they were last queried. This will not be called for the V9 version of - * the API. - * - * @param deviceId The id of the input device that changed. - */ - void onInputDeviceChanged(int deviceId); - - /** - * Called whenever the input manager detects that a device has been - * removed. For the V9 version, this can take some time depending on the - * poll rate. - * - * @param deviceId The id of the input device that was removed. - */ - void onInputDeviceRemoved(int deviceId); - } - - /** - * Use this to construct a compatible InputManager. - */ - public static class Factory { - - /** - * Constructs and returns a compatible InputManger - * - * @param context the Context that will be used to get the system - * service from - * @return a compatible implementation of InputManager - */ - public static InputManagerCompat getInputManager(Context context) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - return new InputManagerV16(context); - } else { - return new InputManagerV9(); - } - } - } -} diff --git a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV16.java b/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV16.java deleted file mode 100644 index f0d265adb0..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV16.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.cocos2dx.lib.inputmanagercompat; - -import android.annotation.TargetApi; -import android.content.Context; -import android.hardware.input.InputManager; -import android.os.Build; -import android.os.Handler; -import android.view.InputDevice; -import android.view.MotionEvent; - -import java.util.HashMap; -import java.util.Map; - -@TargetApi(Build.VERSION_CODES.JELLY_BEAN) -public class InputManagerV16 implements InputManagerCompat { - - private final InputManager mInputManager; - private final Map mListeners; - - public InputManagerV16(Context context) { - mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE); - mListeners = new HashMap(); - } - - @Override - public InputDevice getInputDevice(int id) { - return mInputManager.getInputDevice(id); - } - - @Override - public int[] getInputDeviceIds() { - return mInputManager.getInputDeviceIds(); - } - - static class V16InputDeviceListener implements InputManager.InputDeviceListener { - final InputManagerCompat.InputDeviceListener mIDL; - - public V16InputDeviceListener(InputDeviceListener idl) { - mIDL = idl; - } - - @Override - public void onInputDeviceAdded(int deviceId) { - mIDL.onInputDeviceAdded(deviceId); - } - - @Override - public void onInputDeviceChanged(int deviceId) { - mIDL.onInputDeviceChanged(deviceId); - } - - @Override - public void onInputDeviceRemoved(int deviceId) { - mIDL.onInputDeviceRemoved(deviceId); - } - - } - - @Override - public void registerInputDeviceListener(InputDeviceListener listener, Handler handler) { - V16InputDeviceListener v16Listener = new V16InputDeviceListener(listener); - mInputManager.registerInputDeviceListener(v16Listener, handler); - mListeners.put(listener, v16Listener); - } - - @Override - public void unregisterInputDeviceListener(InputDeviceListener listener) { - V16InputDeviceListener curListener = mListeners.remove(listener); - if (null != curListener) - { - mInputManager.unregisterInputDeviceListener(curListener); - } - - } - - @Override - public void onGenericMotionEvent(MotionEvent event) { - // unused in V16 - } - - @Override - public void onPause() { - // unused in V16 - } - - @Override - public void onResume() { - // unused in V16 - } - -} diff --git a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV9.java b/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV9.java deleted file mode 100644 index abb47a8719..0000000000 --- a/cocos/platform/android/ControllerAutoAdapter/src/org/cocos2dx/lib/inputmanagercompat/InputManagerV9.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.cocos2dx.lib.inputmanagercompat; - -import android.os.Handler; -import android.os.Message; -import android.os.SystemClock; -import android.util.Log; -import android.util.SparseArray; -import android.view.InputDevice; -import android.view.MotionEvent; - -import java.lang.ref.WeakReference; -import java.util.ArrayDeque; -import java.util.HashMap; -import java.util.Map; -import java.util.Queue; - -public class InputManagerV9 implements InputManagerCompat { - private static final String LOG_TAG = "InputManagerV9"; - private static final int MESSAGE_TEST_FOR_DISCONNECT = 101; - private static final long CHECK_ELAPSED_TIME = 3000L; - - private static final int ON_DEVICE_ADDED = 0; - private static final int ON_DEVICE_CHANGED = 1; - private static final int ON_DEVICE_REMOVED = 2; - - private final SparseArray mDevices; - private final Map mListeners; - private final Handler mDefaultHandler; - - private static class PollingMessageHandler extends Handler { - private final WeakReference mInputManager; - - PollingMessageHandler(InputManagerV9 im) { - mInputManager = new WeakReference(im); - } - - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - switch (msg.what) { - case MESSAGE_TEST_FOR_DISCONNECT: - InputManagerV9 imv = mInputManager.get(); - if (null != imv) { - long time = SystemClock.elapsedRealtime(); - int size = imv.mDevices.size(); - for (int i = 0; i < size; i++) { - long[] lastContact = imv.mDevices.valueAt(i); - if (null != lastContact) { - if (time - lastContact[0] > CHECK_ELAPSED_TIME) { - // check to see if the device has been - // disconnected - int id = imv.mDevices.keyAt(i); - if (null == InputDevice.getDevice(id)) { - // disconnected! - imv.notifyListeners(ON_DEVICE_REMOVED, id); - imv.mDevices.remove(id); - } else { - lastContact[0] = time; - } - } - } - } - sendEmptyMessageDelayed(MESSAGE_TEST_FOR_DISCONNECT, - CHECK_ELAPSED_TIME); - } - break; - } - } - - } - - public InputManagerV9() { - mDevices = new SparseArray(); - mListeners = new HashMap(); - mDefaultHandler = new PollingMessageHandler(this); - // as a side-effect, populates our collection of watched - // input devices - getInputDeviceIds(); - } - - @Override - public InputDevice getInputDevice(int id) { - return InputDevice.getDevice(id); - } - - @Override - public int[] getInputDeviceIds() { - // add any hitherto unknown devices to our - // collection of watched input devices - int[] activeDevices = InputDevice.getDeviceIds(); - long time = SystemClock.elapsedRealtime(); - for ( int id : activeDevices ) { - long[] lastContact = mDevices.get(id); - if ( null == lastContact ) { - // we have a new device - mDevices.put(id, new long[] { time }); - } - } - return activeDevices; - } - - @Override - public void registerInputDeviceListener(InputDeviceListener listener, Handler handler) { - mListeners.remove(listener); - if (handler == null) { - handler = mDefaultHandler; - } - mListeners.put(listener, handler); - } - - @Override - public void unregisterInputDeviceListener(InputDeviceListener listener) { - mListeners.remove(listener); - } - - private void notifyListeners(int why, int deviceId) { - // the state of some device has changed - if (!mListeners.isEmpty()) { - // yes... this will cause an object to get created... hopefully - // it won't happen very often - for (InputDeviceListener listener : mListeners.keySet()) { - Handler handler = mListeners.get(listener); - DeviceEvent odc = DeviceEvent.getDeviceEvent(why, deviceId, listener); - handler.post(odc); - } - } - } - - private static class DeviceEvent implements Runnable { - private int mMessageType; - private int mId; - private InputDeviceListener mListener; - private static Queue sEventQueue = new ArrayDeque(); - - private DeviceEvent() { - } - - static DeviceEvent getDeviceEvent(int messageType, int id, - InputDeviceListener listener) { - DeviceEvent curChanged = sEventQueue.poll(); - if (null == curChanged) { - curChanged = new DeviceEvent(); - } - curChanged.mMessageType = messageType; - curChanged.mId = id; - curChanged.mListener = listener; - return curChanged; - } - - @Override - public void run() { - switch (mMessageType) { - case ON_DEVICE_ADDED: - mListener.onInputDeviceAdded(mId); - break; - case ON_DEVICE_CHANGED: - mListener.onInputDeviceChanged(mId); - break; - case ON_DEVICE_REMOVED: - mListener.onInputDeviceRemoved(mId); - break; - default: - Log.e(LOG_TAG, "Unknown Message Type"); - break; - } - // dump this runnable back in the queue - sEventQueue.offer(this); - } - } - - @Override - public void onGenericMotionEvent(MotionEvent event) { - // detect new devices - int id = event.getDeviceId(); - long[] timeArray = mDevices.get(id); - if (null == timeArray) { - notifyListeners(ON_DEVICE_ADDED, id); - timeArray = new long[1]; - mDevices.put(id, timeArray); - } - long time = SystemClock.elapsedRealtime(); - timeArray[0] = time; - } - - @Override - public void onPause() { - mDefaultHandler.removeMessages(MESSAGE_TEST_FOR_DISCONNECT); - } - - @Override - public void onResume() { - mDefaultHandler.sendEmptyMessage(MESSAGE_TEST_FOR_DISCONNECT); - } - -}