From cac2c849e8d7d35bd9c72fc8c9bb8bc322eb24a0 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 12 Nov 2022 22:21:31 +0100 Subject: create UI for message of the day --- .../se/leap/bitmaskclient/base/MainActivity.java | 84 ++++++++++----- .../se/leap/bitmaskclient/base/StartActivity.java | 18 +++- .../bitmaskclient/base/fragments/MotdFragment.java | 103 ++++++++++++++++++ .../leap/bitmaskclient/base/models/Constants.java | 1 + app/src/main/res/drawable-hdpi/ic_arrow_right.png | Bin 0 -> 562 bytes app/src/main/res/drawable-ldpi/ic_arrow_right.png | Bin 0 -> 458 bytes app/src/main/res/drawable-mdpi/ic_arrow_right.png | Bin 0 -> 551 bytes app/src/main/res/drawable-xhdpi/ic_arrow_right.png | Bin 0 -> 753 bytes .../main/res/drawable-xxhdpi/ic_arrow_right.png | Bin 0 -> 784 bytes .../main/res/drawable-xxxhdpi/ic_arrow_right.png | Bin 0 -> 1168 bytes app/src/main/res/layout/f_motd.xml | 116 +++++++++++++++++++++ 11 files changed, 290 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/base/fragments/MotdFragment.java create mode 100644 app/src/main/res/drawable-hdpi/ic_arrow_right.png create mode 100644 app/src/main/res/drawable-ldpi/ic_arrow_right.png create mode 100644 app/src/main/res/drawable-mdpi/ic_arrow_right.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_arrow_right.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_arrow_right.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_arrow_right.png create mode 100644 app/src/main/res/layout/f_motd.xml (limited to 'app/src') diff --git a/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java b/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java index 8cb12652..e2fa0783 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java @@ -17,6 +17,34 @@ package se.leap.bitmaskclient.base; +import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed; +import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; +import static se.leap.bitmaskclient.base.models.Constants.ASK_TO_CANCEL_VPN; +import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_CODE; +import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_KEY; +import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_LAUNCH_VPN; +import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_PREPARE_VPN; +import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START; +import static se.leap.bitmaskclient.base.models.Constants.EIP_REQUEST; +import static se.leap.bitmaskclient.base.models.Constants.EXTRA_MOTD_MSG; +import static se.leap.bitmaskclient.base.models.Constants.LOCATION; +import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY; +import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_CONFIGURE_LEAP; +import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_LOG_IN; +import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_SWITCH_PROVIDER; +import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.storeProviderInPreferences; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_VPN_PREPARE; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORID; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_EXCEPTION; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_TIMEOUT; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.UPDATE_INVALID_VPN_CERTIFICATE; +import static se.leap.bitmaskclient.providersetup.ProviderAPI.USER_MESSAGE; + import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -40,6 +68,7 @@ import se.leap.bitmaskclient.base.fragments.EipFragment; import se.leap.bitmaskclient.base.fragments.ExcludeAppsFragment; import se.leap.bitmaskclient.base.fragments.LogFragment; import se.leap.bitmaskclient.base.fragments.MainActivityErrorDialog; +import se.leap.bitmaskclient.base.fragments.MotdFragment; import se.leap.bitmaskclient.base.fragments.NavigationDrawerFragment; import se.leap.bitmaskclient.base.fragments.SettingsFragment; import se.leap.bitmaskclient.base.models.Provider; @@ -49,38 +78,10 @@ import se.leap.bitmaskclient.eip.EIP; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.eip.EipSetupListener; import se.leap.bitmaskclient.eip.EipSetupObserver; -import se.leap.bitmaskclient.eip.EipStatus; import se.leap.bitmaskclient.providersetup.ProviderAPI; import se.leap.bitmaskclient.providersetup.activities.LoginActivity; import se.leap.bitmaskclient.providersetup.models.LeapSRPSession; -import static se.leap.bitmaskclient.R.string.downloading_vpn_certificate_failed; -import static se.leap.bitmaskclient.R.string.vpn_certificate_user_message; -import static se.leap.bitmaskclient.base.models.Constants.ASK_TO_CANCEL_VPN; -import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_CODE; -import static se.leap.bitmaskclient.base.models.Constants.BROADCAST_RESULT_KEY; -import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_LAUNCH_VPN; -import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_PREPARE_VPN; -import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_START; -import static se.leap.bitmaskclient.base.models.Constants.EIP_REQUEST; -import static se.leap.bitmaskclient.base.models.Constants.LOCATION; -import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY; -import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_CONFIGURE_LEAP; -import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_LOG_IN; -import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_SWITCH_PROVIDER; -import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES; -import static se.leap.bitmaskclient.base.utils.PreferenceHelper.storeProviderInPreferences; -import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_INVALID_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.eip.EIP.EIPErrors.ERROR_VPN_PREPARE; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORID; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.ERRORS; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_DOWNLOADED_EIP_SERVICE; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.INCORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_EXCEPTION; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.TOR_TIMEOUT; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.UPDATE_INVALID_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.providersetup.ProviderAPI.USER_MESSAGE; - public class MainActivity extends AppCompatActivity implements EipSetupListener, Observer { @@ -93,6 +94,7 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, public final static String ACTION_SHOW_VPN_FRAGMENT = "action_show_vpn_fragment"; public final static String ACTION_SHOW_LOG_FRAGMENT = "action_show_log_fragment"; public final static String ACTION_SHOW_DIALOG_FRAGMENT = "action_show_dialog_fragment"; + public final static String ACTION_SHOW_MOTD_FRAGMENT = "action_show_motd_fragment"; /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. @@ -161,10 +163,22 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, bundle.putParcelable(PROVIDER_KEY, provider); fragment.setArguments(bundle); hideActionBarSubTitle(); + showActionBar(); + break; + case ACTION_SHOW_MOTD_FRAGMENT: + fragment = new MotdFragment(); + Bundle motdBundle = new Bundle(); + if (intent.hasExtra(EXTRA_MOTD_MSG)) { + motdBundle.putString(EXTRA_MOTD_MSG, intent.getStringExtra(EXTRA_MOTD_MSG)); + } + fragment.setArguments(motdBundle); + hideActionBarSubTitle(); + hideActionBar(); break; case ACTION_SHOW_LOG_FRAGMENT: fragment = new LogFragment(); setActionBarTitle(R.string.log_fragment_title); + showActionBar(); break; case ACTION_SHOW_DIALOG_FRAGMENT: if (intent.hasExtra(EIP.ERRORID)) { @@ -186,6 +200,20 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, } } + private void hideActionBar() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.hide(); + } + } + + private void showActionBar() { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.show(); + } + } + private void hideActionBarSubTitle() { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { diff --git a/app/src/main/java/se/leap/bitmaskclient/base/StartActivity.java b/app/src/main/java/se/leap/bitmaskclient/base/StartActivity.java index 537c6c91..6e557236 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/StartActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/StartActivity.java @@ -16,9 +16,11 @@ */ package se.leap.bitmaskclient.base; +import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_MOTD_FRAGMENT; import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_VPN_FRAGMENT; import static se.leap.bitmaskclient.base.models.Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE; import static se.leap.bitmaskclient.base.models.Constants.EIP_RESTART_ON_BOOT; +import static se.leap.bitmaskclient.base.models.Constants.EXTRA_MOTD_MSG; import static se.leap.bitmaskclient.base.models.Constants.PREFERENCES_APP_VERSION; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_KEY; @@ -265,7 +267,7 @@ public class StartActivity extends Activity{ Log.e(TAG, "Couldn't show Motd. Invalid timestamp."); } } - showMainActivity(); + showVPNFragment(); } private void showMotd(Provider p, IMessage message) { @@ -277,15 +279,23 @@ public class StartActivity extends Activity{ PreferenceHelper.persistProvider(this, p); ProviderObservable.getInstance().updateProvider(p); } - //TODO: show Motd Activity! - showMainActivity(); + showMotdFragment(message); } - private void showMainActivity() { + private void showVPNFragment() { Intent intent = new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setAction(ACTION_SHOW_VPN_FRAGMENT); startActivity(intent); finish(); } + + private void showMotdFragment(IMessage message) { + Intent intent = new Intent(this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.setAction(ACTION_SHOW_MOTD_FRAGMENT); + intent.putExtra(EXTRA_MOTD_MSG, message.toJson()); + startActivity(intent); + finish(); + } } diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/MotdFragment.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/MotdFragment.java new file mode 100644 index 00000000..16834ab5 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/MotdFragment.java @@ -0,0 +1,103 @@ +package se.leap.bitmaskclient.base.fragments; + +import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_VPN_FRAGMENT; +import static se.leap.bitmaskclient.base.models.Constants.EXTRA_MOTD_MSG; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.text.Html; +import android.text.TextUtils; +import android.text.method.LinkMovementMethod; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatImageButton; +import androidx.appcompat.widget.AppCompatTextView; +import androidx.fragment.app.Fragment; + +import java.util.Locale; + +import de.blinkt.openvpn.core.VpnStatus; +import motd.IMessage; +import motd.Motd; +import se.leap.bitmaskclient.base.MainActivity; +import se.leap.bitmaskclient.databinding.FMotdBinding; + + +public class MotdFragment extends Fragment { + + private static final String TAG = MotdFragment.class.getSimpleName(); + private IMessage message; + FMotdBinding binding; + AppCompatTextView messageView; + AppCompatImageButton nextButton; + + public MotdFragment() { + // Required empty public constructor + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + String messageString = getArguments().getString(EXTRA_MOTD_MSG); + if (messageString != null) { + Log.d(TAG, "MotdFragment received: " + messageString); + message = Motd.newMessage(messageString); + } + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + binding = FMotdBinding.inflate(getLayoutInflater()); + return binding.getRoot(); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + messageView = binding.motdContent; + nextButton = binding.nextBtn; + String currentLang = Locale.getDefault().getLanguage(); + String text = message.getLocalizedText(currentLang); + if (TextUtils.isEmpty(text)) { + text = message.getLocalizedText("en"); + } + + if (TextUtils.isEmpty(text)) { + String error = "Message of the day cannot be shown. Unsupported app language and unknown default langauge."; + Log.e(TAG, error); + VpnStatus.logError(error); + showVpnFragment(view.getContext()); + return; + } + + Log.d(TAG, "set motd text: " + text); + messageView.setText(Html.fromHtml(text)); + messageView.setMovementMethod(LinkMovementMethod.getInstance()); + nextButton.setOnClickListener(v -> { + showVpnFragment(v.getContext()); + }); + + } + + private void showVpnFragment(Context context) { + try { + Intent intent = new Intent(context, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.setAction(ACTION_SHOW_VPN_FRAGMENT); + context.startActivity(intent); + } catch (NullPointerException npe) { + npe.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java index d0af8112..ee5bd2a7 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java @@ -77,6 +77,7 @@ public interface Constants { String ASK_TO_CANCEL_VPN = "ask_to_cancel_vpn"; + String EXTRA_MOTD_MSG = "extra_motd_message"; ////////////////////////////////////////////// diff --git a/app/src/main/res/drawable-hdpi/ic_arrow_right.png b/app/src/main/res/drawable-hdpi/ic_arrow_right.png new file mode 100644 index 00000000..2d081384 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_arrow_right.png differ diff --git a/app/src/main/res/drawable-ldpi/ic_arrow_right.png b/app/src/main/res/drawable-ldpi/ic_arrow_right.png new file mode 100644 index 00000000..7fc572af Binary files /dev/null and b/app/src/main/res/drawable-ldpi/ic_arrow_right.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_arrow_right.png b/app/src/main/res/drawable-mdpi/ic_arrow_right.png new file mode 100644 index 00000000..60702e35 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_arrow_right.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_arrow_right.png b/app/src/main/res/drawable-xhdpi/ic_arrow_right.png new file mode 100644 index 00000000..f1f4f691 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_arrow_right.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_arrow_right.png b/app/src/main/res/drawable-xxhdpi/ic_arrow_right.png new file mode 100644 index 00000000..e0031ca8 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_arrow_right.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_arrow_right.png b/app/src/main/res/drawable-xxxhdpi/ic_arrow_right.png new file mode 100644 index 00000000..1b4f4239 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_arrow_right.png differ diff --git a/app/src/main/res/layout/f_motd.xml b/app/src/main/res/layout/f_motd.xml new file mode 100644 index 00000000..3cfe7dfe --- /dev/null +++ b/app/src/main/res/layout/f_motd.xml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3