diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index ba8af918..2e57295b 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -90,6 +90,7 @@ import com.github.dfa.diaspora_android.web.WebHelper; import com.github.dfa.diaspora_android.web.custom_tab.CustomTabActivityHelper; import net.gsantner.opoc.format.markdown.SimpleMarkdownParser; +import net.gsantner.opoc.util.AndroidSupportMeWrapper; import java.io.IOException; @@ -663,6 +664,7 @@ public class MainActivity extends ThemedActivity protected void onResume() { AppLog.v(this, "onResume()"); super.onResume(); + new AndroidSupportMeWrapper(this).mainOnResume(); AppLog.v(this, "Register BroadcastReceivers"); LocalBroadcastManager.getInstance(this).registerReceiver(brSetTitle, new IntentFilter(ACTION_UPDATE_TITLE_FROM_URL)); LocalBroadcastManager.getInstance(this).registerReceiver(brOpenExternalLink, new IntentFilter(ACTION_OPEN_EXTERNAL_URL)); diff --git a/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java b/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java index 0847021b..908176cf 100644 --- a/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java +++ b/app/src/main/java/net/gsantner/opoc/preference/PropertyBackend.java @@ -10,6 +10,8 @@ #########################################################*/ package net.gsantner.opoc.preference; +import java.util.Calendar; +import java.util.Date; import java.util.List; @SuppressWarnings({"UnusedReturnValue", "SpellCheckingInspection", "unused", "SameParameterValue"}) diff --git a/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java b/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java index 665f1058..6fe97a07 100644 --- a/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java +++ b/app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java @@ -45,6 +45,7 @@ import android.text.TextUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Date; import java.util.List; @@ -523,6 +524,10 @@ public class SharedPreferencesPropertyBackend implements PropertyBackend= begin && h <= end; } + + /** + * Substract current datetime by given amount of days + */ + public Date getDateOfDaysAgo(int days) { + return new Date(System.currentTimeMillis() - days * 1000 * 60 * 60 * 24); + } + + /** + * Substract current datetime by given amount of days and check if the given date passed + */ + public boolean didDaysPassedSince(Date date, int days) { + if (date == null || days < 0) { + return false; + } + return date.before(getDateOfDaysAgo(days)); + } + + public boolean afterDaysTrue(String key, int daysSinceLastTime, int firstTime, final SharedPreferences... pref) { + Date d = new Date(System.currentTimeMillis()); + if (!contains(key)) { + d = getDateOfDaysAgo(daysSinceLastTime-firstTime); + setLong(key, d.getTime()); + return firstTime < 1; + } else { + d = new Date(getLong(key, d.getTime())); + } + boolean trigger = didDaysPassedSince(d, daysSinceLastTime); + if (trigger) { + setLong(key, new Date(System.currentTimeMillis()).getTime()); + } + return trigger; + } } diff --git a/app/src/main/java/net/gsantner/opoc/util/AndroidSupportMeWrapper.java b/app/src/main/java/net/gsantner/opoc/util/AndroidSupportMeWrapper.java new file mode 100644 index 00000000..ace1e60c --- /dev/null +++ b/app/src/main/java/net/gsantner/opoc/util/AndroidSupportMeWrapper.java @@ -0,0 +1,72 @@ +/*####################################################### + * + * Maintained by Gregor Santner, 2018- + * https://gsantner.net/ + * + * License of this file: Apache 2.0 (Commercial upon request) + * https://www.apache.org/licenses/LICENSE-2.0 + * https://github.com/gsantner/opoc/#licensing + * +#########################################################*/ +package net.gsantner.opoc.util; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.support.v7.app.AlertDialog; + +import com.github.dfa.diaspora_android.R; + +import net.gsantner.opoc.preference.SharedPreferencesPropertyBackend; + +public class AndroidSupportMeWrapper extends ActivityUtils { + private LocalSettingsImpl _localSettingsImpl; + + public AndroidSupportMeWrapper(Activity activity) { + super(activity); + _localSettingsImpl = new LocalSettingsImpl(_context.getApplicationContext()); + } + + public void openPayPalDonationPage() { + String id = getPackageIdManifest(); + String url = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=TVV24QBGMN23C&source=self.gsantner.net%2F" + id; + openWebpageInExternalBrowser(url); + } + + + public void openGeneralDonatePage() { + openWebpageInExternalBrowser(_context.getString(R.string.app_donate_url)); + } + + public void mainOnResume() { + if (_localSettingsImpl.all14dRequest()) { + AlertDialog.Builder dialog = new AlertDialog.Builder(_context); + dialog.setTitle(R.string.donate_) + .setCancelable(false) + .setNegativeButton(R.string.close, (dialogInterface, i) -> dialogInterface.dismiss()) + .setPositiveButton("PayPal", (dialogInterface, i) -> { + openPayPalDonationPage(); + dialogInterface.dismiss(); + }) + .setNeutralButton(R.string.donate_, (dialogInterface, i) -> { + openGeneralDonatePage(); + dialogInterface.dismiss(); + }) + .setMessage(R.string.do_you_like_this_project_want_donate_to_keep_alive); + dialog.show(); + } + } + + private class LocalSettingsImpl extends SharedPreferencesPropertyBackend { + private final SharedPreferences _prefCache; + + public LocalSettingsImpl(Context _context) { + super(_context, "AndroidSupportMeWrapper.LocalSettingsImpl"); + _prefCache = _context.getSharedPreferences("cache", Context.MODE_PRIVATE); + } + + public boolean all14dRequest() { + return afterDaysTrue("all14dRequest", 14, 3); + } + } +} diff --git a/app/src/main/res/values/strings-not_translatable.xml b/app/src/main/res/values/strings-not_translatable.xml index 7ffbe26a..b36f4c51 100644 --- a/app/src/main/res/values/strings-not_translatable.xml +++ b/app/src/main/res/values/strings-not_translatable.xml @@ -13,6 +13,9 @@ Tor HTTP + @string/donate + + @string/new_post @string/search http diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fccd49d0..0a5905be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,6 +4,8 @@ Open navigation drawer Close navigation drawer Reload + Close + Cancel Settings @@ -259,4 +261,7 @@ Pull to refresh Pulling down on top of page to refresh.\nYou need to restart the app for changes to take effect. + Donate + Do you like this project? Do you want that it gets improved and problems fixed?\n\nDeveloping apps and writing related blog posts costs a lot of time! If you want to help so that the project can go on, please consider a small donation!\n\nThis project is developed in leisure time, completely for free and without any advertisements! +