mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Merge branch '1045-preference-divider' into 'master'
Don't use a custom widget to separate preference categories Closes #1045 See merge request !609
This commit is contained in:
@@ -11,9 +11,6 @@ import android.support.v7.preference.CheckBoxPreference;
|
||||
import android.support.v7.preference.ListPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceFragmentCompat;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.acra.ACRA;
|
||||
@@ -34,7 +31,6 @@ import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.util.UserFeedback;
|
||||
import org.briarproject.briar.android.widget.PreferenceDividerDecoration;
|
||||
import org.briarproject.briar.api.test.TestDataCreator;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
@@ -200,16 +196,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecyclerView onCreateRecyclerView(LayoutInflater inflater,
|
||||
ViewGroup parent, Bundle savedInstanceState) {
|
||||
RecyclerView list = super.onCreateRecyclerView(inflater, parent,
|
||||
savedInstanceState);
|
||||
list.addItemDecoration(
|
||||
new PreferenceDividerDecoration(getContext()).drawBottom(true));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
package org.briarproject.briar.android.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.DimenRes;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceGroup;
|
||||
import android.support.v7.preference.PreferenceGroupAdapter;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.TintTypedArray;
|
||||
import android.view.View;
|
||||
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
/**
|
||||
* Use this class to add dividers between {@link Preference} items.
|
||||
* <p/>
|
||||
* Source: https://github.com/consp1racy/android-support-preference
|
||||
* <br/>
|
||||
* License: Apache License v2.0
|
||||
*/
|
||||
public class PreferenceDividerDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private boolean mDrawTop = false;
|
||||
private boolean mDrawBottom = false;
|
||||
private boolean mDrawBetweenItems = true;
|
||||
private boolean mDrawBetweenCategories = true;
|
||||
|
||||
private Drawable mDivider;
|
||||
private int mDividerHeight;
|
||||
|
||||
public PreferenceDividerDecoration(Drawable divider, int dividerHeight) {
|
||||
mDivider = divider;
|
||||
mDividerHeight = dividerHeight;
|
||||
}
|
||||
|
||||
public PreferenceDividerDecoration(Context context,
|
||||
@DrawableRes int divider, @DimenRes int dividerHeight) {
|
||||
mDivider = ContextCompat.getDrawable(context, divider);
|
||||
mDividerHeight =
|
||||
context.getResources().getDimensionPixelSize(dividerHeight);
|
||||
}
|
||||
|
||||
public PreferenceDividerDecoration(Context context) {
|
||||
TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, null,
|
||||
new int[] {R.attr.dividerHorizontal});
|
||||
mDivider = a.getDrawable(0);
|
||||
a.recycle();
|
||||
|
||||
mDividerHeight = mDivider.getIntrinsicHeight();
|
||||
}
|
||||
|
||||
public boolean getDrawTop() {
|
||||
return mDrawTop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls whether to draw divider above the first item.
|
||||
*/
|
||||
public PreferenceDividerDecoration drawTop(boolean drawTop) {
|
||||
mDrawTop = drawTop;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getDrawBottom() {
|
||||
return mDrawBottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls whether to draw divider at the bottom of the last item.
|
||||
*/
|
||||
public PreferenceDividerDecoration drawBottom(boolean drawBottom) {
|
||||
mDrawBottom = drawBottom;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getDrawBetweenItems() {
|
||||
return mDrawBetweenItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls whether to draw divider at the bottom of each
|
||||
* {@link Preference} and {@link PreferenceScreen} item.
|
||||
*/
|
||||
public PreferenceDividerDecoration drawBetweenItems(
|
||||
boolean drawBetweenItems) {
|
||||
mDrawBetweenItems = drawBetweenItems;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean getDrawBetweenCategories() {
|
||||
return mDrawBetweenCategories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Controls whether to draw divider above each {@link PreferenceGroup}
|
||||
* usually {@link PreferenceCategory}.
|
||||
*/
|
||||
public PreferenceDividerDecoration drawBetweenCategories(
|
||||
boolean drawBetweenCategories) {
|
||||
mDrawBetweenCategories = drawBetweenCategories;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDrawOver(Canvas c, RecyclerView parent,
|
||||
RecyclerView.State state) {
|
||||
int left = parent.getPaddingLeft();
|
||||
int right = parent.getWidth() - parent.getPaddingRight();
|
||||
|
||||
final PreferenceGroupAdapter adapter =
|
||||
(PreferenceGroupAdapter) parent.getAdapter();
|
||||
final int adapterCount = adapter.getItemCount();
|
||||
|
||||
boolean wasLastPreferenceGroup = false;
|
||||
for (int i = 0, childCount = parent.getChildCount(); i < childCount;
|
||||
i++) {
|
||||
final View child = parent.getChildAt(i);
|
||||
|
||||
final int adapterPosition = parent.getChildAdapterPosition(child);
|
||||
Preference preference = adapter.getItem(adapterPosition);
|
||||
|
||||
boolean skipNextAboveDivider = false;
|
||||
if (adapterPosition == 0) {
|
||||
if (mDrawTop) {
|
||||
drawAbove(c, left, right, child);
|
||||
}
|
||||
skipNextAboveDivider = true;
|
||||
}
|
||||
|
||||
if (preference instanceof PreferenceGroup
|
||||
&& !(preference instanceof PreferenceScreen)) {
|
||||
if (mDrawBetweenCategories) {
|
||||
if (!skipNextAboveDivider) {
|
||||
drawAbove(c, left, right, child);
|
||||
skipNextAboveDivider = true;
|
||||
}
|
||||
}
|
||||
wasLastPreferenceGroup = true;
|
||||
} else {
|
||||
if (mDrawBetweenItems && !wasLastPreferenceGroup) {
|
||||
if (!skipNextAboveDivider) {
|
||||
drawAbove(c, left, right, child);
|
||||
skipNextAboveDivider = true;
|
||||
}
|
||||
}
|
||||
wasLastPreferenceGroup = false;
|
||||
}
|
||||
|
||||
if (adapterPosition == adapterCount - 1) {
|
||||
if (mDrawBottom) {
|
||||
drawBottom(c, left, right, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawAbove(Canvas c, int left, int right, View child) {
|
||||
final RecyclerView.LayoutParams params =
|
||||
(RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int top = child.getTop() - params.topMargin - mDividerHeight;
|
||||
final int bottom = top + mDividerHeight;
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
|
||||
private void drawBottom(Canvas c, int left, int right, View child) {
|
||||
final RecyclerView.LayoutParams params =
|
||||
(RecyclerView.LayoutParams) child.getLayoutParams();
|
||||
final int top =
|
||||
child.getBottom() + params.bottomMargin - mDividerHeight;
|
||||
final int bottom = top + mDividerHeight;
|
||||
mDivider.setBounds(left, top, right, bottom);
|
||||
mDivider.draw(c);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user