diff options
Diffstat (limited to 'media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils')
6 files changed, 0 insertions, 552 deletions
diff --git a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/BitmapHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/BitmapHelper.java deleted file mode 100644 index 7325130e..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/BitmapHelper.java +++ /dev/null @@ -1,83 +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.mediabrowserservice.utils; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -public class BitmapHelper { - private static final String TAG = LogHelper.makeLogTag(BitmapHelper.class); - - // Max read limit that we allow our input stream to mark/reset. - private static final int MAX_READ_LIMIT_PER_IMG = 1024 * 1024; - - public static Bitmap scaleBitmap(Bitmap src, int maxWidth, int maxHeight) { - double scaleFactor = Math.min( - ((double) maxWidth)/src.getWidth(), ((double) maxHeight)/src.getHeight()); - return Bitmap.createScaledBitmap(src, - (int) (src.getWidth() * scaleFactor), (int) (src.getHeight() * scaleFactor), false); - } - - public static 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; - - return BitmapFactory.decodeStream(is, null, bmOptions); - } - - public static 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); - } - - @SuppressWarnings("SameParameterValue") - public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) - throws IOException { - URL url = new URL(uri); - BufferedInputStream is = null; - try { - HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); - is = new BufferedInputStream(urlConnection.getInputStream()); - is.mark(MAX_READ_LIMIT_PER_IMG); - int scaleFactor = findScaleFactor(width, height, is); - LogHelper.d(TAG, "Scaling bitmap ", uri, " by factor ", scaleFactor, " to support ", - width, "x", height, "requested dimension"); - is.reset(); - return scaleBitmap(scaleFactor, is); - } finally { - if (is != null) { - is.close(); - } - } - } -} diff --git a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/CarHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/CarHelper.java deleted file mode 100644 index 74861ba3..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/CarHelper.java +++ /dev/null @@ -1,55 +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.mediabrowserservice.utils; - -import android.os.Bundle; - -public class CarHelper { - private static final String AUTO_APP_PACKAGE_NAME = "com.google.android.projection.gearhead"; - - // Use these extras to reserve space for the corresponding actions, even when they are disabled - // in the playbackstate, so the custom actions don't reflow. - private static final String SLOT_RESERVATION_SKIP_TO_NEXT = - "com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_NEXT"; - private static final String SLOT_RESERVATION_SKIP_TO_PREV = - "com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_PREVIOUS"; - private static final String SLOT_RESERVATION_QUEUE = - "com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_QUEUE"; - - - public static boolean isValidCarPackage(String packageName) { - return AUTO_APP_PACKAGE_NAME.equals(packageName); - } - - public static void setSlotReservationFlags(Bundle extras, boolean reservePlayingQueueSlot, - boolean reserveSkipToNextSlot, boolean reserveSkipToPrevSlot) { - if (reservePlayingQueueSlot) { - extras.putBoolean(SLOT_RESERVATION_QUEUE, true); - } else { - extras.remove(SLOT_RESERVATION_QUEUE); - } - if (reserveSkipToPrevSlot) { - extras.putBoolean(SLOT_RESERVATION_SKIP_TO_PREV, true); - } else { - extras.remove(SLOT_RESERVATION_SKIP_TO_PREV); - } - if (reserveSkipToNextSlot) { - extras.putBoolean(SLOT_RESERVATION_SKIP_TO_NEXT, true); - } else { - extras.remove(SLOT_RESERVATION_SKIP_TO_NEXT); - } - } -} diff --git a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/LogHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/LogHelper.java deleted file mode 100644 index 09d14d26..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/LogHelper.java +++ /dev/null @@ -1,97 +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.mediabrowserservice.utils; - -import android.util.Log; - -import com.example.android.mediabrowserservice.BuildConfig; - -public class LogHelper { - - private static final String LOG_PREFIX = "sample_"; - private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length(); - private static final int MAX_LOG_TAG_LENGTH = 23; - - public static String makeLogTag(String str) { - if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) { - return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1); - } - - return LOG_PREFIX + str; - } - - /** - * Don't use this when obfuscating class names! - */ - public static String makeLogTag(Class cls) { - return makeLogTag(cls.getSimpleName()); - } - - - public static void v(String tag, Object... messages) { - // Only log VERBOSE if build type is DEBUG - if (BuildConfig.DEBUG) { - log(tag, Log.VERBOSE, null, messages); - } - } - - public static void d(String tag, Object... messages) { - // Only log DEBUG if build type is DEBUG - if (BuildConfig.DEBUG) { - 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 (Log.isLoggable(tag, level)) { - String message; - if (t == null && messages != null && messages.length == 1) { - // handle this common case without the extra cost of creating a stringbuffer: - message = messages[0].toString(); - } else { - StringBuilder sb = new StringBuilder(); - if (messages != null) 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/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/MediaIDHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/MediaIDHelper.java deleted file mode 100644 index 604cf8ac..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/MediaIDHelper.java +++ /dev/null @@ -1,115 +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.mediabrowserservice.utils; - -import java.util.Arrays; - -/** - * Utility class to help on queue related tasks. - */ -public class MediaIDHelper { - - private static final String TAG = LogHelper.makeLogTag(MediaIDHelper.class); - - // 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 MEDIA_ID_MUSICS_BY_SEARCH = "__BY_SEARCH__"; - - private static final char CATEGORY_SEPARATOR = '/'; - private static final char LEAF_SEPARATOR = '|'; - - public static String createMediaID(String musicID, String... categories) { - // 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". - StringBuilder sb = new StringBuilder(); - if (categories != null && categories.length > 0) { - sb.append(categories[0]); - for (int i=1; i < categories.length; i++) { - sb.append(CATEGORY_SEPARATOR).append(categories[i]); - } - } - if (musicID != null) { - sb.append(LEAF_SEPARATOR).append(musicID); - } - return sb.toString(); - } - - public static String createBrowseCategoryMediaID(String categoryType, String categoryValue) { - return categoryType + CATEGORY_SEPARATOR + 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 mediaID that contains the musicID - * @return musicID - */ - public static String extractMusicIDFromMediaID(String mediaID) { - int pos = mediaID.indexOf(LEAF_SEPARATOR); - if (pos >= 0) { - return mediaID.substring(pos+1); - } - return 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 that contains a category and categoryValue. - */ - public static String[] getHierarchy(String mediaID) { - int pos = mediaID.indexOf(LEAF_SEPARATOR); - if (pos >= 0) { - mediaID = mediaID.substring(0, pos); - } - return mediaID.split(String.valueOf(CATEGORY_SEPARATOR)); - } - - public static String extractBrowseCategoryValueFromMediaID(String mediaID) { - String[] hierarchy = getHierarchy(mediaID); - if (hierarchy != null && hierarchy.length == 2) { - return hierarchy[1]; - } - return null; - } - - private static boolean isBrowseable(String mediaID) { - return mediaID.indexOf(LEAF_SEPARATOR) < 0; - } - - public static String getParentMediaID(String mediaID) { - String[] hierarchy = getHierarchy(mediaID); - if (!isBrowseable(mediaID)) { - return createMediaID(null, hierarchy); - } - if (hierarchy == null || hierarchy.length <= 1) { - return MEDIA_ID_ROOT; - } - String[] parentHierarchy = Arrays.copyOf(hierarchy, hierarchy.length-1); - return createMediaID(null, parentHierarchy); - } -} diff --git a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/QueueHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/QueueHelper.java deleted file mode 100644 index 9a2caa8a..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/QueueHelper.java +++ /dev/null @@ -1,149 +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.mediabrowserservice.utils; - -import android.media.MediaMetadata; -import android.media.session.MediaSession; - -import com.example.android.mediabrowserservice.model.MusicProvider; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import static com.example.android.mediabrowserservice.utils.MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE; -import static com.example.android.mediabrowserservice.utils.MediaIDHelper.MEDIA_ID_MUSICS_BY_SEARCH; - -/** - * Utility class to help on queue related tasks. - */ -public class QueueHelper { - - private static final String TAG = LogHelper.makeLogTag(QueueHelper.class); - - public static List<MediaSession.QueueItem> getPlayingQueue(String mediaId, - MusicProvider musicProvider) { - - // extract the browsing hierarchy from the media ID: - String[] hierarchy = MediaIDHelper.getHierarchy(mediaId); - - if (hierarchy.length != 2) { - LogHelper.e(TAG, "Could not build a playing queue for this mediaId: ", mediaId); - return null; - } - - String categoryType = hierarchy[0]; - String categoryValue = hierarchy[1]; - LogHelper.d(TAG, "Creating playing queue for ", categoryType, ", ", categoryValue); - - Iterable<MediaMetadata> tracks = null; - // This sample only supports genre and by_search category types. - if (categoryType.equals(MEDIA_ID_MUSICS_BY_GENRE)) { - tracks = musicProvider.getMusicsByGenre(categoryValue); - } else if (categoryType.equals(MEDIA_ID_MUSICS_BY_SEARCH)) { - tracks = musicProvider.searchMusic(categoryValue); - } - - if (tracks == null) { - LogHelper.e(TAG, "Unrecognized category type: ", categoryType, " for mediaId ", mediaId); - return null; - } - - return convertToQueue(tracks, hierarchy[0], hierarchy[1]); - } - - public static List<MediaSession.QueueItem> getPlayingQueueFromSearch(String query, - MusicProvider musicProvider) { - - LogHelper.d(TAG, "Creating playing queue for musics from search ", query); - - return convertToQueue(musicProvider.searchMusic(query), MEDIA_ID_MUSICS_BY_SEARCH, query); - } - - - public static 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 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 List<MediaSession.QueueItem> convertToQueue( - Iterable<MediaMetadata> tracks, String... categories) { - List<MediaSession.QueueItem> queue = new ArrayList<>(); - int count = 0; - for (MediaMetadata track : tracks) { - - // We create a hierarchy-aware mediaID, so we know what the queue is about by looking - // at the QueueItem media IDs. - String hierarchyAwareMediaID = MediaIDHelper.createMediaID( - track.getDescription().getMediaId(), categories); - - MediaMetadata trackCopy = new MediaMetadata.Builder(track) - .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID) - .build(); - - // 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( - trackCopy.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 the provider used for fetching music. - * @return list containing {@link android.media.session.MediaSession.QueueItem}'s - */ - public static List<MediaSession.QueueItem> getRandomQueue(MusicProvider musicProvider) { - Iterator<String> genres = musicProvider.getGenres().iterator(); - if (!genres.hasNext()) { - return Collections.emptyList(); - } - String genre = genres.next(); - Iterable<MediaMetadata> tracks = musicProvider.getMusicsByGenre(genre); - - return convertToQueue(tracks, MEDIA_ID_MUSICS_BY_GENRE, genre); - } - - public static boolean isIndexPlayable(int index, List<MediaSession.QueueItem> queue) { - return (queue != null && index >= 0 && index < queue.size()); - } -} diff --git a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/ResourceHelper.java b/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/ResourceHelper.java deleted file mode 100644 index ed4dcd00..00000000 --- a/media/MediaBrowserService/Application/src/main/java/com/example/android/mediabrowserservice/utils/ResourceHelper.java +++ /dev/null @@ -1,53 +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.mediabrowserservice.utils; - -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.res.Resources; -import android.content.res.TypedArray; - -/** - * Generic reusable methods to handle resources. - */ -public class ResourceHelper { - /** - * Get a color value from a theme attribute. - * @param context used for getting the color. - * @param attribute theme attribute. - * @param defaultColor default to use. - * @return color value - */ - public static int getThemeColor(Context context, int attribute, int defaultColor) { - int themeColor = 0; - String packageName = context.getPackageName(); - try { - Context packageContext = context.createPackageContext(packageName, 0); - ApplicationInfo applicationInfo = - context.getPackageManager().getApplicationInfo(packageName, 0); - packageContext.setTheme(applicationInfo.theme); - Resources.Theme theme = packageContext.getTheme(); - TypedArray ta = theme.obtainStyledAttributes(new int[] {attribute}); - themeColor = ta.getColor(0, defaultColor); - ta.recycle(); - } catch (PackageManager.NameNotFoundException e) { - e.printStackTrace(); - } - return themeColor; - } -} |