1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.test.uiautomator.testapp;
18 
19 import android.app.Activity;
20 import android.content.ClipData;
21 import android.graphics.Rect;
22 import android.os.Bundle;
23 import android.view.DragEvent;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.TextView;
27 
28 import androidx.annotation.RequiresApi;
29 
30 import org.jspecify.annotations.Nullable;
31 
32 @RequiresApi(24)
33 public class DragTestActivity extends Activity {
34 
35     @Override
onCreate(@ullable Bundle savedInstanceState)36     public void onCreate(@Nullable Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         setContentView(R.layout.drag_test_activity);
40 
41         Button dragButton = findViewById(R.id.drag_button);
42         TextView dragDestination = findViewById(R.id.drag_destination);
43 
44         dragButton.setOnLongClickListener(v -> {
45             v.startDragAndDrop(ClipData.newPlainText(null, null),
46                     new View.DragShadowBuilder(dragButton),
47                     null,
48                     0);
49             return true;
50         });
51 
52         dragButton.setOnDragListener((v, e) -> {
53             if (e.getAction() == DragEvent.ACTION_DRAG_ENDED) {
54                 Rect destRegion = new Rect();
55                 dragDestination.getGlobalVisibleRect(destRegion);
56                 if (destRegion.contains((int) e.getX(), (int) e.getY())) {
57                     dragDestination.setText("drag_received");
58                 }
59             }
60             return true;
61         });
62     }
63 }
64