This source file includes following definitions.
- onMenuItemClick
- createContextMenu
- addItem
- addPane
- addSubmenu
- inflateMenuItems
- expandTo
- parent
- display1
- display
- dismiss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.gnu.emacs;
21
22 import java.util.List;
23 import java.util.ArrayList;
24
25 import android.content.Context;
26 import android.content.Intent;
27
28 import android.os.Build;
29
30 import android.view.ContextMenu;
31 import android.view.Menu;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.SubMenu;
35
36 import android.util.Log;
37
38
39
40
41
42
43 public final class EmacsContextMenu
44 {
45 private static final String TAG = "EmacsContextMenu";
46
47
48 public static boolean itemAlreadySelected;
49
50
51
52
53
54 public static long wasSubmenuSelected;
55
56
57 public static int lastMenuEventSerial;
58
59
60 public int lastGroupId;
61
62 private static final class Item implements MenuItem.OnMenuItemClickListener
63 {
64 public int itemID;
65 public String itemName, tooltip;
66 public EmacsContextMenu subMenu;
67 public boolean isEnabled, isCheckable, isChecked;
68 public EmacsView inflatedView;
69 public boolean isRadio;
70
71 @Override
72 public boolean
73 onMenuItemClick (MenuItem item)
74 {
75 Log.d (TAG, "onMenuItemClick: " + itemName + " (" + itemID + ")");
76
77 if (subMenu != null)
78 {
79
80
81
82 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
83 {
84 Log.d (TAG, "onMenuItemClick: displaying submenu " + subMenu);
85
86
87
88
89 wasSubmenuSelected = -2;
90
91
92
93
94
95 inflatedView.post (new Runnable () {
96 @Override
97 public void
98 run ()
99 {
100 inflatedView.popupMenu (subMenu, 0, 0, true);
101 }
102 });
103
104 return true;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 wasSubmenuSelected = System.currentTimeMillis ();
121 return false;
122 }
123
124
125 EmacsNative.sendContextMenu ((short) 0, itemID,
126 lastMenuEventSerial);
127
128
129 itemAlreadySelected = true;
130 return true;
131 }
132 };
133
134
135 public List<Item> menuItems;
136
137
138 private EmacsContextMenu parent;
139
140
141 private String title;
142
143
144
145
146
147
148 public static EmacsContextMenu
149 createContextMenu (String title)
150 {
151 EmacsContextMenu menu;
152
153 menu = new EmacsContextMenu ();
154 menu.title = title;
155 menu.menuItems = new ArrayList<Item> ();
156
157 return menu;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public void
173 addItem (int itemID, String itemName, boolean isEnabled,
174 boolean isCheckable, boolean isChecked,
175 String tooltip, boolean isRadio)
176 {
177 Item item;
178
179 item = new Item ();
180 item.itemID = itemID;
181 item.itemName = itemName;
182 item.isEnabled = isEnabled;
183 item.isCheckable = isCheckable;
184 item.isChecked = isChecked;
185 item.tooltip = tooltip;
186 item.isRadio = isRadio;
187
188 menuItems.add (item);
189 }
190
191
192
193 public void
194 addPane (String itemName)
195 {
196 Item item;
197
198 item = new Item ();
199 item.itemName = itemName;
200
201 menuItems.add (item);
202 }
203
204
205
206
207 public EmacsContextMenu
208 addSubmenu (String itemName, String tooltip)
209 {
210 EmacsContextMenu submenu;
211 Item item;
212
213 item = new Item ();
214 item.itemID = 0;
215 item.itemName = itemName;
216 item.tooltip = tooltip;
217 item.subMenu = createContextMenu (itemName);
218 item.subMenu.parent = this;
219
220 menuItems.add (item);
221 return item.subMenu;
222 }
223
224
225
226
227 private void
228 inflateMenuItems (Menu menu, EmacsView inflatedView)
229 {
230 Intent intent;
231 MenuItem menuItem;
232 SubMenu submenu;
233
234 for (Item item : menuItems)
235 {
236 if (item.subMenu != null)
237 {
238
239
240
241
242
243
244
245
246 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
247 {
248 submenu = menu.addSubMenu (item.itemName);
249 item.subMenu.inflateMenuItems (submenu, inflatedView);
250
251
252 menuItem = submenu.getItem ();
253 }
254 else
255 menuItem = menu.add (item.itemName);
256
257 item.inflatedView = inflatedView;
258 menuItem.setOnMenuItemClickListener (item);
259 }
260 else
261 {
262 if (item.isRadio)
263 menuItem = menu.add (++lastGroupId, Menu.NONE, Menu.NONE,
264 item.itemName);
265 else
266 menuItem = menu.add (item.itemName);
267 menuItem.setOnMenuItemClickListener (item);
268
269
270 if (item.itemID == 0 || !item.isEnabled)
271 menuItem.setEnabled (false);
272
273
274
275
276 if (item.isCheckable)
277 menuItem.setCheckable (true);
278
279 if (item.isChecked)
280 menuItem.setChecked (true);
281
282
283
284
285 if (item.isRadio)
286 menu.setGroupCheckable (lastGroupId, true, true);
287
288
289
290
291 if (item.tooltip != null
292 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
293 menuItem.setTooltipText (item.tooltip);
294 }
295 }
296 }
297
298
299
300
301
302
303
304
305 public void
306 expandTo (Menu menu, EmacsView view)
307 {
308 inflateMenuItems (menu, view);
309
310
311 if (title == null || !(menu instanceof ContextMenu))
312 return;
313
314
315 ((ContextMenu) menu).setHeaderTitle (title);
316 }
317
318
319
320 public EmacsContextMenu
321 parent ()
322 {
323 return this.parent;
324 }
325
326
327
328
329 private boolean
330 display1 (EmacsWindow window, int xPosition, int yPosition)
331 {
332
333
334 itemAlreadySelected = false;
335
336
337 wasSubmenuSelected = -1;
338
339 return window.view.popupMenu (this, xPosition, yPosition,
340 false);
341 }
342
343
344
345
346
347 public boolean
348 display (final EmacsWindow window, final int xPosition,
349 final int yPosition, final int serial)
350 {
351 Runnable runnable;
352 final EmacsHolder<Boolean> rc;
353
354 rc = new EmacsHolder<Boolean> ();
355 rc.thing = false;
356
357 runnable = new Runnable () {
358 @Override
359 public void
360 run ()
361 {
362 synchronized (this)
363 {
364 lastMenuEventSerial = serial;
365 rc.thing = display1 (window, xPosition, yPosition);
366 notify ();
367 }
368 }
369 };
370
371 EmacsService.syncRunnable (runnable);
372 return rc.thing;
373 }
374
375
376
377
378 public void
379 dismiss (final EmacsWindow window)
380 {
381 Runnable runnable;
382
383 EmacsService.SERVICE.runOnUiThread (new Runnable () {
384 @Override
385 public void
386 run ()
387 {
388 window.view.cancelPopupMenu ();
389 itemAlreadySelected = false;
390 }
391 });
392 }
393 };