NPE fixed

This commit is contained in:
Sergey Solovyev 2012-01-21 23:23:30 +04:00
parent c47cccf04a
commit 07ee83b89e
9 changed files with 102 additions and 52 deletions

View File

@ -29,6 +29,7 @@
</activity> </activity>
<!--NOTE: a:configChanges="orientation|keyboardHidden" is needed to correct work of dialog windows (not to close them on orientation change) -->
<activity a:name=".CalculatorPreferencesActivity" <activity a:name=".CalculatorPreferencesActivity"
a:label="@string/c_app_settings" a:label="@string/c_app_settings"
a:configChanges="orientation|keyboardHidden"/> a:configChanges="orientation|keyboardHidden"/>

View File

@ -7,16 +7,12 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.ClipboardManager; import android.text.ClipboardManager;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.*; import android.view.*;
@ -43,7 +39,7 @@ import org.solovyev.android.calculator.view.NumeralBasesButton;
import org.solovyev.android.history.HistoryDragProcessor; import org.solovyev.android.history.HistoryDragProcessor;
import org.solovyev.android.prefs.IntegerPreference; import org.solovyev.android.prefs.IntegerPreference;
import org.solovyev.android.prefs.Preference; import org.solovyev.android.prefs.Preference;
import org.solovyev.android.view.*; import org.solovyev.android.view.VibratorContainer;
import org.solovyev.android.view.drag.*; import org.solovyev.android.view.drag.*;
import org.solovyev.common.utils.Announcer; import org.solovyev.common.utils.Announcer;
import org.solovyev.common.utils.Point2d; import org.solovyev.common.utils.Point2d;
@ -124,7 +120,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText); preferences.registerOnSharedPreferenceChangeListener(additionalAdditionalTitleText);
} catch (ClassCastException e) { } catch (ClassCastException e) {
// super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports) // super fix for issue with class cast in android.view.Window.setFeatureInt() (see app error reports)
Log.d(CalculatorActivity.class.getName(), e.getMessage(), e); Log.e(CalculatorActivity.class.getName(), e.getMessage(), e);
} }
} }
@ -194,7 +190,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} }
CalculatorEngine.instance.reset(this, preferences); CalculatorEngine.instance.softReset(this, preferences);
initMultiplicationButton(); initMultiplicationButton();
@ -420,7 +416,6 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} }
ResourceCache.instance.init(R.id.class, this); ResourceCache.instance.init(R.id.class, this);
CalculatorEngine.instance.init(this, preferences);
initialized = true; initialized = true;
} }
@ -512,30 +507,9 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
CalculatorActivityLauncher.showVars(this); CalculatorActivityLauncher.showVars(this);
} }
private final static String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted";
@SuppressWarnings({"UnusedDeclaration"}) @SuppressWarnings({"UnusedDeclaration"})
public void donateButtonClickHandler(@NotNull View v) { public void donateButtonClickHandler(@NotNull View v) {
final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); CalculatorApplication.showDonationDialog(this);
final View view = layoutInflater.inflate(R.layout.donate, null);
final TextView donate = (TextView) view.findViewById(R.id.donateText);
donate.setMovementMethod(LinkMovementMethod.getInstance());
final AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(paypalDonateUrl));
startActivity(i);
}
})
.setView(view);
builder.create().show();
} }
@Override @Override
@ -639,7 +613,7 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
} }
if (CalculatorEngine.Preferences.getPreferenceKeys().contains(key)) { if (CalculatorEngine.Preferences.getPreferenceKeys().contains(key)) {
CalculatorEngine.instance.reset(this, preferences); CalculatorEngine.instance.softReset(this, preferences);
this.calculatorModel.evaluate(); this.calculatorModel.evaluate();
} }

View File

@ -1,8 +1,19 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import net.robotmedia.billing.BillingController; import net.robotmedia.billing.BillingController;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.ads.AdsController; import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.model.CalculatorEngine;
/** /**
* User: serso * User: serso
@ -11,6 +22,8 @@ import org.solovyev.android.ads.AdsController;
*/ */
public class CalculatorApplication extends android.app.Application { public class CalculatorApplication extends android.app.Application {
private static final String paypalDonateUrl = "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=se%2esolovyev%40gmail%2ecom&lc=RU&item_name=Android%20Calculator&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted";
public static final String AD_FREE_PRODUCT_ID = "ad_free"; public static final String AD_FREE_PRODUCT_ID = "ad_free";
public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free"; public static final String AD_FREE_P_KEY = "org.solovyev.android.calculator_ad_free";
@ -44,5 +57,31 @@ public class CalculatorApplication extends android.app.Application {
return CalculatorSecurity.getPK(); return CalculatorSecurity.getPK();
} }
}); });
CalculatorEngine.instance.init(this, PreferenceManager.getDefaultSharedPreferences(this));
}
public static void showDonationDialog(@NotNull final Context context) {
final LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
final View view = layoutInflater.inflate(R.layout.donate, null);
final TextView donate = (TextView) view.findViewById(R.id.donateText);
donate.setMovementMethod(LinkMovementMethod.getInstance());
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(true)
.setNegativeButton(R.string.c_cancel, null)
.setPositiveButton(R.string.c_donate, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(paypalDonateUrl));
context.startActivity(i);
}
})
.setView(view);
builder.create().show();
} }
} }

View File

@ -73,7 +73,6 @@ public class CalculatorEditor extends EditText implements SharedPreferences.OnSh
super.onCreateContextMenu(menu); super.onCreateContextMenu(menu);
menu.removeItem(android.R.id.selectAll); menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.startSelectingText);
} }
@Override @Override

View File

