LibreOffice Module android (master) 1
SearchController.java
Go to the documentation of this file.
1package org.libreoffice;
2
3import android.view.KeyEvent;
4import android.view.View;
5import android.view.inputmethod.EditorInfo;
6import android.widget.EditText;
7import android.widget.ImageButton;
8import android.widget.TextView;
9
10import org.json.JSONException;
11import org.json.JSONObject;
12
13public class SearchController implements View.OnClickListener {
15
16 private enum SearchDirection {
18 }
19
21 mActivity = activity;
22
23 activity.findViewById(R.id.button_search_up).setOnClickListener(this);
24 activity.findViewById(R.id.button_search_down).setOnClickListener(this);
25
26 ((EditText) mActivity.findViewById(R.id.search_string)).setOnEditorActionListener(new TextView.OnEditorActionListener() {
27 @Override
28 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
29 if (actionId == EditorInfo.IME_ACTION_SEARCH) {
30 // search downward when the "search button" on keyboard is pressed,
32 String searchText = ((EditText) mActivity.findViewById(R.id.search_string)).getText().toString();
33 float x = mActivity.getCurrentCursorPosition().centerX();
34 float y = mActivity.getCurrentCursorPosition().centerY();
35 search(searchText, direction, x, y);
36 return true;
37 }
38 return false;
39 }
40 });
41 }
42
43 private void search(String searchString, SearchDirection direction, float x, float y) {
44 try {
45 JSONObject rootJson = new JSONObject();
46
47 addProperty(rootJson, "SearchItem.SearchString", "string", searchString);
48 addProperty(rootJson, "SearchItem.Backward", "boolean", direction == SearchDirection.UP ? "true" : "false");
49 addProperty(rootJson, "SearchItem.SearchStartPointX", "long", String.valueOf((long) UnitConverter.pixelToTwip(x, LOKitShell.getDpi(mActivity))));
50 addProperty(rootJson, "SearchItem.SearchStartPointY", "long", String.valueOf((long) UnitConverter.pixelToTwip(y, LOKitShell.getDpi(mActivity))));
51 addProperty(rootJson, "SearchItem.Command", "long", String.valueOf(0)); // search all == 1
52
53 LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:ExecuteSearch", rootJson.toString()));
54
55 } catch (JSONException e) {
56 e.printStackTrace();
57 }
58 }
59
60 public static void addProperty(JSONObject json, String parentValue, String type, String value) throws JSONException {
61 JSONObject child = new JSONObject();
62 child.put("type", type);
63 child.put("value", value);
64 json.put(parentValue, child);
65 }
66
67 @Override
68 public void onClick(View view) {
69 ImageButton button = (ImageButton) view;
70
72 if (button.getId() == R.id.button_search_up) {
73 direction = SearchDirection.UP;
74 }
75
76 String searchText = ((EditText) mActivity.findViewById(R.id.search_string)).getText().toString();
77
78 float x = mActivity.getCurrentCursorPosition().centerX();
79 float y = mActivity.getCurrentCursorPosition().centerY();
80 search(searchText, direction, x, y);
81 }
82}
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 void sendEvent(LOEvent event)
Make sure LOKitThread is running and send event to it.
Definition: LOKitShell.java:72
static float getDpi(Context context)
Definition: LOKitShell.java:27
Main activity of the LibreOffice App.
void search(String searchString, SearchDirection direction, float x, float y)
static void addProperty(JSONObject json, String parentValue, String type, String value)
final LibreOfficeMainActivity mActivity
static float pixelToTwip(float input, float dpi)
Any value
float v
float y
float x
ResultType type
const sal_uInt8 R