Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
android:screenOrientation="portrait"/>
<activity android:name=".TouchListenerActivity"
android:screenOrientation="portrait"/>
<activity android:name=".HoverEmulatorActivity" />
</application>

</manifest>
11 changes: 11 additions & 0 deletions res/layout/hover_emulator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.examples.customtouch.HoverEmulatorButton
android:id="@+id/button_hover"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Hover" />
</FrameLayout>
45 changes: 45 additions & 0 deletions src/com/examples/customtouch/HoverEmulatorActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.examples.customtouch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
* Created by Dave Smith
* Xcellent Creations, Inc.
* Date: 12/5/12
* HoverEmulatorActivity
* Transform touch events into hover events
*/
public class HoverEmulatorActivity extends Activity implements View.OnHoverListener {

public static final String TAG = "HoverEmulatorActivity";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hover_emulator);

findViewById(R.id.button_hover).setOnHoverListener(this);
}

@Override
public boolean onHover(View v, MotionEvent event) {
Log.i(TAG, "Hover Event "+getHoverName(event));
return false;
}

private String getHoverName(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
return "HOVER_ENTER";
case MotionEvent.ACTION_HOVER_MOVE:
return "HOVER_MOVE";
case MotionEvent.ACTION_HOVER_EXIT:
return "HOVER_EXIT";
default:
return "";
}
}
}
99 changes: 99 additions & 0 deletions src/com/examples/customtouch/HoverEmulatorButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.examples.customtouch;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

/**
* Created by Dave Smith
* Xcellent Creations, Inc.
* Date: 12/5/12
* HoverEmulatorButton
* Button that transforms single pointer events into hovers and multitouch events into taps
*/
public class HoverEmulatorButton extends Button {

public HoverEmulatorButton(Context context) {
super(context);
}

public HoverEmulatorButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public HoverEmulatorButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getPointerCount()) {
case 1:
handleSingleTouchEvent(event);
break;
default:
handleMultitouchEvent(event);
break;
}

return true;
}

private void handleSingleTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
event.setAction(MotionEvent.ACTION_HOVER_ENTER);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
event.setAction(MotionEvent.ACTION_HOVER_EXIT);
break;
case MotionEvent.ACTION_MOVE:
event.setAction(MotionEvent.ACTION_HOVER_MOVE);
break;
default:
//Ignore unknown events
return;
}

//Forward the converted event to the proper callback
super.dispatchGenericMotionEvent(event);
}

/*
* Forward all events with multiple fingers to super as if they were single pointer
* touch events
*/
private void handleMultitouchEvent(MotionEvent event) {
//Construct a new event that only contains a single pointer
MotionEvent newEvent = MotionEvent.obtain(
event.getDownTime(),
event.getEventTime(),
0,
event.getX(),
event.getY(),
event.getMetaState()
);

switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
newEvent.setAction(MotionEvent.ACTION_DOWN);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
newEvent.setAction(MotionEvent.ACTION_UP);
break;
case MotionEvent.ACTION_MOVE:
//Leave this even unchanged
newEvent.setAction(MotionEvent.ACTION_MOVE);
break;
default:
//Ignore unknown events
return;
}

//Forward the new event to super
super.onTouchEvent(newEvent);
}
}
5 changes: 4 additions & 1 deletion src/com/examples/customtouch/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MainActivity extends ListActivity implements OnItemClickListener {
"Pan Example", "Pan Gesture Example",
"Multi-Touch Example", "Touch Forward Example",
"Touch Delegate Example", "Touch Listener Example",
"Move Logger Example"};
"Move Logger Example", "Hover Emulator"};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -53,6 +53,9 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
case 6: //Move Logger View
startActivity(new Intent(this, MoveLoggerActivity.class));
break;
case 7: //Hover emulator
startActivity(new Intent(this, HoverEmulatorActivity.class));
break;
default:
break;
}
Expand Down