Android Open Source - MediaPicker Folder List Activity Fragmented






From Project

Back to project page MediaPicker.

License

The source code is released under:

Apache License

If you think the Android project MediaPicker 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.urza.mediapicker;
/*from w w w  .ja  va2 s.  c o  m*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * An activity representing a list of Folders. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link com.urza.mediapicker.FolderDetailFragment} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 * <p/>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link FolderListFragment} and the item details
 * (if present) is a {@link FolderDetailFragment}.
 * <p/>
 * This activity also implements the required
 * {@link com.urza.mediapicker.FolderListFragment.Callbacks} interface
 * to listen for item selections.
 */
public class FolderListActivityFragmented extends FragmentActivity
        implements FolderListFragment.Callbacks, FolderDetailFragment.OnEntitySelectedListener {

    final static String TAG = "FolderListActivityFragmented";
    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;
    private HashMap<String, List<MediaEntityWrapper>> selection;
    private int MEDIA_TYPE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_folder_list_fragmented);
        Log.d(TAG, "onCreate");

        //For devel purpose
        getIntent().putExtra(MultiPicker.MEDIATYPE_CHOICE, MultiPicker.VIDEO_LOADER);

        if (findViewById(R.id.folder_detail_container_fragmented) != null) {
            Log.d(TAG, "Found fragment_detail_container_fragmented subview");
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            /*FolderListFragment folderListFragment = new FolderListFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.folder_list_container, folderListFragment).commit();*/
            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((FolderListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.folder_list))
                    .setActivateOnItemClick(true);
        }

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            Log.d(TAG, "Found fragment_container subview");
            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                selection = (HashMap) savedInstanceState.getSerializable(MultiPicker.SELECTION);
                MEDIA_TYPE = savedInstanceState.getInt(MultiPicker.MEDIATYPE_CHOICE);
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            FolderListFragment folderListFragment = new FolderListFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            folderListFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, folderListFragment).commit();
        }

        // TODO: If exposing deep links into your app, handle intents here.
        MEDIA_TYPE = getIntent().getIntExtra(MultiPicker.MEDIATYPE_CHOICE, 0);
        if (MEDIA_TYPE == 0) {
            Log.d(TAG, "Unsupported Media type chosen");
        } else {
            Log.d(TAG, "Chosen Media type: "+MEDIA_TYPE);
        }
        Bundle args = getIntent().getExtras();
        if (args != null && args.containsKey(MultiPicker.SELECTION)) {
            selection = (HashMap) args.getSerializable(MultiPicker.SELECTION);
            Log.d(TAG, "Retaining selection from bundle");
        } else {
            selection = new HashMap<String, List<MediaEntityWrapper>>();
            Log.d(TAG, "Creating new empty selection");
        }
        Log.d(TAG, "selection: " + selection.toString());
    }

    /**
     * Callback method from {@link com.urza.mediapicker.FolderListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            Log.d(TAG, "onItemSelected for TwoPane layout");
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putInt(MultiPicker.MEDIATYPE_CHOICE, MEDIA_TYPE);
            arguments.putString(FolderDetailFragment.ARG_ITEM_ID, id);
            if (!selection.containsKey(id) || selection.get(id) == null)
                selection.put(id, new ArrayList<MediaEntityWrapper>());
            arguments.putSerializable(MultiPicker.SELECTION, selection);
            FolderDetailFragment fragment = new FolderDetailFragment();
            fragment.setArguments(arguments);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.folder_detail_container_fragmented, fragment);
            //transaction.addToBackStack("folderDetail");
            transaction.commit();

        } else {
            // In single-pane mode, simply start the detail fragment
            // for the selected item ID.
            Bundle selectionInfo = new Bundle();
            selectionInfo.putInt(MultiPicker.MEDIATYPE_CHOICE, MEDIA_TYPE);
            selectionInfo.putString(FolderDetailFragment.ARG_ITEM_ID, id);
            if (!selection.containsKey(id) || selection.get(id) == null) {
                Log.d(TAG, "Created selection list for folder id: " + id);
                selection.put(id, new ArrayList<MediaEntityWrapper>());
            }
            selectionInfo.putSerializable(MultiPicker.SELECTION, selection);
            // / Create fragment and give it an argument specifying the folder it should show
            FolderDetailFragment folderDetailFragment = new FolderDetailFragment();
            folderDetailFragment.setArguments(selectionInfo);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, folderDetailFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }

    public void toggleSelection(View view) {
        // Is the toggle on?
        boolean on = ((ToggleButton) view).isChecked();

        if (on) {
            // Create fragment showing currently selected media entities
            Bundle selectionInfo = new Bundle();
            selectionInfo.putInt(MultiPicker.MEDIATYPE_CHOICE, MEDIA_TYPE);
            selectionInfo.putSerializable(MultiPicker.SELECTION, selection);
            CurrentSelectionFragment fragment = new CurrentSelectionFragment();
            fragment.setArguments(selectionInfo);
            if (mTwoPane) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.folder_detail_container_fragmented, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
            } else {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment, "selectedEntities");
                transaction.addToBackStack(null);
                transaction.commit();
            }

        } else {
            // Return back to gallery
            if (mTwoPane) {
                getSupportFragmentManager().popBackStack();
            } else {
                getSupportFragmentManager().popBackStack();
            }
        }
    }

    public void confirmSelection(View view) {
        //Handle the final selection user has chosen
        //for example finish the activity and include selection in result
        Log.d(TAG, "Selection confirmed: "+selection.toString());
        Intent result = new Intent("com.urza.mediapicker.RESULT_ACTION");
        Bundle selectedMedia = new Bundle();
        selectedMedia.putSerializable(MultiPicker.SELECTION, selection);
        result.putExtras(selectedMedia);
        setResult(Activity.RESULT_OK, result);
        finish();
    }

    public void onBackPressed(){
        final Fragment current = getSupportFragmentManager().findFragmentByTag("selectedEntities");
        if (current != null) {
            ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleSelectedEntities);
            toggleButton.toggle();
        }
        super.onBackPressed();
    }


    @Override
    public void onEntitySelected(HashMap<String, List<MediaEntityWrapper>> selection, String parentIndex) {
        this.selection = selection;
        Log.d(TAG, "selection: "+selection);
        if (mTwoPane) {
            ((FolderListFragment) getSupportFragmentManager().findFragmentById(R.id.folder_list)).updateAdapterSelection(selection, parentIndex);
        }
    }

    public int getMEDIA_TYPE() {
        return MEDIA_TYPE;
    }

    public HashMap<String, List<MediaEntityWrapper>> getSelection() {
        return selection;
    }

    public void setSelection(HashMap<String, List<MediaEntityWrapper>> selection) {
        this.selection = selection;
    }

    public void onSaveInstanceState(Bundle outstate) {
        super.onSaveInstanceState(outstate);
        Log.d(TAG, "onSaveInstanceState");
        Log.d(TAG, "selection: " + selection.toString());
        outstate.putSerializable(MultiPicker.SELECTION, selection);
        outstate.putInt(MultiPicker.MEDIATYPE_CHOICE, MEDIA_TYPE);
    }

    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
        Log.d(TAG, "selection: " + selection.toString());
    }

    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
        Log.d(TAG, "selection: " + selection.toString());
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        Log.d(TAG, "selection: " + selection.toString());
    }
}




Java Source Code List

com.urza.masterdetailtest.ApplicationTest.java
com.urza.mediapicker.CurrentSelectionFragment.java
com.urza.mediapicker.FolderBaseLoader.java
com.urza.mediapicker.FolderDetailFragment.java
com.urza.mediapicker.FolderListActivityFragmented.java
com.urza.mediapicker.FolderListFragment.java
com.urza.mediapicker.ImageBaseLoader.java
com.urza.mediapicker.MediaEntityAdapter.java
com.urza.mediapicker.MediaEntityBaseAdapter.java
com.urza.mediapicker.MediaEntityWrapper.java
com.urza.mediapicker.MediaFolderAdapter.java
com.urza.mediapicker.MediaFolder.java
com.urza.mediapicker.MultiPicker.java
com.urza.mediapicker.VideoBaseLoader.java