summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BookmarkDragHandler.java
blob: fc0752fea45634ee38af2a7219d2450fb2fca55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.Activity;
import android.content.ClipData;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BrowserContract;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;

public class BookmarkDragHandler implements Callback {

    public static interface BookmarkDragController {
        boolean startDrag(Cursor item);

        ViewGroup getActionModeView(ActionMode mode, BookmarkDragState state);
        void actionItemClicked(View v, BookmarkDragState state);
    }

    public static interface BookmarkDragAdapter {
        void setBookmarkDragHandler(BookmarkDragHandler handler);
        Cursor getItemForView(View v);
    }

    public static class BookmarkDragState {
        public long id;
        public long parent;
        public Object extraState;
    }

    static final String BOOKMARK_DRAG_LABEL = "com.android.browser.BOOKMARK_LABEL";

    private Activity mActivity;
    private BookmarkDragController mDragController;
    private BookmarkDragAdapter mDragAdapter;
    private ActionMode mActionMode;
    private BookmarkDragState mDragState;

    public BookmarkDragHandler(Activity activity, BookmarkDragController controller,
            BookmarkDragAdapter adapter) {
        mActivity = activity;
        mDragController = controller;
        mDragAdapter = adapter;
        mDragAdapter.setBookmarkDragHandler(this);
    }

    public boolean startDrag(View view, Cursor item, long id, Object extraState) {
        if (!mDragController.startDrag(item)) {
            return false;
        }
        Uri uri = ContentUris.withAppendedId(
                BrowserContract.Bookmarks.CONTENT_URI, id);
        ClipData data = ClipData.newRawUri(BOOKMARK_DRAG_LABEL, uri);
        BookmarkDragState state = new BookmarkDragState();
        state.id = id;
        state.parent = item.getLong(BookmarksLoader.COLUMN_INDEX_PARENT);
        state.extraState = extraState;
        mDragState = state;
        view.startDrag(data, new View.DragShadowBuilder(view), state, 0);
        mActionMode = view.startActionMode(this);
        return true;
    }

    public void registerBookmarkDragHandler(View view) {
        view.setOnDragListener(mBookmarkDragListener);
    }

    private OnDragListener mBookmarkDragListener = new OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            Cursor c = mDragAdapter.getItemForView(v);
            BookmarkDragState state = (BookmarkDragState) event.getLocalState();
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DROP:
                long id = c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
                if (id == state.id) {
                    // We dropped onto ourselves, show the context menu
                    v.showContextMenu();
                    return false;
                }
                long parent = c.getLong(BookmarksLoader.COLUMN_INDEX_PARENT);
                if (isFolder(c)) {
                    parent = c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
                }
                if (parent != state.parent) {
                    ContentResolver cr = mActivity.getContentResolver();
                    ContentValues values = new ContentValues();
                    values.put(BrowserContract.Bookmarks.PARENT, parent);
                    Uri uri = event.getClipData().getItemAt(0).getUri();
                    cr.update(uri, values, null, null);
                }
                break;
            }
            return false;
        }
    };

    private OnDragListener mActionModeDragListener = new OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            BookmarkDragState state = (BookmarkDragState) event.getLocalState();
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DROP:
                mDragController.actionItemClicked(v, state);
                // fall through
            case DragEvent.ACTION_DRAG_ENDED:
                if (mActionMode != null) {
                    mActionMode.finish();
                    mActionMode = null;
                }
                return true;
            }
            return false;
        }
    };

    static boolean isFolder(Cursor c) {
        return c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        ViewGroup view = mDragController.getActionModeView(mode, mDragState);
        int count = view.getChildCount();
        for (int i = 0; i < count; i++) {
            view.getChildAt(i).setOnDragListener(mActionModeDragListener);
        }
        mode.setCustomView(view);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }

}