ArtistDataSource will work with setOfflineOnly

Synchronized SuperUserCommand
Update function and class names
This commit is contained in:
David Schulte 2015-01-25 20:52:57 +01:00
parent 7a1b80d20d
commit 13a4bb22c4
8 changed files with 174 additions and 123 deletions

View file

@ -34,11 +34,9 @@ import java.util.List;
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.enums.ID3v2Version;
import de.arcus.playmusiclib.items.MusicTrack;
import de.arcus.playmusiclib.items.MusicTrackList;
import de.arcus.playmusiclib.items.Playlist;
/**
@ -104,6 +102,12 @@ public class TrackListActivity extends ActionBarActivity
playMusicManager.startUp();
playMusicManager.setOfflineOnly(true);
playMusicManager.setID3Enable(true);
playMusicManager.setID3EnableArtwork(true);
playMusicManager.setID3EnableFallback(true);
playMusicManager.setID3v2Version(ID3v2Version.ID3v23);
PlaylistDataSource playlistDataSource = new PlaylistDataSource(playMusicManager);
playlistDataSource.setSerchKey("Angesagte Songs");

View file

@ -207,113 +207,116 @@ public class SuperUserCommand {
return false;
}
try {
// Gets the streams
DataOutputStream dataOutputStream = new DataOutputStream(SuperUser.getProcess().getOutputStream());
BufferedReader bufferedInputReader = new BufferedReader(new InputStreamReader(SuperUser.getProcess().getInputStream()));
BufferedReader bufferedErrorReader = new BufferedReader(new InputStreamReader(SuperUser.getProcess().getErrorStream()));
// Thread safe
synchronized (SuperUser.getProcess()) {
try {
// Gets the streams
DataOutputStream dataOutputStream = new DataOutputStream(SuperUser.getProcess().getOutputStream());
BufferedReader bufferedInputReader = new BufferedReader(new InputStreamReader(SuperUser.getProcess().getInputStream()));
BufferedReader bufferedErrorReader = new BufferedReader(new InputStreamReader(SuperUser.getProcess().getErrorStream()));
// Sends the command
for (String command : mCommands) {
Logger.getInstance().logInfo("SuperUser", "< " + command);
dataOutputStream.writeBytes(command + "\n");
}
dataOutputStream.flush();
// TODO: This class cannot execute commands without any output (standard and error). These commands will run until the timeout will kill them!
// Start waiting
long timeStarted = System.currentTimeMillis();
// Wait for first data
while (!bufferedInputReader.ready() && !bufferedErrorReader.ready()) {
try {
// Waiting
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
// Sends the command
for (String command : mCommands) {
Logger.getInstance().logInfo("SuperUser", "< " + command);
dataOutputStream.writeBytes(command + "\n");
}
dataOutputStream.flush();
long timeNow = System.currentTimeMillis();
// TODO: This class cannot execute commands without any output (standard and error). These commands will run until the timeout will kill them!
// TimeOut
if (timeNow - timeStarted >= mTimeout) break;
}
// Start waiting
long timeStarted = System.currentTimeMillis();
// We want to read the data as binary
if (mBinaryStandardOutput) {
int len;
byte[] buffer = new byte[1024];
// Byte buffer
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(1024);
// Need the direct input stream
InputStream inputStream = SuperUser.getProcess().getInputStream();
do {
while (bufferedInputReader.ready()) {
// Read to buffer
len = inputStream.read(buffer);
// Write to buffer
byteArrayBuffer.append(buffer, 0, len);
}
// Fix: Wait for the buffer and try again
// Wait for first data
while (!bufferedInputReader.ready() && !bufferedErrorReader.ready()) {
try {
// Sometimes cat is to slow.
// If there is no data anymore we will wait 100ms and check again.
Thread.sleep(100);
// Waiting
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while(bufferedInputReader.ready());
mOutputStandardBinary = byteArrayBuffer.toByteArray();
} else {
// Reads the standard output as text
long timeNow = System.currentTimeMillis();
// TimeOut
if (timeNow - timeStarted >= mTimeout) break;
}
// We want to read the data as binary
if (mBinaryStandardOutput) {
int len;
byte[] buffer = new byte[1024];
// Byte buffer
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(1024);
// Need the direct input stream
InputStream inputStream = SuperUser.getProcess().getInputStream();
do {
while (bufferedInputReader.ready()) {
// Read to buffer
len = inputStream.read(buffer);
// 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 {
// Reads the standard output as text
tmpList.clear();
while (bufferedInputReader.ready()) {
tmpLine = bufferedInputReader.readLine();
// End of data
if (tmpLine == null) break;
if (!mHideStandardOutput)
Logger.getInstance().logInfo("SuperUser", "> " + tmpLine);
tmpList.add(tmpLine);
}
// Convert list to array
mOutputStandard = tmpList.toArray(new String[tmpList.size()]);
}
// Reads the error output
tmpList.clear();
while (bufferedInputReader.ready()) {
tmpLine = bufferedInputReader.readLine();
while (bufferedErrorReader.ready()) {
tmpLine = bufferedErrorReader.readLine();
// End of data
if (tmpLine == null) break;
if (!mHideStandardOutput)
Logger.getInstance().logInfo("SuperUser", "> " + tmpLine);
if (!mHideErrorOutput)
Logger.getInstance().logError("SuperUser", "> " + tmpLine);
tmpList.add(tmpLine);
}
// Convert list to array
mOutputStandard = tmpList.toArray(new String[tmpList.size()]);
mOutputError = tmpList.toArray(new String[tmpList.size()]);
// Done
return true;
} catch (IOException e) {
e.printStackTrace();
mSuperUserFailed = true;
// Command failed
return false;
}
// Reads the error output
tmpList.clear();
while (bufferedErrorReader.ready()) {
tmpLine = bufferedErrorReader.readLine();
// End of data
if (tmpLine == null) break;
if (!mHideErrorOutput)
Logger.getInstance().logError("SuperUser", "> " + tmpLine);
tmpList.add(tmpLine);
}
// Convert list to array
mOutputError = tmpList.toArray(new String[tmpList.size()]);
// Done
return true;
} catch (IOException e) {
e.printStackTrace();
mSuperUserFailed = true;
// Command failed
return false;
}
}
}

View file

@ -45,9 +45,10 @@ 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.enums.ID3v2Version;
import de.arcus.playmusiclib.exceptions.CouldNotOpenDatabaseException;
import de.arcus.playmusiclib.exceptions.NoSuperUserException;
import de.arcus.playmusiclib.exceptions.PlayMusicNotFound;
import de.arcus.playmusiclib.exceptions.PlayMusicNotFoundException;
import de.arcus.playmusiclib.items.MusicTrack;
@ -164,46 +165,41 @@ public class PlayMusicManager {
/**
* If this is set the exporter will add the artwork to the ID2v2 tag
*/
private boolean mID3ExportArtwork = true;
private boolean mID3EnableArtwork = true;
/**
* @return Gets whether the exporter adds the artwork image
*/
public boolean getID3ExportArtwork() {
return mID3ExportArtwork;
public boolean getID3EnableArtwork() {
return mID3EnableArtwork;
}
/**
* @param id3ExportArtwork Sets whether the exporter adds the artwork image
* @param id3EnableArtwork Sets whether the exporter adds the artwork image
*/
public void setID3ExportArtwork(boolean id3ExportArtwork) {
mID3ExportArtwork = id3ExportArtwork;
public void setID3EnableArtwork(boolean id3EnableArtwork) {
mID3EnableArtwork = id3EnableArtwork;
}
/**
* If this is set the exporter will also adds ID3v1 tags
*/
private boolean mID3ExportFallback = true;
private boolean mID3EnableFallback = true;
/**
* @return Gets whether the exporter adds ID3v1 tags as fallback
*/
public boolean getID3ExportFallback() {
return mID3ExportFallback;
public boolean getID3EnableFallback() {
return mID3EnableFallback;
}
/**
* @param id3ExportFallback Sets whether the exporter adds ID3v1 tags as fallback
* @param id3EnableFallback Sets whether the exporter adds ID3v1 tags as fallback
*/
public void setmID3ExportFallback(boolean id3ExportFallback) {
mID3ExportFallback = id3ExportFallback;
public void setID3EnableFallback(boolean id3EnableFallback) {
mID3EnableFallback = id3EnableFallback;
}
/**
* The ID3v2 sub version
*/
public enum ID3v2Version { ID3v22, ID3v23, ID3v24 }
/**
* The sub version of ID3v2
* Use 2.3 for default to fix issues with the Windows Windows Media Player
@ -235,11 +231,11 @@ public class PlayMusicManager {
/**
* Loads all needed information and opens the database
* @throws PlayMusicNotFound PlayMusic is not installed
* @throws de.arcus.playmusiclib.exceptions.PlayMusicNotFoundException PlayMusic is not installed
* @throws NoSuperUserException No super user permissions
* @throws CouldNotOpenDatabase Could not open the database
* @throws de.arcus.playmusiclib.exceptions.CouldNotOpenDatabaseException Could not open the database
*/
public void startUp() throws PlayMusicNotFound, NoSuperUserException, CouldNotOpenDatabase {
public void startUp() throws PlayMusicNotFoundException, NoSuperUserException, CouldNotOpenDatabaseException {
// Gets the package manager
PackageManager packageManager = mContext.getPackageManager();
@ -248,7 +244,7 @@ public class PlayMusicManager {
mPlayMusicApplicationInfo = packageManager.getApplicationInfo(PLAYMUSIC_PACKAGE_ID, 0);
} catch (PackageManager.NameNotFoundException e) {
// No PlayMusic
throw new PlayMusicNotFound();
throw new PlayMusicNotFoundException();
}
@ -274,9 +270,9 @@ public class PlayMusicManager {
/**
* Copies the database to a temp directory and opens it
* @throws NoSuperUserException No super user permissions
* @throws CouldNotOpenDatabase Could not open the database
* @throws de.arcus.playmusiclib.exceptions.CouldNotOpenDatabaseException Could not open the database
*/
private void loadDatabase() throws NoSuperUserException, CouldNotOpenDatabase {
private void loadDatabase() throws NoSuperUserException, CouldNotOpenDatabaseException {
// Ask for super user
if (!SuperUser.askForPermissions())
throw new NoSuperUserException();
@ -286,22 +282,22 @@ public class PlayMusicManager {
// Copy the database to the temp folder
if (!SuperUserTools.fileCopy(getDatabasePath(), getTempDatabasePath()))
throw new CouldNotOpenDatabase();
throw new CouldNotOpenDatabaseException();
// Opens the database
try {
mDatabase = SQLiteDatabase.openDatabase(getTempDatabasePath(), null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
throw new CouldNotOpenDatabase();
throw new CouldNotOpenDatabaseException();
}
}
/**
* Reloads the database from PlayMusic
* @throws NoSuperUserException No super user permissions
* @throws CouldNotOpenDatabase Could not open the database
* @throws de.arcus.playmusiclib.exceptions.CouldNotOpenDatabaseException Could not open the database
*/
public void realoadDatabase() throws NoSuperUserException, CouldNotOpenDatabase {
public void realoadDatabase() throws NoSuperUserException, CouldNotOpenDatabaseException {
loadDatabase();
}
@ -464,7 +460,7 @@ public class PlayMusicManager {
mp3File.removeCustomTag();
// We want to add a fallback ID3v1 tag
if (mID3ExportFallback) {
if (mID3EnableFallback) {
// Create a new tag with ID3v1
ID3v1Tag tagID3v1 = new ID3v1Tag();
@ -520,7 +516,7 @@ public class PlayMusicManager {
}
// Add the artwork to the meta data
if (mID3ExportArtwork) {
if (mID3EnableArtwork) {
String artworkPath = musicTrack.getArtworkPath();
if (artworkPath != null) {

View file

@ -102,6 +102,13 @@ public class ArtistDataSource extends DataSource<Artist> {
* @return The new where command
*/
private String prepareWhere(String where) {
// Ignore non-PlayMusic tracks
where = combineWhere(where, "LocalCopyType != 300");
// Loads only offline tracks
if (mOfflineOnly)
where = combineWhere(where, "LocalCopyPath IS NOT NULL");
// Search only items which contains the key
if (!TextUtils.isEmpty(mSearchKey)) {
String searchKey = DatabaseUtils.sqlEscapeString("%" + mSearchKey + "%");

View file

@ -0,0 +1,30 @@
/*
* 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.playmusiclib.enums;
/**
* Enum for the ID3v2 sub version
*/
public enum ID3v2Version {
ID3v22, ID3v23, ID3v24
}

View file

@ -25,5 +25,5 @@ package de.arcus.playmusiclib.exceptions;
/**
* Exception will thrown if the database could not be copied or opened
*/
public class CouldNotOpenDatabase extends Exception {
public class CouldNotOpenDatabaseException extends Exception {
}

View file

@ -25,5 +25,5 @@ package de.arcus.playmusiclib.exceptions;
/**
* Exception will thrown if PlayMusic is not installed
*/
public class PlayMusicNotFound extends Exception {
public class PlayMusicNotFoundException extends Exception {
}

View file

@ -22,6 +22,10 @@
package de.arcus.playmusiclib.items;
import android.text.TextUtils;
import org.w3c.dom.Text;
import de.arcus.playmusiclib.PlayMusicManager;
/**
@ -394,6 +398,13 @@ public class MusicTrack {
return (mCpData != null);
}
/**
* @return Returns true if this track is offline available
*/
public boolean isOfflineAvailable() {
return !TextUtils.isEmpty(mLocalCopyPath);
}
@Override
public String toString() {
return mTitle;