1 package com.gpac.Osmo4.extra;
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.text.Html;
6 import android.text.method.ScrollingMovementMethod;
7 import android.util.Log;
8 import android.view.KeyEvent;
9 import android.view.View;
10 import android.widget.AdapterView;
11 import android.widget.ArrayAdapter;
12 import android.widget.AutoCompleteTextView;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.TextView;
16 import android.widget.Toast;
18 import com.gpac.Osmo4.R;
21 public class ConfigFileEditorActivity extends Activity {
23 private String TAG = "ConfigFileEditorActivity";
25 private AutoCompleteTextView optionsTextView;
26 private TextView typeTextView;
27 private TextView dfaultTextView;
28 private TextView usageTextView;
29 private TextView descriptionTextView;
30 private TextView currentValueTextView;
31 private EditText newValueEditText;
32 private Button saveButton;
33 private String currentValue;
35 private ConfigFileEditor editor;
36 private ConfigFileEditor.Attribute attribute;
37 private String currentName;
40 protected void onCreate(Bundle savedInstanceState) {
41 super.onCreate(savedInstanceState);
42 setContentView(R.layout.activity_config_file_editor);
44 optionsTextView = (AutoCompleteTextView) findViewById(R.id.optionsTextView);
45 typeTextView = (TextView) findViewById(R.id.typeTextView);
46 dfaultTextView = (TextView) findViewById(R.id.dfaultTextView);
47 usageTextView = (TextView) findViewById(R.id.usageTextView);
48 descriptionTextView = (TextView) findViewById(R.id.descriptionTextView);
49 currentValueTextView = (TextView) findViewById(R.id.currentValueEditText);
50 newValueEditText = (EditText) findViewById(R.id.newValueEditText);
51 saveButton = (Button) findViewById(R.id.saveButton);
53 descriptionTextView.setMovementMethod(new ScrollingMovementMethod());
54 editor = new ConfigFileEditor(this);
55 final String[]keys = editor.getAllKeys();
56 final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
57 android.R.layout.simple_dropdown_item_1line, keys);
58 optionsTextView.setAdapter(adapter);
60 optionsTextView.setOnClickListener(onOptionTextViewClick);
61 optionsTextView.setOnItemClickListener(onItemClick);
62 optionsTextView.setOnKeyListener(keyListener);
63 saveButton.setOnClickListener(onSaveButtonClick);
66 private View.OnClickListener onOptionTextViewClick = new View.OnClickListener() {
68 public void onClick(View v) {
69 optionsTextView.showDropDown();
73 private AdapterView.OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {
75 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
76 setAllFields((String) parent.getItemAtPosition(position));
80 private View.OnKeyListener keyListener = new View.OnKeyListener() {
82 public boolean onKey(View v, int keyCode, KeyEvent event) {
83 if(event.getAction() == KeyEvent.ACTION_DOWN &&
84 keyCode == KeyEvent.KEYCODE_ENTER) {
85 currentName = optionsTextView.getText().toString().trim();
86 if (currentName == null || currentName.equals("")) {
89 optionsTextView.dismissDropDown();
90 setAllFields(currentName);
98 private View.OnClickListener onSaveButtonClick = new View.OnClickListener() {
100 public void onClick(View v) {
101 String newValue = newValueEditText.getText().toString().trim();
102 if(newValue == null || newValue.equals("")) {
103 Toast.makeText(ConfigFileEditorActivity.this, "New value is empty", Toast.LENGTH_LONG).show();
107 if(currentValue == null){
108 Log.e(TAG, getString(R.string.noKey));
112 if (newValue.equals(currentValue)){
113 Toast.makeText(ConfigFileEditorActivity.this, getString(R.string.noChange), Toast.LENGTH_LONG).show();
117 if(editor.isValueValid(currentName, newValue)){
118 editor.setValue(currentName, newValue);
119 setAllFields(currentName);
120 Toast.makeText(ConfigFileEditorActivity.this, getString(R.string.valueUpdated), Toast.LENGTH_LONG).show();
121 newValueEditText.setText("");
123 Toast.makeText(ConfigFileEditorActivity.this, getString(R.string.valueNotValid), Toast.LENGTH_LONG).show();
124 Log.e(TAG, getString(R.string.valueNotValid));
130 private void setAllFields(String key) {
132 attribute = editor.getAllAttributesForKey(key);
133 if(attribute == null) {
134 Log.v(TAG, "no attribute found for the given key in the option file : " + key);
135 typeTextView.setText(getString(R.string.noAttribute));
136 dfaultTextView.setText(getString(R.string.noAttribute));
137 usageTextView.setText(getString(R.string.noAttribute));
138 descriptionTextView.setText(getString(R.string.noAttribute));
141 typeTextView.setText(attribute.getType());
142 dfaultTextView.setText(attribute.getDefault());
143 usageTextView.setText(attribute.getUsage());
144 descriptionTextView.setText(Html.fromHtml(attribute.getDesciption(), null, new XMLTagHandler()));
145 Log.v(TAG, "description " + attribute.getDesciption());
147 currentValue = editor.getValue(key);
148 if (currentValue == null) currentValueTextView.setText(getString(R.string.noKey)); else currentValueTextView.setText(currentValue);