Android Open Source - streaming_project Media Fragment






From Project

Back to project page streaming_project.

License

The source code is released under:

GNU General Public License

If you think the Android project streaming_project 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.example.streaming.streaming;
// ww  w . j a  v a  2 s .  c o  m

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.NavUtils;
import android.support.v4.content.Loader;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;


// Controller that interacts with model (Media) and view objects (widgets)
// Its job is to present the details of a specific media and update those details as the user
// changes them (TODO: not yet implemented)
// NOTE 1: MediaActivity is its hosting activity
// NOTE 2: Its layout is defined in fragment_media.xml
// NOTE 3: It loads information about a particular media, such as its title, artist name, type of
// codec, bitrate, ...
// NOTE 4: It uses MediaLoader instead of talking directly with the MediaManager on the main thread.
// With this loader, the Streaming app is capable of loading all of its important data on a
// background thread
public class MediaFragment extends Fragment {
    // For debugging
    private static final String TAG = "MediaFragment";
    // NOTE: Since MediaActivity hosts MediaFragment, it too needs an extra for the media ID
    private static final String ARG_MEDIA_ID = "MEDIA_ID";
    // ID of the MediaLoader to distinguish it within LoaderManager's collection of loaders for
    // MediaFragment
    private static final int LOAD_MEDIA = 0;
    // MODEL = data
    private Media mMedia;
    // Widget instance variables
    private EditText mTitleField;
    private TextView mMediaInfoText; // TODO: choose a better name
    public MediaFragment() {
        // Required empty public constructor
    }

    // Creates the fragment instance and bundles up and sets its arguments
    // NOTE 1: Attaching arguments to a fragment must be done after the fragment is created but
    // before it is added to an activity
    // NOTE 2: when the MediaActivity needs an instance of MediaFragment, it calls newInstance()
    // rather than calling the constructor directly if the media id is included as an extra in the
    // intent send by MediaListFragment
    public static MediaFragment newInstance(long mediaId) {
        // Create fragment arguments
        // 1) Create a Bundle object
        Bundle args = new Bundle();
        // 2) Add arguments to the bundle
        args.putLong(ARG_MEDIA_ID, mediaId);
        MediaFragment fragment = new MediaFragment();
        // Attach the arguments bundle to the fragment
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle saveInstanceState) { // MORE
        super.onCreate(saveInstanceState);
        // Turn on options menu handling by telling the FragmentManager that MediaFragment will be
        // implementing options menu callbacks on behalf of the activity
        setHasOptionsMenu(true);

        // Check for a Media ID as an argument, and find the corresponding media
        Bundle args = getArguments();
        if (args != null) {
            long mediaId = args.getLong(ARG_MEDIA_ID, -1);
            if (mediaId != -1) {
                // Make use of the callbacks so that the media data is loaded on another thread
                // NOTE: We pass an instance of each LoaderCallback interface for Media
                // to a call to LoaderManager's initLoader(), see the IMPORTANT 1 notice to know
                // why we do so (MORE)
                LoaderManager lm = getLoaderManager();
                lm.initLoader(LOAD_MEDIA, args, new MediaLoaderCallbacks());
            }
            // TODO: what do we do in the case there is no ID as argument?
        }

    }

