From db39d68efa4303d3e51b76b7b9cfdf9843f63fd3 Mon Sep 17 00:00:00 2001 From: Floens Date: Sat, 16 Jan 2016 19:54:06 +0100 Subject: [PATCH] Add option to select with network type videos should load on Still depends on the image network type, so the other options are greyed out where appropiate. For example when you select the image network type to "wifi only", you then can't select the video network type to "all". The video mode will be set to a lower type automatically when the image mode is changed to something lower. --- .../core/presenter/ImageViewerPresenter.java | 29 +++++++------ .../chan/core/settings/ChanSettings.java | 15 +++---- .../ui/controller/MainSettingsController.java | 39 ++++++++++++++--- .../chan/ui/settings/ListSettingView.java | 42 +++++++++++++------ .../org/floens/chan/ui/view/FloatingMenu.java | 23 ++++++---- .../floens/chan/ui/view/FloatingMenuItem.java | 25 +++++++++-- 6 files changed, 119 insertions(+), 54 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/core/presenter/ImageViewerPresenter.java b/Clover/app/src/main/java/org/floens/chan/core/presenter/ImageViewerPresenter.java index e03ed749..c4824d34 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/presenter/ImageViewerPresenter.java +++ b/Clover/app/src/main/java/org/floens/chan/core/presenter/ImageViewerPresenter.java @@ -264,25 +264,24 @@ public class ImageViewerPresenter implements MultiImageView.Callback, ViewPager. private boolean imageAutoLoad(PostImage postImage) { // Auto load the image when it is cached - if (Chan.getFileCache().exists(postImage.imageUrl)) { - return true; - } else { - String autoLoadMode = ChanSettings.imageAutoLoadNetwork.get(); - if (autoLoadMode.equals(ChanSettings.ImageAutoLoadMode.NONE.name)) { - return false; - } else if (autoLoadMode.equals(ChanSettings.ImageAutoLoadMode.WIFI.name)) { - return isConnected(ConnectivityManager.TYPE_WIFI); - } else if (autoLoadMode.equals(ChanSettings.ImageAutoLoadMode.ALL.name)) { - return true; - } + return Chan.getFileCache().exists(postImage.imageUrl) || shouldLoadForNetworkType(ChanSettings.imageAutoLoadNetwork.get()); + } + + private boolean videoAutoLoad(PostImage postImage) { + return imageAutoLoad(postImage) && shouldLoadForNetworkType(ChanSettings.videoAutoLoadNetwork.get()); + } - // Not connected or unrecognized + private boolean shouldLoadForNetworkType(String networkType) { + if (networkType.equals(ChanSettings.MediaAutoLoadMode.NONE.name)) { return false; + } else if (networkType.equals(ChanSettings.MediaAutoLoadMode.WIFI.name)) { + return isConnected(ConnectivityManager.TYPE_WIFI); + } else if (networkType.equals(ChanSettings.MediaAutoLoadMode.ALL.name)) { + return true; } - } - private boolean videoAutoLoad(PostImage postImage) { - return imageAutoLoad(postImage) && ChanSettings.videoAutoLoad.get(); + // Not connected or unrecognized + return false; } private void setTitle(PostImage postImage, int position) { diff --git a/Clover/app/src/main/java/org/floens/chan/core/settings/ChanSettings.java b/Clover/app/src/main/java/org/floens/chan/core/settings/ChanSettings.java index 7559896e..391ea272 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/settings/ChanSettings.java +++ b/Clover/app/src/main/java/org/floens/chan/core/settings/ChanSettings.java @@ -32,7 +32,7 @@ import java.net.InetSocketAddress; import java.net.Proxy; public class ChanSettings { - public enum ImageAutoLoadMode { + public enum MediaAutoLoadMode { // ALways auto load, either wifi or mobile ALL("all"), // Only auto load if on wifi @@ -42,12 +42,12 @@ public class ChanSettings { public String name; - ImageAutoLoadMode(String name) { + MediaAutoLoadMode(String name) { this.name = name; } - public static ImageAutoLoadMode find(String name) { - for (ImageAutoLoadMode mode : ImageAutoLoadMode.values()) { + public static MediaAutoLoadMode find(String name) { + for (MediaAutoLoadMode mode : MediaAutoLoadMode.values()) { if (mode.name.equals(name)) { return mode; } @@ -65,7 +65,7 @@ public class ChanSettings { public static final BooleanSetting autoRefreshThread; // public static final BooleanSetting imageAutoLoad; public static final StringSetting imageAutoLoadNetwork; - public static final BooleanSetting videoAutoLoad; + public static final StringSetting videoAutoLoadNetwork; public static final BooleanSetting videoOpenExternal; public static final BooleanSetting videoErrorIgnore; public static final StringSetting boardViewMode; @@ -132,8 +132,8 @@ public class ChanSettings { openLinkConfirmation = new BooleanSetting(p, "preference_open_link_confirmation", true); autoRefreshThread = new BooleanSetting(p, "preference_auto_refresh_thread", true); // imageAutoLoad = new BooleanSetting(p, "preference_image_auto_load", true); - imageAutoLoadNetwork = new StringSetting(p, "preference_image_auto_load_network", ImageAutoLoadMode.WIFI.name); - videoAutoLoad = new BooleanSetting(p, "preference_autoplay", false); + imageAutoLoadNetwork = new StringSetting(p, "preference_image_auto_load_network", MediaAutoLoadMode.WIFI.name); + videoAutoLoadNetwork = new StringSetting(p, "preference_video_auto_load_network", MediaAutoLoadMode.WIFI.name); videoOpenExternal = new BooleanSetting(p, "preference_video_external", false); videoErrorIgnore = new BooleanSetting(p, "preference_video_error_ignore", false); boardViewMode = new StringSetting(p, "preference_board_view_mode", PostCellInterface.PostViewMode.LIST.name); // "list" or "grid" @@ -223,6 +223,7 @@ public class ChanSettings { // preference_board_view_mode default "list" // preference_board_editor_filler default false // preference_pass_enabled default false + // preference_autoplay false } public static boolean passLoggedIn() { diff --git a/Clover/app/src/main/java/org/floens/chan/ui/controller/MainSettingsController.java b/Clover/app/src/main/java/org/floens/chan/ui/controller/MainSettingsController.java index 82b229ac..e9224eb7 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/controller/MainSettingsController.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/controller/MainSettingsController.java @@ -56,8 +56,8 @@ import static org.floens.chan.utils.AndroidUtils.getString; public class MainSettingsController extends SettingsController implements ToolbarMenuItem.ToolbarMenuItemCallback, WatchSettingsController.WatchSettingControllerListener, PassSettingsController.PassSettingControllerListener { private static final int ADVANCED_SETTINGS = 1; - private SettingView imageAutoLoadView; - private SettingView videoAutoLoadView; + private ListSettingView imageAutoLoadView; + private ListSettingView videoAutoLoadView; private LinkSettingView watchLink; private LinkSettingView passLink; @@ -132,7 +132,7 @@ public class MainSettingsController extends SettingsController implements Toolba super.onPreferenceChange(item); if (item == imageAutoLoadView) { - videoAutoLoadView.setEnabled(!ChanSettings.imageAutoLoadNetwork.get().equals(ChanSettings.ImageAutoLoadMode.NONE.name)); + updateVideoLoadModes(); } else if (item == fontView || item == fontCondensed) { EventBus.getDefault().post(new RefreshUIMessage("font")); } @@ -200,7 +200,8 @@ public class MainSettingsController extends SettingsController implements Toolba browsing.add(new BooleanSettingView(this, ChanSettings.autoRefreshThread, R.string.setting_auto_refresh_thread, 0)); List imageAutoLoadTypes = new ArrayList<>(); - for (ChanSettings.ImageAutoLoadMode mode : ChanSettings.ImageAutoLoadMode.values()) { + List videoAutoLoadTypes = new ArrayList<>(); + for (ChanSettings.MediaAutoLoadMode mode : ChanSettings.MediaAutoLoadMode.values()) { int name = 0; switch (mode) { case ALL: @@ -215,10 +216,15 @@ public class MainSettingsController extends SettingsController implements Toolba } imageAutoLoadTypes.add(new ListSettingView.Item(getString(name), mode.name)); + videoAutoLoadTypes.add(new ListSettingView.Item(getString(name), mode.name)); } - imageAutoLoadView = browsing.add(new ListSettingView(this, ChanSettings.imageAutoLoadNetwork, R.string.setting_image_auto_load, imageAutoLoadTypes.toArray(new ListSettingView.Item[imageAutoLoadTypes.size()]))); - videoAutoLoadView = browsing.add(new BooleanSettingView(this, ChanSettings.videoAutoLoad, R.string.setting_video_auto_load, R.string.setting_video_auto_load_description)); + imageAutoLoadView = new ListSettingView(this, ChanSettings.imageAutoLoadNetwork, R.string.setting_image_auto_load, imageAutoLoadTypes); + browsing.add(imageAutoLoadView); + videoAutoLoadView = new ListSettingView(this, ChanSettings.videoAutoLoadNetwork, R.string.setting_video_auto_load, videoAutoLoadTypes); + browsing.add(videoAutoLoadView); + updateVideoLoadModes(); + browsing.add(new BooleanSettingView(this, ChanSettings.videoOpenExternal, R.string.setting_video_open_external, R.string.setting_video_open_external_description)); browsing.add(new LinkSettingView(this, R.string.setting_clear_thread_hides, 0, new View.OnClickListener() { @Override @@ -335,4 +341,25 @@ public class MainSettingsController extends SettingsController implements Toolba groups.add(about); } + + private void updateVideoLoadModes() { + String currentImageLoadMode = ChanSettings.imageAutoLoadNetwork.get(); + ChanSettings.MediaAutoLoadMode[] modes = ChanSettings.MediaAutoLoadMode.values(); + boolean enabled = false; + boolean resetVideoMode = false; + for (int i = 0; i < modes.length; i++) { + if (modes[i].name.equals(currentImageLoadMode)) { + enabled = true; + if (resetVideoMode) { + ChanSettings.videoAutoLoadNetwork.set(modes[i].name); + videoAutoLoadView.updateSelection(); + onPreferenceChange(videoAutoLoadView); + } + } + videoAutoLoadView.items.get(i).enabled = enabled; + if (!enabled && ChanSettings.videoAutoLoadNetwork.get().equals(modes[i].name)) { + resetVideoMode = true; + } + } + } } diff --git a/Clover/app/src/main/java/org/floens/chan/ui/settings/ListSettingView.java b/Clover/app/src/main/java/org/floens/chan/ui/settings/ListSettingView.java index 62773c79..3d13f16a 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/settings/ListSettingView.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/settings/ListSettingView.java @@ -26,12 +26,13 @@ import org.floens.chan.ui.view.FloatingMenu; import org.floens.chan.ui.view.FloatingMenuItem; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.floens.chan.utils.AndroidUtils.dp; public class ListSettingView extends SettingView implements FloatingMenu.FloatingMenuCallback, View.OnClickListener { - public final Item[] items; + public final List items; private Setting setting; @@ -46,28 +47,36 @@ public class ListSettingView extends SettingView implements FloatingMenu.Floatin this.setting = setting; - items = new Item[itemNames.length]; + items = new ArrayList<>(itemNames.length); for (int i = 0; i < itemNames.length; i++) { - items[i] = new Item(itemNames[i], keys[i]); + items.add(i, new Item(itemNames[i], keys[i])); } - selectItem(); + updateSelection(); } public ListSettingView(SettingsController settingsController, Setting setting, int name, Item[] items) { this(settingsController, setting, getString(name), items); } + public ListSettingView(SettingsController settingsController, Setting setting, int name, List items) { + this(settingsController, setting, getString(name), items); + } + public ListSettingView(SettingsController settingsController, Setting setting, String name, Item[] items) { + this(settingsController, setting, name, Arrays.asList(items)); + } + + public ListSettingView(SettingsController settingsController, Setting setting, String name, List items) { super(settingsController, name); this.setting = setting; this.items = items; - selectItem(); + updateSelection(); } public String getBottomDescription() { - return items[selected].name; + return items.get(selected).name; } public Setting getSetting() { @@ -92,10 +101,9 @@ public class ListSettingView extends SettingView implements FloatingMenu.Floatin @Override public void onClick(View v) { - List menuItems = new ArrayList<>(2); - + List menuItems = new ArrayList<>(items.size()); for (Item item : items) { - menuItems.add(new FloatingMenuItem(item.key, item.name)); + menuItems.add(new FloatingMenuItem(item.key, item.name, item.enabled)); } FloatingMenu menu = new FloatingMenu(v.getContext()); @@ -110,7 +118,7 @@ public class ListSettingView extends SettingView implements FloatingMenu.Floatin public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) { String selectedKey = (String) item.getId(); setting.set(selectedKey); - selectItem(); + updateSelection(); settingsController.onPreferenceChange(this); } @@ -118,10 +126,10 @@ public class ListSettingView extends SettingView implements FloatingMenu.Floatin public void onFloatingMenuDismissed(FloatingMenu menu) { } - private void selectItem() { + public void updateSelection() { String selectedKey = setting.get(); - for (int i = 0; i < items.length; i++) { - if (items[i].key.equals(selectedKey)) { + for (int i = 0; i < items.size(); i++) { + if (items.get(i).key.equals(selectedKey)) { selected = i; break; } @@ -131,10 +139,18 @@ public class ListSettingView extends SettingView implements FloatingMenu.Floatin public static class Item { public final String name; public final String key; + public boolean enabled; public Item(String name, String key) { this.name = name; this.key = key; + enabled = true; + } + + public Item(String name, String key, boolean enabled) { + this.name = name; + this.key = key; + this.enabled = enabled; } } } diff --git a/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenu.java b/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenu.java index 71bb92e4..b859f7e5 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenu.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenu.java @@ -34,10 +34,10 @@ import org.floens.chan.R; import org.floens.chan.utils.AndroidUtils; import org.floens.chan.utils.Logger; -import java.util.ArrayList; import java.util.List; import static org.floens.chan.utils.AndroidUtils.dp; +import static org.floens.chan.utils.AndroidUtils.getAttrColor; public class FloatingMenu { public static final int POPUP_WIDTH_AUTO = -1; @@ -118,10 +118,8 @@ public class FloatingMenu { popupWindow.setContentWidth(popupWidth); } - List stringItems = new ArrayList<>(items.size()); int selectedPosition = 0; for (int i = 0; i < items.size(); i++) { - stringItems.add(items.get(i).getText()); if (items.get(i) == selectedItem) { selectedPosition = i; } @@ -130,15 +128,18 @@ public class FloatingMenu { if (adapter != null) { popupWindow.setAdapter(adapter); } else { - popupWindow.setAdapter(new FloatingMenuArrayAdapter(context, R.layout.toolbar_menu_item, stringItems)); + popupWindow.setAdapter(new FloatingMenuArrayAdapter(context, R.layout.toolbar_menu_item, items)); } popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if (position >= 0 && position < items.size()) { - callback.onFloatingMenuItemClicked(FloatingMenu.this, items.get(position)); - popupWindow.dismiss(); + FloatingMenuItem item = items.get(position); + if (item.isEnabled()) { + callback.onFloatingMenuItemClicked(FloatingMenu.this, item); + popupWindow.dismiss(); + } } else { callback.onFloatingMenuItemClicked(FloatingMenu.this, null); } @@ -193,8 +194,8 @@ public class FloatingMenu { void onFloatingMenuDismissed(FloatingMenu menu); } - private static class FloatingMenuArrayAdapter extends ArrayAdapter { - public FloatingMenuArrayAdapter(Context context, int resource, List objects) { + private static class FloatingMenuArrayAdapter extends ArrayAdapter { + public FloatingMenuArrayAdapter(Context context, int resource, List objects) { super(context, resource, objects); } @@ -204,8 +205,12 @@ public class FloatingMenu { convertView = LayoutInflater.from(getContext()).inflate(R.layout.toolbar_menu_item, parent, false); } + FloatingMenuItem item = getItem(position); + TextView textView = (TextView) convertView; - textView.setText(getItem(position)); + textView.setText(item.getText()); + textView.setEnabled(item.isEnabled()); + textView.setTextColor(getAttrColor(getContext(), item.isEnabled() ? R.attr.text_color_primary : R.attr.text_color_hint)); textView.setTypeface(AndroidUtils.ROBOTO_MEDIUM); return textView; diff --git a/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenuItem.java b/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenuItem.java index d8f28122..56570578 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenuItem.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/view/FloatingMenuItem.java @@ -22,15 +22,24 @@ import static org.floens.chan.utils.AndroidUtils.getString; public class FloatingMenuItem { private Object id; private String text; + private boolean enabled = true; + + public FloatingMenuItem(Object id, int text) { + this(id, getString(text)); + } + + public FloatingMenuItem(Object id, int text, boolean enabled) { + this(id, getString(text), enabled); + } public FloatingMenuItem(Object id, String text) { - this.id = id; - this.text = text; + this(id, text, true); } - public FloatingMenuItem(Object id, int text) { + public FloatingMenuItem(Object id, String text, boolean enabled) { this.id = id; - this.text = getString(text); + this.text = text; + this.enabled = enabled; } public Object getId() { @@ -48,4 +57,12 @@ public class FloatingMenuItem { public void setText(String text) { this.text = text; } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } }