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();
135 getAudioPlayerFragment().setVisualizerVisible(true);
139 public void onPanelCollapsed(View panel) {
140 checkTargetNavigation();
141 getAudioPlayerFragment().setVisualizerVisible(false);
146 private void setupSecondPanel() {
147 mSecondPanel = (SlidingUpPanelLayout)findViewById(R.id.sliding_layout2);
148 mSecondPanel.setPanelSlideListener(new SimplePanelSlideListener() {
150 public void onPanelSlide(View panel, float slideOffset) {
151 // if we are not going to a specific panel, then disable sliding to prevent
152 // the two sliding panels from fighting for touch input
153 if (mTargetNavigatePanel == Panel.None) {
154 mFirstPanel.setSlidingEnabled(false);
157 onSlide(slideOffset);
161 public void onPanelExpanded(View panel) {
162 checkTargetNavigation();
166 public void onPanelCollapsed(View panel) {
167 // re-enable sliding when the second panel is collapsed
168 mFirstPanel.setSlidingEnabled(true);
169 checkTargetNavigation();
173 // setup the header bar
174 setupHeaderBar(R.id.secondHeaderBar, R.string.page_play_queue, mShowMusicPlayer);
176 // set the drag view offset to allow the panel to go past the top of the viewport
177 // since the previous view's is hiding the slide offset, we need to subtract that
178 // from action bat height
179 int slideOffset = getResources().getDimensionPixelOffset(R.dimen.sliding_panel_indicator_height);
180 slideOffset -= ApolloUtils.getActionBarHeight(this);
181 mSecondPanel.setSlidePanelOffset(slideOffset);
185 protected void onPause() {
193 public int setContentView() {
194 return R.layout.activity_base;
198 public void onBackPressed() {
199 Panel panel = getCurrentPanel();
202 super.onBackPressed();
206 showPanel(Panel.Browse);
209 showPanel(Panel.MusicPlayer);
214 public void openAudioPlayer() {
215 showPanel(Panel.MusicPlayer);
218 public void showPanel(Panel panel) {
219 // if we are already at our target panel, then don't do anything
220 if (panel == getCurrentPanel()) {
224 // TODO: Add ability to do this instantaneously as opposed to animate
227 // if we are two panels over, we need special logic to jump twice
228 mTargetNavigatePanel = panel;
229 mSecondPanel.collapsePanel();
230 // re-enable sliding on first panel so we can collapse it
231 mFirstPanel.setSlidingEnabled(true);
232 mFirstPanel.collapsePanel();
235 mSecondPanel.collapsePanel();
236 mFirstPanel.expandPanel();
239 // if we are two panels over, we need special logic to jump twice
240 mTargetNavigatePanel = panel;
241 mSecondPanel.expandPanel();
242 mFirstPanel.expandPanel();
247 protected void onSlide(float slideOffset) {
251 * This checks if we are at our target panel and resets our flag if we are there
253 protected void checkTargetNavigation() {
254 if (mTargetNavigatePanel == getCurrentPanel()) {
255 mTargetNavigatePanel = Panel.None;
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);