LibreOffice Module android (master) 1
ToolbarController.java
Go to the documentation of this file.
1/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9package org.libreoffice;
10
11import android.content.ClipData;
12import android.content.ClipboardManager;
13import android.content.Context;
14import androidx.appcompat.widget.Toolbar;
15import android.util.Log;
16import android.view.KeyEvent;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.widget.Toast;
20
24public class ToolbarController implements Toolbar.OnMenuItemClickListener {
25 private static final String LOGTAG = ToolbarController.class.getSimpleName();
26 private final Toolbar mToolbarTop;
27
29 private final Menu mMainMenu;
30
31 private boolean isEditModeOn = false;
32 private String clipboardText = null;
33 ClipboardManager clipboardManager;
34 ClipData clipData;
35
36 public ToolbarController(LibreOfficeMainActivity context, Toolbar toolbarTop) {
37 mToolbarTop = toolbarTop;
38 mContext = context;
39
40 mToolbarTop.inflateMenu(R.menu.main);
41 mToolbarTop.setOnMenuItemClickListener(this);
42 switchToViewMode();
43
44 mMainMenu = mToolbarTop.getMenu();
45 clipboardManager = (ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE);
46 }
47
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.");
56 }
57 }
58 });
59 }
60
61 public void setEditModeOn(boolean enabled) {
62 isEditModeOn = enabled;
63 }
64
65 public boolean getEditModeStatus() {
66 return isEditModeOn;
67 }
68
72 void switchToEditMode() {
74 return;
75
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);
86 }
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);
91 }
92 mToolbarTop.setNavigationIcon(R.drawable.ic_check);
93 mToolbarTop.setLogo(null);
94 }
95 });
96 }
97
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);
113 }
115 }
116 }
117 });
118 }
119
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);
126 }
127 });
128 }
129
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);
136 }
137 });
138 }
139
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.drawable.lo_icon);
150 mToolbarTop.setLogo(null);
151 setEditModeOn(false);
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);
158 }
159 }
160 });
161 }
162
163 @Override
164 public boolean onMenuItemClick(MenuItem item) {
165 final int itemId = item.getItemId();
166 if (itemId == R.id.action_keyboard) {
168 } else if (itemId == R.id.action_format) {
170 } else if (itemId == R.id.action_about) {
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) {
178 return true;
179 } else if (itemId == R.id.action_parts) {
181 return true;
182 } else if (itemId == R.id.action_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) {
190 return true;
191 } else if (itemId == R.id.action_search) {
193 return true;
194 } else if (itemId == R.id.action_undo) {
196 return true;
197 } else if (itemId == R.id.action_redo) {
199 return true;
200 } else if (itemId == R.id.action_presentation) {
202 return true;
203 } else if (itemId == R.id.action_add_slide || itemId == R.id.action_add_worksheet) {
205 return true;
206 } else if (itemId == R.id.action_rename_worksheet || itemId == R.id.action_rename_slide) {
208 return true;
209 } else if (itemId == R.id.action_delete_worksheet || itemId == R.id.action_delete_slide) {
211 return true;
212 } else if (itemId == R.id.action_back) {
213 hideClipboardActions();
214 return true;
215 } else if (itemId == R.id.action_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);
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));
231 return true;
232 } else if (itemId == R.id.action_UNO_commands) {
234 return true;
235 }
236 return false;
237 }
238
239 void setupToolbars() {
241 boolean enableSaveEntry = !LibreOfficeMainActivity.isReadOnlyMode() && mContext.hasLocationForSave();
242 enableMenuItem(R.id.action_save, enableSaveEntry);
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();
247 }
248 } else {
249 hideItem(R.id.action_save);
250 }
251 mMainMenu.findItem(R.id.action_parts).setVisible(mContext.isDrawerEnabled());
252 }
253
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);
259
260 }
261 });
262 }
263
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);
269
270 }
271 });
272 }
273
274}
275
276/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Events and data that is queued and processed by LOKitThread.
Definition: LOEvent.java:21
static final int UNO_COMMAND
Definition: LOEvent.java:35
Common static LOKit functions, functions to send events.
Definition: LOKitShell.java:26
static boolean isEditingEnabled()
Definition: LOKitShell.java:63
static void sendEvent(LOEvent event)
Make sure LOKitThread is running and send event to it.
Definition: LOKitShell.java:72
static void sendKeyEvent(KeyEvent event)
Send key event to LOKitThread.
Definition: LOKitShell.java:90
static Handler getMainHandler()
Definition: LOKitShell.java:36
Main activity of the LibreOffice App.
void saveDocumentAs()
Open file chooser and save the document to the URI selected there.
void hideSoftKeyboard()
Hides software keyboard on UI thread.
Controls the changes to the toolbar.
ToolbarController(LibreOfficeMainActivity context, Toolbar toolbarTop)
boolean onMenuItemClick(MenuItem item)
final LibreOfficeMainActivity mContext
void enableMenuItem(final int menuItemId, final boolean enabled)
Any value
def run(arg=None, arg2=-1)
const sal_uInt8 R