Skip to content

Commit

Permalink
Adds BasicGestureDetect sample.
Browse files Browse the repository at this point in the history
Change-Id: I90dfce6aa687b1f79c86781d9f2536f55b913b8e
  • Loading branch information
Jeremy Walker committed Jul 30, 2019
1 parent 05f09a2 commit 9da6461
Show file tree
Hide file tree
Showing 43 changed files with 2,036 additions and 0 deletions.
18 changes: 18 additions & 0 deletions BasicGestureDetect/.google/packaging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# GOOGLE SAMPLE PACKAGING DATA
#
# This file is used by Google as part of our samples packaging process.
# End users may safely ignore this file. It has no relevance to other systems.
---
status: PUBLISHED
technologies: [Android]
categories: [UI, Input]
languages: [Java]
solutions: [Mobile]
github: android/input
level: INTERMEDIATE
icon: screenshots/icon-web.png
apiRefs:
- android:android.view.GestureDetector
- android:android.view.MotionEvent
license: apache2
68 changes: 68 additions & 0 deletions BasicGestureDetect/Application/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
}
}

apply plugin: 'com.android.application'

repositories {
google()
jcenter()
}

dependencies {


implementation "com.android.support:support-v4:28.0.0"
implementation "com.android.support:support-v13:28.0.0"
implementation "com.android.support:cardview-v7:28.0.0"
implementation "com.android.support:appcompat-v7:28.0.0"






}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process

android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 18
targetSdkVersion 28
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']

}

}
43 changes: 43 additions & 0 deletions BasicGestureDetect/Application/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 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.
-->



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.basicgesturedetect"
android:versionCode="1"
android:versionName="1.0">

<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 com.example.android.basicgesturedetect;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogFragment;

public class BasicGestureDetectFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View gestureView = getActivity().findViewById(R.id.sample_output);
gestureView.setClickable(true);
gestureView.setFocusable(true);

// BEGIN_INCLUDE(init_detector)

// First create the GestureListener that will include all our callbacks.
// Then create the GestureDetector, which takes that listener as an argument.
GestureDetector.SimpleOnGestureListener gestureListener = new GestureListener();
final GestureDetector gd = new GestureDetector(getActivity(), gestureListener);

/* For the view where gestures will occur, create an onTouchListener that sends
* all motion events to the gesture detector. When the gesture detector
* actually detects an event, it will use the callbacks you created in the
* SimpleOnGestureListener to alert your application.
*/

gestureView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
gd.onTouchEvent(motionEvent);
return false;
}
});
// END_INCLUDE(init_detector)
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.sample_action) {
clearLog();
}
return true;
}

public void clearLog() {
LogFragment logFragment = ((LogFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.log_fragment));
logFragment.getLogView().setText("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 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 com.example.android.basicgesturedetect;

import android.annotation.TargetApi;
import android.os.Build;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.example.android.common.logger.Log;

public class GestureListener extends GestureDetector.SimpleOnGestureListener {

public static final String TAG = "GestureListener";

// BEGIN_INCLUDE(init_gestureListener)
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Up motion completing a single tap occurred.
Log.i(TAG, "Single Tap Up" + getTouchType(e));
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// Touch has been long enough to indicate a long press.
// Does not indicate motion is complete yet (no up event necessarily)
Log.i(TAG, "Long Press" + getTouchType(e));
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// User attempted to scroll
Log.i(TAG, "Scroll" + getTouchType(e1));
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// Fling event occurred. Notification of this one happens after an "up" event.
Log.i(TAG, "Fling" + getTouchType(e1));
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// User performed a down event, and hasn't moved yet.
Log.i(TAG, "Show Press" + getTouchType(e));
}

@Override
public boolean onDown(MotionEvent e) {
// "Down" event - User touched the screen.
Log.i(TAG, "Down" + getTouchType(e));
return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
// User tapped the screen twice.
Log.i(TAG, "Double tap" + getTouchType(e));
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// Since double-tap is actually several events which are considered one aggregate
// gesture, there's a separate callback for an individual event within the doubletap
// occurring. This occurs for down, up, and move.
Log.i(TAG, "Event within double tap" + getTouchType(e));
return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// A confirmed single-tap event has occurred. Only called when the detector has
// determined that the first tap stands alone, and is not part of a double tap.
Log.i(TAG, "Single tap confirmed" + getTouchType(e));
return false;
}
// END_INCLUDE(init_gestureListener)


/**
* Returns a human-readable string describing the type of touch that triggered a MotionEvent.
*/

private static String getTouchType(MotionEvent e){

String touchTypeDescription = " ";
int touchType = e.getToolType(0);

switch (touchType) {
case MotionEvent.TOOL_TYPE_FINGER:
touchTypeDescription += "(finger)";
break;
case MotionEvent.TOOL_TYPE_STYLUS:
touchTypeDescription += "(stylus, ";
//Get some additional information about the stylus touch
float stylusPressure = e.getPressure();
touchTypeDescription += "pressure: " + stylusPressure;

if(Build.VERSION.SDK_INT >= 21) {
touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
}

touchTypeDescription += ")";
break;
case MotionEvent.TOOL_TYPE_ERASER:
touchTypeDescription += "(eraser)";
break;
case MotionEvent.TOOL_TYPE_MOUSE:
touchTypeDescription += "(mouse)";
break;
default:
touchTypeDescription += "(unknown tool)";
break;
}

return touchTypeDescription;
}

/**
* Returns a human-readable string listing all the stylus buttons that were pressed when the
* input MotionEvent occurred.
*/
@TargetApi(21)
private static String getButtonsPressed(MotionEvent e){
String buttons = "";

if(e.isButtonPressed(MotionEvent.BUTTON_PRIMARY)){
buttons += " primary";
}

if(e.isButtonPressed(MotionEvent.BUTTON_SECONDARY)){
buttons += " secondary";
}

if(e.isButtonPressed(MotionEvent.BUTTON_TERTIARY)){
buttons += " tertiary";
}

if(e.isButtonPressed(MotionEvent.BUTTON_BACK)){
buttons += " back";
}

if(e.isButtonPressed(MotionEvent.BUTTON_FORWARD)){
buttons += " forward";
}

if (buttons.equals("")){
buttons = "none";
}

return buttons;
}

}

0 comments on commit 9da6461

Please sign in to comment.