File indexing completed on 2024-12-22 04:41:41
0001 /* 0002 * SPDX-FileCopyrightText: 2018 Erik Duisters <e.duisters1@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.SftpPlugin; 0008 0009 import android.content.Context; 0010 import android.provider.DocumentsContract; 0011 import android.util.AttributeSet; 0012 import android.view.View; 0013 import android.widget.CheckBox; 0014 0015 import androidx.annotation.NonNull; 0016 import androidx.annotation.Nullable; 0017 import androidx.preference.DialogPreference; 0018 import androidx.preference.PreferenceViewHolder; 0019 0020 import org.kde.kdeconnect_tp.R; 0021 0022 public class StoragePreference extends DialogPreference { 0023 @Nullable 0024 private SftpPlugin.StorageInfo storageInfo; 0025 @Nullable 0026 private OnLongClickListener onLongClickListener; 0027 0028 CheckBox checkbox; 0029 public boolean inSelectionMode; 0030 0031 public void setInSelectionMode(boolean inSelectionMode) { 0032 if (this.inSelectionMode != inSelectionMode) { 0033 this.inSelectionMode = inSelectionMode; 0034 notifyChanged(); 0035 } 0036 } 0037 0038 public StoragePreference(Context context, AttributeSet attrs) { 0039 super(context, attrs); 0040 0041 setDialogLayoutResource(R.layout.fragment_storage_preference_dialog); 0042 setWidgetLayoutResource(R.layout.preference_checkbox); 0043 setPersistent(false); 0044 inSelectionMode = false; 0045 } 0046 0047 public StoragePreference(Context context) { 0048 this(context, null); 0049 } 0050 0051 public void setOnLongClickListener(@Nullable OnLongClickListener listener) { 0052 this.onLongClickListener = listener; 0053 } 0054 0055 public void setStorageInfo(@NonNull SftpPlugin.StorageInfo storageInfo) { 0056 if (this.storageInfo != null && this.storageInfo.equals(storageInfo)) { 0057 return; 0058 } 0059 0060 if (callChangeListener(storageInfo)) { 0061 setStorageInfoInternal(storageInfo); 0062 } 0063 } 0064 0065 private void setStorageInfoInternal(@NonNull SftpPlugin.StorageInfo storageInfo) { 0066 this.storageInfo = storageInfo; 0067 0068 setTitle(storageInfo.displayName); 0069 setSummary(DocumentsContract.getTreeDocumentId(storageInfo.uri)); 0070 } 0071 0072 @Nullable 0073 public SftpPlugin.StorageInfo getStorageInfo() { 0074 return storageInfo; 0075 } 0076 0077 @Override 0078 public void setDefaultValue(Object defaultValue) { 0079 if (defaultValue == null || defaultValue instanceof SftpPlugin.StorageInfo) { 0080 super.setDefaultValue(defaultValue); 0081 } else { 0082 throw new RuntimeException("StoragePreference defaultValue must be null or an instance of StfpPlugin.StorageInfo"); 0083 } 0084 } 0085 0086 @Override 0087 protected void onSetInitialValue(@Nullable Object defaultValue) { 0088 if (defaultValue != null) { 0089 setStorageInfoInternal((SftpPlugin.StorageInfo) defaultValue); 0090 } 0091 } 0092 0093 @Override 0094 public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { 0095 super.onBindViewHolder(holder); 0096 0097 checkbox = (CheckBox) holder.itemView.findViewById(R.id.checkbox); 0098 0099 checkbox.setVisibility(inSelectionMode ? View.VISIBLE : View.INVISIBLE); 0100 0101 holder.itemView.setOnLongClickListener(v -> { 0102 if (onLongClickListener != null) { 0103 onLongClickListener.onLongClick(StoragePreference.this); 0104 return true; 0105 } 0106 return false; 0107 }); 0108 } 0109 0110 @Override 0111 protected void onClick() { 0112 if (inSelectionMode) { 0113 checkbox.setChecked(!checkbox.isChecked()); 0114 } else { 0115 super.onClick(); 0116 } 0117 } 0118 0119 public interface OnLongClickListener { 0120 void onLongClick(StoragePreference storagePreference); 0121 } 0122 }