android-app -> app
android-app-tests -> app-tests
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package org.solovyev.android.calculator.preferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.annotation.XmlRes;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import org.solovyev.android.calculator.ActivityUi;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.Preferences;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.checkout.ActivityCheckout;
|
||||
import org.solovyev.android.checkout.Checkout;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import static android.support.v7.app.ActionBar.NAVIGATION_MODE_STANDARD;
|
||||
|
||||
public class PreferencesActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
static final String EXTRA_PREFERENCE = "preference";
|
||||
static final String EXTRA_PREFERENCE_TITLE = "preference-title";
|
||||
|
||||
@Nonnull
|
||||
private static final SparseArray<PrefDef> preferences = new SparseArray<>();
|
||||
|
||||
static {
|
||||
preferences.append(R.xml.preferences, new PrefDef("screen-main", R.string.c_app_settings));
|
||||
preferences.append(R.xml.preferences_calculations, new PrefDef("screen-calculations", R.string.c_prefs_calculations_category));
|
||||
preferences.append(R.xml.preferences_appearance, new PrefDef("screen-appearance", R.string.c_prefs_appearance_category));
|
||||
preferences.append(R.xml.preferences_plot, new PrefDef("screen-plot", R.string.prefs_graph_screen_title));
|
||||
preferences.append(R.xml.preferences_other, new PrefDef("screen-other", R.string.c_prefs_other_category));
|
||||
preferences.append(R.xml.preferences_onscreen, new PrefDef("screen-onscreen", R.string.prefs_onscreen_title));
|
||||
preferences.append(R.xml.preferences_widget, new PrefDef("screen-widget", R.string.prefs_widget_title));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts());
|
||||
private boolean paused = true;
|
||||
|
||||
public PreferencesActivity() {
|
||||
super(R.layout.main_empty);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
static SparseArray<PrefDef> getPreferences() {
|
||||
return preferences;
|
||||
}
|
||||
|
||||
public static void start(@Nonnull Context context, @XmlRes int preference, @StringRes int title) {
|
||||
final Intent intent = makeIntent(context, preference, title);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
static Intent makeIntent(@Nonnull Context context, @XmlRes int preference, @StringRes int title) {
|
||||
final Intent intent = new Intent(context, PreferencesActivity.class);
|
||||
intent.putExtra(EXTRA_PREFERENCE, preference);
|
||||
if (title != 0) {
|
||||
intent.putExtra(EXTRA_PREFERENCE_TITLE, title);
|
||||
}
|
||||
return intent;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
App.getPreferences().registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
final int preferenceTitle = intent.getIntExtra(EXTRA_PREFERENCE_TITLE, 0);
|
||||
if (preferenceTitle != 0) {
|
||||
setTitle(preferenceTitle);
|
||||
}
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
final int preference = intent.getIntExtra(EXTRA_PREFERENCE, R.xml.preferences);
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add(R.id.main_layout, PreferencesFragment.create(preference, R.layout.fragment_preferences))
|
||||
.commit();
|
||||
}
|
||||
|
||||
getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD);
|
||||
|
||||
checkout.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (!paused) {
|
||||
if (Preferences.Gui.theme.isSameKey(key)) {
|
||||
ActivityUi.restartIfThemeChanged(this, ui.getTheme());
|
||||
} else if (Preferences.Gui.language.isSameKey(key)) {
|
||||
ActivityUi.restartIfLanguageChanged(this, ui.getLanguage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
paused = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
paused = true;
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
checkout.stop();
|
||||
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
ActivityCheckout getCheckout() {
|
||||
return checkout;
|
||||
}
|
||||
|
||||
static class PrefDef {
|
||||
@Nonnull
|
||||
public final String id;
|
||||
@StringRes
|
||||
public final int title;
|
||||
|
||||
PrefDef(@Nonnull String id, int title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,237 @@
|
||||
package org.solovyev.android.calculator.preferences;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.util.SparseArray;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
|
||||
import org.solovyev.android.calculator.AdView;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.CalculatorApplication;
|
||||
import org.solovyev.android.calculator.Preferences;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.calculator.language.Language;
|
||||
import org.solovyev.android.calculator.language.Languages;
|
||||
import org.solovyev.android.checkout.BillingRequests;
|
||||
import org.solovyev.android.checkout.Checkout;
|
||||
import org.solovyev.android.checkout.ProductTypes;
|
||||
import org.solovyev.android.checkout.RequestListener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.precision;
|
||||
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.roundResult;
|
||||
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
|
||||
import static org.solovyev.android.wizard.WizardUi.startWizard;
|
||||
|
||||
public class PreferencesFragment extends org.solovyev.android.material.preferences.PreferencesFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private static boolean SUPPORT_HEADERS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
|
||||
@Nullable
|
||||
private Preference buyPremiumPreference;
|
||||
@Nullable
|
||||
private AdView adView;
|
||||
|
||||
@Nonnull
|
||||
public static PreferencesFragment create(int preferencesResId, int layoutResId) {
|
||||
final PreferencesFragment fragment = new PreferencesFragment();
|
||||
fragment.setArguments(createArguments(preferencesResId, layoutResId, NO_THEME));
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
App.getPreferences().registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
private void setPreferenceIntent(int xml, @Nonnull PreferencesActivity.PrefDef def) {
|
||||
final Preference preference = findPreference(def.id);
|
||||
if (preference != null) {
|
||||
final Intent intent = new Intent(getActivity(), PreferencesActivity.class);
|
||||
intent.putExtra(PreferencesActivity.EXTRA_PREFERENCE, xml);
|
||||
intent.putExtra(PreferencesActivity.EXTRA_PREFERENCE_TITLE, def.title);
|
||||
preference.setIntent(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
final int preference = getPreferencesResId();
|
||||
if (preference == R.xml.preferences) {
|
||||
final SparseArray<PreferencesActivity.PrefDef> preferences = PreferencesActivity.getPreferences();
|
||||
for (int i = 0; i < preferences.size(); i++) {
|
||||
final int xml = preferences.keyAt(i);
|
||||
final PreferencesActivity.PrefDef def = preferences.valueAt(i);
|
||||
setPreferenceIntent(xml, def);
|
||||
}
|
||||
final Preference restartWizardPreference = findPreference("restart_wizard");
|
||||
restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
startWizard(CalculatorApplication.getInstance().getWizards(), DEFAULT_WIZARD_FLOW, getActivity());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
buyPremiumPreference = findPreference("buy_premium");
|
||||
if (buyPremiumPreference != null) {
|
||||
buyPremiumPreference.setEnabled(false);
|
||||
buyPremiumPreference.setSelectable(false);
|
||||
buyPremiumPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
startActivity(new Intent(getActivity(), PurchaseDialogActivity.class));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
prepareLanguagePreference(preference);
|
||||
|
||||
getCheckout().whenReady(new Checkout.ListenerAdapter() {
|
||||
@Override
|
||||
public void onReady(@Nonnull BillingRequests requests) {
|
||||
requests.isPurchased(ProductTypes.IN_APP, CalculatorApplication.AD_FREE_PRODUCT_ID, new RequestListener<Boolean>() {
|
||||
@Override
|
||||
public void onSuccess(@Nonnull Boolean purchased) {
|
||||
if (buyPremiumPreference != null) {
|
||||
buyPremiumPreference.setEnabled(!purchased);
|
||||
buyPremiumPreference.setSelectable(!purchased);
|
||||
}
|
||||
onShowAd(!purchased);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(int i, @Nonnull Exception e) {
|
||||
onShowAd(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
final SharedPreferences preferences = App.getPreferences();
|
||||
onSharedPreferenceChanged(preferences, roundResult.getKey());
|
||||
}
|
||||
|
||||
private void prepareLanguagePreference(int preference) {
|
||||
if (preference != R.xml.preferences_appearance) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ListPreference language = (ListPreference) preferenceManager.findPreference(Preferences.Gui.language.getKey());
|
||||
final Languages languages = App.getLanguages();
|
||||
final List<Language> languagesList = languages.getList();
|
||||
final CharSequence[] entries = new CharSequence[languagesList.size()];
|
||||
final CharSequence[] entryValues = new CharSequence[languagesList.size()];
|
||||
for (int i = 0; i < languagesList.size(); i++) {
|
||||
final Language l = languagesList.get(i);
|
||||
entries[i] = l.getName(getActivity());
|
||||
entryValues[i] = l.code;
|
||||
}
|
||||
language.setEntries(entries);
|
||||
language.setEntryValues(entryValues);
|
||||
language.setSummary(languages.getCurrent().getName(getActivity()));
|
||||
language.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
final Language l = languages.get((String) newValue);
|
||||
language.setSummary(l.getName(getActivity()));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Checkout getCheckout() {
|
||||
return ((PreferencesActivity) getActivity()).getCheckout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (roundResult.getKey().equals(key)) {
|
||||
final Preference preference = findPreference(precision.getKey());
|
||||
if (preference != null) {
|
||||
preference.setEnabled(preferences.getBoolean(key, roundResult.getDefaultValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (adView != null) {
|
||||
adView.resume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
if (adView != null) {
|
||||
adView.pause();
|
||||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
if (adView != null) {
|
||||
adView.destroy();
|
||||
}
|
||||
super.onDestroyView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
App.getPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
private boolean supportsHeaders() {
|
||||
return SUPPORT_HEADERS;
|
||||
}
|
||||
|
||||
protected void onShowAd(boolean show) {
|
||||
if (!supportsHeaders()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ListView listView = getListView();
|
||||
if (show) {
|
||||
if (adView != null) {
|
||||
return;
|
||||
}
|
||||
adView = (AdView) LayoutInflater.from(getActivity()).inflate(R.layout.ad, null);
|
||||
adView.show();
|
||||
try {
|
||||
listView.addHeaderView(adView);
|
||||
} catch (IllegalStateException e) {
|
||||
// doesn't support header views
|
||||
SUPPORT_HEADERS = false;
|
||||
adView.hide();
|
||||
adView = null;
|
||||
}
|
||||
} else {
|
||||
if (adView == null) {
|
||||
return;
|
||||
}
|
||||
listView.removeHeaderView(adView);
|
||||
adView.hide();
|
||||
adView = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright 2013 serso aka se.solovyev
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
* Contact details
|
||||
*
|
||||
* Email: se.solovyev@gmail.com
|
||||
* Site: http://se.solovyev.org
|
||||
*/
|
||||
|
||||
package org.solovyev.android.calculator.preferences;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.solovyev.android.calculator.ActivityUi;
|
||||
import org.solovyev.android.calculator.App;
|
||||
import org.solovyev.android.calculator.BaseActivity;
|
||||
import org.solovyev.android.calculator.CalculatorFragment;
|
||||
import org.solovyev.android.calculator.CalculatorFragmentType;
|
||||
import org.solovyev.android.calculator.R;
|
||||
import org.solovyev.android.checkout.ActivityCheckout;
|
||||
import org.solovyev.android.checkout.BillingRequests;
|
||||
import org.solovyev.android.checkout.Checkout;
|
||||
import org.solovyev.android.checkout.ProductTypes;
|
||||
import org.solovyev.android.checkout.Purchase;
|
||||
import org.solovyev.android.checkout.RequestListener;
|
||||
import org.solovyev.android.fragments.FragmentUtils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 1/20/13
|
||||
* Time: 2:36 PM
|
||||
*/
|
||||
public class PurchaseDialogActivity extends BaseActivity {
|
||||
|
||||
@Nonnull
|
||||
private final ActivityCheckout checkout = Checkout.forActivity(this, App.getBilling(), App.getProducts());
|
||||
|
||||
@Nonnull
|
||||
private final RequestListener<Purchase> purchaseListener = new RequestListener<Purchase>() {
|
||||
@Override
|
||||
public void onSuccess(@Nonnull Purchase purchase) {
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(int i, @Nonnull Exception e) {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
|
||||
public PurchaseDialogActivity() {
|
||||
super(R.layout.cpp_dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
FragmentUtils.createFragment(this, PurchaseDialogFragment.class, R.id.dialog_layout, "purchase-dialog");
|
||||
|
||||
checkout.start();
|
||||
checkout.createPurchaseFlow(purchaseListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
ActivityUi.reportActivityStart(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
ActivityUi.reportActivityStop(this);
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
private void purchase() {
|
||||
checkout.whenReady(new Checkout.ListenerAdapter() {
|
||||
@Override
|
||||
public void onReady(@Nonnull BillingRequests requests) {
|
||||
requests.purchase(ProductTypes.IN_APP, "ad_free", null, checkout.getPurchaseFlow());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
checkout.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
checkout.destroyPurchaseFlow();
|
||||
checkout.stop();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public static class PurchaseDialogFragment extends CalculatorFragment {
|
||||
|
||||
public PurchaseDialogFragment() {
|
||||
super(CalculatorFragmentType.purchase_dialog);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@Nonnull View root, Bundle savedInstanceState) {
|
||||
super.onViewCreated(root, savedInstanceState);
|
||||
|
||||
((TextView) root.findViewById(R.id.cpp_purchase_text)).setMovementMethod(ScrollingMovementMethod.getInstance());
|
||||
root.findViewById(R.id.cpp_continue_button).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
((PurchaseDialogActivity) activity).purchase();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user