cid#1607171 Data race condition
[LibreOffice.git] / android / source / src / java / org / libreoffice / ToolbarController.java
blob603d2258167e61a843f2add1133446e3944cad78
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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;
21 /**
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;
34 ClipData clipData;
36 public ToolbarController(LibreOfficeMainActivity context, Toolbar toolbarTop) {
37 mToolbarTop = toolbarTop;
38 mContext = context;
40 mToolbarTop.inflateMenu(R.menu.main);
41 mToolbarTop.setOnMenuItemClickListener(this);
42 switchToViewMode();
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() {
50 public void run() {
51 MenuItem menuItem = mMainMenu.findItem(menuItemId);
52 if (menuItem != null) {
53 menuItem.setEnabled(enabled);
54 } else {
55 Log.e(LOGTAG, "MenuItem not found.");
58 });
61 public void setEditModeOn(boolean enabled) {
62 isEditModeOn = enabled;
65 public boolean getEditModeStatus() {
66 return isEditModeOn;
69 /**
70 * Change the toolbar to edit mode.
72 void switchToEditMode() {
73 if (!LOKitShell.isEditingEnabled())
74 return;
76 setEditModeOn(true);
77 // Ensure the change is done on UI thread
78 LOKitShell.getMainHandler().post(new Runnable() {
79 @Override
80 public void run() {
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);
84 } else {
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);
95 });
98 /**
99 * Show clipboard Actions on the toolbar
100 * */
101 void showClipboardActions(final String value){
102 LOKitShell.getMainHandler().post(new Runnable() {
103 @Override
104 public void run() {
105 if(value != null){
106 mMainMenu.setGroupVisible(R.id.group_edit_actions, false);
107 mMainMenu.setGroupVisible(R.id.group_edit_clipboard, true);
108 if(getEditModeStatus()){
109 showHideClipboardCutAndCopy(true);
110 } else {
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() {
122 @Override
123 public void run() {
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() {
132 @Override
133 public void run() {
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() {
146 @Override
147 public void run() {
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);
163 @Override
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();
172 return true;
173 } else if (itemId == R.id.action_save) {
174 mContext.getTileProvider().saveDocument();
175 return true;
176 } else if (itemId == R.id.action_save_as) {
177 mContext.saveDocumentAs();
178 return true;
179 } else if (itemId == R.id.action_parts) {
180 mContext.openDrawer();
181 return true;
182 } else if (itemId == R.id.action_exportToPDF) {
183 mContext.exportToPDF();
184 return true;
185 } else if (itemId == R.id.action_print) {
186 mContext.getTileProvider().printDocument();
187 return true;
188 } else if (itemId == R.id.action_settings) {
189 mContext.showSettings();
190 return true;
191 } else if (itemId == R.id.action_search) {
192 mContext.showSearchToolbar();
193 return true;
194 } else if (itemId == R.id.action_undo) {
195 LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:Undo"));
196 return true;
197 } else if (itemId == R.id.action_redo) {
198 LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:Redo"));
199 return true;
200 } else if (itemId == R.id.action_presentation) {
201 mContext.preparePresentation();
202 return true;
203 } else if (itemId == R.id.action_add_slide || itemId == R.id.action_add_worksheet) {
204 mContext.addPart();
205 return true;
206 } else if (itemId == R.id.action_rename_worksheet || itemId == R.id.action_rename_slide) {
207 mContext.renamePart();
208 return true;
209 } else if (itemId == R.id.action_delete_worksheet || itemId == R.id.action_delete_slide) {
210 mContext.deletePart();
211 return true;
212 } else if (itemId == R.id.action_back) {
213 hideClipboardActions();
214 return true;
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();
220 return true;
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);
231 return true;
232 } else if (itemId == R.id.action_UNO_commands) {
233 mContext.showUNOCommandsToolbar();
234 return true;
236 return false;
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();
248 } else {
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() {
256 @Override
257 public void run() {
258 mMainMenu.findItem(item).setVisible(true);
264 public void hideItem(final int item){
265 LOKitShell.getMainHandler().post(new Runnable() {
266 @Override
267 public void run() {
268 mMainMenu.findItem(item).setVisible(false);
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */