OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser; | |
6 | |
7 import android.view.ActionMode; | |
8 import android.view.Menu; | |
9 import android.view.MenuItem; | |
10 | |
11 import java.lang.reflect.Field; | |
12 import java.lang.reflect.InvocationTargetException; | |
13 import java.lang.reflect.Method; | |
14 | |
15 /** | |
16 * A class that represents a custom ActionMode.Callback. | |
17 */ | |
18 public class CustomSelectionActionModeCallback implements ActionMode.Callback { | |
19 | |
20 private static boolean sInitializedTypeMethods; | |
21 private static Method sGetTypeMethod; | |
22 private static int sTypeFloating; | |
23 | |
24 private ContextualMenuBar mContextualMenuBar; | |
25 | |
26 /** | |
27 * Sets the @param contextualMenuBar. | |
28 */ | |
29 public void setContextualMenuBar(ContextualMenuBar contextualMenuBar) { | |
30 mContextualMenuBar = contextualMenuBar; | |
31 } | |
32 | |
33 @Override | |
34 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { | |
35 return true; | |
36 } | |
37 | |
38 @Override | |
39 public void onDestroyActionMode(ActionMode mode) { | |
40 if (isFloatingActionMode(mode)) return; | |
41 mContextualMenuBar.hideControls(); | |
42 } | |
43 | |
44 @Override | |
45 public boolean onCreateActionMode(ActionMode mode, Menu menu) { | |
46 if (isFloatingActionMode(mode)) return true; | |
47 mContextualMenuBar.showControls(); | |
48 return true; | |
49 } | |
50 | |
51 @Override | |
52 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { | |
53 return false; | |
54 } | |
55 | |
56 // TODO(tedchoc): Delete this method and replace with just getType() when a
public M SDK is | |
57 // available. | |
58 private static boolean isFloatingActionMode(ActionMode mode) { | |
59 initializeGetTypeMethods(); | |
60 | |
61 if (sGetTypeMethod == null) return false; | |
62 | |
63 Object retVal = null; | |
64 try { | |
65 retVal = sGetTypeMethod.invoke(mode); | |
66 } catch (IllegalAccessException e) { | |
67 return false; | |
68 } catch (IllegalArgumentException e) { | |
69 return false; | |
70 } catch (InvocationTargetException e) { | |
71 return false; | |
72 } | |
73 if (!(retVal instanceof Integer)) return false; | |
74 | |
75 return ((Integer) retVal).intValue() == sTypeFloating; | |
76 } | |
77 | |
78 private static void initializeGetTypeMethods() { | |
79 if (sInitializedTypeMethods) return; | |
80 sInitializedTypeMethods = true; | |
81 | |
82 Method getType = null; | |
83 int typeFloating = -1; | |
84 try { | |
85 getType = ActionMode.class.getMethod("getType"); | |
86 } catch (NoSuchMethodException e) { | |
87 return; | |
88 } | |
89 | |
90 try { | |
91 Field field = ActionMode.class.getField("TYPE_FLOATING"); | |
92 Object value = field.get(null); | |
93 | |
94 if (value instanceof Integer) { | |
95 typeFloating = (Integer) value; | |
96 } else { | |
97 return; | |
98 } | |
99 } catch (NoSuchFieldException e) { | |
100 return; | |
101 } catch (IllegalAccessException e) { | |
102 return; | |
103 } catch (IllegalArgumentException e) { | |
104 return; | |
105 } | |
106 | |
107 sGetTypeMethod = getType; | |
108 sTypeFloating = typeFloating; | |
109 } | |
110 } | |
OLD | NEW |