PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/documentation/tutorial/MusicFileBrowser/MusicFileBrowser_Tutorial0/src/com/google/marvin/tutorials/musicFileBrowser0/MusicFileBrowser.java

http://eyes-free.googlecode.com/
Java | 209 lines | 173 code | 21 blank | 15 comment | 36 complexity | 38715b6f12424b6843deeef62511d78c MD5 | raw file
  1package com.google.marvin.tutorials.musicFileBrowser0;
  2
  3/**
  4 * Basic Music File Browser - No speech, no gestures.
  5 * 
  6 * File browsing code is based on a tutorial from anddev.org by Nicolas Gramlich
  7 * http://www.anddev.org/building_an_android_filebrowser_list-based_ -t67.html
  8 * 
  9 * @author clchen@google.com (Charles L. Chen)
 10 */
 11
 12import java.io.File;
 13import java.util.ArrayList;
 14import java.util.Collections;
 15import java.util.List;
 16
 17import android.app.Activity;
 18import android.content.Context;
 19import android.media.MediaPlayer;
 20import android.net.Uri;
 21import android.os.Bundle;
 22import android.os.Vibrator;
 23import android.view.KeyEvent;
 24import android.view.MotionEvent;
 25import android.view.View;
 26import android.widget.AdapterView;
 27import android.widget.ArrayAdapter;
 28import android.widget.ListView;
 29import android.widget.Toast;
 30import android.widget.AdapterView.OnItemClickListener;
 31
 32public class MusicFileBrowser extends Activity {
 33  private static final long[] VIBE_PATTERN = {0, 10, 70, 80};
 34  private Vibrator vibe;
 35  private List<String> directoryEntries = new ArrayList<String>();
 36  private File currentDirectory = new File("/sdcard/");
 37  private MediaPlayer mp = new MediaPlayer();
 38  private int lastPosition;
 39  private String lastPlayedFilename = "";
 40  private ListView myList;
 41
 42  /** Called when the activity is first created. */
 43  @Override
 44  public void onCreate(Bundle icicle) {
 45    super.onCreate(icicle);
 46
 47    myList = new ListView(this);
 48    myList.setOnItemClickListener(new OnItemClickListener() {
 49      public void onItemClick(AdapterView<?> l, View v, int position, long id) {
 50        if (position == 0) {
 51          playNext();
 52        } else if (position == 1) {
 53          upOneLevel();
 54        } else {
 55          lastPosition = position;
 56          File clickedFile =
 57              new File(currentDirectory.getAbsolutePath() + directoryEntries.get(position));
 58          if (clickedFile != null) {
 59            browseTo(clickedFile);
 60          }
 61        }
 62      }
 63    });
 64    setContentView(myList);
 65
 66    lastPosition = 0;
 67    vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
 68    browseTo(new File("/sdcard/"));
 69  }
 70
 71  /**
 72   * This function browses up one level according to the field: currentDirectory
 73   * Returns whether or not navigation occurred.
 74   */
 75  private boolean upOneLevel() {
 76    String parent = this.currentDirectory.getParent();
 77    if (parent.equals("/")) {
 78      browseTo(new File("/sdcard/"));
 79      return false;
 80    } else if (this.currentDirectory.getParent() != null) {
 81      this.browseTo(this.currentDirectory.getParentFile());
 82    }
 83    return true;
 84  }
 85
 86  private void browseTo(final File aDirectory) {
 87    if (aDirectory.isDirectory()) {
 88      this.currentDirectory = aDirectory;
 89      fill(aDirectory.listFiles());
 90      lastPosition = 0;
 91      myList.setSelection(0);
 92    } else {
 93      togglePlaying(aDirectory);
 94    }
 95  }
 96
 97  private void togglePlaying(final File aFile) {
 98    if (mp.isPlaying()) {
 99      mp.stop();
100    } else {
101      lastPlayedFilename = aFile.getAbsolutePath();
102      mp = MediaPlayer.create(this, Uri.parse(lastPlayedFilename));
103      mp.start();
104    }
105  }
106
107  private void playNext() {
108    if (mp.isPlaying()) {
109      mp.stop();
110    }
111    int i = lastPosition;
112    i++;
113    String path = this.currentDirectory.getAbsolutePath();
114    while (i < this.directoryEntries.size()) {
115      String filename = this.directoryEntries.get(i);
116      File currentFile = new File(path + filename);
117      if (currentFile.isFile()) {
118        lastPosition = i;
119        togglePlaying(currentFile);
120        return;
121      }
122      i++;
123    }
124    i = 0;
125    while (i <= lastPosition) {
126      String filename = this.directoryEntries.get(i);
127      File currentFile = new File(path + filename);
128      if (currentFile.isFile()) {
129        lastPosition = i;
130        togglePlaying(currentFile);
131        return;
132      }
133      i++;
134    }
135    Toast.makeText(this, R.string.no_files, 1).show();
136  }
137
138  private void fill(File[] files) {
139    this.directoryEntries.clear();
140
141    // Add the top two choices
142    this.directoryEntries.add(currentDirectory.getAbsolutePath());
143    if (this.currentDirectory.getParent() != null) {
144      this.directoryEntries.add(" up one level");
145    }
146
147    int currentPathStringLength = this.currentDirectory.getAbsolutePath().length();
148
149    List<String> directories = new ArrayList<String>();
150    for (File file : files) {
151      if (file.isDirectory()) {
152        String filename = file.getAbsolutePath().substring(currentPathStringLength);
153        if (!filename.startsWith("/.")) {
154          directories.add(filename);
155        }
156      }
157    }
158    Collections.sort(directories);
159    this.directoryEntries.addAll(directories);
160
161    List<String> audioFiles = new ArrayList<String>();
162    for (File file : files) {
163      if (file.getPath().endsWith(".mp3")) {
164        String filename = file.getAbsolutePath().substring(currentPathStringLength);
165        if (!filename.startsWith("/.")) {
166          audioFiles.add(filename);
167        }
168      }
169    }
170    Collections.sort(audioFiles);
171    this.directoryEntries.addAll(audioFiles);
172
173    ArrayAdapter<String> directoryList =
174        new ArrayAdapter<String>(this, R.layout.file_row, this.directoryEntries);
175
176    myList.setAdapter(directoryList);
177  }
178
179  @Override
180  public boolean onTrackballEvent(MotionEvent event) {
181    int selectedId = myList.getSelectedItemPosition();
182    if (selectedId == -1) {
183      selectedId = 0;
184    }
185    if (lastPosition != selectedId) {
186      lastPosition = selectedId;
187      vibe.vibrate(VIBE_PATTERN, -1);
188      String filename = directoryEntries.get(selectedId);
189    }
190    return super.onTrackballEvent(event);
191  }
192
193  @Override
194  public boolean onKeyDown(int keyCode, KeyEvent event) {
195    if (keyCode == KeyEvent.KEYCODE_BACK) {
196      // Pressing back should go up a level, not quit the app
197      if (upOneLevel()) {
198        return true;
199      }
200    }
201    return super.onKeyDown(keyCode, event);
202  }
203  
204  @Override
205  public void onDestroy(){
206    mp.stop();
207    super.onDestroy();
208  }
209}