2 * Copyright (C) 2012 Andrew Neal
3 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8 * or agreed to in writing, software distributed under the License is
9 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10 * KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
14 package com.cyanogenmod.eleven.ui.activities;
16 import android.graphics.Color;
17 import android.os.Bundle;
18 import android.support.v4.app.Fragment;
19 import android.support.v4.view.ViewPager;
20 import android.view.View;
21 import android.widget.LinearLayout;
23 import com.cyanogenmod.eleven.R;
24 import com.cyanogenmod.eleven.slidinguppanel.SlidingUpPanelLayout;
25 import com.cyanogenmod.eleven.slidinguppanel.SlidingUpPanelLayout.SimplePanelSlideListener;
26 import com.cyanogenmod.eleven.ui.HeaderBar;
27 import com.cyanogenmod.eleven.ui.fragments.AudioPlayerFragment;
28 import com.cyanogenmod.eleven.ui.fragments.QueueFragment;
29 import com.cyanogenmod.eleven.utils.ApolloUtils;
30 import com.cyanogenmod.eleven.utils.MusicUtils;
31 import com.cyanogenmod.eleven.widgets.BlurScrimImage;
34 * This class is used to display the {@link ViewPager} used to swipe between the
35 * main {@link Fragment}s used to browse the user's music.
37 * @author Andrew Neal (andrewdneal@gmail.com)
39 public abstract class SlidingPanelActivity extends BaseActivity {
48 private static final String STATE_KEY_CURRENT_PANEL = "CurrentPanel";
50 private SlidingUpPanelLayout mFirstPanel;
51 private SlidingUpPanelLayout mSecondPanel;
52 protected Panel mTargetNavigatePanel;
54 private final ShowPanelClickListener mShowBrowse = new ShowPanelClickListener(Panel.Browse);
55 private final ShowPanelClickListener mShowMusicPlayer = new ShowPanelClickListener(Panel.MusicPlayer);
57 // this is the blurred image that goes behind the now playing and queue fragments
58 private BlurScrimImage mBlurScrimImage;
61 * Opens the now playing screen
63 private final View.OnClickListener mOpenNowPlaying = new View.OnClickListener() {
69 public void onClick(final View v) {
70 if (MusicUtils.getCurrentAudioId() != -1) {
73 MusicUtils.shuffleAll(SlidingPanelActivity.this);
79 protected void initBottomActionBar() {
80 super.initBottomActionBar();
82 final LinearLayout bottomActionBar = (LinearLayout)findViewById(R.id.bottom_action_bar);
83 // Display the now playing screen or shuffle if this isn't anything
85 bottomActionBar.setOnClickListener(mOpenNowPlaying);
92 protected void onCreate(final Bundle savedInstanceState) {
93 super.onCreate(savedInstanceState);
95 mTargetNavigatePanel = Panel.None;
100 // get the blur scrim image
101 mBlurScrimImage = (BlurScrimImage)findViewById(R.id.blurScrimImage);
103 if (savedInstanceState != null) {
104 int panelIndex = savedInstanceState.getInt(STATE_KEY_CURRENT_PANEL,
105 Panel.Browse.ordinal());
106 Panel targetPanel = Panel.values()[panelIndex];
108 showPanel(targetPanel);
109 mTargetNavigatePanel = Panel.None;
111 if (targetPanel == Panel.Queue) {
112 mFirstPanel.setSlidingEnabled(false);
118 protected void onSaveInstanceState(Bundle outState) {
119 super.onSaveInstanceState(outState);
121 outState.putInt(STATE_KEY_CURRENT_PANEL, getCurrentPanel().ordinal());
124 private void setupFirstPanel() {
125 mFirstPanel = (SlidingUpPanelLayout)findViewById(R.id.sliding_layout);
126 mFirstPanel.setPanelSlideListener(new SimplePanelSlideListener() {
128 public void onPanelSlide(View panel, float slideOffset) {
129 onSlide(slideOffset);
133 public void onPanelExpanded(View panel) {
134 checkTargetNavigation();
138 public void onPanelCollapsed(View panel) {
139 checkTargetNavigation();
144 private void setupSecondPanel() {
145 mSecondPanel = (SlidingUpPanelLayout)findViewById(R.id.sliding_layout2);
146 mSecondPanel.setPanelSlideListener(new SimplePanelSlideListener() {
148 public void onPanelSlide(View panel, float slideOffset) {
149 // if we are not going to a specific panel, then disable sliding to prevent
150 // the two sliding panels from fighting for touch input
151 if (mTargetNavigatePanel == Panel.None) {
152 mFirstPanel.setSlidingEnabled(false);
155 onSlide(slideOffset);
159 public void onPanelExpanded(View panel) {
160 checkTargetNavigation();
164 public void onPanelCollapsed(View panel) {
165 // re-enable sliding when the second panel is collapsed
166 mFirstPanel.setSlidingEnabled(true);
167 checkTargetNavigation();
171 // setup the header bar
172 setupHeaderBar(R.id.secondHeaderBar, R.string.page_play_queue, mShowMusicPlayer);
174 // set the drag view offset to allow the panel to go past the top of the viewport
175 // since the previous view's is hiding the slide offset, we need to subtract that
176 // from action bat height
177 int slideOffset = getResources().getDimensionPixelOffset(R.dimen.sliding_panel_indicator_height);
178 slideOffset -= ApolloUtils.getActionBarHeight(this);
179 mSecondPanel.setSlidePanelOffset(slideOffset);
183 protected void onPause() {
191 public int setContentView() {
192 return R.layout.activity_base;
196 public void onBackPressed() {
197 Panel panel = getCurrentPanel();
200 super.onBackPressed();
204 showPanel(Panel.Browse);
207 showPanel(Panel.MusicPlayer);
212 public void openAudioPlayer() {
213 showPanel(Panel.MusicPlayer);
216 public void showPanel(Panel panel) {
217 // if we are already at our target panel, then don't do anything
218 if (panel == getCurrentPanel()) {
222 // TODO: Add ability to do this instantaneously as opposed to animate
225 // if we are two panels over, we need special logic to jump twice
226 mTargetNavigatePanel = panel;
227 mSecondPanel.collapsePanel();
228 // re-enable sliding on first panel so we can collapse it
229 mFirstPanel.setSlidingEnabled(true);
230 mFirstPanel.collapsePanel();
233 mSecondPanel.collapsePanel();
234 mFirstPanel.expandPanel();
237 // if we are two panels over, we need special logic to jump twice
238 mTargetNavigatePanel = panel;
239 mSecondPanel.expandPanel();
240 mFirstPanel.expandPanel();
245 protected void onSlide(float slideOffset) {
249 * This checks if we are at our target panel and resets our flag if we are there
251 protected void checkTargetNavigation() {
252 if (mTargetNavigatePanel == getCurrentPanel()) {
253 mTargetNavigatePanel = Panel.None;
256 getAudioPlayerFragment().setVisualizerVisible(getCurrentPanel() == Panel.MusicPlayer);
259 public Panel getCurrentPanel() {
260 if (mSecondPanel.isPanelExpanded()) {
262 } else if (mFirstPanel.isPanelExpanded()) {
263 return Panel.MusicPlayer;
269 public void clearMetaInfo() {
270 super.clearMetaInfo();
271 mBlurScrimImage.transitionToDefaultState();
275 public void onMetaChanged() {
276 super.onMetaChanged();
278 // load the blurred image
279 mBlurScrimImage.loadBlurImage(ApolloUtils.getImageFetcher(this));
283 public void onCacheUnpaused() {
284 super.onCacheUnpaused();
286 // load the blurred image
287 mBlurScrimImage.loadBlurImage(ApolloUtils.getImageFetcher(this));
290 protected AudioPlayerFragment getAudioPlayerFragment() {
291 return (AudioPlayerFragment)getSupportFragmentManager().findFragmentById(R.id.audioPlayerFragment);
294 protected QueueFragment getQueueFragment() {
295 return (QueueFragment)getSupportFragmentManager().findFragmentById(R.id.queueFragment);
298 protected HeaderBar setupHeaderBar(final int containerId, final int textId,
299 final View.OnClickListener headerClickListener) {
300 final HeaderBar headerBar = (HeaderBar) findViewById(containerId);
301 headerBar.setFragment(getQueueFragment());
302 headerBar.setTitleText(textId);
303 headerBar.setBackgroundColor(Color.TRANSPARENT);
304 headerBar.setBackListener(mShowBrowse);
305 headerBar.setHeaderClickListener(headerClickListener);
310 private class ShowPanelClickListener implements View.OnClickListener {
312 private Panel mTargetPanel;
314 public ShowPanelClickListener(Panel targetPanel) {
315 mTargetPanel = targetPanel;
319 public void onClick(View v) {
320 showPanel(mTargetPanel);