2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Make plugin settings use material design

This commit is contained in:
Daniel Tang 2020-04-29 23:04:00 -04:00
parent a75fe69554
commit 31271ec4f9
2 changed files with 81 additions and 57 deletions

View File

@ -18,39 +18,20 @@
type in the "widget_frame" layout. --> type in the "widget_frame" layout. -->
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical" >
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingEnd="?android:attr/scrollbarSize"
android:paddingLeft="12dip"
android:paddingRight="?android:attr/scrollbarSize"
android:paddingStart="12dip">
<!-- Preference will place its actual preference widget here. -->
<LinearLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left|center_vertical|start"
android:minWidth="56dp"
android:orientation="vertical">
</LinearLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/content"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="6dip" android:layout_weight="1"
android:layout_marginEnd="4dip" android:layout_gravity="center"
android:layout_marginLeft="4dip" android:padding="16dp"
android:layout_marginRight="4dip" android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_marginStart="4dip" >
android:layout_marginTop="6dip"
android:layout_weight="1">
<TextView <TextView
android:id="@android:id/title" android:id="@android:id/title"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -58,8 +39,9 @@
android:ellipsize="marquee" android:ellipsize="marquee"
android:fadingEdge="horizontal" android:fadingEdge="horizontal"
android:singleLine="true" android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Plugin title"
/>
<TextView <TextView
android:id="@android:id/summary" android:id="@android:id/summary"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -69,18 +51,26 @@
android:layout_below="@android:id/title" android:layout_below="@android:id/title"
android:maxLines="3" android:maxLines="3"
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary" /> android:textColor="?android:attr/textColorSecondary"
tools:text="Plugin description"
/>
</RelativeLayout> </RelativeLayout>
<View
<androidx.appcompat.widget.AppCompatImageButton android:id="@+id/divider"
android:id="@+id/settingsButton" android:layout_width="1dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:background="?android:attr/textColorSecondary"
/>
<!-- Preference will place its actual preference widget here. -->
<LinearLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:background="@drawable/abc_btn_borderless_material" android:layout_gravity="center"
android:contentDescription="@string/settings_icon_description" android:gravity="center"
android:padding="8dip" android:padding="16dp"
app:tint="?attr/colorControlNormal" android:orientation="vertical"
android:src="@drawable/ic_settings_white_32dp" /> tools:background="@color/primary"
/>
</LinearLayout> </LinearLayout>

View File

@ -1,6 +1,7 @@
package org.kde.kdeconnect.UserInterface; package org.kde.kdeconnect.UserInterface;
import android.content.Context; import android.content.Context;
import android.util.TypedValue;
import android.view.View; import android.view.View;
import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Device;
@ -9,10 +10,10 @@ import org.kde.kdeconnect.Plugins.PluginFactory;
import org.kde.kdeconnect_tp.R; import org.kde.kdeconnect_tp.R;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.preference.CheckBoxPreference;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;
public class PluginPreference extends CheckBoxPreference { public class PluginPreference extends SwitchPreference {
private final Device device; private final Device device;
private final String pluginKey; private final String pluginKey;
private final View.OnClickListener listener; private final View.OnClickListener listener;
@ -51,22 +52,55 @@ setIcon(android.R.color.transparent);
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
final View button = holder.findViewById(R.id.settingsButton); View.OnClickListener toggleListener = v -> {
if (listener == null) {
button.setVisibility(View.GONE);
} else {
button.setEnabled(isChecked());
button.setVisibility(View.VISIBLE);
button.setOnClickListener(listener);
}
holder.itemView.setOnClickListener(v -> {
boolean newState = !device.isPluginEnabled(pluginKey); boolean newState = !device.isPluginEnabled(pluginKey);
setChecked(newState); //It actually works on API<14 setChecked(newState); //It actually works on API<14
button.setEnabled(newState); onStateChanged(holder, newState);
device.setPluginEnabled(pluginKey, newState); device.setPluginEnabled(pluginKey, newState);
}); };
View content = holder.findViewById(R.id.content);
View widget = holder.findViewById(android.R.id.widget_frame);
View parent = holder.itemView;
content.setOnClickListener(listener);
widget.setOnClickListener(toggleListener);
parent.setOnClickListener(toggleListener);
// Disable child backgrounds when known to be unneeded to prevent duplicate ripples
int selectableItemBackground;
if (listener == null) {
selectableItemBackground = 0;
} else {
TypedValue value = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, value, true);
selectableItemBackground = value.resourceId;
}
content.setBackgroundResource(selectableItemBackground);
widget.setBackgroundResource(selectableItemBackground);
onStateChanged(holder, isChecked());
}
private void onStateChanged(PreferenceViewHolder holder, boolean state) {
View content = holder.findViewById(R.id.content);
View divider = holder.findViewById(R.id.divider);
View widget = holder.findViewById(android.R.id.widget_frame);
View parent = holder.itemView;
boolean hasDetails = state && listener != null;
divider.setVisibility(hasDetails ? View.VISIBLE : View.GONE);
content.setClickable(hasDetails);
widget.setClickable(hasDetails);
parent.setClickable(!hasDetails);
if (hasDetails) {
// Cancel duplicate ripple caused by pressed state of parent propagating down
content.setPressed(false);
content.getBackground().jumpToCurrentState();
widget.setPressed(false);
widget.getBackground().jumpToCurrentState();
}
} }
interface PluginPreferenceCallback { interface PluginPreferenceCallback {