InputStream
as a byte[]
.
*
* This method buffers the input internally, so there is no need to use a
* BufferedInputStream
.
*
* @param input the InputStream
to read from
* @return the requested byte array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}
public static long copy(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[1024 * 4];
long count = 0;
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void renameFile(File from, File to) throws IOException {
if(!from.renameTo(to)) {
Log.i(TAG, "Failed to rename " + from + " to " + to);
}
}
public static void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (Throwable x) {
// Ignored
}
}
public static boolean delete(File file) {
if (file != null && file.exists()) {
if (!file.delete()) {
Log.w(TAG, "Failed to delete file " + file);
return false;
}
Log.i(TAG, "Deleted file " + file);
}
return true;
}
public static void toast(Context context, int messageId) {
toast(context, messageId, true);
}
public static void toast(Context context, int messageId, boolean shortDuration) {
toast(context, context.getString(messageId), shortDuration);
}
public static void toast(Context context, String message) {
toast(context, message, true);
}
public static void toast(Context context, String message, boolean shortDuration) {
if (toast == null) {
toast = Toast.makeText(context, message, shortDuration ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(message);
toast.setDuration(shortDuration ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG);
}
toast.show();
}
public static void confirmDialog(Context context, int action, int subject, DialogInterface.OnClickListener onClick) {
Util.confirmDialog(context, context.getResources().getString(action).toLowerCase(), context.getResources().getString(subject), onClick, null);
}
public static void confirmDialog(Context context, int action, int subject, DialogInterface.OnClickListener onClick, DialogInterface.OnClickListener onCancel) {
Util.confirmDialog(context, context.getResources().getString(action).toLowerCase(), context.getResources().getString(subject), onClick, onCancel);
}
public static void confirmDialog(Context context, int action, String subject, DialogInterface.OnClickListener onClick) {
Util.confirmDialog(context, context.getResources().getString(action).toLowerCase(), subject, onClick, null);
}
public static void confirmDialog(Context context, int action, String subject, DialogInterface.OnClickListener onClick, DialogInterface.OnClickListener onCancel) {
Util.confirmDialog(context, context.getResources().getString(action).toLowerCase(), subject, onClick, onCancel);
}
public static void confirmDialog(Context context, String action, String subject, DialogInterface.OnClickListener onClick, DialogInterface.OnClickListener onCancel) {
new AlertDialog.Builder(context)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle(R.string.common_confirm)
.setMessage(context.getResources().getString(R.string.common_confirm_message, action, subject))
.setPositiveButton(R.string.common_ok, onClick)
.setNegativeButton(R.string.common_cancel, onCancel)
.show();
}
/**
* Converts a byte-count to a formatted string suitable for display to the user.
* For instance:
* format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB".format(918)
returns "918 B".format(98765)
returns "96 KB".format(1238476)
returns "1.2 MB".Broadcasts the given song info as the new song being played.
*/ public static void broadcastNewTrackInfo(Context context, MusicDirectory.Entry song) { try { Intent intent = new Intent(EVENT_META_CHANGED); Intent avrcpIntent = new Intent(AVRCP_METADATA_CHANGED); if (song != null) { intent.putExtra("title", song.getTitle()); intent.putExtra("artist", song.getArtist()); intent.putExtra("album", song.getAlbum()); File albumArtFile = FileUtil.getAlbumArtFile(context, song); intent.putExtra("coverart", albumArtFile.getAbsolutePath()); avrcpIntent.putExtra("playing", true); } else { intent.putExtra("title", ""); intent.putExtra("artist", ""); intent.putExtra("album", ""); intent.putExtra("coverart", ""); avrcpIntent.putExtra("playing", false); } addTrackInfo(context, song, avrcpIntent); context.sendBroadcast(intent); context.sendBroadcast(avrcpIntent); } catch(Exception e) { Log.e(TAG, "Failed to broadcastNewTrackInfo", e); } } /** *Broadcasts the given player state as the one being set.
*/ public static void broadcastPlaybackStatusChange(Context context, MusicDirectory.Entry song, PlayerState state) { try { Intent intent = new Intent(EVENT_PLAYSTATE_CHANGED); Intent avrcpIntent = new Intent(AVRCP_PLAYSTATE_CHANGED); switch (state) { case STARTED: intent.putExtra("state", "play"); avrcpIntent.putExtra("playing", true); break; case STOPPED: intent.putExtra("state", "stop"); avrcpIntent.putExtra("playing", false); break; case PAUSED: intent.putExtra("state", "pause"); avrcpIntent.putExtra("playing", false); break; case PREPARED: // Only send quick pause event for samsung devices, causes issues for others if (Build.MANUFACTURER.toLowerCase().indexOf("samsung") != -1) { avrcpIntent.putExtra("playing", false); } else { return; // Don't broadcast anything } break; case COMPLETED: intent.putExtra("state", "complete"); avrcpIntent.putExtra("playing", false); break; default: return; // No need to broadcast. } addTrackInfo(context, song, avrcpIntent); if (state != PlayerState.PREPARED) { context.sendBroadcast(intent); } context.sendBroadcast(avrcpIntent); } catch(Exception e) { Log.e(TAG, "Failed to broadcastPlaybackStatusChange", e); } } private static void addTrackInfo(Context context, MusicDirectory.Entry song, Intent intent) { if (song != null) { DownloadService downloadService = (DownloadService)context; File albumArtFile = FileUtil.getAlbumArtFile(context, song); intent.putExtra("track", song.getTitle()); intent.putExtra("artist", song.getArtist()); intent.putExtra("album", song.getAlbum()); intent.putExtra("ListSize", (long) downloadService.getSongs().size()); intent.putExtra("id", (long) downloadService.getCurrentPlayingIndex() + 1); intent.putExtra("duration", (long) downloadService.getPlayerDuration()); intent.putExtra("position", (long) downloadService.getPlayerPosition()); intent.putExtra("coverart", albumArtFile.getAbsolutePath()); intent.putExtra("package","net.nullsum.audinaut"); } else { intent.putExtra("track", ""); intent.putExtra("artist", ""); intent.putExtra("album", ""); intent.putExtra("ListSize", (long) 0); intent.putExtra("id", (long) 0); intent.putExtra("duration", (long) 0); intent.putExtra("position", (long) 0); intent.putExtra("coverart", ""); intent.putExtra("package","net.nullsum.audinaut"); } } public static WifiManager.WifiLock createWifiLock(Context context, String tag) { WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); return wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, tag); } public static Random getRandom() { if(random == null) { random = new SecureRandom(); } return random; } }