Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/StoragePreference.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Plugins.SftpPlugin; | ||||
22 | | ||||
23 | import android.content.Context; | ||||
24 | import android.os.Build; | ||||
25 | import android.provider.DocumentsContract; | ||||
26 | import android.util.AttributeSet; | ||||
27 | import android.view.View; | ||||
28 | import android.widget.CheckBox; | ||||
29 | | ||||
30 | import org.kde.kdeconnect_tp.R; | ||||
31 | | ||||
32 | import androidx.annotation.NonNull; | ||||
33 | import androidx.annotation.Nullable; | ||||
34 | import androidx.preference.DialogPreference; | ||||
35 | import androidx.preference.PreferenceViewHolder; | ||||
36 | import butterknife.BindView; | ||||
37 | import butterknife.ButterKnife; | ||||
38 | | ||||
39 | public class StoragePreference extends DialogPreference { | ||||
40 | @Nullable | ||||
41 | private SftpPlugin.StorageInfo storageInfo; | ||||
42 | @Nullable | ||||
43 | private OnLongClickListener onLongClickListener; | ||||
44 | | ||||
45 | @BindView(R.id.checkbox) CheckBox checkbox; | ||||
46 | | ||||
47 | public StoragePreference(Context context, AttributeSet attrs) { | ||||
48 | super(context, attrs); | ||||
49 | | ||||
50 | setDialogLayoutResource(R.layout.fragment_storage_preference_dialog); | ||||
51 | setWidgetLayoutResource(R.layout.preference_checkbox); | ||||
52 | setPersistent(false); | ||||
53 | setSelectable(false); | ||||
54 | } | ||||
55 | | ||||
56 | public StoragePreference(Context context) { | ||||
57 | this(context, null); | ||||
58 | } | ||||
59 | | ||||
60 | public void setOnLongClickListener(@Nullable OnLongClickListener listener) { | ||||
61 | this.onLongClickListener = listener; | ||||
62 | } | ||||
63 | | ||||
64 | public void setStorageInfo(@NonNull SftpPlugin.StorageInfo storageInfo) { | ||||
65 | if (this.storageInfo != null && this.storageInfo.equals(storageInfo)) { | ||||
66 | return; | ||||
67 | } | ||||
68 | | ||||
69 | if (callChangeListener(storageInfo)) { | ||||
70 | setStorageInfoInternal(storageInfo); | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | private void setStorageInfoInternal(@NonNull SftpPlugin.StorageInfo storageInfo) { | ||||
75 | this.storageInfo = storageInfo; | ||||
76 | | ||||
77 | setTitle(storageInfo.displayName); | ||||
78 | if (Build.VERSION.SDK_INT < 21) { | ||||
79 | setSummary(storageInfo.uri.getPath()); | ||||
80 | } else { | ||||
81 | setSummary(DocumentsContract.getTreeDocumentId(storageInfo.uri)); | ||||
82 | } | ||||
83 | } | ||||
84 | | ||||
85 | @Nullable | ||||
86 | public SftpPlugin.StorageInfo getStorageInfo() { | ||||
87 | return storageInfo; | ||||
88 | } | ||||
89 | | ||||
90 | @Override | ||||
91 | public void setDefaultValue(Object defaultValue) { | ||||
92 | if (defaultValue == null || defaultValue instanceof SftpPlugin.StorageInfo) { | ||||
93 | super.setDefaultValue(defaultValue); | ||||
94 | } else { | ||||
95 | throw new RuntimeException("StoragePreference defaultValue must be null or an instance of StfpPlugin.StorageInfo"); | ||||
96 | } | ||||
97 | } | ||||
98 | | ||||
99 | @Override | ||||
100 | protected void onSetInitialValue(@Nullable Object defaultValue) { | ||||
101 | if (defaultValue != null) { | ||||
102 | setStorageInfoInternal((SftpPlugin.StorageInfo) defaultValue); | ||||
103 | } | ||||
104 | } | ||||
105 | | ||||
106 | @Override | ||||
107 | public void onBindViewHolder(PreferenceViewHolder holder) { | ||||
108 | super.onBindViewHolder(holder); | ||||
109 | | ||||
110 | ButterKnife.bind(this, holder.itemView); | ||||
111 | | ||||
112 | checkbox.setVisibility(isSelectable() ? View.VISIBLE : View.INVISIBLE); | ||||
113 | | ||||
114 | holder.itemView.setOnLongClickListener(v -> { | ||||
115 | if (onLongClickListener != null) { | ||||
116 | onLongClickListener.onLongClick(StoragePreference.this); | ||||
117 | return true; | ||||
118 | } | ||||
119 | return false; | ||||
120 | }); | ||||
121 | } | ||||
122 | | ||||
123 | @Override | ||||
124 | protected void onClick() { | ||||
125 | if (isSelectable()) { | ||||
126 | checkbox.setChecked(!checkbox.isChecked()); | ||||
127 | } else { | ||||
128 | super.onClick(); | ||||
129 | } | ||||
130 | } | ||||
131 | | ||||
132 | public interface OnLongClickListener { | ||||
133 | void onLongClick(StoragePreference storagePreference); | ||||
134 | } | ||||
135 | } |