1 /* 2 * Copyright (C) 2016 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 android.wm.cts.dndsourceapp; 18 19 import android.app.Activity; 20 import android.content.ClipData; 21 import android.content.ClipDescription; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.os.Handler; 25 import android.os.PersistableBundle; 26 import android.view.MotionEvent; 27 import android.view.View; 28 import android.widget.TextView; 29 30 public class DragSource extends Activity{ 31 private static final String URI_PREFIX = 32 "content://" + DragSourceContentProvider.AUTHORITY + "/data"; 33 34 private static final String MAGIC_VALUE = "42"; 35 private static final long TIMEOUT_CANCEL = 150; 36 37 @Override onCreate(Bundle savedInstanceState)38 public void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 View view = getLayoutInflater().inflate(R.layout.source_activity, null); 42 setContentView(view); 43 44 final Uri plainUri = Uri.parse(URI_PREFIX + "/" + MAGIC_VALUE); 45 46 setUpDragSource("disallow_global", plainUri, 0); 47 setUpDragSource("cancel_soon", plainUri, View.DRAG_FLAG_GLOBAL); 48 49 setUpDragSource("grant_none", plainUri, View.DRAG_FLAG_GLOBAL); 50 setUpDragSource("grant_read", plainUri, 51 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ); 52 setUpDragSource("grant_write", plainUri, 53 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_WRITE); 54 setUpDragSource("grant_read_persistable", plainUri, 55 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | 56 View.DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION); 57 58 final Uri prefixUri = Uri.parse(URI_PREFIX); 59 60 setUpDragSource("grant_read_prefix", prefixUri, 61 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | 62 View.DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION); 63 setUpDragSource("grant_read_noprefix", prefixUri, 64 View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ); 65 } 66 setUpDragSource(String mode, final Uri uri, final int flags)67 private void setUpDragSource(String mode, final Uri uri, final int flags) { 68 if (!mode.equals(getIntent().getStringExtra("mode"))) { 69 return; 70 } 71 final View source = findViewById(R.id.drag_source); 72 ((TextView) source).setText(mode); 73 source.setOnTouchListener(new View.OnTouchListener() { 74 @Override 75 public boolean onTouch(View v, MotionEvent event) { 76 if (event.getAction() != MotionEvent.ACTION_DOWN) { 77 return false; 78 } 79 final ClipDescription clipDescription = new ClipDescription("", new String[] { 80 ClipDescription.MIMETYPE_TEXT_URILIST }); 81 PersistableBundle extras = new PersistableBundle(1); 82 extras.putString("extraKey", "extraValue"); 83 clipDescription.setExtras(extras); 84 final ClipData clipData = new ClipData(clipDescription, new ClipData.Item(uri)); 85 v.startDragAndDrop( 86 clipData, 87 new View.DragShadowBuilder(v), 88 null, 89 flags); 90 if (mode.equals("cancel_soon")) { 91 new Handler().postDelayed(new Runnable() { 92 @Override 93 public void run() { 94 v.cancelDragAndDrop(); 95 } 96 }, TIMEOUT_CANCEL); 97 } 98 return true; 99 } 100 }); 101 } 102 } 103