1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.tuner; 16 17 import android.content.ClipData; 18 import android.content.ClipboardManager; 19 import android.content.ClipboardManager.OnPrimaryClipChangedListener; 20 import android.content.Context; 21 import android.util.AttributeSet; 22 import android.view.DragEvent; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.widget.ImageView; 26 27 import com.android.systemui.R; 28 29 public class ClipboardView extends ImageView implements OnPrimaryClipChangedListener { 30 31 private static final int TARGET_COLOR = 0x4dffffff; 32 private final ClipboardManager mClipboardManager; 33 private ClipData mCurrentClip; 34 ClipboardView(Context context, AttributeSet attrs)35 public ClipboardView(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 mClipboardManager = context.getSystemService(ClipboardManager.class); 38 } 39 40 @Override onAttachedToWindow()41 protected void onAttachedToWindow() { 42 super.onAttachedToWindow(); 43 startListening(); 44 } 45 46 @Override onDetachedFromWindow()47 protected void onDetachedFromWindow() { 48 super.onDetachedFromWindow(); 49 stopListening(); 50 } 51 52 @Override onTouchEvent(MotionEvent ev)53 public boolean onTouchEvent(MotionEvent ev) { 54 if (ev.getActionMasked() == MotionEvent.ACTION_DOWN && mCurrentClip != null) { 55 startPocketDrag(); 56 } 57 return super.onTouchEvent(ev); 58 } 59 60 @Override onDragEvent(DragEvent event)61 public boolean onDragEvent(DragEvent event) { 62 switch (event.getAction()) { 63 case DragEvent.ACTION_DRAG_ENTERED: 64 setBackgroundDragTarget(true); 65 break; 66 case DragEvent.ACTION_DROP: 67 mClipboardManager.setPrimaryClip(event.getClipData()); 68 case DragEvent.ACTION_DRAG_EXITED: 69 case DragEvent.ACTION_DRAG_ENDED: 70 setBackgroundDragTarget(false); 71 break; 72 } 73 return true; 74 } 75 setBackgroundDragTarget(boolean isTarget)76 private void setBackgroundDragTarget(boolean isTarget) { 77 setBackgroundColor(isTarget ? TARGET_COLOR : 0); 78 } 79 startPocketDrag()80 public void startPocketDrag() { 81 startDragAndDrop(mCurrentClip, new View.DragShadowBuilder(this), null, 82 View.DRAG_FLAG_GLOBAL); 83 } 84 startListening()85 public void startListening() { 86 mClipboardManager.addPrimaryClipChangedListener(this); 87 onPrimaryClipChanged(); 88 } 89 stopListening()90 public void stopListening() { 91 mClipboardManager.removePrimaryClipChangedListener(this); 92 } 93 94 @Override onPrimaryClipChanged()95 public void onPrimaryClipChanged() { 96 mCurrentClip = mClipboardManager.getPrimaryClip(); 97 setImageResource(mCurrentClip != null 98 ? R.drawable.clipboard_full : R.drawable.clipboard_empty); 99 } 100 } 101