From 980a6986db013304e02589c41020cdfa67215854 Mon Sep 17 00:00:00 2001 From: cyberta Date: Tue, 29 Nov 2022 18:43:12 +0100 Subject: tint action bar according to connection state --- .../se/leap/bitmaskclient/base/MainActivity.java | 46 +++++++++++++++++++++- .../base/fragments/NavigationDrawerFragment.java | 13 ++++++ .../leap/bitmaskclient/base/utils/ViewHelper.java | 37 +++++++++++++++++ app/src/main/res/values/colors.xml | 12 ++++-- .../EipFragment.java | 35 +++++++++++----- 5 files changed, 128 insertions(+), 15 deletions(-) (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 e2fa0783..3b200fd1 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/MainActivity.java @@ -34,6 +34,7 @@ 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.base.utils.ViewHelper.isBrightColor; 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; @@ -47,12 +48,18 @@ import static se.leap.bitmaskclient.providersetup.ProviderAPI.USER_MESSAGE; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.drawable.ColorDrawable; +import android.os.Build; import android.os.Bundle; import android.util.Log; +import android.view.Window; +import android.view.WindowManager; +import androidx.annotation.ColorRes; import androidx.annotation.StringRes; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; @@ -74,6 +81,7 @@ import se.leap.bitmaskclient.base.fragments.SettingsFragment; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.base.models.ProviderObservable; import se.leap.bitmaskclient.base.utils.PreferenceHelper; +import se.leap.bitmaskclient.base.utils.ViewHelper; import se.leap.bitmaskclient.eip.EIP; import se.leap.bitmaskclient.eip.EipCommand; import se.leap.bitmaskclient.eip.EipSetupListener; @@ -82,7 +90,6 @@ import se.leap.bitmaskclient.providersetup.ProviderAPI; import se.leap.bitmaskclient.providersetup.activities.LoginActivity; import se.leap.bitmaskclient.providersetup.models.LeapSRPSession; - public class MainActivity extends AppCompatActivity implements EipSetupListener, Observer { public final static String TAG = MainActivity.class.getSimpleName(); @@ -96,6 +103,8 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, public final static String ACTION_SHOW_DIALOG_FRAGMENT = "action_show_dialog_fragment"; public final static String ACTION_SHOW_MOTD_FRAGMENT = "action_show_motd_fragment"; + private @ColorRes int actionBarTextColor; + /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ @@ -227,6 +236,41 @@ public class MainActivity extends AppCompatActivity implements EipSetupListener, } } + public @ColorRes int getActionBarTitleColor() { + return actionBarTextColor; + } + + public void setDefaultActivityBarColor() { + setActivityBarColor(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorActionBarTitleFont); + } + + public void setActivityBarColor(@ColorRes int primaryColor, @ColorRes int secondaryColor, @ColorRes int textColor) { + ActionBar bar = getSupportActionBar(); + if (bar == null) { + return; + } + int color = ContextCompat.getColor(this, secondaryColor); + bar.setBackgroundDrawable(new ColorDrawable(color)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Window window = this.getWindow(); + window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); + window.setStatusBarColor(ContextCompat.getColor(this, primaryColor)); + } + + if (bar.getTitle() == null) { + return; + } + + if (textColor == 0) { + actionBarTextColor = isBrightColor(color) ? R.color.actionbar_dark_color : R.color.actionbar_light_color; + } else { + actionBarTextColor = textColor; + } + + ViewHelper.setActionBarTextColor(bar, actionBarTextColor); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/NavigationDrawerFragment.java index cdfee7b2..fe36e00a 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/fragments/NavigationDrawerFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/NavigationDrawerFragment.java @@ -38,6 +38,9 @@ import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -45,12 +48,15 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.ColorRes; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.appcompat.widget.ViewUtils; +import androidx.core.content.ContextCompat; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; @@ -64,6 +70,7 @@ import se.leap.bitmaskclient.base.FragmentManagerEnhanced; import se.leap.bitmaskclient.base.MainActivity; import se.leap.bitmaskclient.base.models.Provider; import se.leap.bitmaskclient.base.models.ProviderObservable; +import se.leap.bitmaskclient.base.utils.ViewHelper; import se.leap.bitmaskclient.base.views.IconSwitchEntry; import se.leap.bitmaskclient.base.views.IconTextEntry; import se.leap.bitmaskclient.eip.EipStatus; @@ -443,6 +450,12 @@ public class NavigationDrawerFragment extends Fragment implements SharedPreferen ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(R.string.app_name); + Activity activity = getActivity(); + if (activity == null) { + return; + } + @ColorRes int titleColor = ((MainActivity) activity).getActionBarTitleColor(); + ViewHelper.setActionBarTextColor(actionBar, titleColor); } private ActionBar getActionBar() { diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java index 7410172f..8076f99e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/ViewHelper.java @@ -1,11 +1,18 @@ package se.leap.bitmaskclient.base.utils; +import android.app.Notification; import android.content.Context; +import android.graphics.Color; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import androidx.annotation.ColorRes; import androidx.annotation.DimenRes; import androidx.annotation.StringRes; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; /** @@ -33,4 +40,34 @@ public class ViewHelper { } } + public static boolean isBrightColor(int color) { + if (android.R.color.transparent == color) + return true; + + boolean rtnValue = false; + + int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) }; + + int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1] + * rgb[1] * .691 + rgb[2] * rgb[2] * .068); + + // color is light + if (brightness >= 200) { + rtnValue = true; + } + + return rtnValue; + } + + public static void setActionBarTextColor(ActionBar bar, @ColorRes int titleColor) { + CharSequence titleCharSequence = bar.getTitle(); + if (titleCharSequence == null) { + return; + } + String title = titleCharSequence.toString(); + Spannable spannableTitle = new SpannableString(title); + spannableTitle.setSpan(new ForegroundColorSpan(ContextCompat.getColor(bar.getThemedContext(), titleColor)), 0, spannableTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + bar.setTitle(spannableTitle); + } + } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 7ec2e81a..b6971138 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -54,8 +54,12 @@ #709152 #CCFFFEFE #CCCCCBCB - #EC6767 - #DB6F66 - #D05D59 - + #EC6767 + #CCff9895 + #FADD85 + #CCffffb6 + #CCDCB8 + #ffffea + @color/black800 + @color/white diff --git a/app/src/normal/java/se.leap.bitmaskclient.base.fragments/EipFragment.java b/app/src/normal/java/se.leap.bitmaskclient.base.fragments/EipFragment.java index 842cb38e..cccd6c23 100644 --- a/app/src/normal/java/se.leap.bitmaskclient.base.fragments/EipFragment.java +++ b/app/src/normal/java/se.leap.bitmaskclient.base.fragments/EipFragment.java @@ -29,8 +29,6 @@ import static se.leap.bitmaskclient.base.models.Constants.REQUEST_CODE_SWITCH_PR import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.base.utils.ConfigHelper.isDefaultBitmask; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getPreferredCity; -import static se.leap.bitmaskclient.base.utils.ViewHelper.convertDimensionToPx; -import static se.leap.bitmaskclient.eip.EipSetupObserver.gatewayOrder; import static se.leap.bitmaskclient.eip.EipSetupObserver.reconnectingWithDifferentGateway; import static se.leap.bitmaskclient.eip.GatewaysManager.Load.UNKNOWN; import static se.leap.bitmaskclient.providersetup.ProviderAPI.DOWNLOAD_GEOIP_JSON; @@ -43,22 +41,18 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; -import android.graphics.ColorMatrix; -import android.graphics.ColorMatrixColorFilter; import android.os.Bundle; import android.os.IBinder; -import android.os.Vibrator; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.RelativeSizeSpan; import android.util.Log; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Toast; +import androidx.annotation.ColorRes; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.AppCompatImageView; @@ -77,7 +71,6 @@ import de.blinkt.openvpn.core.IOpenVPNServiceInternal; import de.blinkt.openvpn.core.OpenVPNService; import de.blinkt.openvpn.core.VpnStatus; import de.blinkt.openvpn.core.connection.Connection; -import se.leap.bitmaskclient.BuildConfig; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.base.FragmentManagerEnhanced; import se.leap.bitmaskclient.base.MainActivity; @@ -268,6 +261,10 @@ public class EipFragment extends Fragment implements Observer { @Override public void onDestroyView() { super.onDestroyView(); + Activity activity = getActivity(); + if (activity != null) { + ((MainActivity) activity).setDefaultActivityBarColor(); + } eipStatus.deleteObserver(this); providerObservable.deleteObserver(this); torStatusObservable.deleteObserver(this); @@ -430,6 +427,14 @@ public class EipFragment extends Fragment implements Observer { } } + private void setActivityBarColor(@ColorRes int primaryColor, @ColorRes int secondaryColor) { + Activity activity = getActivity(); + if (activity == null) { + return; + } + ((MainActivity) getActivity()).setActivityBarColor(primaryColor, secondaryColor, R.color.actionbar_dark_color); + } + private void handleNewState() { Activity activity = getActivity(); if (activity == null) { @@ -460,6 +465,7 @@ public class EipFragment extends Fragment implements Observer { background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_yellow)); stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.yellow_mask)); mainButton.updateState(false, true, false); + setActivityBarColor(R.color.bg_yellow_top, R.color.bg_yellow_top_light_transparent); } else if (eipStatus.isConnecting()) { setMainButtonEnabled(true); String city = getPreferredCity(getContext()); @@ -475,7 +481,7 @@ public class EipFragment extends Fragment implements Observer { background.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.bg_yellow)); stateView.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.yellow_mask)); mainButton.updateState(false, true, false); - + setActivityBarColor(R.color.bg_yellow_top, R.color.bg_yellow_top_light_transparent); } else if (eipStatus.isConnected()) { setMainButtonEnabled(true); mainButton.updateState(true, false, false); @@ -488,6 +494,7 @@ public class EipFragment extends Fragment implements Observer { subDescription.setText(null); background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_green)); stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.green_mask)); + setActivityBarColor(R.color.bg_green_top, R.color.bg_green_top_light_transparent); } else if(isOpenVpnRunningWithoutNetwork()) { Log.d(TAG, "eip fragment eipStatus - isOpenVpnRunningWithoutNetwork"); setMainButtonEnabled(true); @@ -500,6 +507,7 @@ public class EipFragment extends Fragment implements Observer { subDescription.setText(R.string.eip_state_no_network); background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_yellow)); stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.yellow_mask)); + setActivityBarColor(R.color.bg_yellow_top, R.color.bg_yellow_top_light_transparent); } else if (eipStatus.isDisconnected() && reconnectingWithDifferentGateway()) { locationButton.setText(VpnStatus.getCurrentlyConnectingVpnName()); locationButton.setLocationLoad(UNKNOWN); @@ -507,12 +515,17 @@ public class EipFragment extends Fragment implements Observer { locationButton.showRecommendedIndicator(false); mainDescription.setText(R.string.eip_status_connecting); subDescription.setText(R.string.reconnecting); + background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_yellow)); + stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.yellow_mask)); + setActivityBarColor(R.color.bg_yellow_top, R.color.bg_yellow_top_light_transparent); + } else if (eipStatus.isDisconnecting()) { setMainButtonEnabled(false); background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_red)); - stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.yellow_mask)); + stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.red_mask)); mainButton.updateState(false, false, false); mainDescription.setText(R.string.eip_status_unsecured); + setActivityBarColor(R.color.bg_red_top, R.color.bg_red_top_light_transparent); } else if (eipStatus.isBlocking()) { setMainButtonEnabled(true); mainButton.updateState(true, false, true); @@ -524,6 +537,7 @@ public class EipFragment extends Fragment implements Observer { subDescription.setText(getString(R.string.eip_state_blocking, getString(R.string.app_name))); background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_red)); stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.red_mask)); + setActivityBarColor(R.color.bg_red_top, R.color.bg_red_top_light_transparent); } else { locationButton.setText(getContext().getString(R.string.vpn_button_turn_on)); setMainButtonEnabled(true); @@ -537,6 +551,7 @@ public class EipFragment extends Fragment implements Observer { subDescription.setText(null); background.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.bg_red)); stateView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.red_mask)); + setActivityBarColor(R.color.bg_red_top, R.color.bg_red_top_light_transparent); } } -- cgit v1.2.3