blob: fc0752fea45634ee38af2a7219d2450fb2fca55b [file] [log] [blame]
John Reck66302e52011-05-18 17:12:33 -07001/*
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
17package com.android.browser;
18
John Recke7c97de2011-05-27 14:42:43 -070019import android.app.Activity;
John Reck66302e52011-05-18 17:12:33 -070020import android.content.ClipData;
21import android.content.ContentResolver;
22import android.content.ContentUris;
23import android.content.ContentValues;
John Reck66302e52011-05-18 17:12:33 -070024import android.database.Cursor;
25import android.net.Uri;
26import android.provider.BrowserContract;
John Recke7c97de2011-05-27 14:42:43 -070027import android.view.ActionMode;
28import android.view.ActionMode.Callback;
John Reck66302e52011-05-18 17:12:33 -070029import android.view.DragEvent;
John Recke7c97de2011-05-27 14:42:43 -070030import android.view.Menu;
31import android.view.MenuItem;
John Reck66302e52011-05-18 17:12:33 -070032import android.view.View;
33import android.view.View.OnDragListener;
John Recke7c97de2011-05-27 14:42:43 -070034import android.view.ViewGroup;
John Reck66302e52011-05-18 17:12:33 -070035
John Recke7c97de2011-05-27 14:42:43 -070036public class BookmarkDragHandler implements Callback {
John Reck66302e52011-05-18 17:12:33 -070037
38 public static interface BookmarkDragController {
39 boolean startDrag(Cursor item);
John Recke7c97de2011-05-27 14:42:43 -070040
41 ViewGroup getActionModeView(ActionMode mode, BookmarkDragState state);
42 void actionItemClicked(View v, BookmarkDragState state);
John Reck66302e52011-05-18 17:12:33 -070043 }
44
45 public static interface BookmarkDragAdapter {
46 void setBookmarkDragHandler(BookmarkDragHandler handler);
47 Cursor getItemForView(View v);
48 }
49
John Recke7c97de2011-05-27 14:42:43 -070050 public static class BookmarkDragState {
51 public long id;
52 public long parent;
53 public Object extraState;
John Reck66302e52011-05-18 17:12:33 -070054 }
55
56 static final String BOOKMARK_DRAG_LABEL = "com.android.browser.BOOKMARK_LABEL";
57
John Recke7c97de2011-05-27 14:42:43 -070058 private Activity mActivity;
John Reck66302e52011-05-18 17:12:33 -070059 private BookmarkDragController mDragController;
60 private BookmarkDragAdapter mDragAdapter;
John Recke7c97de2011-05-27 14:42:43 -070061 private ActionMode mActionMode;
62 private BookmarkDragState mDragState;
John Reck66302e52011-05-18 17:12:33 -070063
John Recke7c97de2011-05-27 14:42:43 -070064 public BookmarkDragHandler(Activity activity, BookmarkDragController controller,
John Reck66302e52011-05-18 17:12:33 -070065 BookmarkDragAdapter adapter) {
John Recke7c97de2011-05-27 14:42:43 -070066 mActivity = activity;
John Reck66302e52011-05-18 17:12:33 -070067 mDragController = controller;
68 mDragAdapter = adapter;
69 mDragAdapter.setBookmarkDragHandler(this);
70 }
71
John Recke7c97de2011-05-27 14:42:43 -070072 public boolean startDrag(View view, Cursor item, long id, Object extraState) {
John Reck66302e52011-05-18 17:12:33 -070073 if (!mDragController.startDrag(item)) {
74 return false;
75 }
76 Uri uri = ContentUris.withAppendedId(
77 BrowserContract.Bookmarks.CONTENT_URI, id);
78 ClipData data = ClipData.newRawUri(BOOKMARK_DRAG_LABEL, uri);
79 BookmarkDragState state = new BookmarkDragState();
80 state.id = id;
81 state.parent = item.getLong(BookmarksLoader.COLUMN_INDEX_PARENT);
John Recke7c97de2011-05-27 14:42:43 -070082 state.extraState = extraState;
83 mDragState = state;
John Reck66302e52011-05-18 17:12:33 -070084 view.startDrag(data, new View.DragShadowBuilder(view), state, 0);
John Recke7c97de2011-05-27 14:42:43 -070085 mActionMode = view.startActionMode(this);
John Reck66302e52011-05-18 17:12:33 -070086 return true;
87 }
88
89 public void registerBookmarkDragHandler(View view) {
90 view.setOnDragListener(mBookmarkDragListener);
91 }
92
93 private OnDragListener mBookmarkDragListener = new OnDragListener() {
94
95 @Override
96 public boolean onDrag(View v, DragEvent event) {
97 Cursor c = mDragAdapter.getItemForView(v);
98 BookmarkDragState state = (BookmarkDragState) event.getLocalState();
99 switch (event.getAction()) {
100 case DragEvent.ACTION_DRAG_STARTED:
101 return true;
102 case DragEvent.ACTION_DROP:
103 long id = c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
104 if (id == state.id) {
105 // We dropped onto ourselves, show the context menu
106 v.showContextMenu();
107 return false;
108 }
109 long parent = c.getLong(BookmarksLoader.COLUMN_INDEX_PARENT);
110 if (isFolder(c)) {
111 parent = c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
112 }
113 if (parent != state.parent) {
John Recke7c97de2011-05-27 14:42:43 -0700114 ContentResolver cr = mActivity.getContentResolver();
John Reck66302e52011-05-18 17:12:33 -0700115 ContentValues values = new ContentValues();
John Recke7c97de2011-05-27 14:42:43 -0700116 values.put(BrowserContract.Bookmarks.PARENT, parent);
John Reck66302e52011-05-18 17:12:33 -0700117 Uri uri = event.getClipData().getItemAt(0).getUri();
118 cr.update(uri, values, null, null);
119 }
120 break;
121 }
122 return false;
123 }
124 };
125
John Recke7c97de2011-05-27 14:42:43 -0700126 private OnDragListener mActionModeDragListener = new OnDragListener() {
127
128 @Override
129 public boolean onDrag(View v, DragEvent event) {
130 BookmarkDragState state = (BookmarkDragState) event.getLocalState();
131 switch (event.getAction()) {
132 case DragEvent.ACTION_DRAG_STARTED:
133 return true;
134 case DragEvent.ACTION_DROP:
135 mDragController.actionItemClicked(v, state);
136 // fall through
137 case DragEvent.ACTION_DRAG_ENDED:
138 if (mActionMode != null) {
139 mActionMode.finish();
140 mActionMode = null;
141 }
142 return true;
143 }
144 return false;
145 }
146 };
147
John Reck66302e52011-05-18 17:12:33 -0700148 static boolean isFolder(Cursor c) {
149 return c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
150 }
John Recke7c97de2011-05-27 14:42:43 -0700151
152 @Override
153 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
154 ViewGroup view = mDragController.getActionModeView(mode, mDragState);
155 int count = view.getChildCount();
156 for (int i = 0; i < count; i++) {
157 view.getChildAt(i).setOnDragListener(mActionModeDragListener);
158 }
159 mode.setCustomView(view);
160 return true;
161 }
162
163 @Override
164 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
165 return true;
166 }
167
168 @Override
169 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
170 return false;
171 }
172
173 @Override
174 public void onDestroyActionMode(ActionMode mode) {
175 }
176
John Reck66302e52011-05-18 17:12:33 -0700177}