Android Open Source - easympsd Main Activity






From Project

Back to project page easympsd.

License

The source code is released under:

GNU General Public License

If you think the Android project easympsd 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

/*
Copyright 2014-2015 Francesco Palumbo <franzodev@gmail.com>
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./*from   w  w  w.  j  av a2s. c o  m*/

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.franz.easympsd;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


  //private static final String TAG = MainActivity.class.getSimpleName();
  
  private TextView tinfo;
  private ListView flist;
  private SeekBar   sbar;
  private Button   plbtn;
  private TextView tdur;
  
  // Index to use for array of File (mp3)
  private int mindex;
  private MusicReader mreader;
  
  private ListAdapter adapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.activity_main);
    
    tinfo = (TextView) findViewById(R.id.track_info);
    flist = (ListView) findViewById(R.id.file_list);
    
    tdur  = (TextView) findViewById(R.id.duration);
    sbar  = (SeekBar)  findViewById(R.id.seek_bar);
    plbtn = (Button)   findViewById(R.id.play);
    
    mreader = MusicReader.getInstance();
    mreader.setSeekBar(sbar);
    mreader.setPlayTimeTextView(tdur);
  }

  @Override
  public void onStop() {
    super.onStop();
    mreader.pause();
  }
  
  @Override
  public void onRestart() {
    super.onRestart();
    mreader.resume();
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    mreader.destroy();
    
    mreader = null;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  public Mp3File[] scanMusic() {
    return Mp3File.retrieveMp3Files(
          Environment.getExternalStorageDirectory() + 
          "/Music/");
  }
  
  @SuppressLint("InlinedApi")
  @Override
  public void onStart() {
    super.onStart();
    
    tinfo.setSelected(true);
    
    Mp3File[] mp3_arr = scanMusic();
    
    mreader.setPlaylist(new Playlist(mp3_arr));
    
    if (mp3_arr == null)
      throw new NullPointerException("Mp3File[] is null");

    if (mp3_arr.length == 0) {
      tinfo.setText("No mp3 files found");
      return;
    }

    adapter = new MusicListAdapter(this,
              R.layout.music_list_layout,
              mp3_arr);
    
    flist.setAdapter(adapter);
    mreader.setMusicList(flist);
    
    flist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, final View view, int pos, long id) {
        final Mp3File item = (Mp3File) parent.getItemAtPosition(pos);
        
        tinfo.setText(item.getName());
        
        mindex = pos;
      }
    });
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  
  public void controlsClick(View sender) {
    int btid = ((Button) sender).getId();
    int tidx = 0;
    
    int maxidx = adapter.getCount() - 1;
    
    if (btid == R.id.prev) {
      mreader.stop();
      tidx = (mindex > 0)
                 ?  --mindex
                 :  (mindex=maxidx)
                 ;
        }
        else if (btid == R.id.play) {
          tidx = mindex;
    }
    else if (btid == R.id.stop) {
      mreader.stop();
      plbtn.setText(R.string.play_btn_str);
      return;
    }
    else if (btid == R.id.next) {
      mreader.stop();
      tidx = (mindex < maxidx)
                 ?  ++mindex
                 :  (mindex=0)
                 ;
    }
    else {
      throw new RuntimeException("Wrong Sender Object");
    }
    
    flist.performItemClick(flist.getChildAt(tidx),
                 tidx, 
                 adapter.getItemId(tidx));
    
        plbtn.setText(mreader.play(tidx) 
                     ? R.string.pause_btn_str
                     : R.string.play_btn_str)
                     ;
  }
}




Java Source Code List

com.franz.easympsd.MainActivity.java
com.franz.easympsd.Mp3File.java
com.franz.easympsd.MusicListAdapter.java
com.franz.easympsd.MusicReader.java
com.franz.easympsd.Playlist.java