5 package com.andrew.apollo.utils;
\r
7 import java.io.IOException;
\r
8 import java.io.InputStream;
\r
9 import java.net.HttpURLConnection;
\r
10 import java.net.URL;
\r
12 import android.app.ActionBar;
\r
13 import android.content.Context;
\r
14 import android.content.Intent;
\r
15 import android.content.SharedPreferences;
\r
16 import android.content.res.Configuration;
\r
17 import android.graphics.Bitmap;
\r
18 import android.graphics.BitmapFactory;
\r
19 import android.graphics.Canvas;
\r
20 import android.graphics.Matrix;
\r
21 import android.graphics.drawable.AnimationDrawable;
\r
22 import android.graphics.drawable.BitmapDrawable;
\r
23 import android.graphics.drawable.Drawable;
\r
24 import android.net.ConnectivityManager;
\r
25 import android.net.NetworkInfo;
\r
26 import android.net.Uri;
\r
27 import android.provider.MediaStore.Audio;
\r
28 import android.support.v4.app.Fragment;
\r
29 import android.view.View;
\r
30 import android.widget.ImageView;
\r
31 import android.widget.ListView;
\r
32 import android.widget.TextView;
\r
33 import android.widget.Toast;
\r
35 import com.andrew.apollo.R;
\r
36 import com.androidquery.util.AQUtility;
\r
38 import static com.andrew.apollo.Constants.MIME_TYPE;
\r
41 * @author Andrew Neal
\r
42 * @Note Various methods used to help with specific Apollo statements
\r
44 public class ApolloUtils {
\r
47 * Used to fit a Bitmap nicely inside a View
\r
52 public static void setBackground(View view, Bitmap bitmap) {
\r
54 if (bitmap == null) {
\r
55 view.setBackgroundResource(0);
\r
59 int vwidth = view.getWidth();
\r
60 int vheight = view.getHeight();
\r
61 int bwidth = bitmap.getWidth();
\r
62 int bheight = bitmap.getHeight();
\r
64 float scalex = (float)vwidth / bwidth;
\r
65 float scaley = (float)vheight / bheight;
\r
66 float scale = Math.max(scalex, scaley) * 1.0f;
\r
68 Bitmap.Config config = Bitmap.Config.ARGB_8888;
\r
69 Bitmap background = Bitmap.createBitmap(vwidth, vheight, config);
\r
71 Canvas canvas = new Canvas(background);
\r
73 Matrix matrix = new Matrix();
\r
74 matrix.setTranslate(-bwidth / 2, -bheight / 2);
\r
75 matrix.postScale(scale, scale);
\r
76 matrix.postTranslate(vwidth / 2, vheight / 2);
\r
78 canvas.drawBitmap(bitmap, matrix, null);
\r
80 view.setBackgroundDrawable(new BitmapDrawable(view.getResources(), background));
\r
85 * @param bitmap This is to avoid Bitmap's IllegalArgumentException
\r
87 public static void runnableBackground(final ImageView view, final Bitmap bitmap) {
\r
88 view.post(new Runnable() {
\r
92 ApolloUtils.setBackground(view, bitmap);
\r
99 * @return whether there is an active data connection
\r
101 public static boolean isOnline(Context context) {
\r
102 boolean state = false;
\r
103 ConnectivityManager cm = (ConnectivityManager)context
\r
104 .getSystemService(Context.CONNECTIVITY_SERVICE);
\r
106 NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
\r
107 if (wifiNetwork != null) {
\r
108 state = wifiNetwork.isConnectedOrConnecting();
\r
111 NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
\r
112 if (mobileNetwork != null) {
\r
113 state = mobileNetwork.isConnectedOrConnecting();
\r
116 NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
\r
117 if (activeNetwork != null) {
\r
118 state = activeNetwork.isConnectedOrConnecting();
\r
124 * Sets cached image URLs
\r
126 * @param artistName
\r
131 public static void setImageURL(String name, String url, String key, Context context) {
\r
132 SharedPreferences settings = context.getSharedPreferences(key, 0);
\r
133 SharedPreferences.Editor editor = settings.edit();
\r
134 editor.putString(name, url);
\r
142 * @return cached image URLs
\r
144 public static String getImageURL(String name, String key, Context context) {
\r
145 SharedPreferences settings = context.getSharedPreferences(key, 0);
\r
146 return settings.getString(name, null);
\r
151 * @return if a Tablet is the device being used
\r
153 public static boolean isTablet(Context context) {
\r
154 return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
\r
158 * UP accordance without the icon
\r
162 public static void showUpTitleOnly(ActionBar actionBar) {
\r
163 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE,
\r
164 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
\r
165 | ActionBar.DISPLAY_SHOW_HOME);
\r
172 * @return a scaled Bitmap
\r
174 public static Bitmap getResizedBitmap(Bitmap bitmap, int newHeight, int newWidth) {
\r
176 if (bitmap == null) {
\r
180 int width = bitmap.getWidth();
\r
181 int height = bitmap.getHeight();
\r
182 float scaleWidth = ((float)newWidth) / width;
\r
183 float scaleHeight = ((float)newHeight) / height;
\r
184 Matrix matrix = new Matrix();
\r
185 matrix.postScale(scaleWidth, scaleHeight);
\r
186 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
\r
187 return resizedBitmap;
\r
191 * Header used in the track browser
\r
197 public static void listHeader(Fragment fragment, View view, String string) {
\r
198 if (fragment.getArguments() != null) {
\r
199 TextView mHeader = (TextView)view.findViewById(R.id.title);
\r
200 String mimetype = fragment.getArguments().getString(MIME_TYPE);
\r
201 if (Audio.Artists.CONTENT_TYPE.equals(mimetype)) {
\r
202 mHeader.setVisibility(View.VISIBLE);
\r
203 mHeader.setText(string);
\r
204 } else if (Audio.Albums.CONTENT_TYPE.equals(mimetype)) {
\r
205 mHeader.setVisibility(View.VISIBLE);
\r
206 mHeader.setText(string);
\r
212 * Sets the ListView paddingLeft for the header
\r
217 public static void setListPadding(Fragment fragment, ListView mListView, int left, int top,
\r
218 int right, int bottom) {
\r
219 if (fragment.getArguments() != null) {
\r
220 String mimetype = fragment.getArguments().getString(MIME_TYPE);
\r
221 if (Audio.Albums.CONTENT_TYPE.equals(mimetype)) {
\r
222 mListView.setPadding(AQUtility.dip2pixel(fragment.getActivity(), left), top,
\r
223 AQUtility.dip2pixel(fragment.getActivity(), right), bottom);
\r
224 } else if (Audio.Artists.CONTENT_TYPE.equals(mimetype)) {
\r
225 mListView.setPadding(AQUtility.dip2pixel(fragment.getActivity(), left), top,
\r
226 AQUtility.dip2pixel(fragment.getActivity(), right), bottom);
\r
231 // Returns if we're viewing an album
\r
232 public static boolean isAlbum(String mimeType) {
\r
233 return Audio.Albums.CONTENT_TYPE.equals(mimeType);
\r
236 // Returns if we're viewing an artists albums
\r
237 public static boolean isArtist(String mimeType) {
\r
238 return Audio.Artists.CONTENT_TYPE.equals(mimeType);
\r
241 // Returns if we're viewing a genre
\r
242 public static boolean isGenre(String mimeType) {
\r
243 return Audio.Genres.CONTENT_TYPE.equals(mimeType);
\r
247 * @param artistName
\r
252 public static void setArtistId(String artistName, long id, String key, Context context) {
\r
253 SharedPreferences settings = context.getSharedPreferences(key, 0);
\r
254 SharedPreferences.Editor editor = settings.edit();
\r
255 editor.putLong(artistName, id);
\r
260 * @param artistName
\r
263 * @return artist ID
\r
265 public static Long getArtistId(String artistName, String key, Context context) {
\r
266 SharedPreferences settings = context.getSharedPreferences(key, 0);
\r
267 return settings.getLong(artistName, 0);
\r
271 * @param artistName
\r
273 public static void shopFor(Context mContext, String artistName) {
\r
274 String str = "https://market.android.com/search?q=%s&c=music&featured=MUSIC_STORE_SEARCH";
\r
275 Intent shopIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(str,
\r
276 Uri.encode(artistName))));
\r
277 shopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
\r
278 shopIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
\r
279 mContext.startActivity(shopIntent);
\r
284 * @return Bitmap fro URL
\r
286 public static Bitmap getBitmapFromURL(String src) {
\r
288 URL url = new URL(src);
\r
289 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
\r
290 connection.setDoInput(true);
\r
291 connection.connect();
\r
292 InputStream input = connection.getInputStream();
\r
293 Bitmap myBitmap = BitmapFactory.decodeStream(input);
\r
295 } catch (IOException e) {
\r
296 e.printStackTrace();
\r
304 public static void showToast(int message, Toast mToast, Context context) {
\r
305 if (mToast == null) {
\r
306 mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
\r
308 mToast.setText(context.getString(message));
\r
316 public static AnimationDrawable getNyanCat(Context context) {
\r
317 final AnimationDrawable animation = new AnimationDrawable();
\r
318 for (int i = 0; i < 12; i++) {
\r
320 animation.addFrame(Drawable.createFromStream(
\r
321 context.getAssets().open("Frame" + i + ".png"), null), 75);
\r
322 } catch (IOException e) {
\r
325 animation.setOneShot(false);
\r