Android Open Source - ponyville-live-android Main Activity






From Project

Back to project page ponyville-live-android.

License

The source code is released under:

Apache License

If you think the Android project ponyville-live-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ponyvillelive.app.ui;
//  ww w  .  ja v  a2  s . c  om
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.MediaRouteActionProvider;
import android.support.v7.media.MediaControlIntent;
import android.support.v7.media.MediaRouteSelector;
import android.support.v7.media.MediaRouter;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;
import com.ponyvillelive.app.PvlApp;
import com.ponyvillelive.app.R;
import com.ponyvillelive.app.model.Station;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import timber.log.Timber;

public class MainActivity extends ActionBarActivity implements
        StationFragment.StationFragmentListener,
        BottomDrawerFragment.DrawerListener {

    private static final String BUNDLE_KEY_STATION = "bundle_key_station";

    @Inject
    AppContainer appContainer;

    @InjectView(R.id.pager)
    ViewPager            pager;
    @InjectView(R.id.tabs)
    PagerSlidingTabStrip tabStrip;
    @InjectView(R.id.view_slideup_panel)
    SlidingUpPanelLayout slideUpPanel;
    @InjectView(R.id.drawer_metadata)
    View drawerHandle;

    Toolbar toolbar;

    private boolean tester = false;
    private Station station;
    private BottomDrawerFragment bottomDrawer;
    private MediaRouteSelector routeSelector;
    private MediaRouter mediaRouter;
    private MediaRouter.Callback mediaRouterCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PvlApp app = PvlApp.get(this);
        app.inject(this);

        long start = System.nanoTime();

        ViewGroup container = appContainer.get(this, app);
        View view = getLayoutInflater().inflate(R.layout.activity_main, container);

        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ButterKnife.inject(this, view);

        long diff = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
        Timber.d("Took %sms to inject activity", diff);

        bottomDrawer = (BottomDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.bottom_drawer);
        pager.setAdapter(new FragmentTabAdapter(getSupportFragmentManager()));
        tabStrip.setViewPager(pager);
        slideUpPanel.setDragView(drawerHandle);
        slideUpPanel.hidePanel();
        slideUpPanel.setPanelSlideListener(new ActionbarHideSlidePanelListener(this));

        routeSelector = new MediaRouteSelector.Builder()
                .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
                .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
                .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
                .build();
        mediaRouter = MediaRouter.getInstance(this);
        mediaRouterCallback = new MediaRouter.Callback() {
        };

        if(savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_KEY_STATION)) {
            station = savedInstanceState.getParcelable(BUNDLE_KEY_STATION);
            handleStationSelected(station);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem mediaRouteMenuItem = menu.findItem(R.id.action_cast);
        MediaRouteActionProvider mediaRouteActionProvider =
                (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
        mediaRouteActionProvider.setRouteSelector(routeSelector);

        // Show the menu
        return true;
    }

    @Override
    protected void onStart() {
        mediaRouter.addCallback(routeSelector, mediaRouterCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
        super.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if(station != null) {
            outState.putParcelable(BUNDLE_KEY_STATION, station);
        }
    }

    @Override
    protected void onStop() {
        mediaRouter.removeCallback(mediaRouterCallback);
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
            case R.id.action_settings:
                return true;
            case R.id.action_tester:
                tester = !tester;
                item.setChecked(tester);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void handleStationSelected(Station station) {
        this.station = station;
        bottomDrawer.showStationInfo(station);
        if(tester) {
            if(slideUpPanel.isPanelHidden()) {
                slideUpPanel.showPanel();
            }
            slideUpPanel.expandPanel();
        } else {
            if(slideUpPanel.isPanelHidden()) {
                slideUpPanel.showPanel();
                slideUpPanel.expandPanel();
            }
        }
        // TODO: Fire intent for media server
    }

    @Override
    public void handleStationCleared() {
        this.station = null;
        if(!slideUpPanel.isPanelHidden()) {
            slideUpPanel.hidePanel();
        }
    }

    public class FragmentTabAdapter extends FragmentPagerAdapter {
        private final String[] titles;
        // Removing Fragments until the actual fragment is ready
        private final Fragment[] fragments = new Fragment[]{
                StationFragment.newInstance(Station.STATION_TYPE_AUDIO),
                StationFragment.newInstance(Station.STATION_TYPE_VIDEO),
        };

        public FragmentTabAdapter(FragmentManager manager) {
            super(manager);
            titles = getResources().getStringArray(R.array.navItems);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }
    }
}




Java Source Code List

com.ponyvillelive.app.DebugPvlModule.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.Modules.java
com.ponyvillelive.app.PvlApp.java
com.ponyvillelive.app.PvlModule.java
com.ponyvillelive.app.model.ArrayResponse.java
com.ponyvillelive.app.model.DebugData.java
com.ponyvillelive.app.model.Entity.java
com.ponyvillelive.app.model.MapResponse.java
com.ponyvillelive.app.model.NowPlayingMeta.java
com.ponyvillelive.app.model.ObjectResponse.java
com.ponyvillelive.app.model.Show.java
com.ponyvillelive.app.model.SongWrapper.java
com.ponyvillelive.app.model.Song.java
com.ponyvillelive.app.model.StationMeta.java
com.ponyvillelive.app.model.Station.java
com.ponyvillelive.app.net.API.java
com.ponyvillelive.app.net.DebugNetModule.java
com.ponyvillelive.app.net.MockAPI.java
com.ponyvillelive.app.net.NetModule.java
com.ponyvillelive.app.prefs.AnimationSpeed.java
com.ponyvillelive.app.prefs.ApiEndpoint.java
com.ponyvillelive.app.prefs.ApiEndpoints.java
com.ponyvillelive.app.prefs.BooleanPreference.java
com.ponyvillelive.app.prefs.Endpoint.java
com.ponyvillelive.app.prefs.Endpoints.java
com.ponyvillelive.app.prefs.IntPreference.java
com.ponyvillelive.app.prefs.IsMockMode.java
com.ponyvillelive.app.prefs.MockDownloader.java
com.ponyvillelive.app.prefs.NetworkProxy.java
com.ponyvillelive.app.prefs.ObjectPreference.java
com.ponyvillelive.app.prefs.PicassoDebugging.java
com.ponyvillelive.app.prefs.PixelGridEnabled.java
com.ponyvillelive.app.prefs.PixelRatioEnabled.java
com.ponyvillelive.app.prefs.ScalpelEnabled.java
com.ponyvillelive.app.prefs.ScalpelWireframeEnabled.java
com.ponyvillelive.app.prefs.SeenDebugDrawer.java
com.ponyvillelive.app.prefs.StringPreference.java
com.ponyvillelive.app.ui.ActionbarHideSlidePanelListener.java
com.ponyvillelive.app.ui.ActivityHierarchyServer.java
com.ponyvillelive.app.ui.AnimationSpeedAdapter.java
com.ponyvillelive.app.ui.AppContainer.java
com.ponyvillelive.app.ui.BindableAdapter.java
com.ponyvillelive.app.ui.BottomDrawerFragment.java
com.ponyvillelive.app.ui.DebugAppContainer.java
com.ponyvillelive.app.ui.DebugUiModule.java
com.ponyvillelive.app.ui.EnumAdapter.java
com.ponyvillelive.app.ui.HierarchyTreeChangeListener.java
com.ponyvillelive.app.ui.MainActivity.java
com.ponyvillelive.app.ui.NetworkDelayAdapter.java
com.ponyvillelive.app.ui.NetworkErrorAdapter.java
com.ponyvillelive.app.ui.NetworkVarianceAdapter.java
com.ponyvillelive.app.ui.ProxyAdapter.java
com.ponyvillelive.app.ui.ServerEndpointAdapter.java
com.ponyvillelive.app.ui.SocketActivityHierarchyServer.java
com.ponyvillelive.app.ui.StationAdapter.java
com.ponyvillelive.app.ui.StationFragment.java
com.ponyvillelive.app.ui.TrackListAdapter.java
com.ponyvillelive.app.ui.UiModule.java
com.ponyvillelive.app.util.Strings.java