John Reck | 66302e5 | 2011-05-18 17:12:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 com.android.browser; |
| 18 | |
| 19 | import android.content.ClipData; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentUris; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
| 25 | import android.net.Uri; |
| 26 | import android.provider.BrowserContract; |
| 27 | import android.provider.BrowserContract.Bookmarks; |
| 28 | import android.view.DragEvent; |
| 29 | import android.view.View; |
| 30 | import android.view.View.OnDragListener; |
| 31 | |
| 32 | public class BookmarkDragHandler { |
| 33 | |
| 34 | public static interface BookmarkDragController { |
| 35 | boolean startDrag(Cursor item); |
| 36 | } |
| 37 | |
| 38 | public static interface BookmarkDragAdapter { |
| 39 | void setBookmarkDragHandler(BookmarkDragHandler handler); |
| 40 | Cursor getItemForView(View v); |
| 41 | } |
| 42 | |
| 43 | static class BookmarkDragState { |
| 44 | long id; |
| 45 | long parent; |
| 46 | } |
| 47 | |
| 48 | static final String BOOKMARK_DRAG_LABEL = "com.android.browser.BOOKMARK_LABEL"; |
| 49 | |
| 50 | private Context mContext; |
| 51 | private BookmarkDragController mDragController; |
| 52 | private BookmarkDragAdapter mDragAdapter; |
| 53 | |
| 54 | public BookmarkDragHandler(Context context, BookmarkDragController controller, |
| 55 | BookmarkDragAdapter adapter) { |
| 56 | mContext = context; |
| 57 | mDragController = controller; |
| 58 | mDragAdapter = adapter; |
| 59 | mDragAdapter.setBookmarkDragHandler(this); |
| 60 | } |
| 61 | |
| 62 | public boolean startDrag(View view, Cursor item, long id) { |
| 63 | if (!mDragController.startDrag(item)) { |
| 64 | return false; |
| 65 | } |
| 66 | Uri uri = ContentUris.withAppendedId( |
| 67 | BrowserContract.Bookmarks.CONTENT_URI, id); |
| 68 | ClipData data = ClipData.newRawUri(BOOKMARK_DRAG_LABEL, uri); |
| 69 | BookmarkDragState state = new BookmarkDragState(); |
| 70 | state.id = id; |
| 71 | state.parent = item.getLong(BookmarksLoader.COLUMN_INDEX_PARENT); |
| 72 | view.startDrag(data, new View.DragShadowBuilder(view), state, 0); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | public void registerBookmarkDragHandler(View view) { |
| 77 | view.setOnDragListener(mBookmarkDragListener); |
| 78 | } |
| 79 | |
| 80 | private OnDragListener mBookmarkDragListener = new OnDragListener() { |
| 81 | |
| 82 | @Override |
| 83 | public boolean onDrag(View v, DragEvent event) { |
| 84 | Cursor c = mDragAdapter.getItemForView(v); |
| 85 | BookmarkDragState state = (BookmarkDragState) event.getLocalState(); |
| 86 | switch (event.getAction()) { |
| 87 | case DragEvent.ACTION_DRAG_STARTED: |
| 88 | return true; |
| 89 | case DragEvent.ACTION_DROP: |
| 90 | long id = c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 91 | if (id == state.id) { |
| 92 | // We dropped onto ourselves, show the context menu |
| 93 | v.showContextMenu(); |
| 94 | return false; |
| 95 | } |
| 96 | long parent = c.getLong(BookmarksLoader.COLUMN_INDEX_PARENT); |
| 97 | if (isFolder(c)) { |
| 98 | parent = c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 99 | } |
| 100 | if (parent != state.parent) { |
| 101 | ContentResolver cr = mContext.getContentResolver(); |
| 102 | ContentValues values = new ContentValues(); |
| 103 | values.put(Bookmarks.PARENT, parent); |
| 104 | Uri uri = event.getClipData().getItemAt(0).getUri(); |
| 105 | cr.update(uri, values, null, null); |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | static boolean isFolder(Cursor c) { |
| 114 | return c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 115 | } |
| 116 | } |