summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/EipFragment.java')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipFragment.java122
1 files changed, 95 insertions, 27 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
index 9fcdcac9..535322e5 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
@@ -25,20 +25,27 @@ 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.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
+import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
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.Locale;
import java.util.Observable;
import java.util.Observer;
@@ -56,17 +63,17 @@ 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.BROADCAST_EIP_EVENT;
-import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
-import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
-import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP;
-import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
+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.ProviderAPI.DOWNLOAD_VPN_CERTIFICATE;
+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;
@@ -108,6 +115,9 @@ public class EipFragment extends Fragment implements Observer {
private IOpenVPNServiceInternal mService;
private ServiceConnection openVpnConnection;
+ private final String DATE_PATTERN = "dd/MM/yyyy";
+ private final int ONE_DAY = 86400000; //1000*60*60*24
+
@Override
public void onAttach(Context context) {
super.onAttach(context);
@@ -158,6 +168,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
@@ -186,7 +204,6 @@ public class EipFragment extends Fragment implements Observer {
} else if (showPendingStartCancellation) {
outState.putBoolean(KEY_SHOW_PENDING_START_CANCELLATION, true);
alertDialog.dismiss();
-
}
}
@@ -240,7 +257,7 @@ public class EipFragment extends Fragment implements Observer {
askUserToLogIn(getString(vpn_certificate_user_message));
} else {
// provider has no VpnCertificate but user is logged in
- downloadVpnCertificate();
+ updateInvalidVpnCertificate();
}
}
@@ -280,24 +297,11 @@ public class EipFragment extends Fragment implements Observer {
protected void stopEipIfPossible() {
Context context = getContext();
- if (context != null) {
- if (isOpenVpnRunningWithoutNetwork()) {
- // TODO move to EIP
- // TODO see stopEIP function
- Bundle resultData = new Bundle();
- resultData.putString(EIP_REQUEST, EIP_ACTION_STOP);
- Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT);
- intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
- intentUpdate.putExtra(BROADCAST_RESULT_CODE, Activity.RESULT_OK);
- intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData);
- Log.d(TAG, "sending broadcast");
- LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intentUpdate);
- } else {
- EipCommand.stopVPN(getContext());
- }
- } else {
+ if (context == null) {
Log.e(TAG, "context is null when trying to stop EIP");
+ return;
}
+ EipCommand.stopVPN(context);
}
private void askPendingStartCancellation() {
@@ -462,8 +466,8 @@ public class EipFragment extends Fragment implements Observer {
background.setImageAlpha(210);
}
- private void downloadVpnCertificate() {
- ProviderAPICommand.execute(getContext(), DOWNLOAD_VPN_CERTIFICATE, provider);
+ private void updateInvalidVpnCertificate() {
+ ProviderAPICommand.execute(getContext(), UPDATE_INVALID_VPN_CERTIFICATE, provider);
}
private void askUserToLogIn(String userMessage) {
@@ -502,4 +506,68 @@ 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;
+ }
+ String message = TextUtils.isEmpty(activity.getString(R.string.donate_message)) ?
+ activity.getString(R.string.donate_default_message) : activity.getString(R.string.donate_message);
+ AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);
+ alertDialog = alertBuilder.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) {
+ }
+ }).setOnDismissListener(new DialogInterface.OnDismissListener() {
+ @Override
+ public void onDismiss(DialogInterface dialog) {
+ saveLastDonationReminderDate();
+ }
+ }).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, Locale.US);
+ 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()) / ONE_DAY;
+ return diffDays >= DONATION_REMINDER_DURATION;
+ }
+
+ private void saveLastDonationReminderDate() {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US);
+ Date lastDate = new Date();
+ preferences.edit().putString(LAST_DONATION_REMINDER_DATE, sdf.format(lastDate)).apply();
+ }
}