Created
January 1, 2017 18:26
-
-
Save AmandaCameron/ab4b2a0a435a1d1bf1a14546c49b4ab1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.darkdna.amanda.things.thingy.rpi; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.PixelFormat; | |
import android.os.Binder; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.view.Gravity; | |
import android.view.InputDevice; | |
import android.view.KeyEvent; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.WindowManager; | |
import android.widget.ImageView; | |
import com.google.android.things.userdriver.InputDriver; | |
import com.google.android.things.userdriver.UserDriverManager; | |
/** | |
* Created by amanda on 1/1/17. | |
*/ | |
public class BackService extends Service { | |
private static final String TAG = BackService.class.getSimpleName(); | |
private static final int KEY_CODE = KeyEvent.KEYCODE_ESCAPE; | |
private WindowManager mWindowManager; | |
private ImageView mBackButton; | |
private InputDriver mDriver; | |
private BackBinder mBinder = new BackBinder(); | |
public class BackBinder extends Binder { | |
private BackBinder() { | |
} | |
public void showButton() { | |
Log.d(TAG, "showButton()"); | |
if (mBackButton == null) { | |
Log.w(TAG, "mBackButton is null."); | |
return; | |
} | |
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.TYPE_PHONE, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, | |
PixelFormat.TRANSLUCENT); | |
layoutParams.gravity = Gravity.BOTTOM | Gravity.START; | |
layoutParams.x = 0; | |
layoutParams.y = 0; | |
mWindowManager.addView(mBackButton, layoutParams); | |
} | |
public void hideButton() { | |
Log.d(TAG, "hideButton()"); | |
if (mBackButton == null) { | |
Log.w(TAG, "mBackButton is null."); | |
return; | |
} | |
mWindowManager.removeView(mBackButton); | |
} | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mBinder; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Log.d(TAG, "onCreate"); | |
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); | |
mDriver = InputDriver.builder(InputDevice.SOURCE_CLASS_BUTTON) | |
.setName("BackService") | |
.setVersion(1) | |
.setKeys(new int[]{KEY_CODE}) | |
.build(); | |
UserDriverManager.getManager().registerInputDriver(mDriver); | |
mBackButton = new ImageView(this); | |
mBackButton.setBackgroundColor(0xDD000000); | |
mBackButton.setImageResource(android.R.drawable.ic_media_previous); | |
mBackButton.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if(v != mBackButton) { | |
return false; | |
} | |
Log.d(TAG, "mBackButton pressed."); | |
if (mDriver == null) { | |
Log.w(TAG, "mDriver is null."); | |
return false; | |
} | |
if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
Log.d(TAG, "Action is ACTION_DOWN"); | |
mDriver.emit(new KeyEvent[]{ | |
new KeyEvent(KeyEvent.ACTION_DOWN, KEY_CODE), | |
}); | |
return true; | |
} else if (event.getAction() == MotionEvent.ACTION_UP) { | |
Log.d(TAG, "Action is ACTION_UP"); | |
mDriver.emit(new KeyEvent[]{ | |
new KeyEvent(KeyEvent.ACTION_UP, KEY_CODE), | |
}); | |
return true; | |
} | |
Log.w(TAG, "Unknown action."); | |
return false; | |
} | |
}); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
Log.d(TAG, "onDestroy"); | |
if (mDriver != null) { | |
UserDriverManager.getManager().unregisterInputDriver(mDriver); | |
} | |
mBinder.hideButton(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment