org.mythtv.client.ui.frontends.MythmoteActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.mythtv.client.ui.frontends.MythmoteActivity.java

Source

/**
 * This file is part of MythTV Android Frontend
 *
 * MythTV Android Frontend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MythTV Android Frontend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MythTV Android Frontend.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This software can be found at <https://github.com/MythTV-Clients/MythTV-Android-Frontend/>
 */
package org.mythtv.client.ui.frontends;

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

import org.mythtv.R;
import org.mythtv.client.ui.navigationDrawer.FrontendsRow;
import org.mythtv.client.ui.preferences.LocationProfile;
import org.mythtv.service.util.NetworkHelper;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * @author Daniel Frey
 * @author Thomas G. Kenny Jr
 *
 */
public class MythmoteActivity extends AbstractFrontendsActivity {

    private static final String TAG = MythmoteActivity.class.getSimpleName();

    private List<Fragment> fragmentArrayList;
    private List<String> headerArrayList;
    private TextView mTextViewStatus;

    /* (non-Javadoc)
     * @see org.mythtv.client.ui.AbstractMythtvFragmentActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "onCreate : enter");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mythmote);

        mTextViewStatus = (TextView) findViewById(R.id.textView_mythmote_status);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //setup the viewpager if layout contains one
        setupViewPager();

        Log.v(TAG, "onCreate : exit");
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        Frontend fe = FrontendsRow.getSelectedFrontend();

        if (null == fe) {
            this.setStatus("Selected Frontend is undefined", Color.RED);
            return;
        }

        this.setStatus("Connecting to " + fe.getName(), Color.BLUE);
        FrontendConnectedTask frontendConnectedTask = new FrontendConnectedTask(this,
                mLocationProfileDaoHelper.findConnectedProfile(this));
        frontendConnectedTask.execute(fe.getUrl(), fe.getNameStripped());
    }

    /* (non-Javadoc)
     * @see org.mythtv.client.ui.AbstractMythtvFragmentActivity#setupActionBar()
     */
    @Override
    protected void setupActionBar() {
        super.setupActionBar();
        Log.v(TAG, "MythmoteActivity.setupActionBar : enter");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();
            actionBar.setTitle(R.string.frontends_title);
        }

        Log.v(TAG, "MythmoteActivity.setupActionBar : exit");
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "onOptionsItemSelected : enter");

        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        ;

        Log.d(TAG, "onOptionsItemSelected : exit");
        return super.onOptionsItemSelected(item);
    }

    /**
     * Setups up the viewpager and MythmotePagerAdapter if
     * the current layout contains the mythmote_pager view pager.
     */
    private void setupViewPager() {

        // get viewpager from layout
        ViewPager pager = (ViewPager) findViewById(R.id.mythmote_pager);

        // if there is a viewpager set it up
        if (null != pager) {

            //get fragment manager
            FragmentManager fm = getSupportFragmentManager();

            //create fragment and header arrays
            fragmentArrayList = new ArrayList<Fragment>();
            headerArrayList = new ArrayList<String>();

            //mythmote navigation page fragment
            Fragment nav = Fragment.instantiate(this, NavigationFragment.class.getName());
            fragmentArrayList.add(nav);
            headerArrayList.add(this.getString(R.string.mythmote_page_navigation));

            //mythmote numbers page fragment
            Fragment num = Fragment.instantiate(this, NumbersFragment.class.getName());
            fragmentArrayList.add(num);
            headerArrayList.add(this.getString(R.string.mythmote_page_numbers));

            //         //mythmote action list fragment
            //         Fragment actions = Fragment.instantiate(this, MythmoteActionListFragment.class.getName());
            //         fragmentArrayList.add(actions);
            //         headerArrayList.add(this.getString(R.string.mythmote_page_actionlist));

            //set pager adapter and initial item
            pager.setAdapter(new MythmotePagerAdapter(fm));
            pager.setCurrentItem(0);

        }
    }

    /**
     * Sets the status textview's Text
     * @param status
     * @param color
     */
    private void setStatus(String status, int color) {
        if (null != mTextViewStatus) {
            mTextViewStatus.setText(status);
            mTextViewStatus.setTextColor(color);
        }
    }

    class MythmotePagerAdapter extends FragmentStatePagerAdapter {

        public MythmotePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragmentArrayList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentArrayList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return headerArrayList.get(position);
        }

    }

    class FrontendConnectedTask extends AsyncTask<String, Void, Boolean> {

        private final String TAG = FrontendConnectedTask.class.getSimpleName();
        private final Context mContext;
        private final LocationProfile mLocationProfile;
        private String frontendName;

        public FrontendConnectedTask(Context context, LocationProfile locationProfile) {
            mContext = context;
            mLocationProfile = locationProfile;
        }

        /* (non-Javadoc)
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Boolean doInBackground(String... params) {
            Log.d(TAG, "doInBackground : enter");

            try {
                if (null == mContext)
                    throw new IllegalArgumentException("Context is required");
                if (null == mLocationProfile)
                    throw new IllegalArgumentException("LocationProfile is required");
                if (null == params || params.length != 2)
                    throw new IllegalArgumentException("Params is required");

                String url = params[0];
                frontendName = params[1];

                return NetworkHelper.getInstance().isFrontendConnected(mContext, mLocationProfile, url);
            } catch (Exception e) {
                Log.d(TAG, e.getMessage());
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if (result) {
                setStatus("Connected to " + frontendName, 0xff1d7015);
            } else {
                setStatus(frontendName + " connection refused", Color.RED);
            }

        }

    }

}