cid#1607171 Data race condition
[LibreOffice.git] / android / source / src / java / org / libreoffice / SearchController.java
blob6095e1fd2afeb606bffc5d35b627b77a05f6911a
1 package org.libreoffice;
3 import android.view.KeyEvent;
4 import android.view.View;
5 import android.view.inputmethod.EditorInfo;
6 import android.widget.EditText;
7 import android.widget.ImageButton;
8 import android.widget.TextView;
10 import org.json.JSONException;
11 import org.json.JSONObject;
13 public class SearchController implements View.OnClickListener {
14 private final LibreOfficeMainActivity mActivity;
16 private enum SearchDirection {
17 UP, DOWN
20 SearchController(LibreOfficeMainActivity activity) {
21 mActivity = activity;
23 activity.findViewById(R.id.button_search_up).setOnClickListener(this);
24 activity.findViewById(R.id.button_search_down).setOnClickListener(this);
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,
31 SearchDirection direction = SearchDirection.DOWN;
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;
38 return false;
40 });
43 private void search(String searchString, SearchDirection direction, float x, float y) {
44 try {
45 JSONObject rootJson = new JSONObject();
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
53 LOKitShell.sendEvent(new LOEvent(LOEvent.UNO_COMMAND, ".uno:ExecuteSearch", rootJson.toString()));
55 } catch (JSONException e) {
56 e.printStackTrace();
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);
67 @Override
68 public void onClick(View view) {
69 ImageButton button = (ImageButton) view;
71 SearchDirection direction = SearchDirection.DOWN;
72 if (button.getId() == R.id.button_search_up) {
73 direction = SearchDirection.UP;
76 String searchText = ((EditText) mActivity.findViewById(R.id.search_string)).getText().toString();
78 float x = mActivity.getCurrentCursorPosition().centerX();
79 float y = mActivity.getCurrentCursorPosition().centerY();
80 search(searchText, direction, x, y);