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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* Communication module for Android terminals. -*- c-file-style: "GNU" -*-
Copyright (C) 2023 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
package org.gnu.emacs;
import java.util.List;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.SubMenu;
import android.util.Log;
import android.widget.PopupMenu;
/* Context menu implementation. This object is built from JNI and
describes a menu hiearchy. Then, `inflate' can turn it into an
Android menu, which can be turned into a popup (or other kind of)
menu. */
public class EmacsContextMenu
{
private static final String TAG = "EmacsContextMenu";
/* Whether or not an item was selected. */
public static boolean itemAlreadySelected;
/* Whether or not a submenu was selected. */
public static boolean wasSubmenuSelected;
private class Item implements MenuItem.OnMenuItemClickListener
{
public int itemID;
public String itemName;
public EmacsContextMenu subMenu;
public boolean isEnabled;
@Override
public boolean
onMenuItemClick (MenuItem item)
{
Log.d (TAG, "onMenuItemClick: " + itemName + " (" + itemID + ")");
if (subMenu != null)
{
/* After opening a submenu within a submenu, Android will
send onContextMenuClosed for a ContextMenuBuilder. This
will normally confuse Emacs into thinking that the
context menu has been dismissed. Wrong!
Setting this flag makes EmacsActivity to only handle
SubMenuBuilder being closed, which always means the menu
has actually been dismissed. */
wasSubmenuSelected = true;
return false;
}
/* Send a context menu event. */
EmacsNative.sendContextMenu ((short) 0, itemID);
/* Say that an item has already been selected. */
itemAlreadySelected = true;
return true;
}
};
public List<Item> menuItems;
public String title;
private EmacsContextMenu parent;
/* Create a context menu with no items inside and the title TITLE,
which may be NULL. */
public static EmacsContextMenu
createContextMenu (String title)
{
EmacsContextMenu menu;
menu = new EmacsContextMenu ();
menu.menuItems = new ArrayList<Item> ();
menu.title = title;
return menu;
}
/* Add a normal menu item to the context menu with the id ITEMID and
the name ITEMNAME. Enable it if ISENABLED, else keep it
disabled. */
public void
addItem (int itemID, String itemName, boolean isEnabled)
{
Item item;
item = new Item ();
item.itemID = itemID;
item.itemName = itemName;
item.isEnabled = isEnabled;
menuItems.add (item);
}
/* Create a disabled menu item with the name ITEMNAME. */
public void
addPane (String itemName)
{
Item item;
item = new Item ();
item.itemName = itemName;
menuItems.add (item);
}
/* Add a submenu to the context menu with the specified title and
item name. */
public EmacsContextMenu
addSubmenu (String itemName, String title)
{
EmacsContextMenu submenu;
Item item;
item = new Item ();
item.itemID = 0;
item.itemName = itemName;
item.subMenu = createContextMenu (title);
item.subMenu.parent = this;
menuItems.add (item);
return item.subMenu;
}
/* Add the contents of this menu to MENU. */
private void
inflateMenuItems (Menu menu)
{
Intent intent;
MenuItem menuItem;
SubMenu submenu;
for (Item item : menuItems)
{
if (item.subMenu != null)
{
try
{
/* This is a submenu. On versions of Android which
support doing so, create the submenu and add the
contents of the menu to it. */
submenu = menu.addSubMenu (item.itemName);
}
catch (UnsupportedOperationException exception)
{
/* This version of Android has a restriction
preventing submenus from being added to submenus.
Inflate everything into the parent menu
instead. */
item.subMenu.inflateMenuItems (menu);
continue;
}
/* This is still needed to set wasSubmenuSelected. */
menuItem = submenu.getItem ();
menuItem.setOnMenuItemClickListener (item);
}
else
{
menuItem = menu.add (item.itemName);
menuItem.setOnMenuItemClickListener (item);
/* If the item ID is zero, then disable the item. */
if (item.itemID == 0 || !item.isEnabled)
menuItem.setEnabled (false);
}
}
}
/* Enter the items in this context menu to MENU. Create each menu
item with an Intent containing a Bundle, where the key
"emacs:menu_item_hi" maps to the high 16 bits of the
corresponding item ID, and the key "emacs:menu_item_low" maps to
the low 16 bits of the item ID. */
public void
expandTo (Menu menu)
{
inflateMenuItems (menu);
}
/* Return the parent or NULL. */
public EmacsContextMenu
parent ()
{
return this.parent;
}
/* Like display, but does the actual work and runs in the main
thread. */
private boolean
display1 (EmacsWindow window, int xPosition, int yPosition)
{
/* Set this flag to false. It is used to decide whether or not to
send 0 in response to the context menu being closed. */
itemAlreadySelected = false;
/* No submenu has been selected yet. */
wasSubmenuSelected = false;
return window.view.popupMenu (this, xPosition, yPosition);
}
/* Display this context menu on WINDOW, at xPosition and
yPosition. */
public boolean
display (final EmacsWindow window, final int xPosition,
final int yPosition)
{
Runnable runnable;
final Holder<Boolean> rc;
rc = new Holder<Boolean> ();
runnable = new Runnable () {
@Override
public void
run ()
{
synchronized (this)
{
rc.thing = display1 (window, xPosition, yPosition);
notify ();
}
}
};
synchronized (runnable)
{
EmacsService.SERVICE.runOnUiThread (runnable);
try
{
runnable.wait ();
}
catch (InterruptedException e)
{
EmacsNative.emacsAbort ();
}
}
return rc.thing;
}
/* Dismiss this context menu. WINDOW is the window where the
context menu is being displayed. */
public void
dismiss (final EmacsWindow window)
{
Runnable runnable;
EmacsService.SERVICE.runOnUiThread (new Runnable () {
@Override
public void
run ()
{
window.view.cancelPopupMenu ();
itemAlreadySelected = false;
}
});
}
};
|