1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 package org
.libreoffice
;
11 import android
.content
.ClipData
;
12 import android
.content
.ClipboardManager
;
13 import android
.content
.Context
;
14 import androidx
.appcompat
.widget
.Toolbar
;
15 import android
.util
.Log
;
16 import android
.view
.KeyEvent
;
17 import android
.view
.Menu
;
18 import android
.view
.MenuItem
;
19 import android
.widget
.Toast
;
22 * Controls the changes to the toolbar.
24 public class ToolbarController
implements Toolbar
.OnMenuItemClickListener
{
25 private static final String LOGTAG
= ToolbarController
.class.getSimpleName();
26 private final Toolbar mToolbarTop
;
28 private final LibreOfficeMainActivity mContext
;
29 private final Menu mMainMenu
;
31 private boolean isEditModeOn
= false;
32 private String clipboardText
= null;
33 ClipboardManager clipboardManager
;
36 public ToolbarController(LibreOfficeMainActivity context
, Toolbar toolbarTop
) {
37 mToolbarTop
= toolbarTop
;
40 mToolbarTop
.inflateMenu(R
.menu
.main
);
41 mToolbarTop
.setOnMenuItemClickListener(this);
44 mMainMenu
= mToolbarTop
.getMenu();
45 clipboardManager
= (ClipboardManager
)mContext
.getSystemService(Context
.CLIPBOARD_SERVICE
);
48 private void enableMenuItem(final int menuItemId
, final boolean enabled
) {
49 LOKitShell
.getMainHandler().post(new Runnable() {
51 MenuItem menuItem
= mMainMenu
.findItem(menuItemId
);
52 if (menuItem
!= null) {
53 menuItem
.setEnabled(enabled
);
55 Log
.e(LOGTAG
, "MenuItem not found.");
61 public void setEditModeOn(boolean enabled
) {
62 isEditModeOn
= enabled
;
65 public boolean getEditModeStatus() {
70 * Change the toolbar to edit mode.
72 void switchToEditMode() {
73 if (!LOKitShell
.isEditingEnabled())
77 // Ensure the change is done on UI thread
78 LOKitShell
.getMainHandler().post(new Runnable() {
81 mMainMenu
.setGroupVisible(R
.id
.group_edit_actions
, true);
82 if (!LibreOfficeMainActivity
.isDeveloperMode() && mMainMenu
.findItem(R
.id
.action_UNO_commands
) != null) {
83 mMainMenu
.findItem(R
.id
.action_UNO_commands
).setVisible(false);
85 mMainMenu
.findItem(R
.id
.action_UNO_commands
).setVisible(true);
87 if(mContext
.getTileProvider() != null && mContext
.getTileProvider().isSpreadsheet()){
88 mMainMenu
.setGroupVisible(R
.id
.group_spreadsheet_options
, true);
89 } else if(mContext
.getTileProvider() != null && mContext
.getTileProvider().isPresentation()){
90 mMainMenu
.setGroupVisible(R
.id
.group_presentation_options
, true);
92 mToolbarTop
.setNavigationIcon(R
.drawable
.ic_check
);
93 mToolbarTop
.setLogo(null);
99 * Show clipboard Actions on the toolbar
101 void showClipboardActions(final String value
){
102 LOKitShell
.getMainHandler().post(new Runnable() {
106 mMainMenu
.setGroupVisible(R
.id
.group_edit_actions
, false);
107 mMainMenu
.setGroupVisible(R
.id
.group_edit_clipboard
, true);
108 if(getEditModeStatus()){
109 showHideClipboardCutAndCopy(true);
111 mMainMenu
.findItem(R
.id
.action_cut
).setVisible(false);
112 mMainMenu
.findItem(R
.id
.action_paste
).setVisible(false);
114 clipboardText
= value
;
120 void hideClipboardActions(){
121 LOKitShell
.getMainHandler().post(new Runnable() {
124 mMainMenu
.setGroupVisible(R
.id
.group_edit_actions
, getEditModeStatus());
125 mMainMenu
.setGroupVisible(R
.id
.group_edit_clipboard
, false);
130 void showHideClipboardCutAndCopy(final boolean option
){
131 LOKitShell
.getMainHandler().post(new Runnable() {
134 mMainMenu
.findItem(R
.id
.action_copy
).setVisible(option
);
135 mMainMenu
.findItem(R
.id
.action_cut
).setVisible(option
);
141 * Change the toolbar to view mode.
143 void switchToViewMode() {
144 // Ensure the change is done on UI thread
145 LOKitShell
.getMainHandler().post(new Runnable() {
148 mMainMenu
.setGroupVisible(R
.id
.group_edit_actions
, false);
149 mToolbarTop
.setNavigationIcon(R
.mipmap
.ic_launcher
);
150 mToolbarTop
.setLogo(null);
151 setEditModeOn(false);
152 mContext
.hideBottomToolbar();
153 mContext
.hideSoftKeyboard();
154 if(mContext
.getTileProvider() != null && mContext
.getTileProvider().isSpreadsheet()){
155 mMainMenu
.setGroupVisible(R
.id
.group_spreadsheet_options
, false);
156 } else if(mContext
.getTileProvider() != null && mContext
.getTileProvider().isPresentation()){
157 mMainMenu
.setGroupVisible(R
.id
.group_presentation_options
, false);
164 public boolean onMenuItemClick(MenuItem item
) {
165 final int itemId
= item
.getItemId();
166 if (itemId
== R
.id
.action_keyboard
) {
167 mContext
.showSoftKeyboard();
168 } else if (itemId
== R
.id
.action_format
) {
169 mContext
.showFormattingToolbar();
170 } else if (itemId
== R
.id
.action_about
) {
171 mContext
.showAbout();
173 } else if (itemId
== R
.id
.action_save
) {
174 mContext
.getTileProvider().saveDocument();
176 } else if (itemId
== R
.id
.action_save_as
) {
177 mContext
.saveDocumentAs();
179 } else if (itemId
== R
.id
.action_parts
) {
180 mContext
.openDrawer();
182 } else if (itemId
== R
.id
.action_exportToPDF
) {
183 mContext
.exportToPDF();
185 } else if (itemId
== R
.id
.action_print
) {
186 mContext
.getTileProvider().printDocument();
188 } else if (itemId
== R
.id
.action_settings
) {
189 mContext
.showSettings();
191 } else if (itemId
== R
.id
.action_search
) {
192 mContext
.showSearchToolbar();
194 } else if (itemId
== R
.id
.action_undo
) {
195 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:Undo"));
197 } else if (itemId
== R
.id
.action_redo
) {
198 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:Redo"));
200 } else if (itemId
== R
.id
.action_presentation
) {
201 mContext
.preparePresentation();
203 } else if (itemId
== R
.id
.action_add_slide
|| itemId
== R
.id
.action_add_worksheet
) {
206 } else if (itemId
== R
.id
.action_rename_worksheet
|| itemId
== R
.id
.action_rename_slide
) {
207 mContext
.renamePart();
209 } else if (itemId
== R
.id
.action_delete_worksheet
|| itemId
== R
.id
.action_delete_slide
) {
210 mContext
.deletePart();
212 } else if (itemId
== R
.id
.action_back
) {
213 hideClipboardActions();
215 } else if (itemId
== R
.id
.action_copy
) {
216 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:Copy"));
217 clipData
= ClipData
.newPlainText("clipboard data", clipboardText
);
218 clipboardManager
.setPrimaryClip(clipData
);
219 Toast
.makeText(mContext
, mContext
.getResources().getString(R
.string
.action_text_copied
), Toast
.LENGTH_SHORT
).show();
221 } else if (itemId
== R
.id
.action_paste
) {
222 clipData
= clipboardManager
.getPrimaryClip();
223 ClipData
.Item clipItem
= clipData
.getItemAt(0);
224 mContext
.setDocumentChanged(true);
225 return mContext
.getTileProvider().paste("text/plain;charset=utf-16", clipItem
.getText().toString());
226 } else if (itemId
== R
.id
.action_cut
) {
227 clipData
= ClipData
.newPlainText("clipboard data", clipboardText
);
228 clipboardManager
.setPrimaryClip(clipData
);
229 LOKitShell
.sendKeyEvent(new KeyEvent(KeyEvent
.ACTION_DOWN
, KeyEvent
.KEYCODE_DEL
));
230 mContext
.setDocumentChanged(true);
232 } else if (itemId
== R
.id
.action_UNO_commands
) {
233 mContext
.showUNOCommandsToolbar();
239 void setupToolbars() {
240 if (LibreOfficeMainActivity
.isExperimentalMode()) {
241 boolean enableSaveEntry
= !LibreOfficeMainActivity
.isReadOnlyMode() && mContext
.hasLocationForSave();
242 enableMenuItem(R
.id
.action_save
, enableSaveEntry
);
243 if (LibreOfficeMainActivity
.isReadOnlyMode()) {
244 // show message in case experimental mode is enabled (i.e. editing is supported in general),
245 // but current document is readonly
246 Toast
.makeText(mContext
, mContext
.getString(R
.string
.readonly_file
), Toast
.LENGTH_LONG
).show();
249 hideItem(R
.id
.action_save
);
251 mMainMenu
.findItem(R
.id
.action_parts
).setVisible(mContext
.isDrawerEnabled());
254 public void showItem(final int item
){
255 LOKitShell
.getMainHandler().post(new Runnable() {
258 mMainMenu
.findItem(item
).setVisible(true);
264 public void hideItem(final int item
){
265 LOKitShell
.getMainHandler().post(new Runnable() {
268 mMainMenu
.findItem(item
).setVisible(false);
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */