first iteration, disable predicate for KeyChoiceAdapter
This commit is contained in:
+1
-1
@@ -89,7 +89,7 @@ public class AppSettingsAllowedKeysListFragment extends RecyclerFragment<KeyChoi
|
|||||||
|
|
||||||
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
if (keyChoiceAdapter == null) {
|
if (keyChoiceAdapter == null) {
|
||||||
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data);
|
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data, null);
|
||||||
setAdapter(keyChoiceAdapter);
|
setAdapter(keyChoiceAdapter);
|
||||||
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
Set<Long> checkedIds = apiAppDao.getAllowedKeyIdsForApp(packageName);
|
||||||
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
keyChoiceAdapter.setSelectionByIds(checkedIds);
|
||||||
|
|||||||
+3
-1
@@ -123,7 +123,9 @@ public class SelectPublicKeyFragment extends RecyclerFragment<KeyChoiceAdapter>
|
|||||||
|
|
||||||
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
public void onLoadUnifiedKeyData(List<UnifiedKeyInfo> data) {
|
||||||
if (keyChoiceAdapter == null) {
|
if (keyChoiceAdapter == null) {
|
||||||
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data);
|
keyChoiceAdapter = KeyChoiceAdapter.createMultiChoiceAdapter(data, (keyInfo -> {
|
||||||
|
return keyInfo.is_revoked() ? R.string.keychoice_cannot_encrypt : null;
|
||||||
|
}));
|
||||||
setAdapter(keyChoiceAdapter);
|
setAdapter(keyChoiceAdapter);
|
||||||
keyChoiceAdapter.setSelectionByIds(selectedMasterKeyIds);
|
keyChoiceAdapter.setSelectionByIds(selectedMasterKeyIds);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+43
-14
@@ -9,12 +9,14 @@ import java.util.Set;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.annotation.StringRes;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.CheckBox;
|
import android.widget.CheckBox;
|
||||||
import android.widget.RadioButton;
|
import android.widget.RadioButton;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
import eu.davidea.flexibleadapter.FlexibleAdapter;
|
||||||
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
|
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
|
||||||
@@ -26,43 +28,57 @@ import org.sufficientlysecure.keychain.ui.adapter.KeyChoiceAdapter.KeyChoiceItem
|
|||||||
|
|
||||||
|
|
||||||
public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
||||||
|
@Nullable
|
||||||
private final OnKeyClickListener onKeyClickListener;
|
private final OnKeyClickListener onKeyClickListener;
|
||||||
|
@Nullable
|
||||||
|
private final KeyDisabledPredicate keyDisabledPredicate;
|
||||||
|
@Nullable
|
||||||
private Integer activeItem;
|
private Integer activeItem;
|
||||||
|
|
||||||
public static KeyChoiceAdapter createSingleClickableAdapter(List<UnifiedKeyInfo> items,
|
public static KeyChoiceAdapter createSingleClickableAdapter(List<UnifiedKeyInfo> items,
|
||||||
OnKeyClickListener onKeyClickListener) {
|
OnKeyClickListener onKeyClickListener) {
|
||||||
return new KeyChoiceAdapter(items, Objects.requireNonNull(onKeyClickListener), Mode.IDLE);
|
return new KeyChoiceAdapter(items, Objects.requireNonNull(onKeyClickListener), Mode.IDLE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KeyChoiceAdapter createSingleChoiceAdapter(List<UnifiedKeyInfo> items) {
|
public static KeyChoiceAdapter createSingleChoiceAdapter(List<UnifiedKeyInfo> items) {
|
||||||
return new KeyChoiceAdapter(items, null, Mode.SINGLE);
|
return new KeyChoiceAdapter(items, null, Mode.SINGLE, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KeyChoiceAdapter createMultiChoiceAdapter(List<UnifiedKeyInfo> items) {
|
public static KeyChoiceAdapter createMultiChoiceAdapter(List<UnifiedKeyInfo> items, KeyDisabledPredicate keyDisabledPredicate) {
|
||||||
return new KeyChoiceAdapter(items, null, Mode.MULTI);
|
return new KeyChoiceAdapter(items, null, Mode.MULTI, keyDisabledPredicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private KeyChoiceAdapter(List<UnifiedKeyInfo> items, OnKeyClickListener onKeyClickListener, int idle) {
|
private KeyChoiceAdapter(List<UnifiedKeyInfo> items, @Nullable OnKeyClickListener onKeyClickListener, int idle,
|
||||||
super(getKeyChoiceItems(items));
|
@Nullable KeyDisabledPredicate keyDisabledPredicate) {
|
||||||
|
super(getKeyChoiceItems(items, keyDisabledPredicate));
|
||||||
setMode(idle);
|
setMode(idle);
|
||||||
addListener((OnItemClickListener) (view, position) -> onClickItem(position));
|
addListener((OnItemClickListener) (view, position) -> onClickItem(position));
|
||||||
this.onKeyClickListener = onKeyClickListener;
|
this.onKeyClickListener = onKeyClickListener;
|
||||||
|
this.keyDisabledPredicate = keyDisabledPredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static ArrayList<KeyChoiceItem> getKeyChoiceItems(@Nullable List<UnifiedKeyInfo> items) {
|
private static ArrayList<KeyChoiceItem> getKeyChoiceItems(@Nullable List<UnifiedKeyInfo> items,
|
||||||
|
@Nullable KeyDisabledPredicate keyDisabledPredicate) {
|
||||||
if (items == null) {
|
if (items == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ArrayList<KeyChoiceItem> choiceItems = new ArrayList<>();
|
ArrayList<KeyChoiceItem> choiceItems = new ArrayList<>();
|
||||||
for (UnifiedKeyInfo keyInfo : items) {
|
for (UnifiedKeyInfo keyInfo : items) {
|
||||||
KeyChoiceItem keyChoiceItem = new KeyChoiceItem(keyInfo);
|
Integer disabledString = keyDisabledPredicate != null ? keyDisabledPredicate.getDisabledString(keyInfo) : null;
|
||||||
|
KeyChoiceItem keyChoiceItem = new KeyChoiceItem(keyInfo, disabledString);
|
||||||
choiceItems.add(keyChoiceItem);
|
choiceItems.add(keyChoiceItem);
|
||||||
}
|
}
|
||||||
return choiceItems;
|
return choiceItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean onClickItem(int position) {
|
private boolean onClickItem(int position) {
|
||||||
|
KeyChoiceItem item = getItem(position);
|
||||||
|
if (item != null && item.disabledStringRes != null) {
|
||||||
|
Toast.makeText(getRecyclerView().getContext(), item.disabledStringRes, Toast.LENGTH_SHORT).show();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (getMode() == Mode.MULTI) {
|
if (getMode() == Mode.MULTI) {
|
||||||
toggleSelection(position);
|
toggleSelection(position);
|
||||||
notifyItemChanged(position);
|
notifyItemChanged(position);
|
||||||
@@ -72,8 +88,7 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyChoiceItem item = getItem(position);
|
Objects.requireNonNull(onKeyClickListener).onKeyClick(item.keyInfo);
|
||||||
onKeyClickListener.onKeyClick(item.keyInfo);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +124,7 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setUnifiedKeyInfoItems(List<UnifiedKeyInfo> keyInfos) {
|
public void setUnifiedKeyInfoItems(List<UnifiedKeyInfo> keyInfos) {
|
||||||
List<KeyChoiceItem> keyChoiceItems = getKeyChoiceItems(keyInfos);
|
List<KeyChoiceItem> keyChoiceItems = getKeyChoiceItems(keyInfos, keyDisabledPredicate);
|
||||||
updateDataSet(keyChoiceItems);
|
updateDataSet(keyChoiceItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,9 +166,12 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
|
|
||||||
public static class KeyChoiceItem extends AbstractFlexibleItem<KeyChoiceViewHolder> {
|
public static class KeyChoiceItem extends AbstractFlexibleItem<KeyChoiceViewHolder> {
|
||||||
private UnifiedKeyInfo keyInfo;
|
private UnifiedKeyInfo keyInfo;
|
||||||
|
@StringRes
|
||||||
|
private Integer disabledStringRes;
|
||||||
|
|
||||||
KeyChoiceItem(UnifiedKeyInfo keyInfo) {
|
KeyChoiceItem(UnifiedKeyInfo keyInfo, @StringRes Integer disabledStringRes) {
|
||||||
this.keyInfo = keyInfo;
|
this.keyInfo = keyInfo;
|
||||||
|
this.disabledStringRes = disabledStringRes;
|
||||||
setSelectable(true);
|
setSelectable(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +189,8 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, KeyChoiceViewHolder holder, int position,
|
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, KeyChoiceViewHolder holder, int position,
|
||||||
List<Object> payloads) {
|
List<Object> payloads) {
|
||||||
boolean isActive = adapter.isSelected(position);
|
boolean isActive = adapter.isSelected(position);
|
||||||
holder.bind(keyInfo, adapter.getMode(), isActive);
|
boolean isEnabled = disabledStringRes == null;
|
||||||
|
holder.bind(keyInfo, adapter.getMode(), isActive, isEnabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -206,7 +225,7 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
vRadio = itemView.findViewById(R.id.radio_keychoice);
|
vRadio = itemView.findViewById(R.id.radio_keychoice);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bind(UnifiedKeyInfo keyInfo, int choiceMode, boolean isActive) {
|
void bind(UnifiedKeyInfo keyInfo, int choiceMode, boolean isActive, boolean isEnabled) {
|
||||||
vName.setText(keyInfo.name());
|
vName.setText(keyInfo.name());
|
||||||
|
|
||||||
Context context = vCreation.getContext();
|
Context context = vCreation.getContext();
|
||||||
@@ -239,10 +258,20 @@ public class KeyChoiceAdapter extends FlexibleAdapter<KeyChoiceItem> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vCheckbox.setEnabled(isEnabled);
|
||||||
|
vRadio.setEnabled(isEnabled);
|
||||||
|
vName.setEnabled(isEnabled);
|
||||||
|
vCreation.setEnabled(isEnabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface OnKeyClickListener {
|
public interface OnKeyClickListener {
|
||||||
void onKeyClick(UnifiedKeyInfo keyInfo);
|
void onKeyClick(UnifiedKeyInfo keyInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface KeyDisabledPredicate {
|
||||||
|
@StringRes
|
||||||
|
Integer getDisabledString(UnifiedKeyInfo keyInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2033,4 +2033,6 @@
|
|||||||
<string name="snack_keysync_start">Started updating all keys…</string>
|
<string name="snack_keysync_start">Started updating all keys…</string>
|
||||||
<string name="snack_keysync_finished">Key update successful</string>
|
<string name="snack_keysync_finished">Key update successful</string>
|
||||||
<string name="snack_keysync_error">An error occurred while updating all keys</string>
|
<string name="snack_keysync_error">An error occurred while updating all keys</string>
|
||||||
|
|
||||||
|
<string name="keychoice_cannot_encrypt">This key cannot be used for encryption!</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user