@ -46,7 +46,7 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray())); protected final static List<Character> acceptableChars = Arrays.asList(StringUtils.toObject("1234567890abcdefghijklmnopqrstuvwxyzйцукенгшщзхъфывапролджэячсмитьбюё_".toCharArray()));
@NotNull @Nullable
private MathEntityArrayAdapter<T> adapter; private MathEntityArrayAdapter<T> adapter;
@Nullable @Nullable
@ -168,11 +168,6 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
return t != null && EqualsTool.areEqual(getMathEntityCategory(t), category); return t != null && EqualsTool.areEqual(getMathEntityCategory(t), category);
} }
@NotNull
protected MathEntityArrayAdapter<T> getAdapter() {
return adapter;
}
@NotNull @NotNull
protected abstract MathEntityDescriptionGetter getDescriptionGetter(); protected abstract MathEntityDescriptionGetter getDescriptionGetter();
@ -183,15 +178,18 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
abstract String getMathEntityCategory(@NotNull T t); abstract String getMathEntityCategory(@NotNull T t);
protected void sort() { protected void sort() {
AbstractMathEntityListActivity.this.adapter.sort(new Comparator<T>() { final MathEntityArrayAdapter<T> localAdapter = adapter;
@Override if (localAdapter != null) {
public int compare(T function1, T function2) { localAdapter.sort(new Comparator<T>() {
return function1.getName().compareTo(function2.getName()); @Override
} public int compare(T function1, T function2) {
}); return function1.getName().compareTo(function2.getName());
}
});
AbstractMathEntityListActivity.this.adapter.notifyDataSetChanged(); localAdapter.notifyDataSetChanged();
} }
}
protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> { protected static class MathEntityArrayAdapter<T extends MathEntity> extends ArrayAdapter<T> {
@ -257,4 +255,22 @@ public abstract class AbstractMathEntityListActivity<T extends MathEntity> exten
@Nullable @Nullable
String getDescription(@NotNull Context context, @NotNull String mathEntityName); String getDescription(@NotNull Context context, @NotNull String mathEntityName);
} }
public void addToAdapter(@NotNull T mathEntity) {
if (this.adapter != null) {
this.adapter.add(mathEntity);
}
}
public void removeFromAdapter(@NotNull T mathEntity) {
if (this.adapter != null) {
this.adapter.remove(mathEntity);
}
}
public void notifyAdapter() {
if (this.adapter != null) {
this.adapter.notifyDataSetChanged();
}
}
} }

View File

@ -140,9 +140,9 @@ public class FunctionEditorSaver implements DialogInterface.OnClickListener{
final Function addedVar = mathRegistry.add(varBuilder); final Function addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) { if (activity.isInCategory(addedVar)) {
if (editedInstance != null) { if (editedInstance != null) {
activity.getAdapter().remove(editedInstance); activity.removeFromAdapter(editedInstance);
} }
activity.getAdapter().add(addedVar); activity.addToAdapter(addedVar);
} }
mathRegistry.save(activity); mathRegistry.save(activity);

View File

@ -61,13 +61,13 @@ class MathEntityRemover<T extends MathEntity> implements DialogInterface.OnClick
showConfirmationDialog(); showConfirmationDialog();
} else { } else {
if (activity.isInCategory(mathEntity)) { if (activity.isInCategory(mathEntity)) {
activity.getAdapter().remove(mathEntity); activity.removeFromAdapter(mathEntity);
} }
varsRegistry.remove(mathEntity); varsRegistry.remove(mathEntity);
varsRegistry.save(activity); varsRegistry.save(activity);
if (activity.isInCategory(mathEntity)) { if (activity.isInCategory(mathEntity)) {
activity.getAdapter().notifyDataSetChanged(); activity.notifyAdapter();
} }
} }
} }

View File

@ -137,9 +137,9 @@ class VarEditorSaver<T extends MathEntity> implements DialogInterface.OnClickLis
final T addedVar = mathRegistry.add(varBuilder); final T addedVar = mathRegistry.add(varBuilder);
if (activity.isInCategory(addedVar)) { if (activity.isInCategory(addedVar)) {
if (editedInstance != null) { if (editedInstance != null) {
activity.getAdapter().remove(editedInstance); activity.removeFromAdapter(editedInstance);
} }
activity.getAdapter().add(addedVar); activity.addToAdapter(addedVar);
} }
mathRegistry.save(activity); mathRegistry.save(activity);

View File

@ -319,6 +319,27 @@ public enum CalculatorEngine {
} }
} }
public void softReset(@Nullable Context context, @Nullable SharedPreferences preferences) {
synchronized (lock) {
if (preferences != null) {
this.setPrecision(Preferences.precision.getPreference(preferences));
this.setRoundResult(Preferences.roundResult.getPreference(preferences));
this.setAngleUnits(getAngleUnitsFromPrefs(preferences));
this.setNumeralBase(getNumeralBaseFromPrefs(preferences));
this.setMultiplicationSign(Preferences.multiplicationSign.getPreference(preferences));
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
if (StringUtils.isEmpty(groupingSeparator)) {
this.getEngine().setUseGroupingSeparator(false);
} else {
this.getEngine().setUseGroupingSeparator(true);
this.getEngine().setGroupingSeparator(groupingSeparator.charAt(0));
}
}
}
}
@NotNull @NotNull
public NumeralBase getNumeralBaseFromPrefs(@NotNull SharedPreferences preferences) { public NumeralBase getNumeralBaseFromPrefs(@NotNull SharedPreferences preferences) {
return Preferences.numeralBase.getPreference(preferences); return Preferences.numeralBase.getPreference(preferences);