com.cnm.cnmrc.fragment.vodtvch.VodTvchMain.java Source code

Java tutorial

Introduction

Here is the source code for com.cnm.cnmrc.fragment.vodtvch.VodTvchMain.java

Source

/*
 * Copyright (C) 2011 Chris Gao <chris@exina.net>
 *
 * 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.cnm.cnmrc.fragment.vodtvch;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.cnm.cnmrc.MainActivity;
import com.cnm.cnmrc.R;
import com.cnm.cnmrc.adapter.VodTvchMainAdapter;
import com.cnm.cnmrc.fragment.rc.RcBottomMenu;
import com.cnm.cnmrc.slidingmenu.SlidingMenu;

public class VodTvchMain extends Fragment implements View.OnClickListener, SlidingMenu.Listener {

    /**
     * vodtvchmaind? loadingDataForSidrebar()? ?? vod, tvch? 4? ?  ?...
     *  ? ?? ??  ??   Base? loadingData()  ?.
     * isFirstDepth???
     */
    public static VodTvchMain newInstance(String type) {
        VodTvchMain f = new VodTvchMain();
        Bundle args = new Bundle();
        args.putString("type", type); // vod or tvch
        f.setArguments(args);
        return f;
    }

    SlidingMenu mSlidingMenu;

    FrameLayout mCategoryCover, mLoadingData;
    ListView mCategory;

    String[] mCategoryArray = null;
    String[] mClassTypeArray = null;

    int selectedCategory = 0; // ?  vod  (:0, ?:1, TV:2)

    public int mBackStackEntry = 100;

    public int currentDepth = 1;

    // 2013-12-10  ??  .
    boolean isSkipTitle = true;

    String type;

    boolean isSidebarItemClick = false;

    public HashMap<Integer, String> mMapTitle = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.vod_tvch, container, false);

        mSlidingMenu = (SlidingMenu) layout.findViewById(R.id.animation_layout);
        mSlidingMenu.setListener(this);

        mLoadingData = (FrameLayout) layout.findViewById(R.id.loading_data_panel);

        mCategoryCover = (FrameLayout) layout.findViewById(R.id.category_cover);
        mCategoryCover.setOnClickListener(this);
        mCategoryCover.setVisibility(View.GONE);

        mCategory = (ListView) layout.findViewById(R.id.category_list);

        //        FragmentManager fm = getActivity().getSupportFragmentManager();
        //        fm.addOnBackStackChangedListener(mAddOnBackStackChangedListener);
        //        fm.removeOnBackStackChangedListener(mRemoveOnBackStackChangedListener);

        // --------------------------
        // type check (vod or tvch)
        // --------------------------
        type = getArguments().getString("type");
        if (type.equals("vod")) {
            mCategoryArray = getActivity().getResources().getStringArray(R.array.vod_category);
            mClassTypeArray = getActivity().getResources().getStringArray(R.array.vod_class_type);
        }
        if (type.equals("tvch")) {
            mCategoryArray = getActivity().getResources().getStringArray(R.array.tvch_category);
            mClassTypeArray = getActivity().getResources().getStringArray(R.array.tvch_class_type);
        }

        ArrayList<String> arrayList = new ArrayList<String>(mCategoryArray.length);
        for (String item : mCategoryArray) {
            arrayList.add(item);
        }

        // -------------------------
        // set title : category
        // -------------------------
        // ??  , ? back key    . depth   .
        mMapTitle = new HashMap<Integer, String>();

        setTitle(selectedCategory);

        // -------------------
        // sidebar category
        // -------------------
        VodTvchMainAdapter adapter = new VodTvchMainAdapter(getActivity(), R.layout.list_item_vod_category,
                arrayList);
        mCategory.setAdapter(adapter);
        mCategory.setDivider(null);
        mCategory.setDividerHeight(0);
        mCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // sidebar  return...   ? sidebar  ?? ??...
                // 2013-12-06 comment later
                // 
                if (!mSlidingMenu.isOpening())
                    return;

                // 2013-12-10 ? ?  close ?  ???  ...
                isSidebarItemClick = true;

                // sidebar ?  item??  ...
                mCategoryCover.setVisibility(View.VISIBLE);

                // change category text color
                view.setSelected(true);
                selectedCategory = position;

                // change title
                clearCurrentDepth();
                isSkipTitle = true;
                setTitle(position);

                // close sidebar
                mSlidingMenu.toggleSidebar();

                Log.i("hwang", "category is selected");

                /*
                 * View v; int count = parent.getChildCount(); v =
                 * parent.getChildAt(position);
                 */
            }

        });

        // -----------------------
        // ? bottom menu 
        // -----------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null)
            ((RcBottomMenu) f).setVodTvchMode();

        // ----------------------------------------------------------------------------------------------------------
        // ? ? ?  fragment VodTvch ?? loadingDataForSidrebar()? addToBackStack?  .
        // ----------------------------------------------------------------------------------------------------------
        loadingDataForSidrebar();

        return layout;
    }

    /**
     * ? ? ?  fragment VodTvch ?? loadingDataForSidrebar()?
     * addToBackStack?   ?. ? MainActivity onBackPressed()? back
     * key ?. ? VodTvch fragment addToBackStack ?? ?, 
     * ?? Base back key .  back key?   ??... Base?
     * destoryView()?  ? ?. !!!!!!!   ? isFirstDepth true
     * .  4? ?(,?,TV,)?. vod (1st selectedCategory arg :
     * 2nd title arg) : (0:) / (1:?) / (2:TV) / (3:)
     */
    private void loadingDataForSidrebar() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Log.i("hwang", "before vodTvch fragment count (no add to stack) --> "
                + Integer.toString(fm.getBackStackEntryCount()));

        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        Class<?> classObject;
        try {
            String packageName = this.getClass().getPackage().getName() + ".";
            classObject = Class.forName(packageName + mClassTypeArray[selectedCategory]); // VodList, VodSemi, VodDetail, TvchList, TvchSemi, TvchDetail
            Object obj = classObject.newInstance();

            // vod    (1st arg : 2nd arg) : (0:) / (1:?) / (2:TV) / (3:)
            // tvch (1st arg : 2nd arg) : (0:?) / (1:?) / (2:HD?) / (3:?)
            // true : 1 depth (vod: VodSemi, VodSemi, VodSemi, VodList)
            // true : 1 depth (tvch: TvchSemi, TvchList, TvchSemi, TvchSemi)
            Bundle bundle = new Bundle();
            bundle.putString("genreId", ""); //   ?     ...
            Base base = ((Base) obj).newInstance(selectedCategory, mCategoryArray[selectedCategory], true, bundle); //  true depth   ?.(,?,TV,)(?,?,HD?,)

            //ft.addToBackStack(null);   // addTBackStack  onBackPressed() ? ?... remove fragment onDestroyView() ?.

            // 2013-12-10 update vodsemi after config>adultcert change
            if (mClassTypeArray[selectedCategory].equals("VodSemi")) {
                ft.replace(R.id.loading_data_panel, base, "vod_semi_for_config");
            } else {
                ft.replace(R.id.loading_data_panel, base);
            }
            ft.commit(); //fm.executePendingTransactions();   // fragment? ? pending recursive error,  ?? pending .

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Log.i("hwang", "after vodTvch fragment count (no add to stack) --> "
                + Integer.toString(fm.getBackStackEntryCount()));

        // ------------------------------------
        // ? bottom menu? depth level 
        // ------------------------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null)
            ((RcBottomMenu) f).setDepthLevelClear(); // set 1 depth
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        //      if (((MainActivity) getActivity()).noVodTvchDestroy)
        //         return;

        // ----------------------------------------------------------------------------------------------
        // vod? ?? vodtopmenu fragment vod destory?  ??? vodtopmenu? ? destory .
        // ?  ???? ? fragment ? vod? ?  ? ? Activity?   .
        // ----------------------------------------------------------------------------------------------
        {
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.fragment_vod_tvch_top_menu);
            if (f != null) {
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
            }
        }

        {
            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
            if (f != null) {
                // fragment   Base? onDestroyView callBack?.
                // ?  back stack? count ? minus? destoryView?   0??.
                // 0?  Base? onDestroyView()? .

                // ? ? onDestroyView()? ?  R.id.loading_data_panel?  fragment ? backstack  ? minus?.
                // ? ? ??. ? before after ? minus? count?.
                //  remove ? ?. rc  ?.  Base? onDestroyView() callBack .
                //  ? Base? onDestroyView()?   ??  ? remove   ? ? ? .
                // ?? remove Base? onDestroyView()? callBack ?.
                FragmentManager fm = getActivity().getSupportFragmentManager();
                Log.i("hwang", "before at vodTvch : remove(R.id.loading_data_panel).commit fragment count --> "
                        + Integer.toString(fm.getBackStackEntryCount()));
                getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
                Log.i("hwang", "after at vodTvch : remove(R.id.loading_data_panel).commit fragment count --> "
                        + Integer.toString(fm.getBackStackEntryCount()));

                //   base? remove ?? TAG_FRAGMENT_BASE  ? ?.
                //   mainActity? backpress?  base? destoryView callback?.
                // .
                // ??  ? ? ?.
                // getActivity().getSupportFragmentManager().executePendingTransactions();   
            }
        }

        // -----------------------
        // ? bottom menu 
        // -----------------------
        {
            Fragment f = ((MainActivity) getActivity()).getFragmentRcBottomMenu();
            if (f != null)
                ((RcBottomMenu) f).setRemoconMode();
        }
    }

    /*
     * back key? ?? ??.
     */
    public int allowBackPressed() {
        if (mSlidingMenu.isOpening())
            return 1; //  ? sidebar ? ?.
        else
            return 0;
    }

    private void clearSelectedAll() {
        for (int i = 0; i < mCategoryArray.length; i++) {
            mCategory.getChildAt(i).setSelected(false);
        }
    }

    public String[] getCategoryArray() {
        return mCategoryArray;
    }

    //  ?? VodTvch Base ? ? ?. (1)
    public void setTitle(int position) {
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_vod_tvch_top_menu);
        if (f != null)
            ((VodTvchTopMenu) f).setTitle(mCategoryArray[position]);

        saveTitle(currentDepth, mCategoryArray[position]);
    }

    public void setTitle(String title) {
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_vod_tvch_top_menu);
        if (f != null)
            ((VodTvchTopMenu) f).setTitle(title);

        saveTitle(currentDepth, title);
    }

    public void setTitle() {
        if (isSkipTitle) {
            String title = getTitle(--currentDepth);
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.fragment_vod_tvch_top_menu);
            if (f != null)
                ((VodTvchTopMenu) f).setTitle(title);
        } else {
            isSkipTitle = false;
        }
    }

    public void saveTitle(int key, String title) {
        mMapTitle.put(key, title);
    }

    public String getTitle(int key) {
        return mMapTitle.get(key);
    }

    public void clearCurrentDepth() {
        currentDepth = 1;
        mMapTitle.clear();
    }

    public SlidingMenu getSlidingMenu() {
        return mSlidingMenu;
    }

    @Override
    public void onSidebarOpenedStart() {
        //  ?? category clear...
        clearSelectedAll();

        // listview? getChildAt()? ?   view .
        // ?  null? .
        View selectedItem = mCategory.getChildAt(selectedCategory);
        if (selectedItem != null)
            selectedItem.setSelected(true);

        //mCategory.performItemClick(mCategory.getChildAt(selectedCategory), selectedCategory, mCategory.getAdapter().getItemId(selectedCategory));
    }

    @Override
    public void onSidebarOpenedEnd() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSidebarClosedStart() {
        if (isSidebarItemClick) {
            {
                Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
                if (f != null)
                    getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
            }

            // ------------------------------------
            // ? bottom menu? depth level 
            // onSidebarClosedEnd() > loadingDataForSidrebar()?? ? ...
            // ------------------------------------
            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
            if (f != null)
                ((RcBottomMenu) f).setDepthLevelClear(); // set 1 depth
        }
    }

    @Override
    public void onSidebarClosedEnd() {
        mCategoryCover.setVisibility(View.GONE);

        // ---------------
        // data loading
        // ---------------
        if (isSidebarItemClick)
            loadingDataForSidrebar();
        isSidebarItemClick = false;
    }

    @Override
    public boolean onContentTouchedWhenOpening() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    OnBackStackChangedListener mRemoveOnBackStackChangedListener = new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            int i = getActivity().getSupportFragmentManager().getBackStackEntryCount();
            BackStackEntry tt = getActivity().getSupportFragmentManager().getBackStackEntryAt(i - 1);
            int checkedId = tt.getBreadCrumbTitleRes();
        }
    };

    OnBackStackChangedListener mAddOnBackStackChangedListener = new OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            //int i = getActivity().getSupportFragmentManager().getBackStackEntryCount();
            //BackStackEntry tt = getSupportFragmentManager().getBackStackEntryAt(i - 1);
            //int checkedId = tt.getBreadCrumbTitleRes();
        }
    };

}