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 ++++-- 4 files changed, 103 insertions(+), 5 deletions(-) (limited to 'app/src/main') 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 -- cgit v1.2.3