Skip to content

Commit e9f757a

Browse files
committed
fix: disable write options on popup menu for a readonly video
1 parent 5733f03 commit e9f757a

File tree

11 files changed

+69
-39
lines changed

11 files changed

+69
-39
lines changed

app/src/main/java/com/liuzhenlin/videos/view/fragment/LocalSearchedVideosFragment.kt

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class LocalSearchedVideosFragment : BaseFragment(), View.OnClickListener, View.O
235235

236236
mVideoOptionsMenu = FloatingMenu(mRecyclerView)
237237
mVideoOptionsMenu!!.inflate(R.menu.floatingmenu_video_ops)
238+
mVideoOptionsMenu!!.setItemEnabled(R.id.delete, video.isWritable)
239+
mVideoOptionsMenu!!.setItemEnabled(R.id.rename, video.isWritable)
238240
mVideoOptionsMenu!!.setOnItemClickListener { menuItem, _ ->
239241
when (menuItem.iconResId) {
240242
R.drawable.ic_delete_24dp_menu -> mVideoOpCallback?.showDeleteItemDialog(video) {

app/src/main/java/com/liuzhenlin/videos/view/fragment/OnlineVideosFragment.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.liuzhenlin.common.utils.UiUtils
2222
import com.liuzhenlin.common.utils.Utils
2323
import com.liuzhenlin.common.view.SwipeRefreshLayout
2424
import com.liuzhenlin.floatingmenu.FloatingMenu
25+
import com.liuzhenlin.floatingmenu.MenuItem
2526
import com.liuzhenlin.slidingdrawerlayout.SlidingDrawerLayout
2627
import com.liuzhenlin.videos.R
2728
import com.liuzhenlin.videos.bean.TV
@@ -264,7 +265,7 @@ class OnlineVideosFragment : Fragment(), SlidingDrawerLayout.OnDrawerScrollListe
264265
getChildView(groupPosition, childPosition, false /* ignored */, view, parent)
265266

266267
val fm = FloatingMenu(view)
267-
fm.items(getString(R.string.copyURL))
268+
fm.items(Collections.singletonList(MenuItem(View.NO_ID, getString(R.string.copyURL))))
268269
fm.show(mDownX, mDownY)
269270
fm.setOnItemClickListener { _, _ ->
270271
Utils.copyPlainTextToClipboard(parent.context, child.name, child.url)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="@color/grayVideoOptionBtnColor" android:state_enabled="true" />
4+
<item android:color="@color/grayVideoOptionBtnColor_translucent" />
5+
</selector>

app/src/main/res/drawable/ic_delete_24dp_menu.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
android:viewportHeight="24">
77

88
<path
9-
android:fillColor="@color/grayVideoOptionBtnColor"
9+
android:fillColor="@color/color_selector_menu_gray_video_option_btn"
1010
android:pathData="M16,9v10H8V9h8m-1.5,-6h-5l-1,1H5v2h14V4h-3.5l-1,-1zM18,7H6v12c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7z" />
1111
</vector>

app/src/main/res/drawable/ic_edit_24dp_menu.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
android:viewportHeight="24">
77

88
<path
9-
android:fillColor="@color/grayVideoOptionBtnColor"
9+
android:fillColor="@color/color_selector_menu_gray_video_option_btn"
1010
android:pathData="M14.06,9.02l0.92,0.92L5.92,19L5,19v-0.92l9.06,-9.06M17.66,3c-0.25,0 -0.51,0.1 -0.7,0.29l-1.83,1.83 3.75,3.75 1.83,-1.83c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.2,-0.2 -0.45,-0.29 -0.71,-0.29zM14.06,6.19L3,17.25L3,21h3.75L17.81,9.94l-3.75,-3.75z" />
1111
</vector>

app/src/main/res/drawable/ic_info_24dp_menu.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
android:viewportHeight="24">
77

88
<path
9-
android:fillColor="@color/grayVideoOptionBtnColor"
9+
android:fillColor="@color/color_selector_menu_gray_video_option_btn"
1010
android:pathData="M11,7h2v2h-2zM11,11h2v6h-2zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z" />
1111
</vector>

app/src/main/res/drawable/ic_share_24dp_menu.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
android:viewportHeight="24">
77

88
<path
9-
android:fillColor="@color/grayVideoOptionBtnColor"
9+
android:fillColor="@color/color_selector_menu_gray_video_option_btn"
1010
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92c0,-1.61 -1.31,-2.92 -2.92,-2.92zM18,4c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM6,13c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1zM18,20.02c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1z" />
1111
</vector>
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4-
xmlns:app="http://schemas.android.com/apk/res-auto"
54
tools:ignore="MenuTitle">
65
<item
76
android:id="@+id/delete"
8-
app:text="@string/delete"
9-
app:icon="@drawable/ic_delete_24dp_menu" />
7+
android:text="@string/delete"
8+
android:icon="@drawable/ic_delete_24dp_menu" />
109
<item
1110
android:id="@+id/rename"
12-
app:text="@string/rename"
13-
app:icon="@drawable/ic_edit_24dp_menu" />
11+
android:text="@string/rename"
12+
android:icon="@drawable/ic_edit_24dp_menu" />
1413
<item
1514
android:id="@+id/share"
16-
app:text="@string/share"
17-
app:icon="@drawable/ic_share_24dp_menu" />
15+
android:text="@string/share"
16+
android:icon="@drawable/ic_share_24dp_menu" />
1817
<item
1918
android:id="@+id/details"
20-
app:text="@string/details"
21-
app:icon="@drawable/ic_info_24dp_menu" />
19+
android:text="@string/details"
20+
android:icon="@drawable/ic_info_24dp_menu" />
2221
</menu>

floating-menu/src/main/java/com/liuzhenlin/floatingmenu/FloatingMenu.java

+17-20
Original file line numberDiff line numberDiff line change
@@ -162,31 +162,18 @@ private void parseMenu(XmlPullParser parser, AttributeSet attrs)
162162

163163
private void readItem(AttributeSet attrs) {
164164
TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItem);
165-
final int iconResId = ta.getResourceId(R.styleable.MenuItem_icon, 0);
166-
final String text = ta.getText(R.styleable.MenuItem_text).toString();
165+
final int id = ta.getResourceId(R.styleable.MenuItem_android_id, View.NO_ID);
166+
final int iconResId = ta.getResourceId(R.styleable.MenuItem_android_icon, 0);
167+
final String text = ta.getText(R.styleable.MenuItem_android_text).toString();
168+
final boolean enabled = ta.getBoolean(R.styleable.MenuItem_android_enabled, true);
167169
ta.recycle();
168170

169-
MenuItem item = new MenuItem();
170-
item.setIconResId(iconResId);
171-
item.setText(text);
171+
MenuItem item = new MenuItem(id, text, iconResId);
172+
item.setEnabled(enabled);
172173

173174
mMenuItems.add(item);
174175
}
175176

176-
public void items(@Nullable String... items) {
177-
items(DEFAULT_ITEM_WIDTH, items);
178-
}
179-
180-
public void items(int itemWidth, @Nullable String... items) {
181-
mMenuItems.clear();
182-
if (items != null) {
183-
for (String item : items) {
184-
mMenuItems.add(new MenuItem(item));
185-
}
186-
}
187-
generateLayout(itemWidth);
188-
}
189-
190177
public <T extends MenuItem> void items(@Nullable List<T> items) {
191178
items(items, DEFAULT_ITEM_WIDTH);
192179
}
@@ -211,6 +198,8 @@ private void generateLayout(int itemWidth) {
211198
MenuItem menuItem = mMenuItems.get(i);
212199

213200
TextView textView = new TextView(mContext);
201+
textView.setId(menuItem.getId());
202+
textView.setEnabled(menuItem.isEnabled());
214203
textView.setLayoutParams(new LinearLayout.LayoutParams(
215204
itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
216205
ViewCompat.setPaddingRelative(textView, padding, padding, padding * 2, padding);
@@ -222,7 +211,7 @@ private void generateLayout(int itemWidth) {
222211
textView.setTextSize(15);
223212
//noinspection RestrictedApi
224213
textView.setTextColor(
225-
ThemeUtils.getThemeAttrColor(mContext, android.R.attr.textColorPrimary));
214+
ThemeUtils.getThemeAttrColorStateList(mContext, android.R.attr.textColorPrimary));
226215
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
227216
textView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
228217
}
@@ -260,6 +249,14 @@ private void generateLayout(int itemWidth) {
260249
setContentView(menuLayout);
261250
}
262251

252+
public boolean isItemEnabled(int id) {
253+
return getContentView().findViewById(id).isEnabled();
254+
}
255+
256+
public void setItemEnabled(int id, boolean enabled) {
257+
getContentView().findViewById(id).setEnabled(enabled);
258+
}
259+
263260
public void show(int x, int y) {
264261
mDownX = x;
265262
mDownY = y;

floating-menu/src/main/java/com/liuzhenlin/floatingmenu/MenuItem.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
package com.liuzhenlin.floatingmenu;
22

3+
import android.view.View;
4+
35
import androidx.annotation.NonNull;
46

57
public class MenuItem {
68

9+
private int id = View.NO_ID;
710
private CharSequence text;
811
private int iconResId;
12+
private boolean enabled = true;
913

1014
public MenuItem() {
1115
}
1216

13-
public MenuItem(String text) {
17+
public MenuItem(int id, String text) {
18+
this.id = id;
1419
this.text = text;
1520
}
1621

17-
public MenuItem(String text, int iconResId) {
22+
public MenuItem(int id, String text, int iconResId) {
23+
this.id = id;
1824
this.text = text;
1925
this.iconResId = iconResId;
2026
}
2127

28+
public int getId() {
29+
return id;
30+
}
31+
32+
public void setId(int id) {
33+
this.id = id;
34+
}
35+
2236
public CharSequence getText() {
2337
return text;
2438
}
@@ -35,11 +49,21 @@ public void setIconResId(int itemResId) {
3549
this.iconResId = itemResId;
3650
}
3751

52+
public boolean isEnabled() {
53+
return enabled;
54+
}
55+
56+
public void setEnabled(boolean enabled) {
57+
this.enabled = enabled;
58+
}
59+
3860
@NonNull
3961
@Override
4062
public String toString() {
4163
return "MenuItem{" +
42-
"text='" + text + '\'' +
64+
"id=" + id + ", " +
65+
"text='" + text + "', " +
66+
"enabled=" + enabled +
4367
'}';
4468
}
4569
}
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<declare-styleable name="MenuItem">
4-
<attr name="text" format="string" />
5-
<attr name="icon" format="reference" />
4+
<attr name="android:id" />
5+
<attr name="android:text" />
6+
<attr name="android:icon" />
7+
<attr name="android:enabled" />
68
</declare-styleable>
79
</resources>

0 commit comments

Comments
 (0)