diff --git a/app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java b/app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java index d6e40e1d..b7ff330f 100644 --- a/app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java +++ b/app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java @@ -278,6 +278,7 @@ public class ApiConfig { setDefaultParse(parseBeanList.get(0)); } // 直播源 + channelGroupList.clear(); //修复从后台切换重复加载频道列表 try { String lives = infoJson.get("lives").getAsJsonArray().toString(); int index = lives.indexOf("proxy://"); @@ -354,7 +355,7 @@ public class ApiConfig { liveChannel.setChannelName(obj.get("name").getAsString().trim()); liveChannel.setChannelNum(channelIndex++); ArrayList urls = DefaultConfig.safeJsonStringList(obj, "urls"); - liveChannel.setUrls(urls); + liveChannel.setChannelUrls(urls); channelGroup.getLiveChannels().add(liveChannel); } channelGroupList.add(channelGroup); diff --git a/app/src/main/java/com/github/tvbox/osc/bean/ChannelGroup.java b/app/src/main/java/com/github/tvbox/osc/bean/ChannelGroup.java index 6ad64299..2a562aa3 100644 --- a/app/src/main/java/com/github/tvbox/osc/bean/ChannelGroup.java +++ b/app/src/main/java/com/github/tvbox/osc/bean/ChannelGroup.java @@ -12,7 +12,8 @@ public class ChannelGroup { private String groupName; private String groupPassword; private ArrayList liveChannels; - private boolean isDefault; + private boolean isSelected = false; + private boolean isFocused = false; public int getGroupNum() { @@ -39,11 +40,19 @@ public class ChannelGroup { this.liveChannels = liveChannels; } - public boolean isDefault() { - return isDefault; + public boolean isSelected() { + return isSelected; } - public void setDefault(boolean aDefault) { - isDefault = aDefault; + public void setSelected(boolean selected) { + isSelected = selected; + } + + public boolean isFocused() { + return isFocused; + } + + public void setFocused(boolean focused) { + isFocused = focused; } } diff --git a/app/src/main/java/com/github/tvbox/osc/bean/LiveChannel.java b/app/src/main/java/com/github/tvbox/osc/bean/LiveChannel.java index 949e0cb9..b9718402 100644 --- a/app/src/main/java/com/github/tvbox/osc/bean/LiveChannel.java +++ b/app/src/main/java/com/github/tvbox/osc/bean/LiveChannel.java @@ -17,9 +17,11 @@ public class LiveChannel { private int channelNum; private String channelName; - private ArrayList urls; - private boolean isDefault; - public int sourceIdx = 0; + private ArrayList channelUrls; + private boolean isSelected = false; + private boolean isFocused = false; + public int sourceIndex = 0; + public int sourceNum = 0; public void setChannelNum(int channelNum) { this.channelNum = channelNum; @@ -37,21 +39,44 @@ public class LiveChannel { return channelName; } - public String getUrls() { - if (sourceIdx <= 0 || sourceIdx >= urls.size()) - sourceIdx = 0; - return urls.get(sourceIdx); + public ArrayList getChannelUrls() { return channelUrls; } + + public void setChannelUrls(ArrayList channelUrls) { + this.channelUrls = channelUrls; + sourceNum = channelUrls.size(); + } + public void preSource() { + sourceIndex--; + if (sourceIndex < 0) sourceIndex = sourceNum - 1; + } + public void nextSource() { + sourceIndex++; + if (sourceIndex == sourceNum) sourceIndex = 0; + } + + public int getSourceIndex() { + return sourceIndex; } - public boolean isDefault() { - return isDefault; + public String getUrl() { + return channelUrls.get(sourceIndex); } - public void setDefault(boolean b) { - isDefault = b; + public boolean isSelected() { + return isSelected; + } + + public void setSelected(boolean b) { + isSelected = b; + } + + public int getSourceNum() { return sourceNum; } + + public boolean isFocused() { + return isFocused; } - public void setUrls(ArrayList urls) { - this.urls = urls; + public void setFocused(boolean focused) { + isFocused = focused; } } \ No newline at end of file diff --git a/app/src/main/java/com/github/tvbox/osc/ui/activity/LivePlayActivity.java b/app/src/main/java/com/github/tvbox/osc/ui/activity/LivePlayActivity.java index bc4ef86d..e402278e 100644 --- a/app/src/main/java/com/github/tvbox/osc/ui/activity/LivePlayActivity.java +++ b/app/src/main/java/com/github/tvbox/osc/ui/activity/LivePlayActivity.java @@ -64,6 +64,8 @@ public class LivePlayActivity extends BaseActivity { private List channelGroupList = new ArrayList<>(); private int selectedGroupIndex = 0; + private int focusedGroupIndex = 0; + private int focusedChannelIndex = 0; private int currentGroupIndex = 0; private int currentChannelIndex = 0; private LiveChannel currentChannel = null; @@ -79,10 +81,7 @@ public class LivePlayActivity extends BaseActivity { setLoadSir(findViewById(R.id.live_root)); mVideoView = findViewById(R.id.mVideoView); PlayerHelper.updateCfg(mVideoView); -// ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams(); -// layoutParams.width = 100; -// layoutParams.height = 50; -// mVideoView.setLayoutParams(layoutParams); + tvLeftLinearLayout = findViewById(R.id.tvLeftLinearLayout); mGroupGridView = findViewById(R.id.mGroupGridView); mChannelGridView = findViewById(R.id.mChannelGridView); @@ -117,16 +116,48 @@ public class LivePlayActivity extends BaseActivity { mHandler.postDelayed(mHideChannelListRun, 5000); } }); + + //电视 + mGroupGridView.setOnItemListener(new TvRecyclerView.OnItemListener() { + @Override + public void onItemPreSelected(TvRecyclerView parent, View itemView, int position) { + } + + @Override + public void onItemSelected(TvRecyclerView parent, View itemView, int position) { + focusChannelGroup(position); + if (position == selectedGroupIndex) return; + selectChannelGroup(position); + channelAdapter.setNewData(channelGroupList.get(position).getLiveChannels()); + if (position == currentGroupIndex) + mChannelGridView.scrollToPosition(currentChannelIndex); + else + mChannelGridView.scrollToPosition(0); + mHandler.removeCallbacks(mHideChannelListRun); + mHandler.postDelayed(mHideChannelListRun, 5000); + } + + @Override + public void onItemClick(TvRecyclerView parent, View itemView, int position) { + channelAdapter.setNewData(channelGroupList.get(position).getLiveChannels()); + mHandler.removeCallbacks(mHideChannelListRun); + mHandler.postDelayed(mHideChannelListRun, 5000); + } + }); + + //手机/模拟器 groupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { FastClickCheckUtil.check(view); - channelGroupList.get(selectedGroupIndex).setDefault(false); - groupAdapter.notifyItemChanged(selectedGroupIndex); - selectedGroupIndex = position; - channelGroupList.get(selectedGroupIndex).setDefault(true); - groupAdapter.notifyItemChanged(selectedGroupIndex); + selectChannelGroup(position); channelAdapter.setNewData(channelGroupList.get(position).getLiveChannels()); + if (position == currentGroupIndex) + mChannelGridView.scrollToPosition(currentChannelIndex); + else + mChannelGridView.scrollToPosition(0); + mHandler.removeCallbacks(mHideChannelListRun); + mHandler.postDelayed(mHideChannelListRun, 5000); } }); @@ -140,11 +171,38 @@ public class LivePlayActivity extends BaseActivity { mHandler.postDelayed(mHideChannelListRun, 5000); } }); + + //电视 + mChannelGridView.setOnItemListener(new TvRecyclerView.OnItemListener() { + @Override + public void onItemPreSelected(TvRecyclerView parent, View itemView, int position) { + + } + + @Override + public void onItemSelected(TvRecyclerView parent, View itemView, int position) { + if (position < 0) return; + focusLiveChannel(position); + mHandler.removeCallbacks(mHideChannelListRun); + mHandler.postDelayed(mHideChannelListRun, 5000); + } + + @Override + public void onItemClick(TvRecyclerView parent, View itemView, int position) { + if (selectedGroupIndex == currentGroupIndex && position == currentChannelIndex) return; + if (playChannel(position, false)) { + mHandler.post(mHideChannelListRun); + } + } + }); + + //手机/模拟器 channelAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { FastClickCheckUtil.check(view); - if (playChannel(selectedGroupIndex, position, false)) { + if (selectedGroupIndex == currentGroupIndex && position == currentChannelIndex) return; + if (playChannel(position, false)) { mHandler.post(mHideChannelListRun); } } @@ -212,7 +270,7 @@ public class LivePlayActivity extends BaseActivity { } } else if (event.getAction() == KeyEvent.ACTION_UP) { if (tvLeftLinearLayout.getVisibility() == View.VISIBLE) { - mHandler.postDelayed(mHideChannelListRun, 5000); +// mHandler.postDelayed(mHideChannelListRun, 5000); } } return super.dispatchKeyEvent(event); @@ -302,7 +360,7 @@ public class LivePlayActivity extends BaseActivity { liveChannel.setChannelName(obj.get("name").getAsString().trim()); liveChannel.setChannelNum(channelNum++); ArrayList urls = DefaultConfig.safeJsonStringList(obj, "urls"); - liveChannel.setUrls(urls); + liveChannel.setChannelUrls(urls); channelGroup.getLiveChannels().add(liveChannel); } channelGroupList.add(channelGroup); @@ -328,6 +386,8 @@ public class LivePlayActivity extends BaseActivity { selectedGroupIndex = groupIndex; currentGroupIndex = groupIndex; currentChannelIndex = channelIndex; + focusedGroupIndex = groupIndex; + focusedChannelIndex = channelIndex; break; } groupIndex++; @@ -340,7 +400,11 @@ public class LivePlayActivity extends BaseActivity { groupAdapter.setNewData(channelGroupList); channelAdapter.setNewData(channelGroupList.get(currentGroupIndex).getLiveChannels()); - playChannel(currentGroupIndex, currentChannelIndex, false); + mGroupGridView.scrollToPosition(currentGroupIndex); + mChannelGridView.scrollToPosition(currentChannelIndex); + selectChannelGroup(currentGroupIndex); + + playChannel(currentChannelIndex, false); } private void refreshTextInfo() { @@ -359,6 +423,17 @@ public class LivePlayActivity extends BaseActivity { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); + if (channelGroupList.size() > 0) { //修复未配置接口,进入直播会导致崩溃 + deselectChannelGroup(selectedGroupIndex); + if (focusedGroupIndex > -1) { + defocusChannelGroup(focusedGroupIndex); + focusedGroupIndex = -1; + } + if (focusedChannelIndex > -1) { + defocusLiveChannel(focusedChannelIndex); + focusedChannelIndex = -1; + } + } tvLeftLinearLayout.setVisibility(View.INVISIBLE); tvHint.setVisibility(View.INVISIBLE); } @@ -387,22 +462,25 @@ public class LivePlayActivity extends BaseActivity { } }; - private Runnable showListAfterScrollOk = new Runnable() { + private Runnable mFocusCurrentChannelAndShowChannelList = new Runnable() { @Override public void run() { if (mGroupGridView.isScrolling() || mChannelGridView.isScrolling()) { mHandler.postDelayed(this, 100); } else { + RecyclerView.ViewHolder holder = mChannelGridView.findViewHolderForAdapterPosition(currentChannelIndex); + if (holder != null) + holder.itemView.requestFocus(); ViewObj viewObj = new ViewObj(tvLeftLinearLayout, (ViewGroup.MarginLayoutParams) tvLeftLinearLayout.getLayoutParams()); ObjectAnimator animator = ObjectAnimator.ofObject(viewObj, "marginLeft", new IntEvaluator(), -tvLeftLinearLayout.getLayoutParams().width, 0); animator.setDuration(200); - animator.start(); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); } }); + animator.start(); mHandler.removeCallbacks(mHideChannelListRun); mHandler.postDelayed(mHideChannelListRun, 5000); } @@ -411,19 +489,18 @@ public class LivePlayActivity extends BaseActivity { private void showChannelList() { if (tvLeftLinearLayout.getVisibility() == View.INVISIBLE) { - if (selectedGroupIndex != currentGroupIndex) { - channelGroupList.get(selectedGroupIndex).setDefault(false); - groupAdapter.notifyItemChanged(selectedGroupIndex); - selectedGroupIndex = currentGroupIndex; - channelGroupList.get(selectedGroupIndex).setDefault(true); - groupAdapter.notifyItemChanged(selectedGroupIndex); - } + tvHint.setVisibility(View.VISIBLE); + tvLeftLinearLayout.setVisibility(View.VISIBLE); + + //重新载入上一次状态 channelAdapter.setNewData(channelGroupList.get(currentGroupIndex).getLiveChannels()); + mGroupGridView.scrollToPosition(currentGroupIndex); + mChannelGridView.scrollToPosition(currentChannelIndex); mGroupGridView.setSelection(currentGroupIndex); mChannelGridView.setSelection(currentChannelIndex); - tvHint.setVisibility(View.VISIBLE); - tvLeftLinearLayout.setVisibility(View.VISIBLE); - mHandler.postDelayed(showListAfterScrollOk, 100); + selectChannelGroup(currentGroupIndex); + selectLiveChannel(currentChannelIndex); + mHandler.postDelayed(mFocusCurrentChannelAndShowChannelList, 200); } } @@ -446,38 +523,15 @@ public class LivePlayActivity extends BaseActivity { mHandler.postDelayed(mHideChannelNumRun, 4000); } - private boolean playChannel(int groupIndex, int channelIndex, boolean changeSource) { - if (groupIndex < 0 || groupIndex >= channelGroupList.size() - || channelIndex < 0 || channelIndex >= channelGroupList.get(groupIndex).getLiveChannels().size()) - return false; - - if (groupIndex != selectedGroupIndex) { - channelGroupList.get(selectedGroupIndex).setDefault(false); - groupAdapter.notifyItemChanged(selectedGroupIndex); - } - if (channelIndex != currentChannelIndex) { - channelGroupList.get(currentGroupIndex).getLiveChannels().get(currentChannelIndex).setDefault(false); - channelAdapter.notifyItemChanged(currentChannelIndex); - } - + private boolean playChannel(int channelIndex, boolean changeSource) { if (!changeSource) { - LiveChannel liveChannel = channelGroupList.get(groupIndex).getLiveChannels().get(channelIndex); - if ((liveChannel == currentChannel)) - return false; - - currentGroupIndex = groupIndex; - currentChannelIndex = channelIndex; - currentChannel = liveChannel; - channelGroupList.get(currentGroupIndex).setDefault(true); - groupAdapter.notifyItemChanged(currentGroupIndex); - channelGroupList.get(currentGroupIndex).getLiveChannels().get(currentChannelIndex).setDefault(true); - channelAdapter.notifyItemChanged(currentChannelIndex); - + selectLiveChannel(channelIndex); + currentChannel = channelGroupList.get(currentGroupIndex).getLiveChannels().get(currentChannelIndex); showChannelNum(); Hawk.put(HawkConfig.LIVE_CHANNEL, currentChannel.getChannelName()); } mVideoView.release(); - mVideoView.setUrl(currentChannel.getUrls()); + mVideoView.setUrl(currentChannel.getUrl()); mVideoView.start(); return true; } @@ -489,7 +543,12 @@ public class LivePlayActivity extends BaseActivity { for (ChannelGroup channelGroup : channelGroupList) { for (LiveChannel liveChannel : channelGroup.getLiveChannels()) { if (liveChannel.getChannelNum() == channelNum) { - return playChannel(groupIndex, channelIndex, false); + if (groupIndex == currentGroupIndex && channelIndex == currentChannelIndex) + return true; + else { + selectChannelGroup(groupIndex); + return playChannel(channelIndex, false); + } } channelIndex++; } @@ -500,29 +559,90 @@ public class LivePlayActivity extends BaseActivity { } private void playNext() { - currentChannelIndex++; - if (currentChannelIndex >= channelGroupList.get(currentGroupIndex).getLiveChannels().size()) { - currentChannelIndex = 0; - } - playChannel(currentGroupIndex, currentChannelIndex, false); + int newChannelIndex = currentChannelIndex; + newChannelIndex++; + if (newChannelIndex >= channelGroupList.get(currentGroupIndex).getLiveChannels().size()) + newChannelIndex = 0; + + playChannel(newChannelIndex, false); } private void playPrevious() { - int playIndex = channelGroupList.indexOf(currentChannel); - currentChannelIndex--; - if (currentChannelIndex < 0) { - currentChannelIndex = channelGroupList.get(currentGroupIndex).getLiveChannels().size() - 1; - } - playChannel(currentGroupIndex, currentChannelIndex, false); + int newChannelIndex = currentChannelIndex; + newChannelIndex--; + if (newChannelIndex < 0) + newChannelIndex = channelGroupList.get(currentGroupIndex).getLiveChannels().size() - 1; + + playChannel(newChannelIndex, false); } public void preSourceUrl() { - currentChannel.sourceIdx--; - playChannel(currentGroupIndex, currentChannelIndex, true); + currentChannel.preSource(); + playChannel(currentChannelIndex, true); } public void nextSourceUrl() { - currentChannel.sourceIdx++; - playChannel(currentGroupIndex, currentChannelIndex, true); + currentChannel.nextSource(); + playChannel(currentChannelIndex, true); + } + + public void selectChannelGroup(int groupIndex) { + channelGroupList.get(selectedGroupIndex).setSelected(false); + groupAdapter.notifyItemChanged(selectedGroupIndex); + selectedGroupIndex = groupIndex; + channelGroupList.get(selectedGroupIndex).setSelected(true); + groupAdapter.notifyItemChanged(selectedGroupIndex); + } + + public void deselectChannelGroup(int groupIndex) { + channelGroupList.get(groupIndex).setSelected(false); + groupAdapter.notifyItemChanged(groupIndex); + } + + public void selectLiveChannel(int channelIndex) { + channelGroupList.get(currentGroupIndex).getLiveChannels().get(currentChannelIndex).setSelected(false); + channelAdapter.notifyItemChanged(currentChannelIndex); + currentChannelIndex = channelIndex; + currentGroupIndex = selectedGroupIndex; + channelGroupList.get(currentGroupIndex).getLiveChannels().get(currentChannelIndex).setSelected(true); + channelAdapter.notifyItemChanged(currentChannelIndex); + } + + public void focusChannelGroup(int groupIndex) { + if (focusedChannelIndex > -1) { + defocusLiveChannel(focusedChannelIndex); + focusedChannelIndex = -1; + } + if (focusedGroupIndex > -1) { + channelGroupList.get(focusedGroupIndex).setFocused(false); + groupAdapter.notifyItemChanged(focusedGroupIndex); + } + focusedGroupIndex = groupIndex; + channelGroupList.get(focusedGroupIndex).setFocused(true); + groupAdapter.notifyItemChanged(focusedGroupIndex); + } + + public void defocusChannelGroup(int groupIndex) { + channelGroupList.get(groupIndex).setFocused(false); + groupAdapter.notifyItemChanged(groupIndex); + } + + public void focusLiveChannel(int channelIndex) { + if (focusedGroupIndex > -1) { + defocusChannelGroup(focusedGroupIndex); + focusedGroupIndex = -1; + } + if (focusedChannelIndex > -1) { + channelGroupList.get(selectedGroupIndex).getLiveChannels().get(focusedChannelIndex).setFocused(false); + channelAdapter.notifyItemChanged(focusedChannelIndex); + } + focusedChannelIndex = channelIndex; + channelGroupList.get(selectedGroupIndex).getLiveChannels().get(focusedChannelIndex).setFocused(true); + channelAdapter.notifyItemChanged(focusedChannelIndex); + } + + public void defocusLiveChannel(int channelIndex) { + channelGroupList.get(selectedGroupIndex).getLiveChannels().get(channelIndex).setFocused(false); + channelAdapter.notifyItemChanged(channelIndex); } } \ No newline at end of file diff --git a/app/src/main/java/com/github/tvbox/osc/ui/adapter/ChannelGroupAdapter.java b/app/src/main/java/com/github/tvbox/osc/ui/adapter/ChannelGroupAdapter.java index 411d0e12..e3806ede 100644 --- a/app/src/main/java/com/github/tvbox/osc/ui/adapter/ChannelGroupAdapter.java +++ b/app/src/main/java/com/github/tvbox/osc/ui/adapter/ChannelGroupAdapter.java @@ -22,10 +22,10 @@ public class ChannelGroupAdapter extends BaseQuickAdapter @@ -38,17 +38,17 @@ android:layout_gravity="center_horizontal" /> + android:id="@+id/mChannelGridView" + android:layout_width="240mm" + android:layout_height="match_parent" + android:background="@color/color_66000000" + android:paddingLeft="5mm" + android:paddingTop="10mm" + android:paddingRight="5mm" + android:paddingBottom="10mm" + android:visibility="visible" + app:tv_selectedItemIsCentered="true" + app:tv_verticalSpacingWithMargins="10mm" />