| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 1 | |
| 2 | Android DragAndDropAcrossApps-new Sample |
| 3 | =================================== |
| 4 | |
| Trevor Johns | 0d2f2c6 | 2016-09-28 22:15:23 -0700 | [diff] [blame] | 5 | This sample contains two separate Android applications: DragSource and |
| 6 | DropTarget. DragSource contains images and text that can be dropped into the DropTarget |
| 7 | app. Images are shared between the two apps through a URI for which the receiving app |
| 8 | must request permission first, before it can be used. |
| 9 | |
| 10 | It also demonstrates the use of the DragStartHelper from the v13 support library to easily |
| 11 | handle drag and drop events. |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 12 | |
| 13 | Introduction |
| 14 | ------------ |
| 15 | |
| 16 | Android N introduces support for drag and drop between applications, |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 17 | augmenting the existing APIs that have enabled this within a single |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 18 | window before. |
| 19 | |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 20 | To start a drag operation you need to call `View.startDragAndDrop`. |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 21 | Which gesture or action triggers this is up to you as an app developer. |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 22 | The API guide recommends doing this from |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 23 | `View.OnLongClickListener.onLongClick` and this seems to be the de-facto |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 24 | standard, but you are free to use other gestures (single tap, tap and drag |
| 25 | etc). |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 26 | However, if you go for a unconventional drag start gesture, note that |
| 27 | the framework implementation assumes that the pointer (touch or mouse) |
| 28 | is down while the drag is starting, and the most recent touch/click |
| 29 | position is used as the original position of the drag shadow. |
| 30 | |
| 31 | See also `android.support.v13.view.DragStartHelper` which uses different |
| 32 | gestures for touch and mouse (click and drag works better for mouse |
| 33 | than a long click). |
| 34 | |
| 35 | By default a drag and drop operation is constrained by the window |
| 36 | containing the view that started the drag. |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 37 | To enable cross-window and cross-app drag and drop add |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 38 | `View.DRAG_FLAG_GLOBAL` to the flags passed to the `View.startDragAndDrop` |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 39 | call. |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 40 | |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 41 | If a Uri requiring permission grants is being sent, then the |
| 42 | `android.view.View.DRAG_FLAG_GLOBAL_URI_READ` and/or the |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 43 | `android.view.View.DRAG_FLAG_GLOBAL_URI_WRITE` flags must be used also. |
| 44 | To access content URIs requiring permissions on the receiving side, the target |
| 45 | app needs to request the `android.view.DropPermissions` from the activity via |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 46 | `android.app.Activity.requestDropPermissions`. This permission will stay either |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 47 | until the activity is alive, or until the `release()` method is called on the |
| 48 | `android.view.DropPermissions` object. |
| 49 | |
| 50 | Pre-requisites |
| 51 | -------------- |
| 52 | |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 53 | - Android SDK 28 |
| Yuichi Araki | f302c13 | 2019-01-11 17:19:47 +0900 | [diff] [blame] | 54 | - Android Build Tools v28.0.3 |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 55 | - Android Support Repository |
| 56 | |
| 57 | Screenshots |
| 58 | ------------- |
| 59 | |
| Trevor Johns | 0d2f2c6 | 2016-09-28 22:15:23 -0700 | [diff] [blame] | 60 | <img src="screenshots/phone.png" height="400" alt="Screenshot"/> <img src="screenshots/tablet.png" height="400" alt="Screenshot"/> |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 61 | |
| 62 | Getting Started |
| 63 | --------------- |
| 64 | |
| 65 | This sample uses the Gradle build system. To build this project, use the |
| 66 | "gradlew build" command or use "Import Project" in Android Studio. |
| 67 | |
| 68 | Support |
| 69 | ------- |
| 70 | |
| 71 | - Google+ Community: https://plus.google.com/communities/105153134372062985968 |
| 72 | - Stack Overflow: http://stackoverflow.com/questions/tagged/android |
| 73 | |
| 74 | If you've found an error in this sample, please file an issue: |
| 75 | https://github.com/googlesamples/android-DragAndDropAcrossApps-new |
| 76 | |
| 77 | Patches are encouraged, and may be submitted by forking this project and |
| 78 | submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. |
| 79 | |
| 80 | License |
| 81 | ------- |
| 82 | |
| Ben Weiss | 4bc4237 | 2019-01-10 14:40:12 +0000 | [diff] [blame] | 83 | Copyright 2019 The Android Open Source Project, Inc. |
| Jan-Felix Schmakeit | 0bf3ed8 | 2016-03-02 19:06:48 +1100 | [diff] [blame] | 84 | |
| 85 | Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 86 | license agreements. See the NOTICE file distributed with this work for |
| 87 | additional information regarding copyright ownership. The ASF licenses this |
| 88 | file to you under the Apache License, Version 2.0 (the "License"); you may not |
| 89 | use this file except in compliance with the License. You may obtain a copy of |
| 90 | the License at |
| 91 | |
| 92 | http://www.apache.org/licenses/LICENSE-2.0 |
| 93 | |
| 94 | Unless required by applicable law or agreed to in writing, software |
| 95 | distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 96 | WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 97 | License for the specific language governing permissions and limitations under |
| 98 | the License. |