View on GitHub


2 hrs
Test Coverage
package com.yashketkar.ykplayer;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;

 * A fragment representing a list of Items.
 * <p/>
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
public class VideosFragment extends Fragment implements AbsListView.OnItemClickListener {

    private OnFragmentInteractionListener mListener;

     * The fragment's ListView/GridView.
    private AbsListView mListView;

     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
    private ListAdapter mAdapter;

    public static VideosFragment newInstance() {
        return new VideosFragment();

     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).

    public VideosFragment() {

    public void onCreate(Bundle savedInstanceState) {


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View videosFragmentView = inflater.inflate(R.layout.fragment_videos, container, false);
        mListView = videosFragmentView.findViewById(android.R.id.list);
        requestPermissions(getActivity(), getActivity().findViewById(R.id.fragment_container));
        // Set the adapter
        // Set OnItemClickListener so we can be notified on item clicks
        return videosFragmentView;

    public void onAttach(Context context) {
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");

    public void onDetach() {
        mListener = null;

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            Cursor cursor = ((VideosAdapter) mAdapter).getCursor();

     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.

    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();

        if (emptyView instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);

    private void requestPermissions(final Activity thisActivity, View videosFragmentView) {
        if (ContextCompat.checkSelfPermission(thisActivity,
                != PackageManager.PERMISSION_GRANTED) {
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                MainActivity.permissionsSnackbar = Snackbar.make(videosFragmentView, "Storage access permission is required to scan and play media files on this device.", Snackbar.LENGTH_INDEFINITE)
                        .setAction("OK", new View.OnClickListener() {
                            public void onClick(View view) {
                                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},

            } else {
                // No explanation needed, we can request the permission.
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                // app-defined int constant. The callback method gets the
                // result of the request.
        } else {
            Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            String[] projection = {MediaStore.Video.VideoColumns.DISPLAY_NAME, MediaStore.Video.VideoColumns.DATA, MediaStore.Video.VideoColumns._ID};
            Cursor c = getActivity().getContentResolver().query(uri, projection, null, null, null);
            mAdapter = new VideosAdapter(getActivity(), c, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
    public interface OnFragmentInteractionListener {
        public void onVideosFragmentInteraction(String id);

        public void onSectionAttached(int number);