summaryrefslogtreecommitdiff
path: root/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils
diff options
context:
space:
mode:
Diffstat (limited to 'MusicDemo/src/main/java/com/example/android/musicservicedemo/utils')
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/BitmapHelper.java77
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/LogHelper.java67
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/MediaIDHelper.java88
-rw-r--r--MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/QueueHelper.java129
4 files changed, 0 insertions, 361 deletions
diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/BitmapHelper.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/BitmapHelper.java
deleted file mode 100644
index 984de49..0000000
--- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/BitmapHelper.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.example.android.musicservicedemo.utils;
-
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-
-public class BitmapHelper {
-
- // Bitmap size for album art in media notifications when there are more than 3 playback actions
- public static final int MEDIA_ART_SMALL_WIDTH=64;
- public static final int MEDIA_ART_SMALL_HEIGHT=64;
-
- // Bitmap size for album art in media notifications when there are no more than 3 playback actions
- public static final int MEDIA_ART_BIG_WIDTH=128;
- public static final int MEDIA_ART_BIG_HEIGHT=128;
-
- public static final Bitmap scaleBitmap(int scaleFactor, InputStream is) {
- // Get the dimensions of the bitmap
- BitmapFactory.Options bmOptions = new BitmapFactory.Options();
-
- // Decode the image file into a Bitmap sized to fill the View
- bmOptions.inJustDecodeBounds = false;
- bmOptions.inSampleSize = scaleFactor;
-
- Bitmap bitmap = BitmapFactory.decodeStream(is, null, bmOptions);
- return bitmap;
- }
-
- public static final int findScaleFactor(int targetW, int targetH, InputStream is) {
- // Get the dimensions of the bitmap
- BitmapFactory.Options bmOptions = new BitmapFactory.Options();
- bmOptions.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(is, null, bmOptions);
- int actualW = bmOptions.outWidth;
- int actualH = bmOptions.outHeight;
-
- // Determine how much to scale down the image
- return Math.min(actualW/targetW, actualH/targetH);
- }
-
- public static final Bitmap fetchAndRescaleBitmap(String uri, int width, int height)
- throws IOException {
- URL url = new URL(uri);
- HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
- httpConnection.setDoInput(true);
- httpConnection.connect();
- InputStream inputStream = httpConnection.getInputStream();
- int scaleFactor = findScaleFactor(width, height, inputStream);
-
- httpConnection = (HttpURLConnection) url.openConnection();
- httpConnection.setDoInput(true);
- httpConnection.connect();
- inputStream = httpConnection.getInputStream();
- Bitmap bitmap = scaleBitmap(scaleFactor, inputStream);
- return bitmap;
- }
-
-}
diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/LogHelper.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/LogHelper.java
deleted file mode 100644
index 31f1aef..0000000
--- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/LogHelper.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2014 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.example.android.musicservicedemo.utils;
-
-import android.util.Log;
-
-public class LogHelper {
- public static void v(String tag, Object... messages) {
- log(tag, Log.VERBOSE, null, messages);
- }
-
- public static void d(String tag, Object... messages) {
- log(tag, Log.DEBUG, null, messages);
- }
-
- public static void i(String tag, Object... messages) {
- log(tag, Log.INFO, null, messages);
- }
-
- public static void w(String tag, Object... messages) {
- log(tag, Log.WARN, null, messages);
- }
-
- public static void w(String tag, Throwable t, Object... messages) {
- log(tag, Log.WARN, t, messages);
- }
-
- public static void e(String tag, Object... messages) {
- log(tag, Log.ERROR, null, messages);
- }
-
- public static void e(String tag, Throwable t, Object... messages) {
- log(tag, Log.ERROR, t, messages);
- }
-
- public static void log(String tag, int level, Throwable t, Object... messages) {
- if (messages != null && Log.isLoggable(tag, level)) {
- String message;
- if (messages.length == 1) {
- message = messages[0] == null ? null : messages[0].toString();
- } else {
- StringBuilder sb = new StringBuilder();
- for (Object m: messages) {
- sb.append(m);
- }
- if (t != null) {
- sb.append("\n").append(Log.getStackTraceString(t));
- }
- message = sb.toString();
- }
- Log.println(level, tag, message);
- }
- }
-}
diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/MediaIDHelper.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/MediaIDHelper.java
deleted file mode 100644
index 2406886..0000000
--- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/MediaIDHelper.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright (C) 2014 Google Inc. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.example.android.musicservicedemo.utils;
-
-import android.media.MediaMetadata;
-
-/**
- * Utility class to help on queue related tasks.
- */
-public class MediaIDHelper {
-
- private static final String TAG = "MediaIDHelper";
-
- // Media IDs used on browseable items of MediaBrowser
- public static final String MEDIA_ID_ROOT = "__ROOT__";
- public static final String MEDIA_ID_MUSICS_BY_GENRE = "__BY_GENRE__";
-
- public static final String createTrackMediaID(String categoryType, String categoryValue,
- MediaMetadata track) {
- // MediaIDs are of the form <categoryType>/<categoryValue>|<musicUniqueId>, to make it easy to
- // find the category (like genre) that a music was selected from, so we
- // can correctly build the playing queue. This is specially useful when
- // one music can appear in more than one list, like "by genre -> genre_1"
- // and "by artist -> artist_1".
- return categoryType + "/" + categoryValue + "|" +
- track.getString(MediaMetadata.METADATA_KEY_MEDIA_ID);
- }
-
- public static final String createBrowseCategoryMediaID(String categoryType, String categoryValue) {
- return categoryType + "/" + categoryValue;
- }
-
- /**
- * Extracts unique musicID from the mediaID. mediaID is, by this sample's convention, a
- * concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and unique
- * musicID. This is necessary so we know where the user selected the music from, when the music
- * exists in more than one music list, and thus we are able to correctly build the playing queue.
- *
- * @param musicID
- * @return
- */
- public static final String extractMusicIDFromMediaID(String musicID) {
- String[] segments = musicID.split("\\|", 2);
- return segments.length == 2 ? segments[1] : null;
- }
-
- /**
- * Extracts category and categoryValue from the mediaID. mediaID is, by this sample's
- * convention, a concatenation of category (eg "by_genre"), categoryValue (eg "Classical") and
- * mediaID. This is necessary so we know where the user selected the music from, when the music
- * exists in more than one music list, and thus we are able to correctly build the playing queue.
- *
- * @param mediaID
- * @return
- */
- public static final String[] extractBrowseCategoryFromMediaID(String mediaID) {
- if (mediaID.indexOf('|') >= 0) {
- mediaID = mediaID.split("\\|")[0];
- }
- if (mediaID.indexOf('/') == 0) {
- return new String[]{mediaID, null};
- } else {
- return mediaID.split("/", 2);
- }
- }
-
- public static final String extractBrowseCategoryValueFromMediaID(String mediaID) {
- String[] categoryAndValue = extractBrowseCategoryFromMediaID(mediaID);
- if (categoryAndValue != null && categoryAndValue.length == 2) {
- return categoryAndValue[1];
- }
- return null;
- }
-} \ No newline at end of file
diff --git a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/QueueHelper.java b/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/QueueHelper.java
deleted file mode 100644
index 9a510fb..0000000
--- a/MusicDemo/src/main/java/com/example/android/musicservicedemo/utils/QueueHelper.java
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2014 Google Inc. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.example.android.musicservicedemo.utils;
-
-import android.media.MediaMetadata;
-import android.media.session.MediaSession;
-
-import com.example.android.musicservicedemo.model.MusicProvider;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import static com.example.android.musicservicedemo.utils.MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE;
-
-/**
- * Utility class to help on queue related tasks.
- */
-public class QueueHelper {
-
- private static final String TAG = "QueueHelper";
-
- public static final List<MediaSession.QueueItem> getPlayingQueue(String mediaId,
- MusicProvider musicProvider) {
-
- // extract the category and unique music ID from the media ID:
- String[] category = MediaIDHelper.extractBrowseCategoryFromMediaID(mediaId);
-
- // This sample only supports genre category.
- if (!category[0].equals(MEDIA_ID_MUSICS_BY_GENRE) || category.length != 2) {
- LogHelper.e(TAG, "Could not build a playing queue for this mediaId: ", mediaId);
- return null;
- }
-
- String categoryValue = category[1];
- LogHelper.e(TAG, "Creating playing queue for musics of genre ", categoryValue);
-
- List<MediaSession.QueueItem> queue = convertToQueue(
- musicProvider.getMusicsByGenre(categoryValue));
-
- return queue;
- }
-
- public static final List<MediaSession.QueueItem> getPlayingQueueFromSearch(String query,
- MusicProvider musicProvider) {
-
- LogHelper.e(TAG, "Creating playing queue for musics from search ", query);
-
- return convertToQueue(musicProvider.searchMusics(query));
- }
-
-
- public static final int getMusicIndexOnQueue(Iterable<MediaSession.QueueItem> queue,
- String mediaId) {
- int index = 0;
- for (MediaSession.QueueItem item: queue) {
- if (mediaId.equals(item.getDescription().getMediaId())) {
- return index;
- }
- index++;
- }
- return -1;
- }
-
- public static final int getMusicIndexOnQueue(Iterable<MediaSession.QueueItem> queue,
- long queueId) {
- int index = 0;
- for (MediaSession.QueueItem item: queue) {
- if (queueId == item.getQueueId()) {
- return index;
- }
- index++;
- }
- return -1;
- }
-
- private static final List<MediaSession.QueueItem> convertToQueue(
- Iterable<MediaMetadata> tracks) {
- List<MediaSession.QueueItem> queue = new ArrayList<>();
- int count = 0;
- for (MediaMetadata track : tracks) {
- // We don't expect queues to change after created, so we use the item index as the
- // queueId. Any other number unique in the queue would work.
- MediaSession.QueueItem item = new MediaSession.QueueItem(
- track.getDescription(), count++);
- queue.add(item);
- }
- return queue;
-
- }
-
- /**
- * Create a random queue. For simplicity sake, instead of a random queue, we create a
- * queue using the first genre,
- *
- * @param musicProvider
- * @return
- */
- public static final List<MediaSession.QueueItem> getRandomQueue(MusicProvider musicProvider) {
- Iterator<String> genres = musicProvider.getGenres().iterator();
- if (!genres.hasNext()) {
- return new ArrayList<>();
- }
- String genre = genres.next();
- Iterable<MediaMetadata> tracks = musicProvider.getMusicsByGenre(genre);
-
- return convertToQueue(tracks);
- }
-
-
-
- public static final boolean isIndexPlayable(int index, List<MediaSession.QueueItem> queue) {
- return (queue != null && index >= 0 && index < queue.size());
- }
-}