mirror of https://github.com/tasks/tasks
Consolidate BasicPreferences
parent
659cd0a76b
commit
5df88f2093
@ -1,21 +0,0 @@
|
||||
package org.tasks.preferences;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import org.tasks.R;
|
||||
import org.tasks.injection.ActivityComponent;
|
||||
|
||||
public class BasicPreferences extends BaseBasicPreferences {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
requires(R.string.settings_general, false, R.string.synchronization);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inject(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package org.tasks.preferences;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
|
||||
import org.tasks.R;
|
||||
import org.tasks.injection.ActivityComponent;
|
||||
|
||||
public class BasicPreferences extends BaseBasicPreferences {
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
getPref(R.string.TLA_menu_donate).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://tasks.org/donate")));
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
requires(R.string.settings_general, false, R.string.synchronization);
|
||||
requires(R.string.get_plugins, false, R.string.p_purchased_tasker, R.string.p_purchased_dashclock);
|
||||
requires(R.string.privacy, false, R.string.p_collect_statistics);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inject(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
}
|
@ -1,188 +0,0 @@
|
||||
package org.tasks.preferences;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.TwoStatePreference;
|
||||
|
||||
import org.tasks.BuildConfig;
|
||||
import org.tasks.R;
|
||||
import org.tasks.billing.PurchaseHelper;
|
||||
import org.tasks.billing.PurchaseHelperCallback;
|
||||
import org.tasks.dialogs.DialogBuilder;
|
||||
import org.tasks.injection.ActivityComponent;
|
||||
import org.tasks.receivers.TeslaUnreadReceiver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybeanMR1;
|
||||
|
||||
public class BasicPreferences extends BaseBasicPreferences implements PurchaseHelperCallback {
|
||||
|
||||
private static final int REQUEST_PURCHASE = 10005;
|
||||
|
||||
@Inject TeslaUnreadReceiver teslaUnreadReceiver;
|
||||
@Inject Preferences preferences;
|
||||
@Inject PurchaseHelper purchaseHelper;
|
||||
@Inject DialogBuilder dialogBuilder;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
getPref(R.string.TLA_menu_donate).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
final List<String> donationValues = getDonationValues();
|
||||
dialogBuilder.newDialog()
|
||||
.setTitle(R.string.select_amount)
|
||||
.setItems(donationValues, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
String value = donationValues.get(which);
|
||||
Pattern pattern = Pattern.compile("\\$(\\d+) USD");
|
||||
Matcher matcher = pattern.matcher(value);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
matcher.matches();
|
||||
String sku = String.format(Locale.ENGLISH, "%03d", Integer.parseInt(matcher.group(1)));
|
||||
purchaseHelper.purchase(dialogBuilder, BasicPreferences.this, sku, null, REQUEST_PURCHASE, BasicPreferences.this);
|
||||
}
|
||||
})
|
||||
.show();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
getPref(R.string.p_purchased_themes).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue != null && (boolean) newValue && !preferences.hasPurchase(R.string.p_purchased_themes)) {
|
||||
purchaseHelper.purchase(dialogBuilder, BasicPreferences.this, getString(R.string.sku_themes), getString(R.string.p_purchased_themes), REQUEST_PURCHASE, BasicPreferences.this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
getPref(R.string.p_tesla_unread_enabled).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue != null) {
|
||||
if ((boolean) newValue && !preferences.hasPurchase(R.string.p_purchased_tesla_unread)) {
|
||||
purchaseHelper.purchase(dialogBuilder, BasicPreferences.this, getString(R.string.sku_tesla_unread), getString(R.string.p_purchased_tesla_unread), REQUEST_PURCHASE, BasicPreferences.this);
|
||||
} else {
|
||||
teslaUnreadReceiver.setEnabled((boolean) newValue);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
getPref(R.string.p_purchased_tasker).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue != null && (boolean) newValue && !preferences.hasPurchase(R.string.p_purchased_tasker)) {
|
||||
purchaseHelper.purchase(dialogBuilder, BasicPreferences.this, getString(R.string.sku_tasker), getString(R.string.p_purchased_tasker), REQUEST_PURCHASE, BasicPreferences.this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
getPref(R.string.p_purchased_dashclock).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (newValue != null && (boolean) newValue && !preferences.hasPurchase(R.string.p_purchased_dashclock)) {
|
||||
purchaseHelper.purchase(dialogBuilder, BasicPreferences.this, getString(R.string.sku_dashclock), getString(R.string.p_purchased_dashclock), REQUEST_PURCHASE, BasicPreferences.this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
addPreferencesFromResource(R.xml.preferences_debug);
|
||||
|
||||
findPreference(getString(R.string.debug_unlock_purchases)).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
preferences.setBoolean(R.string.p_purchased_dashclock, true);
|
||||
preferences.setBoolean(R.string.p_purchased_tasker, true);
|
||||
preferences.setBoolean(R.string.p_purchased_tesla_unread, true);
|
||||
preferences.setBoolean(R.string.p_purchased_themes, true);
|
||||
recreate();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
findPreference(getString(R.string.debug_consume_purchases)).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
purchaseHelper.consumePurchases();
|
||||
recreate();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
requires(R.string.get_plugins, atLeastJellybeanMR1(), R.string.p_purchased_dashclock);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQUEST_PURCHASE) {
|
||||
purchaseHelper.handleActivityResult(this, requestCode, resultCode, data);
|
||||
} else {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inject(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purchaseCompleted(final boolean success, final String sku) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (getString(R.string.sku_tasker).equals(sku)) {
|
||||
((TwoStatePreference) getPref(R.string.p_purchased_tasker)).setChecked(success);
|
||||
} else if (getString(R.string.sku_tesla_unread).equals(sku)) {
|
||||
((TwoStatePreference) getPref(R.string.p_tesla_unread_enabled)).setChecked(success);
|
||||
} else if (getString(R.string.sku_dashclock).equals(sku)) {
|
||||
((TwoStatePreference) getPref(R.string.p_purchased_dashclock)).setChecked(success);
|
||||
} else if (getString(R.string.sku_themes).equals(sku)) {
|
||||
((TwoStatePreference) getPref(R.string.p_purchased_themes)).setChecked(success);
|
||||
} else {
|
||||
Timber.d("Unhandled sku: %s", sku);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
if (!isChangingConfigurations()) {
|
||||
purchaseHelper.disposeIabHelper();
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getDonationValues() {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1 ; i <= 100 ; i++) {
|
||||
values.add(String.format("$%s USD", Integer.toString(i)));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
@ -1,8 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="debug_unlock_purchases">Unlock purchases</string>
|
||||
<string name="debug_consume_purchases">Consume purchases</string>
|
||||
<string name="debug">Debug</string>
|
||||
|
||||
</resources>
|
||||
|
@ -0,0 +1,62 @@
|
||||
package org.tasks.dialogs;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
|
||||
import org.tasks.R;
|
||||
import org.tasks.billing.PurchaseHelper;
|
||||
import org.tasks.injection.InjectingNativeDialogFragment;
|
||||
import org.tasks.injection.NativeDialogFragmentComponent;
|
||||
import org.tasks.preferences.BasicPreferences;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class DonationDialog extends InjectingNativeDialogFragment {
|
||||
|
||||
public static DonationDialog newDonationDialog() {
|
||||
return new DonationDialog();
|
||||
}
|
||||
|
||||
@Inject DialogBuilder dialogBuilder;
|
||||
@Inject PurchaseHelper purchaseHelper;
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
final List<String> donationValues = getDonationValues();
|
||||
return dialogBuilder.newDialog()
|
||||
.setTitle(R.string.select_amount)
|
||||
.setItems(donationValues, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
String value = donationValues.get(which);
|
||||
Pattern pattern = Pattern.compile("\\$(\\d+) USD");
|
||||
Matcher matcher = pattern.matcher(value);
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
matcher.matches();
|
||||
String sku = String.format(java.util.Locale.ENGLISH, "%03d", Integer.parseInt(matcher.group(1)));
|
||||
purchaseHelper.purchase(dialogBuilder, getActivity(), sku, null, BasicPreferences.REQUEST_PURCHASE, (BasicPreferences) getActivity());
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.show();
|
||||
}
|
||||
|
||||
private List<String> getDonationValues() {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1 ; i <= 100 ; i++) {
|
||||
values.add(String.format("$%s USD", Integer.toString(i)));
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inject(NativeDialogFragmentComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue