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

Java tutorial

Introduction

Here is the source code for com.cnm.cnmrc.fragment.vodtvch.TvchSemi.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 android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
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 android.widget.Toast;

import com.cnm.cnmrc.MainActivity;
import com.cnm.cnmrc.R;
import com.cnm.cnmrc.adapter.TvchSemiAdapter;
import com.cnm.cnmrc.item.ItemTvchSemi;
import com.cnm.cnmrc.item.ItemTvchSemiList;
import com.cnm.cnmrc.parser.TvchSemiParser;
import com.cnm.cnmrc.slidingmenu.SlidingMenu;
import com.cnm.cnmrc.util.UiUtil;
import com.cnm.cnmrc.util.UrlAddress;
import com.cnm.cnmrc.util.Util;

public class TvchSemi extends Base implements View.OnClickListener {

    /**
     * tvch (1st arg : 2nd arg) : (0:) / (1:?) / (2:TV) / (3:)
     */
    public TvchSemi newInstance(int selectedCategory, String title, boolean isFirstDepth, Bundle bundle) {
        TvchSemi f = new TvchSemi();
        Bundle args = new Bundle();
        args.putInt("selectedCategory", selectedCategory);
        args.putString("title", title);
        args.putBoolean("isFirstDepth", isFirstDepth);
        args.putBundle("bundle", bundle);
        f.setArguments(args);
        return f;
    }

    FrameLayout preventClickDispatching; //  ?  ?? ?? .

    ListView listView;
    TvchSemiAdapter adapter;
    ArrayList<ItemTvchSemi> mResult = null;

    int selectedCategory; // where to declare? Base or here / vodtvch?? vod? ?? . (0:) / (1:?) / (2:TV)
    String title;
    String genreCode = ""; // vodtvch?? ""? ?, TvchList? genreCode ?.

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

        preventClickDispatching = (FrameLayout) layout.findViewById(R.id.prevent_click_dispatching);
        preventClickDispatching.setOnClickListener(this);

        selectedCategory = getArguments().getInt("selectedCategory");
        title = getArguments().getString("title");
        isFirstDepth = getArguments().getBoolean("isFirstDepth");
        Bundle bundle = getArguments().getBundle("bundle");
        genreCode = bundle.getString("genreId");

        // listview
        listView = (ListView) layout.findViewById(R.id.listview);
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // sidebar  return.
                // 2013-12-06 comment later
                // 
                //if (UiUtil.isSlidingMenuOpening(getActivity())) return;

                // 2013-12-10
                // ?  .
                if (UiUtil.isSlidingMenuOpening(getActivity()))
                    UiUtil.toggleSidebar(getActivity());

                increaseCurrentDepth();

                // Base :: mClassTypeArray = {"VodList", "VodSemi", "VodDetail", "TvchList", "TvchSemi", "TvchDetail"};
                String dateIndex = "1"; // ??  ? .
                Bundle bundle = UiUtil.makeTvchDetailBundle(getActivity(), adapter, view, position, dateIndex);
                String title = "ch" + adapter.getItem(position).getNumber() + ". "
                        + adapter.getItem(position).getName();
                loadingData(5, title, false, bundle); // TvchDetail(5:?), false : 1 depth .
            }

        });

        setTitle(title);
        showTvchSemi();

        return layout;
    }

    private void showTvchSemi() {
        // check network and data loading
        if (Util.GetNetworkInfo(getActivity()) == 99) {
            Util.AlertShow(getActivity());
        } else {
            new TvchSemiAsyncTask().execute();
        }
    }

    long elapsedTime;
    ItemTvchSemiList list;

    private class TvchSemiAsyncTask extends AsyncTask<Void, Void, ArrayList<ItemTvchSemi>> {
        @Override
        protected void onPreExecute() {
            ((MainActivity) getActivity()).getMyProgressBar().show();
            super.onPreExecute();
        }

        protected ArrayList<ItemTvchSemi> doInBackground(Void... params) {
            String requestURL = determineUrl();
            TvchSemiParser parser = new TvchSemiParser(requestURL);

            elapsedTime = System.currentTimeMillis();
            parser.start();
            Log.i("hwang", "elapsedTime of vod semi : " + (System.currentTimeMillis() - elapsedTime));

            list = parser.getList();
            return parser.getList().getList();
        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(ArrayList<ItemTvchSemi> result) {
            mResult = result;

            if (mResult.size() == 0) {
                Toast.makeText(getActivity(), "?? !", Toast.LENGTH_LONG).show();
                ((MainActivity) getActivity()).getMyProgressBar().dismiss();
                return;
            }

            if (mResult == null)
                onCancelled();
            else {
                Log.i("hwang", "search vod result count : " + mResult.size());

                if (!list.getResultCode().equals("100")) {
                    String desc = Util.getErrorCodeDesc(list.getResultCode());
                    Toast.makeText(getActivity(), desc, Toast.LENGTH_LONG).show();
                } else {
                    adapter = new TvchSemiAdapter(getActivity(), R.layout.list_item_tvch_semi, mResult);
                    listView.setAdapter(adapter);
                }

                ((MainActivity) getActivity()).getMyProgressBar().dismiss();
            }
        }

        @Override
        protected void onCancelled() {
            Util.onCancelled(getActivity());

            super.onCancelled();
        }
    }

    private String determineUrl() {
        String url = "";
        String areaCode = Util.getChannelAreaCode(getActivity());
        String productCode = Util.getChannelProductCode(getActivity());
        String mode = ""; // default ?.

        if (selectedCategory == 0)
            mode = UrlAddress.Channel.Mode.ALL; // ? (from VodTvchMain)
        if (selectedCategory == 2)
            mode = UrlAddress.Channel.Mode.HD; // HD? (from VodTvchMain)
        if (selectedCategory == 3)
            mode = UrlAddress.Channel.Mode.PAY; // ? (from VodTvchMain)

        if (selectedCategory == 4)
            mode = UrlAddress.Channel.Mode.ALL; // genreCode?  ? ??.  (from TvchList)

        url = UrlAddress.Channel.getGetChannelList(areaCode, productCode, genreCode, mode);

        return url;
    }

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

        // ----------------------------------------------------------------------------------------------
        // vod? ?? vodtopmenu fragment vod destory?  ??? vodtopmenu? ? destory .
        // ?  ???? ? fragment ? vod? ?  ? ? Activity?   .
        // ----------------------------------------------------------------------------------------------
    }

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

    }

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

    }

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

    }

}