1 package org
.libreoffice
;
3 import android
.app
.Activity
;
4 import android
.content
.Context
;
5 import android
.view
.View
;
6 import android
.widget
.AdapterView
;
7 import android
.widget
.ArrayAdapter
;
8 import android
.widget
.Spinner
;
10 import org
.json
.JSONArray
;
11 import org
.json
.JSONException
;
12 import org
.json
.JSONObject
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
20 public class FontController
implements AdapterView
.OnItemSelectedListener
{
22 private boolean mFontNameSpinnerSet
= false;
23 private boolean mFontSizeSpinnerSet
= false;
24 private Activity mActivity
;
25 private List
<String
> mFontList
= null;
26 private List
<String
> mFontSizes
= new ArrayList
<String
>();
27 private Map
<String
, List
<String
>> mAllFontSizes
= null;
29 private String mCurrentFontSelected
= null;
30 private String mCurrentFontSizeSelected
= null;
32 public FontController(Activity activity
) {
36 private void sendFontChange(String fontName
) {
38 JSONObject json
= new JSONObject();
39 JSONObject valueJson
= new JSONObject();
40 valueJson
.put("type", "string");
41 valueJson
.put("value", fontName
);
42 json
.put("CharFontName.FamilyName", valueJson
);
44 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:CharFontName", json
.toString()));
46 } catch (JSONException e
) {
51 private void sendFontSizeChange(String fontSize
) {
53 JSONObject json
= new JSONObject();
54 JSONObject valueJson
= new JSONObject();
55 valueJson
.put("type", "float");
56 valueJson
.put("value", fontSize
);
57 json
.put("FontHeight.Height", valueJson
);
59 LOKitShell
.sendEvent(new LOEvent(LOEvent
.UNO_COMMAND
, ".uno:FontHeight", json
.toString()));
61 } catch (JSONException e
) {
67 public void onItemSelected(AdapterView
<?
> parent
, View view
, int pos
, long id
) {
68 if (mFontList
== null || !mFontNameSpinnerSet
)
70 if (parent
== mActivity
.findViewById(R
.id
.font_name_spinner
)) {
71 String currentFontSelected
= parent
.getItemAtPosition(pos
).toString();
72 if (!currentFontSelected
.equals(mCurrentFontSelected
)) {
73 mCurrentFontSelected
= currentFontSelected
;
74 sendFontChange(mCurrentFontSelected
);
76 } else if (parent
== mActivity
.findViewById(R
.id
.font_size_spinner
)) {
77 String currentFontSizeSelected
= parent
.getItemAtPosition(pos
).toString();
78 if (!currentFontSizeSelected
.equals(mCurrentFontSizeSelected
)) {
79 mCurrentFontSizeSelected
= currentFontSizeSelected
;
80 sendFontSizeChange(mCurrentFontSizeSelected
);
86 public void onNothingSelected(AdapterView parent
) {
90 public void parseJson(String json
) {
91 mFontList
= new ArrayList
<String
>();
92 mAllFontSizes
= new HashMap
<String
, List
<String
>>();
94 JSONObject jObject
= new JSONObject(json
);
95 JSONObject jObject2
= jObject
.getJSONObject("commandValues");
96 Iterator
<String
> keys
= jObject2
.keys();
97 List
<String
> fontSizes
;
98 while (keys
.hasNext()) {
99 String key
= keys
.next();
101 JSONArray array
= jObject2
.getJSONArray(key
);
102 fontSizes
= new ArrayList
<String
>();
103 for (int i
= 0; i
< array
.length(); i
++) {
104 fontSizes
.add(array
.getString(i
));
106 mAllFontSizes
.put(key
, fontSizes
);
108 } catch (JSONException e
) {
113 public void setupFontViews() {
114 LOKitShell
.getMainHandler().post(new Runnable() {
116 setupFontNameSpinner();
117 setupFontSizeSpinner();
122 private void setupFontNameSpinner() {
123 Spinner fontSpinner
= (Spinner
) mActivity
.findViewById(R
.id
.font_name_spinner
);
124 ArrayAdapter
<String
> dataAdapter
= new ArrayAdapter
<String
>(mActivity
, android
.R
.layout
.simple_spinner_item
, mFontList
);
125 dataAdapter
.setDropDownViewResource(android
.R
.layout
.simple_spinner_dropdown_item
);
126 fontSpinner
.setAdapter(dataAdapter
);
129 private void setupFontSizeSpinner() {
130 Spinner fontSizeSpinner
= (Spinner
) mActivity
.findViewById(R
.id
.font_size_spinner
);
131 ArrayAdapter
<String
> dataAdapter
= new ArrayAdapter
<String
>(mActivity
, android
.R
.layout
.simple_spinner_item
, mFontSizes
);
132 dataAdapter
.setDropDownViewResource(android
.R
.layout
.simple_spinner_dropdown_item
);
133 fontSizeSpinner
.setAdapter(dataAdapter
);
136 public void selectFont(final String fontName
) {
137 LOKitShell
.getMainHandler().post(new Runnable() {
139 selectFontCurrentThread(fontName
);
144 private void selectFontCurrentThread(String fontName
) {
145 Spinner spinner
= (Spinner
) mActivity
.findViewById(R
.id
.font_name_spinner
);
147 if (!mFontNameSpinnerSet
) {
148 spinner
.setOnItemSelectedListener(this);
149 mFontNameSpinnerSet
= true;
152 if (fontName
.equals(mCurrentFontSelected
))
155 ArrayAdapter
<String
> arrayAdapter
= (ArrayAdapter
<String
>) spinner
.getAdapter();
156 int position
= arrayAdapter
.getPosition(fontName
);
157 if (position
!= -1) {
158 mCurrentFontSelected
= fontName
;
159 spinner
.setSelection(position
);
162 resetFontSizes(fontName
);
165 private void resetFontSizes(String fontName
) {
166 Spinner spinner
= (Spinner
) mActivity
.findViewById(R
.id
.font_size_spinner
);
167 ArrayAdapter
<String
> arrayAdapter
= (ArrayAdapter
<String
>) spinner
.getAdapter();
169 List
<String
> fontSizes
= mAllFontSizes
.get(fontName
);
170 if (fontSizes
!= null) {
171 arrayAdapter
.clear();
172 arrayAdapter
.addAll(mAllFontSizes
.get(fontName
));
176 public void selectFontSize(final String fontSize
) {
177 LOKitShell
.getMainHandler().post(new Runnable() {
179 selectFontSizeCurrentThread(fontSize
);
184 private void selectFontSizeCurrentThread(String fontSize
) {
185 Spinner spinner
= (Spinner
) mActivity
.findViewById(R
.id
.font_size_spinner
);
186 if (!mFontSizeSpinnerSet
) {
187 spinner
.setOnItemSelectedListener(this);
188 mFontSizeSpinnerSet
= true;
191 if (fontSize
.equals(mCurrentFontSizeSelected
))
194 ArrayAdapter
<String
> arrayAdapter
= (ArrayAdapter
<String
>) spinner
.getAdapter();
196 int position
= arrayAdapter
.getPosition(fontSize
);
197 if (position
!= -1) {
198 mCurrentFontSizeSelected
= fontSize
;
199 spinner
.setSelection(position
, false);