var activity
This commit is contained in:
@@ -47,6 +47,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
@NotNull
|
||||
private BroadcastReceiver insertTextReceiver;
|
||||
|
||||
private volatile boolean initialized;
|
||||
|
||||
/**
|
||||
* Called when the activity is first created.
|
||||
*/
|
||||
@@ -55,17 +57,11 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main);
|
||||
|
||||
try {
|
||||
if (!CalculatorModel.isLoaded()) {
|
||||
CalculatorModel.init(this);
|
||||
}
|
||||
this.calculatorModel = CalculatorModel.getInstance();
|
||||
} catch (EvalError evalError) {
|
||||
// todo serso: create serso runtime exception
|
||||
throw new RuntimeException("Could not initialize interpreter!");
|
||||
}
|
||||
firstTimeInit();
|
||||
|
||||
this.calculatorView = new CalculatorView(this, this.calculatorModel);
|
||||
init();
|
||||
|
||||
dpclRegister.clear();
|
||||
|
||||
final SimpleOnDragListener.Preferences dragPreferences = SimpleOnDragListener.getPreferences(this);
|
||||
|
||||
@@ -99,7 +95,13 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
((DragButton) findViewById(R.id.leftButton)).setOnDragListener(toPositionOnDragListener);
|
||||
dpclRegister.addListener(toPositionOnDragListener);
|
||||
|
||||
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
this.onSharedPreferenceChanged(defaultSharedPreferences, null);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
insertTextReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@@ -118,11 +120,24 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
||||
};
|
||||
|
||||
registerReceiver(insertTextReceiver, new IntentFilter(INSERT_TEXT_INTENT));
|
||||
}
|
||||
|
||||
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
defaultSharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||
private synchronized void firstTimeInit() {
|
||||
if (!initialized) {
|
||||
try {
|
||||
if (!CalculatorModel.isLoaded()) {
|
||||
CalculatorModel.init(this);
|
||||
}
|
||||
this.calculatorModel = CalculatorModel.getInstance();
|
||||
} catch (EvalError evalError) {
|
||||
// todo serso: create serso runtime exception
|
||||
throw new RuntimeException("Could not initialize interpreter!");
|
||||
}
|
||||
|
||||
this.onSharedPreferenceChanged(defaultSharedPreferences, null);
|
||||
this.calculatorView = new CalculatorView(this, this.calculatorModel);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -6,10 +6,13 @@
|
||||
|
||||
package org.solovyev.android.calculator;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ListActivity;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.*;
|
||||
@@ -23,25 +26,47 @@ import java.util.List;
|
||||
* Date: 9/28/11
|
||||
* Time: 10:55 PM
|
||||
*/
|
||||
public class CalculatorVarsActivity extends ListActivity {
|
||||
public class CalculatorVarsActivity extends ListActivity implements DialogInterface.OnClickListener {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setTheme(android.R.style.Theme_Dialog);
|
||||
|
||||
final List<Var> vars = new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars());
|
||||
setListAdapter(new VarsArrayAdapter(this, R.layout.var, R.id.var_text, vars));
|
||||
setListAdapter(new VarsArrayAdapter(this, R.layout.var, R.id.var_text, new ArrayList<Var>(CalculatorModel.getInstance().getVarsRegister().getVars())));
|
||||
|
||||
final ListView lv = getListView();
|
||||
lv.setTextFilterEnabled(true);
|
||||
|
||||
|
||||
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@Override
|
||||
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
|
||||
final View editView = layoutInflater.inflate(R.layout.var_edit, null);
|
||||
|
||||
final Var var = (Var)parent.getItemAtPosition(position);
|
||||
|
||||
final EditText editName = (EditText)editView.findViewById(R.id.var_name);
|
||||
editName.setText(var.getName());
|
||||
final EditText editValue = (EditText)editView.findViewById(R.id.var_value);
|
||||
editValue.setText(var.getValue());
|
||||
final EditText editDescription = (EditText)editView.findViewById(R.id.var_description);
|
||||
editDescription.setText(var.getDescription());
|
||||
|
||||
|
||||
new AlertDialog.Builder(CalculatorVarsActivity.this)
|
||||
.setCancelable(true)
|
||||
.setPositiveButton("Save", CalculatorVarsActivity.this)
|
||||
.setView(editView).create().show();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
public void onItemClick(AdapterView<?> parent, View view,
|
||||
int position, long id) {
|
||||
final Intent intent = new Intent(CalculatorActivity.INSERT_TEXT_INTENT);
|
||||
intent.putExtra(CalculatorActivity.INSERT_TEXT_INTENT_EXTRA_STRING, vars.get(position).getName());
|
||||
intent.putExtra(CalculatorActivity.INSERT_TEXT_INTENT_EXTRA_STRING, ((Var) parent.getItemAtPosition(position)).getName());
|
||||
sendOrderedBroadcast(intent, null);
|
||||
|
||||
CalculatorVarsActivity.this.finish();
|
||||
@@ -50,6 +75,11 @@ public class CalculatorVarsActivity extends ListActivity {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
|
||||
private class VarsArrayAdapter extends ArrayAdapter<Var> {
|
||||
|
||||
private VarsArrayAdapter(Context context, int resource, int textViewResourceId, List<Var> objects) {
|
||||
@@ -64,12 +94,15 @@ public class CalculatorVarsActivity extends ListActivity {
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
final ViewGroup result = (ViewGroup)super.getView(position, convertView, parent);
|
||||
|
||||
final Var var = getItem(position);
|
||||
if (convertView == null) {
|
||||
final Var var = getItem(position);
|
||||
|
||||
if (!StringUtils.isEmpty(var.getDescription())) {
|
||||
final TextView description = new TextView(getContext());
|
||||
description.setText(var.getDescription());
|
||||
result.addView(description, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
if (!StringUtils.isEmpty(var.getDescription())) {
|
||||
final TextView description = new TextView(getContext());
|
||||
description.setText(var.getDescription());
|
||||
description.setPadding(6, 0, 6, 6);
|
||||
result.addView(description, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user