com.mplayer_remote.RemoteControl.java Source code

Java tutorial

Introduction

Here is the source code for com.mplayer_remote.RemoteControl.java

Source

/*
MPlayer Remote
Copyright (C) 2015  Rafa Kacki
    
This program 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.
    
This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.mplayer_remote;

import android.app.ActionBar;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;

/**
 * Aktywno wywietlajca przyciski oraz pasek przewijania pozwalajce na sterowanie odtwarzaniem pliku multimedialnego.
 * Graficzny interfejs uytkownika tej aktywnoci wywietla rwnie informacje o dugoci odtwarzanego pliku i miejscu, w ktrym obecnie znajduje si odtwarzanie.
 * Menu aktywnoci zawiera pozycje, ktrej wybranie uruchamia aktywno <code>SubtitleFileChooser</code>.
 * @author sokar
 * @see android.app.Activity
 */
public class RemoteControl extends FragmentActivity implements RemoteControlFragment.OnFragmentInteractionListener,
        PlayListFragment.OnFragmentInteractionListener {

    //w celach diagnostycznych nazwa logu dla tego Activity
    private static final String TAG = RemoteControl.class.getSimpleName();

    /**
     * ?acuch znakw zawierajcy ciek absolutn, w ktrej znajduje si plik wskazany przez uytkownika.
     */
    private String absolutePathString;

    /**
     * ?acuch znakw zawierajcy pena nazw wraz ze ciek absolutn do pliku wskazanego przez uytkownika.
     */
    private String fileToPlayString;

    private ConnectAndPlayService mConnectAndPlayService;
    private boolean mBound = false;
    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            // We've bound to ConnectAndPlayService, cast the IBinder and get ConnectAndPlayService instance
            ConnectAndPlayService.LocalBinder binder = (ConnectAndPlayService.LocalBinder) service;
            mConnectAndPlayService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    public static final int NUM_ITEMS = 2;

    private ViewPager mViewPager = null;
    private FragmentPagerAdapter myFragmentPagerAdapter = null;

    private ActionBar actionBar = null;

    /**Metoda wywoywana przez system Android przy starcie aktywnoci.
     * Wczytuje definicje GUI z pliku XML. Definiuje akcje wywoywane poprzez interakcji uytkownika z graficznym interfejsem uytkownika aktywnoci.
     * Definiuje <code>progressHandler</code>, <code>timeLengthTextViewUpdateHandler</code>, <code>timePositionUpdateHandler</code>.
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getActionBar().setDisplayHomeAsUpEnabled(false);

        Intent intentFromstartActivity = getIntent(); //getIntent() zwraca obiekt Intent ktry wystartowa Activity
        fileToPlayString = intentFromstartActivity.getStringExtra("file_to_play");
        absolutePathString = intentFromstartActivity.getStringExtra("absolute_path");

        //gui
        setContentView(R.layout.layout_for_remotecontrol);
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        Log.v(TAG, "mViewPager" + mViewPager.toString());
        Log.v(TAG, "myFragmentPagerAdapter" + myFragmentPagerAdapter.toString());
        mViewPager.setAdapter(myFragmentPagerAdapter);
        //tabs
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                mViewPager.setCurrentItem(tab.getPosition(), true);
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }
        };
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
        ActionBar.Tab nowPlayingTab = actionBar.newTab();
        nowPlayingTab.setText(R.string.title_for_now_playing_tab);
        nowPlayingTab.setTabListener(tabListener);
        actionBar.addTab(nowPlayingTab);
        ActionBar.Tab playlistTab = actionBar.newTab();
        playlistTab.setText(R.string.title_for_playlist_tab);
        playlistTab.setTabListener(tabListener);
        actionBar.addTab(playlistTab);
    }

    @Override
    public void onStart() {
        super.onStart();
        // Bind to ConnectAndPlayService
        Intent intent = new Intent(this, ConnectAndPlayService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    }

    @Override
    public void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }

    }

    ///////////////////////////THIS MAST STAY IN ACTIVITY//////////////////////////////////////
    /**
     * Metoda wywoywana w reakcji na wciniecie, ktrego z przyciskw fizycznych urzdzenia.
     * W tym wypadku definiuje akcje wykonywane, kiedy uytkownik wcinie przycisk poganiania i przyciszania.
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        //onBackPressed();
        if ((keyCode == KeyEvent.KEYCODE_BACK))
            onBackPressed();
        if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
            if (mBound == true) {
                mConnectAndPlayService.sendCommand("echo key_down_event 42 > fifofile");
            }
            return true; //because I handled the event
        }
        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
            if (mBound == true) {
                mConnectAndPlayService.sendCommand("echo key_down_event 47 > fifofile");
            }
            return true; //because I handled the event
        }
        return false; //otherwise the system can handle it        
    }

    /**
     * Sprawdza czy usuga ConnectAndPlayService dziaa w tle.
     * @return <code>true</code> jeli usuga ConnectAndPlayService dziaa w tle, <code>false</code> w przeciwnym wypadku.
     */
    private boolean isMyServiceRunning() {

        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.mplayer_remote.ConnectAndPlayService".equals(service.service.getClassName())) {
                return true;
            }
        }
        this.finish(); //to finish activity when user close mplayer by other whey
        return false;

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return RemoteControlFragment.newInstance(fileToPlayString, absolutePathString);
            } else {
                return PlayListFragment.newInstance("dupa", "dupa");
            }
        }

    }

}