FairEmail/app/src/main/java/eu/faircode/email/ViewCardOptional.java

95 lines
3.1 KiB
Java
Raw Normal View History

2019-08-14 15:21:37 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2019-08-14 15:21:37 +00:00
*/
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.AttributeSet;
2019-10-09 11:24:16 +00:00
import android.view.ViewGroup;
2019-08-14 15:21:37 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.preference.PreferenceManager;
public class ViewCardOptional extends CardView {
2019-10-12 13:40:13 +00:00
private boolean cards;
private boolean compact;
2021-11-07 15:34:08 +00:00
private int padding;
2019-10-12 13:40:13 +00:00
private int margin;
private int ident;
private Integer color = null;
2019-10-12 13:40:13 +00:00
2019-08-14 15:21:37 +00:00
public ViewCardOptional(@NonNull Context context) {
super(context);
2019-10-12 13:40:13 +00:00
init(context);
2019-08-14 15:21:37 +00:00
}
public ViewCardOptional(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
2019-10-12 13:40:13 +00:00
init(context);
2019-08-14 15:21:37 +00:00
}
public ViewCardOptional(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
2019-10-12 13:40:13 +00:00
init(context);
2019-08-14 15:21:37 +00:00
}
2019-10-12 13:40:13 +00:00
private void init(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
cards = prefs.getBoolean("cards", true);
compact = prefs.getBoolean("compact", false);
2021-11-07 15:34:08 +00:00
padding = prefs.getInt("view_padding", compact ? 0 : 1);
2019-08-14 15:21:37 +00:00
2021-11-07 15:34:08 +00:00
margin = Helper.dp2pixels(context, (padding + 1) * 3);
2019-08-14 15:21:37 +00:00
2019-10-12 13:40:13 +00:00
setRadius(cards ? margin : 0);
setCardElevation(0);
setCardBackgroundColor(Color.TRANSPARENT);
}
2019-10-10 20:05:29 +00:00
2019-10-12 13:40:13 +00:00
@Override
protected void onAttachedToWindow() {
if (cards) {
ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) getLayoutParams();
lparam.setMargins(margin, margin, margin, margin);
setLayoutParams(lparam);
setContentPadding(margin, margin, margin, margin);
2019-10-10 20:05:29 +00:00
}
2019-10-12 13:40:13 +00:00
super.onAttachedToWindow();
}
2019-10-10 20:05:29 +00:00
@Override
public void setCardBackgroundColor(int color) {
if (this.color == null || this.color != color) {
this.color = color;
2019-10-10 20:05:29 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean cards = prefs.getBoolean("cards", true);
if (cards && color == Color.TRANSPARENT)
color = Helper.resolveColor(getContext(), R.attr.colorCardBackground);
super.setCardBackgroundColor(color);
}
}
2019-08-14 15:21:37 +00:00
}