Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76c19066 authored by Narinder Rana's avatar Narinder Rana
Browse files

get Accent color from OS and set Toolbar and Status bar programmatically

parent 2ebe52a9
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -20,8 +20,11 @@ import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;

import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
@@ -34,6 +37,7 @@ import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import androidx.appcompat.view.ContextThemeWrapper;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.core.view.MenuItemCompat;
@@ -44,6 +48,8 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SearchView.OnQueryTextListener;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -321,6 +327,8 @@ public class TaskListActivity extends BaseActivity implements TaskListFragment.C
                }
            });
        }

        fetchAccentColor(this);
    }


@@ -687,4 +695,43 @@ public class TaskListActivity extends BaseActivity implements TaskListFragment.C
    public boolean isInTransientState() {
        return mTransientState;
    }

    /*
     * get Accent color from OS
     * */
    private int fetchAccentColor(Context context) {

        TypedValue typedValue = new TypedValue();
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this,
                android.R.style.Theme_DeviceDefault);
        contextThemeWrapper.getTheme().resolveAttribute(android.R.attr.colorAccent,
                typedValue, true);
        int color_accent = typedValue.data;
        Log.e("TAG", "accent Colour  #"+Integer.toHexString(color_accent));

        //change toolbar color
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(color_accent));

        //change status bar color
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(darkenColor(color_accent));
        }

        return color_accent;
    }





    @ColorInt int darkenColor(@ColorInt int color) {
        float[] hsv = new float[3];
        android.graphics.Color.colorToHSV(color, hsv);
        hsv[2] *= 0.8f;
        return android.graphics.Color.HSVToColor(hsv);
    }
}