1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.controllers.android; 18 19 import android.content.Context; 20 import android.hardware.input.InputManager; 21 import android.hardware.input.InputManager.InputDeviceListener; 22 23 import com.badlogic.gdx.Gdx; 24 import com.badlogic.gdx.LifecycleListener; 25 import com.badlogic.gdx.backends.android.AndroidApplication; 26 import com.badlogic.gdx.backends.android.AndroidInput; 27 28 /** 29 * Used on +4.1 to get events on device connects/disconnects. 30 * @author mzechner 31 * 32 */ 33 public class ControllerLifeCycleListener implements LifecycleListener, InputDeviceListener { 34 private static final String TAG = "ControllerLifeCycleListener"; 35 private final InputManager inputManager; 36 private final AndroidControllers controllers; 37 38 public ControllerLifeCycleListener(AndroidControllers controllers) { 39 this.controllers = controllers; 40 this.inputManager = (InputManager)((Context)Gdx.app).getSystemService(Context.INPUT_SERVICE); 41 Gdx.app.addLifecycleListener(this); 42 inputManager.registerInputDeviceListener(this, ((AndroidApplication)Gdx.app).handler); 43 } 44 45 @Override 46 public void resume () { 47 inputManager.registerInputDeviceListener(this, ((AndroidApplication)Gdx.app).handler); 48 Gdx.app.log(TAG, "controller life cycle listener resumed"); 49 } 50 51 @Override 52 public void pause () { 53 inputManager.unregisterInputDeviceListener(this); 54 Gdx.app.log(TAG, "controller life cycle listener paused"); 55 } 56 57 @Override 58 public void onInputDeviceAdded (int deviceId) { 59 controllers.addController(deviceId, true); 60 Gdx.app.log(TAG, "device " + deviceId + " added"); 61 } 62 63 @Override 64 public void onInputDeviceRemoved (int deviceId) { 65 controllers.removeController(deviceId); 66 Gdx.app.log(TAG, "device " + deviceId + " removed"); 67 } 68 69 @Override 70 public void onInputDeviceChanged (int deviceId) { 71 } 72 73 @Override 74 public void dispose () { 75 } 76 }