1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-12-22 19:08:03 +01:00

Add AndroidSupportMeWrapper

This commit is contained in:
Gregor Santner 2019-03-08 12:32:10 +01:00
parent 4f44d1acd3
commit 49c6a97ce7
6 changed files with 122 additions and 0 deletions

View file

@ -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));

View file

@ -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"})

View file

@ -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<String,
return this;
}
public boolean contains(String key, final SharedPreferences... pref) {
return gp(pref).contains(key);
}
/**
* A method to determine if current hour is between begin and end.
* This is especially useful for time-based light/dark mode
@ -533,4 +538,37 @@ public class SharedPreferencesPropertyBackend implements PropertyBackend<String,
int h = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
return h >= 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;
}
}

View file

@ -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);
}
}
}

View file

@ -13,6 +13,9 @@
<string name="tor" translatable="false">Tor</string>
<string name="HTTP" translatable="false">HTTP</string>
<string name="donate_" translatable="false">@string/donate</string>
<string name="app_donate_url" translatable="false"><![CDATA[https://gsantner.net/supportme/?project=markor&source=inapp_supdon]]></string>
<string name="action_compose_new_post" translatable="false">@string/new_post</string>
<string name="search_alert_title" translatable="false">@string/search</string>
<string name="http" translatable="false">http</string>

View file

@ -4,6 +4,8 @@
<string name="open_navdrawer">Open navigation drawer</string>
<string name="close_navdrawer">Close navigation drawer</string>
<string name="reload">Reload</string>
<string name="close">Close</string>
<string name="cancel">Cancel</string>
<!-- Common Words -->
<string name="settings">Settings</string>
@ -259,4 +261,7 @@
<string name="pull_to_refresh">Pull to refresh</string>
<string name="pulling_down_on_top_of_page_to_refresh">Pulling down on top of page to refresh.\nYou need to restart the app for changes to take effect.</string>
<string name="donate">Donate</string>
<string name="do_you_like_this_project_want_donate_to_keep_alive">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!</string>
</resources>