diff options
Diffstat (limited to 'app/src/main/java')
3 files changed, 94 insertions, 4 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java index bcce1549..b4e8646e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java @@ -91,6 +91,12 @@ public interface Constants { ////////////////////////////////////////////// // CUSTOM CONSTANTS ///////////////////////////////////////////// - String DONATION_URL = BuildConfig.donation_url; + boolean ENABLE_DONATION = BuildConfig.enable_donation; + boolean ENABLE_DONATION_REMINDER = BuildConfig.enable_donation_reminder; + int DONATION_REMINDER_DURATION = BuildConfig.donation_reminder_duration; + String DONATION_URL = BuildConfig.donation_url == null || BuildConfig.donation_url.equals("")? + BuildConfig.default_donation_url:BuildConfig.donation_url; + String LAST_DONATION_REMINDER_DATE = "last_daonation_reminder_date"; + } diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java index 5dbec7b0..86a5d7c66 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java @@ -25,9 +25,11 @@ import android.content.ServiceConnection; import android.content.SharedPreferences; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; +import android.net.Uri; import android.os.Bundle; import android.os.IBinder; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.support.v7.widget.AppCompatImageView; @@ -38,6 +40,10 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.temporal.ChronoUnit; +import java.util.Date; import java.util.Observable; import java.util.Observer; @@ -55,11 +61,16 @@ import se.leap.bitmaskclient.views.VpnStateImage; import static android.view.View.GONE; import static android.view.View.VISIBLE; import static de.blinkt.openvpn.core.ConnectionStatus.LEVEL_NONETWORK; +import static se.leap.bitmaskclient.Constants.DONATION_REMINDER_DURATION; import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT; import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_LOG_IN; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; +import static se.leap.bitmaskclient.Constants.DONATION_URL; +import static se.leap.bitmaskclient.Constants.ENABLE_DONATION; +import static se.leap.bitmaskclient.Constants.ENABLE_DONATION_REMINDER; +import static se.leap.bitmaskclient.Constants.LAST_DONATION_REMINDER_DATE; import static se.leap.bitmaskclient.ProviderAPI.UPDATE_INVALID_VPN_CERTIFICATE; import static se.leap.bitmaskclient.ProviderAPI.USER_MESSAGE; import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; @@ -102,6 +113,8 @@ public class EipFragment extends Fragment implements Observer { private IOpenVPNServiceInternal mService; private ServiceConnection openVpnConnection; + private final String DATE_PATTERN = "dd/MM/yyyy"; + @Override public void onAttach(Context context) { super.onAttach(context); @@ -152,6 +165,14 @@ public class EipFragment extends Fragment implements Observer { } @Override + public void onStart() { + super.onStart(); + if (isDonationReminderCallable()){ + showDonationReminder(); + } + } + + @Override public void onResume() { super.onResume(); //FIXME: avoid race conditions while checking certificate an logging in at about the same time @@ -483,4 +504,65 @@ public class EipFragment extends Fragment implements Observer { mService = null; } } + + private void showDonationReminder() { + Activity activity = getActivity(); + if (activity == null) { + Log.e(TAG, "activity is null when triggering donation reminder"); + return; + } + saveLastDonationReminderDate(); + String message = activity.getString(R.string.donate_message) == null || activity.getString(R.string.donate_message).equals("")? + activity.getString(R.string.donate_default_message):activity.getString(R.string.donate_message); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(activity.getString(R.string.donate_title)) + .setMessage(message) + .setPositiveButton(R.string.donate_button_donate, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(DONATION_URL)); + startActivity(browserIntent); + } + }) + .setNegativeButton(R.string.donate_button_remind_later, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + } + }) + .show(); + } + + private boolean isDonationReminderCallable(){ + if (!ENABLE_DONATION||!ENABLE_DONATION_REMINDER){ + return false; + } + + if (preferences==null){ + Log.e(TAG, "preferences is null!"); + return false; + } + + String lastDonationReminderDate = preferences.getString(LAST_DONATION_REMINDER_DATE, null); + if (lastDonationReminderDate==null){ + return true; + } + + SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); + Date lastDate; + try { + lastDate = sdf.parse(lastDonationReminderDate); + } catch (ParseException e) { + e.printStackTrace(); + Log.e(TAG, e.getMessage()); + return false; + } + + Date currentDate = new Date(); + long diffDays = (currentDate.getTime() - lastDate.getTime())/(1000*60*60*24); + return diffDays >= DONATION_REMINDER_DURATION; + } + + private void saveLastDonationReminderDate(){ + SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN); + Date lastDate = new Date(); + preferences.edit().putString(LAST_DONATION_REMINDER_DATE, sdf.format(lastDate)).apply(); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java index 371e65fc..14c2eacf 100644 --- a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java @@ -47,7 +47,6 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CompoundButton; import android.widget.ListView; -import android.widget.Toast; import se.leap.bitmaskclient.ConfigHelper; import se.leap.bitmaskclient.DrawerSettingsAdapter; @@ -69,6 +68,7 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.REQUEST_CODE_SWITCH_PROVIDER; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.Constants.DONATION_URL; +import static se.leap.bitmaskclient.Constants.ENABLE_DONATION; import static se.leap.bitmaskclient.DrawerSettingsAdapter.ABOUT; import static se.leap.bitmaskclient.DrawerSettingsAdapter.ALWAYS_ON; import static se.leap.bitmaskclient.DrawerSettingsAdapter.BATTERY_SAVER; @@ -80,7 +80,7 @@ import static se.leap.bitmaskclient.DrawerSettingsAdapter.SWITCH_PROVIDER; import static se.leap.bitmaskclient.R.string.about_fragment_title; import static se.leap.bitmaskclient.R.string.log_fragment_title; import static se.leap.bitmaskclient.R.string.switch_provider_menu_option; -import static se.leap.bitmaskclient.R.string.navigation_drawer_donation_button; +import static se.leap.bitmaskclient.R.string.donate_title; /** * Fragment used for managing interactions for and presentation of a navigation drawer. @@ -188,7 +188,9 @@ public class NavigationDrawerFragment extends Fragment { settingsListAdapter.addItem(getSimpleTextInstance(getString(switch_provider_menu_option), SWITCH_PROVIDER)); settingsListAdapter.addItem(getSimpleTextInstance(getString(log_fragment_title), LOG)); settingsListAdapter.addItem(getSimpleTextInstance(getString(about_fragment_title), ABOUT)); - settingsListAdapter.addItem(getSimpleTextInstance(getString(navigation_drawer_donation_button), DONATE)); + if (ENABLE_DONATION) { + settingsListAdapter.addItem(getSimpleTextInstance(getString(donate_title), DONATE)); + } mDrawerSettingsListView.setAdapter(settingsListAdapter); |