summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/text/FloatingActionModeCallback.java
blob: 07f17590d1bd777b3dc7f3321d61bdd86e6db670 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.text;

import android.annotation.TargetApi;
import android.graphics.Rect;
import android.os.Build;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import org.mozilla.gecko.GeckoAppShell;

import java.util.List;

@TargetApi(Build.VERSION_CODES.M)
public class FloatingActionModeCallback extends ActionMode.Callback2 {
    private FloatingToolbarTextSelection textSelection;
    private List<TextAction> actions;

    public FloatingActionModeCallback(FloatingToolbarTextSelection textSelection, List<TextAction> actions) {
        this.textSelection = textSelection;
        this.actions = actions;
    }

    public void updateActions(List<TextAction> actions) {
        this.actions = actions;
    }

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

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        menu.clear();

        for (int i = 0; i < actions.size(); i++) {
            final TextAction action = actions.get(i);
            menu.add(Menu.NONE, i, action.getFloatingOrder(), action.getLabel());
        }

        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        final TextAction action = actions.get(item.getItemId());

        GeckoAppShell.notifyObservers("TextSelection:Action", action.getId());

        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {}

    @Override
    public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
        final Rect contentRect = textSelection.contentRect;
        if (contentRect != null) {
            outRect.set(contentRect);
        }
    }
}