diff --git a/.travis.yml b/.travis.yml index b75d9f72..296d6efb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,27 +1,29 @@ language: android jdk: oraclejdk7 -android: - components: - - build-tools-19.1.0 - - doc-19 - env: matrix: - - ANDROID_SDKS=sysimg-19 ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a SKIN=WXGA800 LINT=true MAVEN=true + - ANDROID_TARGET=15 LINT=false MAVEN=false + - ANDROID_TARGET=16 LINT=false MAVEN=false + - ANDROID_TARGET=17 LINT=false MAVEN=false + - ANDROID_TARGET=18 LINT=false MAVEN=false + - ANDROID_TARGET=19 LINT=true MAVEN=true global: - TERM=dumb + - COMPONENTS=build-tools-20.0.0,android-20,extra-android-m2repository,extra-android-support,extra-google-m2repository + - LICENSES="android-sdk-license-5be876d5|android-sdk-preview-license-52d11cd2" - secure: Fo/btrr+HpwmQL6TNxZuh/TWRMouz9Z1RCo77tf3crNP4OHq9EAkduLc/b4i6HCGFM3fyfJ7AM5XMa3OQvXfF84FQnew7NGE6zB/CKFfKBIZyzntvrDg4VqWEAHzunT6+eBnGtxtrAE0bBo4MmUCklXWP8j5UAyjfFpSeJdOywY= - secure: s56rqSiiwX40CQWeHTcbyVLRtwLP/pcHWqaVCKuXtZ/m9SgRI/SHp9o8ITI2dzr3syEeAkHjkJOkHQSpH0bHi1xKY4M9ODcCugwv+yDEELw1qmI6Cvwjvj1cKsG7ebt39OnCkz+TqhtI/iOEkxq7mTizFjNIxIImMkKxeQwcFgE= - secure: UOQ9keLBuJw5Ed/eYtXarwoGuJSSvgGTob+D9aU8hVRoTUXpSDuHtk9TdS4MNXQ33jgFZEsmkpwRmV54XVEGe6xcri1uys1JDzjc+SqTEgbEh1QktfIWGvOx49YZtI2Zei0324S2qXRtemKMw4N+1XRRMP/IXEogr26Xi5JLVcI= before_install: + - curl -3L https://raw.github.com/embarkmobile/android-sdk-installer/version-2/android-sdk-installer | bash /dev/stdin --install=$COMPONENTS,android-$ANDROID_TARGET,sys-img-armeabi-v7a-android-$ANDROID_TARGET --accept=$LICENSES && source ~/.android-sdk-installer/env - curl -3L -o wait_for_emulator https://github.com/embarkmobile/android-sdk-installer/raw/version-2/wait_for_emulator - sudo chmod +x wait_for_emulator # Start the emulator - - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI - - emulator -avd test -no-skin -no-audio -no-window & + - echo no | android create avd --force -n test -t android-$ANDROID_TARGET --abi armeabi-v7a + - emulator -avd test -no-audio -no-window & before_script: # Downloads gradle stuff diff --git a/example/src/main/java/com/haarman/listviewanimations/itemmanipulation/DragAndDropActivity.java b/example/src/main/java/com/haarman/listviewanimations/itemmanipulation/DragAndDropActivity.java index 6c1d0a05..5435cee1 100644 --- a/example/src/main/java/com/haarman/listviewanimations/itemmanipulation/DragAndDropActivity.java +++ b/example/src/main/java/com/haarman/listviewanimations/itemmanipulation/DragAndDropActivity.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 Niek Haarman + * Copyright 2014 Niek Haarman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,21 @@ */ package com.haarman.listviewanimations.itemmanipulation; +import android.content.Context; import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.TextView; import android.widget.Toast; import com.haarman.listviewanimations.MyListActivity; import com.haarman.listviewanimations.R; import com.nhaarman.listviewanimations.ArrayAdapter; -import com.nhaarman.listviewanimations.itemmanipulation.widget.DynamicListView; +import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListView; +import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovedListener; +import com.nhaarman.listviewanimations.itemmanipulation.dragdrop.TouchViewDraggableManager; import com.nhaarman.listviewanimations.swinginadapters.simple.AlphaInAnimationAdapter; public class DragAndDropActivity extends MyListActivity { @@ -31,10 +39,10 @@ protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_draganddrop); - DynamicListView listView = (DynamicListView) findViewById(R.id.activity_draganddrop_listview); + final DynamicListView listView = (DynamicListView) findViewById(R.id.activity_draganddrop_listview); listView.setDivider(null); - final ArrayAdapter adapter = createListAdapter(); + final ArrayAdapter adapter = new DragAndDropListAdapter(this); AlphaInAnimationAdapter animAdapter = new AlphaInAnimationAdapter(adapter); animAdapter.setAbsListView(listView); @@ -42,21 +50,77 @@ protected void onCreate(final Bundle savedInstanceState) { animAdapter.getViewAnimator().setInitialDelayMillis(300); listView.setAdapter(animAdapter); + listView.setOnItemLongClickListener(new MyOnItemLongClickListener(listView)); + listView.setDraggableManager(new TouchViewDraggableManager(R.id.list_row_draganddrop_touchview)); + listView.setOnItemMovedListener(new MyOnItemMovedListener(adapter)); Toast.makeText(this, getString(R.string.long_press_to_drag), Toast.LENGTH_LONG).show(); } - private class MyOnItemMovedListener implements DynamicListView.OnItemMovedListener { + private static class DragAndDropListAdapter extends ArrayAdapter { + private final Context mContext; + + DragAndDropListAdapter(final Context context) { + mContext = context; + for (int i = 0; i < 1000; i++) { + add(mContext.getString(R.string.row_number, i)); + } + } + + @Override + public long getItemId(final int position) { + return getItem(position).hashCode(); + } + + @Override + public boolean hasStableIds() { + return true; + } + + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + View view = convertView; + if (view == null) { + view = LayoutInflater.from(mContext).inflate(R.layout.list_row_draganddrop, parent, false); + } + + ((TextView) view.findViewById(R.id.list_row_draganddrop_textview)).setText(getItem(position)); + + return view; + } + } + + private static class MyOnItemLongClickListener implements AdapterView.OnItemLongClickListener { + private final DynamicListView mListView; + + MyOnItemLongClickListener(final DynamicListView listView) { + mListView = listView; + } + + @Override + public boolean onItemLongClick(final AdapterView parent, final View view, final int position, final long id) { + mListView.startDragging(position); + return true; + } + } + + private class MyOnItemMovedListener implements OnItemMovedListener { private final ArrayAdapter mAdapter; + private Toast mToast; MyOnItemMovedListener(final ArrayAdapter adapter) { mAdapter = adapter; } @Override - public void onItemMoved(final int newPosition) { - Toast.makeText(getApplicationContext(), getString(R.string.moved, mAdapter.getItem(newPosition), newPosition), Toast.LENGTH_SHORT).show(); + public void onItemMoved(final int originalPosition, final int newPosition) { + if (mToast != null) { + mToast.cancel(); + } + + mToast = Toast.makeText(getApplicationContext(), getString(R.string.moved, mAdapter.getItem(newPosition), newPosition), Toast.LENGTH_SHORT); + mToast.show(); } } } diff --git a/example/src/main/res/layout/activity_draganddrop.xml b/example/src/main/res/layout/activity_draganddrop.xml index b0076d82..fd59b1d5 100644 --- a/example/src/main/res/layout/activity_draganddrop.xml +++ b/example/src/main/res/layout/activity_draganddrop.xml @@ -1,11 +1,28 @@ - - + - + + + android:clipToPadding="false" + android:divider="@null" + android:paddingBottom="48dp" /> \ No newline at end of file diff --git a/example/src/main/res/layout/list_row_draganddrop.xml b/example/src/main/res/layout/list_row_draganddrop.xml new file mode 100644 index 00000000..b1e59bf6 --- /dev/null +++ b/example/src/main/res/layout/list_row_draganddrop.xml @@ -0,0 +1,43 @@ + + + + + + + + \ No newline at end of file diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/InsertQueueTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/InsertQueueTest.java index 866e877f..3f893c52 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/InsertQueueTest.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/InsertQueueTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation; import android.util.Pair; diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTest.java new file mode 100644 index 00000000..8ea946a4 --- /dev/null +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.dragdrop; + +import android.support.annotation.NonNull; +import android.test.ActivityInstrumentationTestCase2; +import android.view.View; + +import org.mockito.*; + +import static org.mockito.Mockito.*; + +public class DynamicListViewTest extends ActivityInstrumentationTestCase2 { + + private DynamicListView mDynamicListView; + + @Mock + private OnItemMovedListener mOnItemMovedListener; + + + public DynamicListViewTest() { + super(DynamicListViewTestActivity.class); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + + MockitoAnnotations.initMocks(this); + + mDynamicListView = getActivity().getDynamicListView(); + mDynamicListView.setDraggableManager(new MyDraggableManager()); + mDynamicListView.setOnItemMovedListener(mOnItemMovedListener); + + getInstrumentation().waitForIdleSync(); + Thread.sleep(5000); + } + + public void testOnItemMovedListenerCalled() throws InterruptedException { + MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 0, 1); + verify(mOnItemMovedListener).onItemMoved(0, 1); + } + + public void testReverseOnItemMovedListenerCalled() throws InterruptedException { + MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 2, 1); + verify(mOnItemMovedListener).onItemMoved(2, 1); + } + + public void testOnItemMovedListenerCalledMultipleItems() throws InterruptedException { + MotionEventUtils.dispatchDragMotionEvents(getInstrumentation(), mDynamicListView, 1, 5); + verify(mOnItemMovedListener).onItemMoved(1, 5); + } + + public void testScroll() throws InterruptedException { + MotionEventUtils.dispatchDragScrollDownMotionEvents(getInstrumentation(), mDynamicListView, 1); + verify(mOnItemMovedListener).onItemMoved(1, 19); + } + + private static class MyDraggableManager implements DraggableManager { + + @Override + public boolean isDraggable(@NonNull final View view, final int position, final float x, final float y) { + return true; + } + } +} \ No newline at end of file diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTestActivity.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTestActivity.java new file mode 100644 index 00000000..dd824dcd --- /dev/null +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DynamicListViewTestActivity.java @@ -0,0 +1,110 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.dragdrop; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.AbsListView; +import android.widget.ListAdapter; +import android.widget.TextView; + +import com.nhaarman.listviewanimations.ArrayAdapter; + +import java.util.ArrayList; +import java.util.List; + +public class DynamicListViewTestActivity extends Activity { + + private DynamicListView mListView; + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + Window window = getWindow(); + window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); + + mListView = new DynamicListView(this); + List integers = new ArrayList(); + for (int i = 0; i < 20; i++) { + integers.add(i); + } + + ListAdapter myListAdapter = new MyListAdapter(this, integers); + mListView.setAdapter(myListAdapter); + + setContentView(mListView); + } + + @Override + public boolean dispatchTouchEvent(MotionEvent ev) { + int[] location = new int[2]; + mListView.getLocationOnScreen(location); + + ev = MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), ev.getX() - location[0], ev.getY() - location[1], ev.getMetaState()); + boolean handled = mListView.onInterceptTouchEvent(ev); + if (!handled) { + handled = mListView.dispatchTouchEvent(ev); + } + if (!handled) { + handled = onTouchEvent(ev); + } + return handled; + } + + public DynamicListView getDynamicListView() { + return mListView; + } + + private static class MyListAdapter extends ArrayAdapter { + + private final Context mContext; + + MyListAdapter(final Context context, final List items) { + super(items); + mContext = context; + } + + @Override + public long getItemId(final int position) { + return getItem(position).hashCode(); + } + + @Override + public boolean hasStableIds() { + return true; + } + + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + TextView view = (TextView) convertView; + if (view == null) { + view = new TextView(mContext); + view.setTextSize(30); + } + + view.setText("This is row number " + getItem(position)); + return view; + } + } +} diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/HoverDrawableTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/HoverDrawableTest.java new file mode 100644 index 00000000..bbc3694b --- /dev/null +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/HoverDrawableTest.java @@ -0,0 +1,131 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.dragdrop; + +import android.content.Context; +import android.test.AndroidTestCase; +import android.view.MotionEvent; +import android.view.View; + +import org.mockito.*; + +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +@SuppressWarnings("ALL") +public class HoverDrawableTest extends AndroidTestCase { + + private static final int START_Y = 29; + + private HoverDrawable mHoverDrawable; + + private View mView; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + MockitoAnnotations.initMocks(this); + + mView = new MyMockView(getContext()); + mView.measure(0, 0); + mHoverDrawable = new HoverDrawable(mView, START_Y); + } + + public void testInitialState() { + assertThat(mHoverDrawable.getDeltaY(), is(0)); + assertThat(mHoverDrawable.getBounds().top, is(0)); + assertThat(mHoverDrawable.isMovingUpwards(), is(false)); + } + + public void testMovedState() { + MotionEvent motionEvent = createMotionEvent(START_Y + 10); + mHoverDrawable.handleMoveEvent(motionEvent); + + assertThat(mHoverDrawable.getDeltaY(), is(10)); + assertThat(mHoverDrawable.getBounds().top, is(10)); + assertThat(mHoverDrawable.isMovingUpwards(), is(false)); + } + + public void testDoubleMovedState() { + MotionEvent motionEvent = createMotionEvent(START_Y + 10); + mHoverDrawable.handleMoveEvent(motionEvent); + + MotionEvent motionEvent2 = createMotionEvent(START_Y + 20); + mHoverDrawable.handleMoveEvent(motionEvent2); + + assertThat(mHoverDrawable.getDeltaY(), is(20)); + assertThat(mHoverDrawable.getBounds().top, is(20)); + assertThat(mHoverDrawable.isMovingUpwards(), is(false)); + } + + public void testReversedMovedState() { + MotionEvent motionEvent = createMotionEvent(START_Y - 10); + mHoverDrawable.handleMoveEvent(motionEvent); + + assertThat(mHoverDrawable.getDeltaY(), is(-10)); + assertThat(mHoverDrawable.getBounds().top, is(-10)); + assertThat(mHoverDrawable.isMovingUpwards(), is(true)); + } + + public void testReversedDoubleMovedState() { + MotionEvent motionEvent = createMotionEvent(START_Y - 10); + mHoverDrawable.handleMoveEvent(motionEvent); + + MotionEvent motionEvent2 = createMotionEvent(START_Y - 20); + mHoverDrawable.handleMoveEvent(motionEvent2); + + assertThat(mHoverDrawable.getDeltaY(), is(-20)); + assertThat(mHoverDrawable.getBounds().top, is(-20)); + assertThat(mHoverDrawable.isMovingUpwards(), is(true)); + } + + public void testShift() { + MotionEvent motionEvent = createMotionEvent(START_Y - 10); + mHoverDrawable.handleMoveEvent(motionEvent); + + mHoverDrawable.shift(10); + + assertThat(mHoverDrawable.getDeltaY(), is(0)); + assertThat(mHoverDrawable.isMovingUpwards(), is(false)); + assertThat(mHoverDrawable.getBounds().top, is(-10)); + } + + public void testScroll() { + mHoverDrawable.onScroll(20); + + assertThat(mHoverDrawable.getDeltaY(), is(-20)); + assertThat(mHoverDrawable.isMovingUpwards(), is(true)); + assertThat(mHoverDrawable.getBounds().top, is(0)); + } + + private static MotionEvent createMotionEvent(final float y) { + return MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0, y, 0); + } + + private static class MyMockView extends View { + + MyMockView(final Context context) { + super(context); + } + + @Override + protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { + setMeasuredDimension(100, 100); + } + } +} \ No newline at end of file diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/MotionEventUtils.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/MotionEventUtils.java new file mode 100644 index 00000000..0c1efcbc --- /dev/null +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/MotionEventUtils.java @@ -0,0 +1,102 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.dragdrop; + +import android.annotation.TargetApi; +import android.app.Instrumentation; +import android.os.SystemClock; +import android.view.MotionEvent; +import android.view.View; +import android.widget.AbsListView; + + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@TargetApi(14) +public class MotionEventUtils { + + private MotionEventUtils() { + } + + public static void dispatchDragMotionEvents(final Instrumentation instrumentation, final DynamicListView dynamicListView, final int fromPosition, + final int toPosition) throws InterruptedException { + int[] location = new int[2]; + dynamicListView.getLocationOnScreen(location); + + View view = dynamicListView.getChildAt(fromPosition); + float fromY = (int) (view.getY() + view.getHeight() / 2) + location[1]; + + View toView = dynamicListView.getChildAt(toPosition); + float toY = (int) (toView.getY() + toView.getHeight() / 2) + location[1]; + + toY += fromPosition < toPosition ? 2 : -2; + + List motionEvents = createMotionEvents(dynamicListView, fromY, toY); + dispatchMotionEvents(instrumentation, motionEvents, true); + } + + public static void dispatchDragScrollDownMotionEvents(final Instrumentation instrumentation, final DynamicListView dynamicListView, + final int fromPosition) throws InterruptedException { + int[] location = new int[2]; + dynamicListView.getLocationOnScreen(location); + + View view = dynamicListView.getChildAt(fromPosition); + float fromY = (int) (view.getY() + view.getHeight() / 2) + location[1]; + + View toView = dynamicListView.getChildAt(dynamicListView.getLastVisiblePosition()); + float toY = (int) (toView.getY() + toView.getHeight()) + location[1] + 2; + + List motionEvents = createMotionEvents(dynamicListView, fromY, toY); + MotionEvent upEvent = motionEvents.remove(motionEvents.size() - 1); + dispatchMotionEvents(instrumentation, motionEvents, true); + Thread.sleep(10000); + dispatchMotionEvents(instrumentation, Arrays.asList(upEvent), true); + } + + public static List createMotionEvents(final AbsListView absListView, final float fromY, final float toY) { + int x = (int) (absListView.getX() + absListView.getWidth() / 2); + + List results = new ArrayList<>(); + results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, fromY, 0)); + + float diff = (toY - fromY) / 25; + float y = fromY; + for (int i = 0; i < 25; i++) { + y += diff; + results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0)); + } + results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, toY, 0)); + + return results; + } + + public static void dispatchMotionEvents(final Instrumentation instrumentation, final Iterable motionEvents, final boolean wait) throws InterruptedException { + for (final MotionEvent event : motionEvents) { + instrumentation.sendPointerSync(event); + Thread.sleep(100); + } + + if (wait) { + /* We need to wait for the fling animation to complete */ + Thread.sleep(1500); + } + } + + +} diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/matchers/Matchers.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/matchers/Matchers.java index 2c02f6f9..fa17b6e2 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/matchers/Matchers.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/matchers/Matchers.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.matchers; import android.util.Pair; diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/MotionEventUtils.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/MotionEventUtils.java index 229507d1..baa328e3 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/MotionEventUtils.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/MotionEventUtils.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.swipedismiss; import android.app.Activity; @@ -58,8 +74,10 @@ public static List createMotionEvents(final AbsListView absListView List results = new ArrayList<>(); results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, fromX, y, 0)); - for (int i = 0; i < 10; i++) { - float x = fromX < toX ? toX / 10 * i : fromX - fromX / 10 * i; + + float diff = fromX - toX; + for (int i = 1; i < 10; i++) { + float x = fromX + diff / 10 * i; results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, x, y, 0)); } results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, toX, y, 0)); diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeDismissTouchListenerTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeDismissTouchListenerTest.java index ec30fbb5..bac7677e 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeDismissTouchListenerTest.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeDismissTouchListenerTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.swipedismiss; import android.test.ActivityInstrumentationTestCase2; diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTest.java index a83ab1e0..ec0927c1 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTest.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.swipedismiss; import android.support.annotation.NonNull; diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTestActivity.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTestActivity.java index 17f289bf..790cfd1d 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTestActivity.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/SwipeTouchListenerTestActivity.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.swipedismiss; import android.app.Activity; diff --git a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/undo/SwipeUndoTouchListenerTest.java b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/undo/SwipeUndoTouchListenerTest.java index 044aad2a..13c6ce5e 100644 --- a/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/undo/SwipeUndoTouchListenerTest.java +++ b/lib-manipulation/src/androidTest/java/com/nhaarman/listviewanimations/itemmanipulation/swipedismiss/undo/SwipeUndoTouchListenerTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 2014 Niek Haarman + * + * 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.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo; import android.test.ActivityInstrumentationTestCase2; diff --git a/lib-manipulation/src/main/AndroidManifest.xml b/lib-manipulation/src/main/AndroidManifest.xml index 0af6b40d..6b6eccb3 100644 --- a/lib-manipulation/src/main/AndroidManifest.xml +++ b/lib-manipulation/src/main/AndroidManifest.xml @@ -1,9 +1,26 @@ + + + \ No newline at end of file