summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/libreoffice/ToolbarController.java
blob: 34eff3fc3d9c0a04037ca14223932dc6e6583326 (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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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.libreoffice;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.libreoffice.canvas.ImageUtils;
import org.libreoffice.kit.Document;

/**
 * Controls the changes to the toolbar.
 */
public class ToolbarController {
    private static final String LOGTAG = ToolbarController.class.getSimpleName();
    private final Toolbar mToolbar;
    private final ActionBar mActionBar;
    private Menu mOptionsMenu;
    private Context mContext;

    public ToolbarController(Context context, ActionBar actionBar, Toolbar toolbar) {
        mToolbar = toolbar;
        mActionBar = actionBar;
        mContext = context;
        switchToViewMode();
    }

    public void onToggleStateChanged(int type, boolean pressed) {
        MenuItem menuItem = null;
        Bitmap icon = null;
        switch (type) {
            case Document.BOLD:
                menuItem = mOptionsMenu.findItem(R.id.action_bold);
                icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.action_bold);
                break;
            case Document.ITALIC:
                menuItem = mOptionsMenu.findItem(R.id.action_italic);
                icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.action_italic);
                break;
            case Document.UNDERLINE:
                menuItem = mOptionsMenu.findItem(R.id.action_underline);
                icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.action_underline);
                break;
            case Document.STRIKEOUT:
                menuItem = mOptionsMenu.findItem(R.id.action_strikeout);
                icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.action_strikeout);
                break;
            default:
                Log.e(LOGTAG, "Uncaptured state change type: " + type);
                return;
        }

        if (menuItem == null) {
            Log.e(LOGTAG, "MenuItem not found.");
            return;
        }

        if (pressed) {
            icon = ImageUtils.bitmapToPressed(icon);
        }

        menuItem.setIcon(new BitmapDrawable(mContext.getResources(), icon));
    }

    public void setOptionMenu(Menu menu) {
        mOptionsMenu = menu;
    }

    /**
     * Change the toolbar to edit mode.
     */
    void switchToEditMode() {
        // Ensure the change is done on UI thread
        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                mActionBar.setDisplayHomeAsUpEnabled(false);
                mToolbar.setNavigationIcon(R.drawable.ic_check_grey600_24dp);
                mToolbar.setTitle(null);

            }
        });
    }

    /**
     * Change the toolbar to view mode.
     */
    void switchToViewMode() {
        // Ensure the change is done on UI thread
        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                mToolbar.setNavigationIcon(R.drawable.ic_menu_grey600_24dp);
                mToolbar.setTitle("LibreOffice");
                mActionBar.setDisplayHomeAsUpEnabled(true);
            }
        });
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */