Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NotesViewThemeUtils.java « branding « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3970b338a81752c50ea2a323ad6c2f33b651e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package it.niedermann.owncloud.notes.branding;

import static com.nextcloud.android.common.ui.util.ColorStateListUtilsKt.buildColorStateList;
import static com.nextcloud.android.common.ui.util.PlatformThemeUtil.isDarkMode;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.card.MaterialCardView;
import com.nextcloud.android.common.ui.theme.MaterialSchemes;
import com.nextcloud.android.common.ui.theme.ViewThemeUtilsBase;
import com.nextcloud.android.common.ui.theme.utils.MaterialViewThemeUtils;

import it.niedermann.android.util.ColorUtil;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.shared.util.NotesColorUtil;
import kotlin.Pair;

public class NotesViewThemeUtils extends ViewThemeUtilsBase {

    private static final String TAG = NotesViewThemeUtils.class.getSimpleName();

    public NotesViewThemeUtils(@NonNull MaterialSchemes schemes) {
        super(schemes);
    }

    /**
     * The Notes app uses custom navigation view items because they have several features which are
     * not covered by {@link NavigationItem}.
     */
    public void colorNavigationViewItem(@NonNull View view) {
        withScheme(view, scheme -> {
            view.setBackgroundTintList(buildColorStateList(
                    new Pair<>(android.R.attr.state_selected, scheme.getSecondaryContainer()),
                    new Pair<>(-android.R.attr.state_selected, Color.TRANSPARENT)
            ));
            return view;
        });
    }

    /**
     * The Notes app uses custom navigation view items because they have several features which are
     * not covered by {@link NavigationItem}.
     */
    public void colorNavigationViewItemIcon(@NonNull ImageView view) {
        withScheme(view, scheme -> {
            view.setImageTintList(buildColorStateList(
                    new Pair<>(android.R.attr.state_selected, scheme.getOnSecondaryContainer()),
                    new Pair<>(-android.R.attr.state_selected, scheme.getOnSurfaceVariant())
            ));
            return view;
        });
    }

    /**
     * The Notes app uses custom navigation view items because they have several features which are
     * not covered by {@link NavigationItem}.
     */
    public void colorNavigationViewItemText(@NonNull TextView view) {
        withScheme(view, scheme -> {
            view.setTextColor(buildColorStateList(
                    new Pair<>(android.R.attr.state_selected, scheme.getOnSecondaryContainer()),
                    new Pair<>(-android.R.attr.state_selected, scheme.getOnSurfaceVariant())
            ));
            return view;
        });
    }

    /**
     * @deprecated should be replaced by {@link MaterialViewThemeUtils#themeToolbar(MaterialToolbar)}.
     */
    @Deprecated(forRemoval = true)
    public void applyBrandToPrimaryToolbar(@NonNull AppBarLayout appBarLayout,
                                           @NonNull Toolbar toolbar,
                                           @ColorInt int color) {
        // FIXME Workaround for https://github.com/nextcloud/notes-android/issues/889
        appBarLayout.setBackgroundColor(ContextCompat.getColor(appBarLayout.getContext(), R.color.primary));

        final var overflowDrawable = toolbar.getOverflowIcon();
        if (overflowDrawable != null) {
            overflowDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            toolbar.setOverflowIcon(overflowDrawable);
        }

        final var navigationDrawable = toolbar.getNavigationIcon();
        if (navigationDrawable != null) {
            navigationDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
            toolbar.setNavigationIcon(navigationDrawable);
        }
    }

    /**
     * Colorizes only a specific part of a drawable
     */
    public void colorLayerDrawable(@NonNull LayerDrawable check, @IdRes int areaToColor, @ColorInt int mainColor) {
        final var drawable = check.findDrawableByLayerId(areaToColor);
        if (drawable == null) {
            Log.e(TAG, "Could not find areaToColor (" + areaToColor + "). Cannot apply brand.");
        } else {
            DrawableCompat.setTint(drawable, mainColor);
        }
    }

    @ColorInt
    public int getTextHighlightBackgroundColor(@NonNull Context context,
                                               @ColorInt int mainColor,
                                               @ColorInt int colorPrimary,
                                               @ColorInt int colorAccent) {
        if (isDarkMode(context)) { // Dark background
            if (ColorUtil.INSTANCE.isColorDark(mainColor)) { // Dark brand color
                if (NotesColorUtil.contrastRatioIsSufficient(mainColor, colorPrimary)) { // But also dark text
                    return mainColor;
                } else {
                    return ContextCompat.getColor(context, R.color.defaultTextHighlightBackground);
                }
            } else { // Light brand color
                if (NotesColorUtil.contrastRatioIsSufficient(mainColor, colorAccent)) { // But also dark text
                    return Color.argb(77, Color.red(mainColor), Color.green(mainColor), Color.blue(mainColor));
                } else {
                    return ContextCompat.getColor(context, R.color.defaultTextHighlightBackground);
                }
            }
        } else { // Light background
            if (ColorUtil.INSTANCE.isColorDark(mainColor)) { // Dark brand color
                if (NotesColorUtil.contrastRatioIsSufficient(mainColor, colorAccent)) { // But also dark text
                    return Color.argb(77, Color.red(mainColor), Color.green(mainColor), Color.blue(mainColor));
                } else {
                    return ContextCompat.getColor(context, R.color.defaultTextHighlightBackground);
                }
            } else { // Light brand color
                if (NotesColorUtil.contrastRatioIsSufficient(mainColor, colorPrimary)) { // But also dark text
                    return mainColor;
                } else {
                    return ContextCompat.getColor(context, R.color.defaultTextHighlightBackground);
                }
            }
        }
    }

    /**
     * @deprecated Should be replaced with {@link com.google.android.material.search.SearchBar} component.
     */
    @Deprecated
    public void themeSearchCardView(@NonNull MaterialCardView searchBarWrapper) {
        withScheme(searchBarWrapper, scheme -> {
            searchBarWrapper.setBackgroundTintList(ColorStateList.valueOf(scheme.getSurface()));
            return searchBarWrapper;
        });
    }

    /**
     * @deprecated Should be replaced with {@link com.google.android.material.search.SearchBar} or
     * {@link MaterialViewThemeUtils#themeToolbar(MaterialToolbar)}
     */
    @Deprecated
    public void themeSearchToolbar(@NonNull MaterialToolbar toolbar) {
        withScheme(toolbar, scheme -> {
            toolbar.setNavigationIconTint(scheme.getOnSurface());
            toolbar.setTitleTextColor(scheme.getOnSurface());
            return toolbar;
        });
    }

    /**
     * @deprecated Should be replaced with {@link com.google.android.material.search.SearchView}
     * @see com.nextcloud.android.common.ui.theme.utils.AndroidXViewThemeUtils#themeToolbarSearchView(SearchView)
     */
    @Deprecated
    public void themeToolbarSearchView(@NonNull SearchView searchView) {
        withScheme(searchView, scheme -> {
            // hacky as no default way is provided
            final var editText = (SearchView.SearchAutoComplete) searchView
                    .findViewById(androidx.appcompat.R.id.search_src_text);
            final var closeButton = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
            final var searchButton = (ImageView) searchView.findViewById(androidx.appcompat.R.id.search_button);
            editText.setHintTextColor(scheme.getOnSurfaceVariant());
            editText.setHighlightColor(scheme.getInverseOnSurface());
            editText.setTextColor(scheme.getOnSurface());
            closeButton.setColorFilter(scheme.getOnSurface());
            searchButton.setColorFilter(scheme.getOnSurface());
            return searchView;
        });
    }
}