Fix album and artist group

Fix slow cut command when reading binary files via superuser commands
Run the media scanner after exporting a track
This commit is contained in:
David Schulte 2015-01-25 18:50:17 +01:00
parent 0aeba42d12
commit 7a1b80d20d
8 changed files with 179 additions and 42 deletions

View file

@ -35,8 +35,11 @@ import de.arcus.framework.logger.Logger;
import de.arcus.framework.crashhandler.CrashHandler;
import de.arcus.playmusiclib.PlayMusicManager;
import de.arcus.playmusiclib.datasources.AlbumDataSource;
import de.arcus.playmusiclib.datasources.PlaylistDataSource;
import de.arcus.playmusiclib.items.Album;
import de.arcus.playmusiclib.items.MusicTrack;
import de.arcus.playmusiclib.items.MusicTrackList;
import de.arcus.playmusiclib.items.Playlist;
/**
* An activity representing a list of Tracks. This activity
@ -101,29 +104,23 @@ public class TrackListActivity extends ActionBarActivity
playMusicManager.startUp();
playMusicManager.setOfflineOnly(true);
AlbumDataSource albumDataSource = new AlbumDataSource(playMusicManager);
PlaylistDataSource playlistDataSource = new PlaylistDataSource(playMusicManager);
albumDataSource.setSerchKey("Ed Sheeran");
playlistDataSource.setSerchKey("Angesagte Songs");
// Load all albums
List<Album> albums = albumDataSource.getAll();
if (albums.size() > 0) {
// Gets the first album
Album album = albums.get(0);
List<Playlist> playlists = playlistDataSource.getAll();
for (Playlist playlist : playlists) {
// Load tracks from album
List<MusicTrack> tracks = album.getMusicTrackList();
if (tracks.size() > 0) {
// Gets the first track
MusicTrack track = tracks.get(0);
List<MusicTrack> tracks = playlist.getMusicTrackList();
for (MusicTrack track : tracks) {
// Test: exports the track to the sd card
String filename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/test.mp3";
String filename = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/"+track.getTitle()+".mp3";
boolean success = playMusicManager.exportMusicTrack(track, filename);
Log.d("Debug", "Success: " + success);
Log.d("Debug", track.getTitle() + ": " + success);
}
}

View file

@ -251,13 +251,24 @@ public class SuperUserCommand {
// Need the direct input stream
InputStream inputStream = SuperUser.getProcess().getInputStream();
while (bufferedInputReader.ready()) {
// Read to buffer
len = inputStream.read(buffer);
do {
while (bufferedInputReader.ready()) {
// Read to buffer
len = inputStream.read(buffer);
// Write to buffer
byteArrayBuffer.append(buffer, 0, len);
}
// Write to buffer
byteArrayBuffer.append(buffer, 0, len);
}
// Fix: Wait for the buffer and try again
try {
// Sometimes cat is to slow.
// If there is no data anymore we will wait 100ms and check again.
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while(bufferedInputReader.ready());
mOutputStandardBinary = byteArrayBuffer.toByteArray();
} else {

View file

@ -44,6 +44,10 @@ public class SuperUserTools {
"chmod 0777 '" + dest + "'", // Change the access mode to all users (chown sdcard_r will fail on some devices)
"echo 'done'" // Fix to prevent the 'no output' bug in SuperUserCommand
});
// Don't spam the log
superUserCommand.setHideStandardOutput(true);
// Executes the command
superUserCommand.execute();
@ -59,6 +63,9 @@ public class SuperUserTools {
public static boolean fileExists(String path) {
SuperUserCommand superUserCommand = new SuperUserCommand("ls '" + path + "'");
// Don't spam the log
superUserCommand.setHideStandardOutput(true);
// Executes the command
superUserCommand.execute();

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2015 David Schulte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.arcus.framework.utils;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
/**
* A media scanner which adds files to the android media system
*/
public class MediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {
/**
* Connection to the media scanner
*/
private MediaScannerConnection mMediaScanner;
/**
* File to scan
*/
private String mFile;
/**
* Adds a file to the android media system
* @param context The context of the app
* @param string Path to the file
*/
public MediaScanner(Context context, String string) {
mFile = string;
// Connect the media scanner
mMediaScanner = new MediaScannerConnection(context, this);
mMediaScanner.connect();
}
@Override
public void onMediaScannerConnected() {
// Is connected
mMediaScanner.scanFile(mFile, null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
// Done; close the connection
mMediaScanner.disconnect();
}
}

View file

@ -44,6 +44,7 @@ import de.arcus.framework.logger.Logger;
import de.arcus.framework.superuser.SuperUser;
import de.arcus.framework.superuser.SuperUserTools;
import de.arcus.framework.utils.FileTools;
import de.arcus.framework.utils.MediaScanner;
import de.arcus.playmusiclib.exceptions.CouldNotOpenDatabase;
import de.arcus.playmusiclib.exceptions.NoSuperUserException;
import de.arcus.playmusiclib.exceptions.PlayMusicNotFound;
@ -436,6 +437,11 @@ public class PlayMusicManager {
}
}
cleanUp();
// Adds the file to the media system
new MediaScanner(mContext, dest);
// Done
return true;
}
@ -500,7 +506,7 @@ public class PlayMusicManager {
// Set all tag values
tagID3v2.setTitle(musicTrack.getTitle());
tagID3v2.setArtist(musicTrack.getArtist());
tagID3v2.setAlbum(musicTrack.getTitle());
tagID3v2.setAlbum(musicTrack.getAlbum());
tagID3v2.setAlbumArtist(musicTrack.getAlbumArtist());
tagID3v2.setTrack("" + musicTrack.getTrackNumber());
tagID3v2.setPartOfSet("" + musicTrack.getDiscNumber());
@ -515,25 +521,30 @@ public class PlayMusicManager {
// Add the artwork to the meta data
if (mID3ExportArtwork) {
// Reads the artwork with root permissions (maybe it is in /data)
byte[] artwork = SuperUserTools.fileReadToByteArray(musicTrack.getArtworkPath());
if (artwork != null) {
// The file extension is always .jpg even if the image is a PNG file,
// so we need to check the magic number
String artworkPath = musicTrack.getArtworkPath();
// JPEG is default
String mimeType = "image/jpeg";
if (artworkPath != null) {
// Check for other image formats
if (artwork.length > 4) {
// PNG-Header
if (artwork[0] == -119 && artwork[1] == 80 && artwork[2] == 78 && artwork[3] == 71) {
mimeType = "image/png";
// Reads the artwork with root permissions (maybe it is in /data)
byte[] artworkData = SuperUserTools.fileReadToByteArray(artworkPath);
if (artworkData != null) {
// The file extension is always .jpg even if the image is a PNG file,
// so we need to check the magic number
// JPEG is default
String mimeType = "image/jpeg";
// Check for other image formats
if (artworkData.length > 4) {
// PNG-Header
if (artworkData[0] == -119 && artworkData[1] == 80 && artworkData[2] == 78 && artworkData[3] == 71) {
mimeType = "image/png";
}
}
}
// Adds the artwork to the meta data
tagID3v2.setAlbumImage(artwork, mimeType);
// Adds the artwork to the meta data
tagID3v2.setAlbumImage(artworkData, mimeType);
}
}
}
@ -581,4 +592,12 @@ public class PlayMusicManager {
return false;
}
/**
* Deletes all cache files
*/
private void cleanUp() {
FileTools.fileDelete(getTempPath() + "/tmp.mp3");
FileTools.fileDelete(getTempPath() + "/crypt.mp3");
}
}

View file

@ -30,6 +30,7 @@ import java.util.List;
import de.arcus.playmusiclib.PlayMusicManager;
import de.arcus.playmusiclib.items.Album;
import de.arcus.playmusiclib.items.Artist;
/**
* Data source for albums
@ -152,7 +153,15 @@ public class AlbumDataSource extends DataSource<Album> {
* @return Returns the album or null
*/
public Album getById(long id) {
return getItem(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(COLUMN_ALBUM_ID + " = " + id));
return getItem(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(COLUMN_ALBUM_ID + " = " + id), null, COLUMN_ALBUM_ID);
}
/**
* Gets a list of all albums by an artist
* @return Returns albums
*/
public List<Album> getByArtist(Artist artist) {
return getItems(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(COLUMN_ALBUM_ARTIST + " = " + DatabaseUtils.sqlEscapeString(artist.getArtist())), COLUMN_ALBUM, COLUMN_ALBUM_ID);
}
/**
@ -160,6 +169,6 @@ public class AlbumDataSource extends DataSource<Album> {
* @return Returns all albums
*/
public List<Album> getAll() {
return getItems(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(""), COLUMN_ALBUM);
return getItems(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(""), COLUMN_ALBUM, COLUMN_ALBUM_ID);
}
}

View file

@ -135,7 +135,7 @@ public class ArtistDataSource extends DataSource<Artist> {
* @return Returns the artist or null
*/
public Artist getById(long id) {
return getItem(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(COLUMN_ARTIST_ID + " = " + id));
return getItem(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(COLUMN_ARTIST_ID + " = " + id), null, COLUMN_ARTIST_ID);
}
/**
@ -143,6 +143,6 @@ public class ArtistDataSource extends DataSource<Artist> {
* @return Returns all artists
*/
public List<Artist> getAll() {
return getItems(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(""), COLUMN_ARTIST);
return getItems(TABLE_MUSIC, COLUMNS_ALL, prepareWhere(""), COLUMN_ARTIST, COLUMN_ARTIST_ID);
}
}

View file

@ -86,6 +86,19 @@ public abstract class DataSource<T> {
* @return Returns a list with all items
*/
protected List<T> getItems(String table, String[] columns, String where, String orderBy) {
return getItems(table, columns, where, orderBy, null);
}
/**
* Loads all items from the database
* @param table The table
* @param columns All columns
* @param where The where-command
* @param orderBy Order
* @param groupBy Group
* @return Returns a list with all items
*/
protected List<T> getItems(String table, String[] columns, String where, String orderBy, String groupBy) {
// No connection; abort
if (!mPlayMusicManager.getDatabase().isOpen()) return null;
@ -94,7 +107,7 @@ public abstract class DataSource<T> {
try {
// Gets the first data row
Cursor cursor = mPlayMusicManager.getDatabase().query(table, columns, where, null, null, null, orderBy);
Cursor cursor = mPlayMusicManager.getDatabase().query(table, columns, where, null, groupBy, null, orderBy);
// SQL error
if (cursor == null) return null;
@ -141,8 +154,21 @@ public abstract class DataSource<T> {
* @return Returns the item
*/
protected T getItem(String table, String[] columns, String where, String orderBy) {
return getItem(table, columns, where, orderBy, null);
}
/**
* Loads one item from the database
* @param table The table
* @param columns All columns
* @param where The where-command
* @param orderBy Order
* @param groupBy Group
* @return Returns the item
*/
protected T getItem(String table, String[] columns, String where, String orderBy, String groupBy) {
// Loads the list
List<T> items = getItems(table, columns, where, orderBy);
List<T> items = getItems(table, columns, where, orderBy, groupBy);
// Gets the first item
if (items.size() > 0)