com.pyamsoft.dontsuckmp.uicore.NowPlayingDelegate.java Source code

Java tutorial

Introduction

Here is the source code for com.pyamsoft.dontsuckmp.uicore.NowPlayingDelegate.java

Source

/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pyamsoft.dontsuckmp.uicore;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.pyamsoft.dontsuckmp.R;
import com.pyamsoft.dontsuckmp.playback.NowPlayingFragment;

public class NowPlayingDelegate implements MediaMenuItemDelegate {

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.now_playing_menu, menu);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Toolbar toolbar) {
        toolbar.inflateMenu(R.menu.now_playing_menu);
    }

    @CheckResult
    public boolean onOptionsItemSelected(@NonNull FragmentActivity activity, @NonNull MenuItem item) {
        final int id = item.getItemId();
        final boolean handled;
        switch (id) {
        case R.id.menu_now_playing:
            showPlaybackFragment(activity);
            handled = true;
            break;
        default:
            handled = false;
        }

        return handled;
    }

    private void showPlaybackFragment(@NonNull FragmentActivity activity) {
        final FragmentManager fragmentManager = activity.getSupportFragmentManager();
        if (fragmentManager.findFragmentByTag(NowPlayingFragment.TAG) == null) {
            fragmentManager.beginTransaction()
                    .replace(R.id.main_view_container, new NowPlayingFragment(), NowPlayingFragment.TAG)
                    .addToBackStack(null).commit();
        }
    }
}