    @Override
    // Inflates the layout for the fragment's view and returns the inflated View to the hosting
    // activity
    // NOTE: this is the place to wire up widgets in the fragment's view
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {
        // Inflate the fragment's view
        // NOTE: the first param is the layout resource ID, the second param is the view's parent,
        // and the third param tells the layout inflater whether to add the inflated view to the
        // view's parent. We pass in false because we will add the view n the activity's code.
        View v = inflater.inflate(R.layout.fragment_media, parent, false);

        // Turn on Up button
        // NOTE: the setDisplayHomeAsUpEnabled() method is from API level 11, so it needs to be
        // wrapped to keep devices with API level less than 11 safe
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Enable the app icon to be an Up button and display the caret (left-pointing caret to the
            // left of the app icon) in the fragment's view
            // MORE
            if (NavUtils.getParentActivityName(getActivity()) != null) {
                getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }

        // Get a reference to the EditText
        // NOTE: we call findViewById on the fragment's view
        mTitleField = (EditText) v.findViewById(R.id.media_title);
        // Get a reference to the Details field
        mMediaInfoText = (TextView) v.findViewById(R.id.media_infoTextView);
        // Make the TextView scrollable
        mMediaInfoText.setMovementMethod(new ScrollingMovementMethod());

        updateUI();

        return v;
    }

    @Override
    // Responds to the Up button (app icon) menu item
    // NOTE 1: you don't need to define or inflate the app icon menu item in an XML file (like you
    // did with the "add a media" menu item in RunListFragment). It comes with a ready-made
    // resource ID: android.R.id.home
    // NOTE 2: see onOptionsItemSelected() from MediaListFragment for more info on this method
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Return the user to the list of media
                // Check to see if there is a parent activity named in the metadata (MORE)
                if (NavUtils.getParentActivityName(getActivity()) != null) {
                    // Navigate to the parent activity (MediaListActivity)
                    // NOTE: navigateUpFromSameTask() implements  "up" and sends the user up one
                    // level to MediaPagerActivity's parent
                    NavUtils.navigateUpFromSameTask(getActivity());
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // TODO: Add more comments in the method description
    // Displays some info about the current media
    private void updateUI() {
        // TODO: we only do the following if there is a Media saved in memory, i.e. mMedia is not null
        if (mMedia != null) {
            mTitleField.setText(mMedia.getTitle());

            // TODO: It is better to have a method in Media that outputs all the required info about the media
            // TODO: No album info received
            String info = String.format("<b>Artist</b>=%s" +
                            "<br> <b>Album</b>=%s" +
                            "<br> <b>length</b> =%s:%s " +
                            "<br> <b>Encoder</b>=%s " +
                            "<br> <b>Bitrate</b>=%s " +
                            "<br> <b>Sample Rate</b>=%s " +
                            "<br> <b>Layer</b>=%s " +
                            "<br> <b>Filename</b>=%s",
                    mMedia.getArtist(),
                    "NO ALBUM",
                    mMedia.getMins(), mMedia.getSecs(),
                    mMedia.getEncoder(),
                    mMedia.getBitrate(),
                    mMedia.getSampleRate(),
                    mMedia.getLayer(),
                    mMedia.getFilename());
            mMediaInfoText.setText(Html.fromHtml(info));
        }
    }

    // Inner class that implements LoaderCallbacks<D> for Media (MORE)
    private class MediaLoaderCallbacks implements LoaderManager.LoaderCallbacks<Media> {
        @Override
        // Returns a new MediaLoader pointing at the fragment's current activity and the media ID
        // pulled from the arguments bundle
        // NOTE: the arguments bundle will be passed along in onCreate()
        public Loader<Media> onCreateLoader(int id, Bundle args) {
            return new MediaLoader(getActivity(), args.getLong(ARG_MEDIA_ID));
        }

        @Override
        // Stashes away the loaded media in the fragment's mMedia instance variable and calls the
        // updateUI() method so that the UI will reflect the updated data
        public void onLoadFinished(Loader<Media> loader, Media media) {
            mMedia = media;
            updateUI();
        }

        @Override
        // NOTE: Not needed because the Media instance is completely in memory
        public void onLoaderReset(Loader<Media> loader) {
            // Do nothing
        }
    }
}




Java Source Code List

com.example.streaming.streaming.ApplicationTest.java
com.example.streaming.streaming.AssetsPropertyReader.java
com.example.streaming.streaming.DataFetcher.java
com.example.streaming.streaming.DataLoader.java
com.example.streaming.streaming.MainActivity.java
com.example.streaming.streaming.MediaActivity.java
com.example.streaming.streaming.MediaDatabaseHelper.java
com.example.streaming.streaming.MediaFragment.java
com.example.streaming.streaming.MediaListActivity.java
com.example.streaming.streaming.MediaListFragment.java
com.example.streaming.streaming.MediaLoader.java
com.example.streaming.streaming.MediaManager.java
com.example.streaming.streaming.MediaProvider.java
com.example.streaming.streaming.Media.java
com.example.streaming.streaming.SQLiteCursorLoader.java
com.example.streaming.streaming.SingleFragmentActivity.java