1 package org
.libreoffice
;
3 import android
.view
.View
;
4 import android
.widget
.EditText
;
5 import android
.widget
.ImageButton
;
7 import org
.json
.JSONException
;
8 import org
.json
.JSONObject
;
10 public class SearchController
implements View
.OnClickListener
{
11 private LibreOfficeMainActivity mActivity
;
13 private enum SearchDriection
{
17 public SearchController(LibreOfficeMainActivity activity
) {
20 ((ImageButton
) activity
.findViewById(R
.id
.button_search_up
)).setOnClickListener(this);
21 ((ImageButton
) activity
.findViewById(R
.id
.button_search_down
)).setOnClickListener(this);
24 private void search(String searchString
, SearchDriection direction
, float x
, float y
) {
26 JSONObject rootJson
= new JSONObject();
28 addProperty(rootJson
, "SearchItem.SearchString", "string", searchString
);
29 addProperty(rootJson
, "SearchItem.Backward", "boolean", direction
== SearchDriection
.DOWN ?
"true" : "false");
30 addProperty(rootJson
, "SearchItem.SearchStartPointX", "long", String
.valueOf((long) UnitConverter
.pixelToTwip(x
, LOKitShell
.getDpi())));
31 addProperty(rootJson
, "SearchItem.SearchStartPointY", "long", String
.valueOf((long) UnitConverter
.pixelToTwip(y
, LOKitShell
.getDpi())));
32 addProperty(rootJson
, "SearchItem.Command", "long", String
.valueOf(0)); // search all == 1
34 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:ExecuteSearch", rootJson
.toString()));
36 } catch (JSONException e
) {
41 private void addProperty(JSONObject json
, String parentValue
, String type
, String value
) throws JSONException
{
42 JSONObject child
= new JSONObject();
43 child
.put("type", type
);
44 child
.put("value", value
);
45 json
.put(parentValue
, child
);
49 public void onClick(View view
) {
50 ImageButton button
= (ImageButton
) view
;
52 SearchDriection direction
= SearchDriection
.DOWN
;
53 switch(button
.getId()) {
54 case R
.id
.button_search_down
:
55 direction
= SearchDriection
.DOWN
;
57 case R
.id
.button_search_up
:
58 direction
= SearchDriection
.UP
;
64 String searchText
= ((EditText
) mActivity
.findViewById(R
.id
.search_string
)).getText().toString();
66 float x
= mActivity
.getCurrentCursorPosition().centerX();
67 float y
= mActivity
.getCurrentCursorPosition().centerY();
68 search(searchText
, direction
, x
, y
);