package com.feebe.lib; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Random; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.DialogInterface.OnCancelListener; import android.content.SharedPreferences.Editor; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.database.Cursor; import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.Build; import android.provider.Contacts; import android.provider.MediaStore; import android.provider.Contacts.People; import android.util.Log; public class Util { //private static final String urlString = " http://ggapp.appspot.com/mobile/getfeed/"; private static final String feedsFile = "feeds"; //private static Random generator = new Random(); public static void runFeed(int chance, Activity at, int resource) { //if (run(chance)) { // getFeeds(at, resource, urlString); //} } /* private static boolean run(int chance) { int t = generator.nextInt(); return t % chance == 0; } */ private static void downloadRandom(final String urlStr) { (new Thread() { public void run() { saveDownload(urlStr, Const.homedir + feedsFile); } }).start(); } private static final String UNUSED1 = "http://"; private static final String UNUSED2 = "http://ggapp.appspot.com/"; public static String getHashcode(String str) { String s; if (str.startsWith(UNUSED2)) { s = str.substring(UNUSED2.length()); } else if (str.startsWith(UNUSED1)) { s = str.substring(UNUSED1.length()); } else { s = str; } int hash = s.hashCode(); return String.valueOf(hash); } public static JSONArray getJsonArrayFromUrl(String url, long expire) { File cache = null; String data = null; boolean inCache = false; if (expire > 0) { cache = new File(Const.cachedir, getHashcode(url)); inCache = inCache(url, expire, cache); if (inCache) { data = readFile(cache); } } if (data == null) { data = Util.download(url); } if (data != null) { // Log.e("del", data); try { JSONArray entries; entries = new JSONArray(data); int len = entries.length(); if (len > 0) { if (expire > 0) Util.saveFile(data, cache); return entries; } } catch (JSONException e) { if (inCache && cache != null) { // // Log.e("del", url + " " + data); if (cache.delete()) { // // Log.e("del", "succeed"); return getJsonArrayFromUrl(url, expire); } } } } //Toast.makeText(Ring.main, R.string.no_result,Toast.LENGTH_SHORT).show(); return null; } public static List dedup(List entries) { if(entries == null) { return null; } //combine the same List newList = new ArrayList(entries.size()); Hashtable newListHt= new Hashtable(); for(int i = 0; i < entries.size(); i++) { JSONObject jObject = (JSONObject) (entries.get(i)); String title = null; String artist = null; int rating = -1; try { if(jObject.has("song")) title = jObject.getString("song"); if(jObject.has("title")) title = jObject.getString("title"); if(jObject.has("artist")) artist = jObject.getString("artist"); if(jObject.has("rating")) rating = Integer.parseInt(jObject.getString("rating")); } catch (JSONException e) { e.printStackTrace(); } if(newListHt.size() == 0) { newListHt.put(artist+title, jObject); } else { boolean in = false; if(newListHt.containsKey(artist+title)) { in = true; JSONObject oldjObject = (JSONObject) newListHt.get(artist+title); int oldRating = -1; try { if(oldjObject.has("rating")) oldRating = Integer.parseInt(oldjObject.getString("rating")); } catch (NumberFormatException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } if(rating > oldRating) { //remove the one with low rating newListHt.remove(artist+title); in = false; } } if(!in) newListHt.put(artist+title, jObject); } } //add elements in hash table to newList Iterator<JSONObject> it = newListHt.values().iterator(); while(it.hasNext()) { newList.add(it.next()); } //list size no less than 10 if(newList.size() < 10) { for(int i = 0; i < entries.size(); i++) { JSONObject o = (JSONObject) (entries.get(i)); if(!newList.contains(o)) newList.add(o); if(newList.size() > 9) break; } } return newList; } public static boolean getloc(Context context) { if (hasKey("pp")) { return true; } LocationManager mLocationManager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE); if (mLocationManager == null) { return true; } Location loc = mLocationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER); if (loc == null) { return true; } double lat = loc.getLatitude(); double lng = loc.getLongitude(); if (lat == 0 || lng == 0) return true; boolean isin = in(lat, lng); if (isin) { setBoolKey("pp"); } return isin; } private static boolean in(double lat, double lng) { // 41,115 39 117 37 137 34 140 return (lat < 41 && lng > 115 && lat > 39 && lng < 117) || (lat < 37 && lng > 137 && lat > 34 && lng < 140); } public static JSONObject getJsonFromUrl(String url, long expire) { File cache = null; String data = null; if (expire > 0) { cache = new File(Const.cachedir, getHashcode(url)); if (inCache(url, expire, cache)) { data = readFile(cache); } } boolean newDownload = data == null || data.length() == 0; if (newDownload) { data = Util.download(url); } if (data != null) { try { JSONObject obj; obj = new JSONObject(data); if (obj != null) { if (expire > 0 && newDownload) Util.saveFile(data, cache); return obj; } } catch (JSONException e) { Log.e("getJsonFromUrl", e.getMessage()); } } Const.noResultToast(); return null; } public static String downloadAndCache(String url, long expire) { String httpresponse = null; boolean inCache = inCache(url, expire); if (inCache) { return readFile(Const.cachedir+Util.getHashcode(url)); } else { httpresponse = Util.download(url); } if (httpresponse!=null && httpresponse.length()>0) { saveFileInThread(httpresponse, Const.cachedir+Util.getHashcode(url)); } return httpresponse; } public static boolean inCache(String urlStr, long expire) { File cache = null; if (expire > 0) { cache = new File(Const.cachedir, getHashcode(urlStr)); if (inCache(urlStr, expire, cache)) { return true; } } return false; } public static boolean inCache(String urlStr, long expire, File cache) { if (cache.exists()) { long lastModify = cache.lastModified(); if (System.currentTimeMillis() - lastModify < expire) { return true; } } return false; } /* * expire <= 0 means do not cache it * caller should save cache file * popup.saveCacheFile(data, url); * because caller maybe asyn, and need a quick fix right now * so not yet have a good way to handle this. * TODO: make donwload save file only when the file is good. */ public static String download(String urlStr) { URL url = null; HttpURLConnection urlConn = null; InputStream stream = null; InputStreamReader is = null; try { url = new URL(urlStr); urlConn = (HttpURLConnection)url.openConnection(); //urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3 -Java"); urlConn.setConnectTimeout(4000); urlConn.connect(); stream = urlConn.getInputStream(); StringBuilder builder = new StringBuilder(4096); char[] buff = new char[4096]; is = new InputStreamReader(stream); int len; while ((len = is.read(buff)) > 0) { builder.append(buff, 0, len); } urlConn.disconnect(); String content = builder.toString(); return content; } catch (IOException e) { // // // // Log.e("download", e.getMessage()); return null; } } public static String downloadFile(String urlStr, long expire) throws IOException { File cache = null; String filename = null; filename =Const.cachedir + getHashcode(urlStr); cache = new File(filename); if (cache.exists()) { long lastModify = cache.lastModified(); if (System.currentTimeMillis() - lastModify < expire) { return filename; } } URL imageURL = new URL(urlStr); URLConnection conn = imageURL.openConnection(); conn.setConnectTimeout(4000); conn.connect(); InputStream in = conn.getInputStream(); saveFile(in, cache); return filename; } public static File download(String urlStr, String name) { URL url = null; HttpURLConnection urlConn = null; InputStream stream = null; DataInputStream is = null; try { url = new URL(urlStr); urlConn = (HttpURLConnection)url.openConnection(); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3 -Java"); urlConn.setConnectTimeout(4000); urlConn.connect(); stream = urlConn.getInputStream(); byte[] buff = new byte[4096]; is = new DataInputStream(stream); int len; File f = new File(name); FileOutputStream file = new FileOutputStream(f); while ((len = is.read(buff)) > 0) { file.write(buff, 0, len); } urlConn.disconnect(); return f; } catch (IOException e) { // // Log.e("download", e.getMessage()); } return null; } public static boolean saveFile(String content, String name) { try { FileOutputStream file = new FileOutputStream(name); file.write(content.getBytes()); // feeds.writeBytes(httpresponse); return true; } catch (IOException e) { return false; } } private static void saveFile(InputStream in, File name) throws IOException { name.createNewFile(); FileOutputStream file = new FileOutputStream(name); byte[] buff = new byte[4096]; int len; while ((len = in.read(buff)) > 0) { file.write(buff, 0, len); } } public static void saveFile(String content, File name) { try { name.createNewFile(); FileOutputStream file = new FileOutputStream(name); file.write(content.getBytes()); } catch (IOException e) { // // Log.e("saveFile", e.getMessage() + " file "+ name.getAbsolutePath() + " cache dir " + Const.cachedir); } } public static void saveFileInThread(final String content, final String name) { Thread th = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub saveFile(content, name); } }); th.start(); } private static boolean saveDownload(String urlStr, String name) { try { String httpresponse = download(urlStr); if (httpresponse == null) return false; FileOutputStream file = new FileOutputStream(name); file.write(httpresponse.getBytes()); // feeds.writeBytes(httpresponse); return true; } catch (IOException e) { //ShowToastMessage("get feeds error: network"); e.printStackTrace(); return false; } } public static String title; public static String des; public static Uri intent; public static String finalIntent = "market://search?q=pub:mobileworld"; public static boolean has(String n1, Context ct) { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final PackageManager manager = ct.getPackageManager(); // TODO manager.getApplicationInfo final List<ResolveInfo> apps = manager.queryIntentActivities( mainIntent, 0); for (int i = 0; i < apps.size(); i++) { ResolveInfo info = apps.get(i); if (info.activityInfo.applicationInfo.packageName.equals(n1)) { return true; } } return false; } public static String readFile(String name) { return readFile(new File(name)); } public static String readFile(File name) { try { InputStreamReader f = new InputStreamReader(new FileInputStream(name)); StringBuilder builder = new StringBuilder(4096); char[] buff = new char[4096]; int len; while ((len = f.read(buff)) > 0) { builder.append(buff, 0, len); } return builder.toString(); } catch (Exception e) { // // // Log.e("readFile", e.getMessage()); } return null; } static SharedPreferences mSetting; private static String RATING = "rate"; public static String FULLSEARCH = "com.jokes.search"; public static boolean hasRate() { return hasKey(RATING); } public static void startRate(Activity act) { setBoolKey(RATING); startMarket(act, act.getPackageName()); } public static void startMarket(Activity act, String pkg) { Intent i = new Intent( Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg)); act.startActivity(i); } public static boolean hasKey(String key) { return mSetting.getBoolean(key, false); } public static void setBoolKey(String key) { if (mSetting != null) { Editor e = mSetting.edit(); e.putBoolean(key, true); e.commit(); } } public static final int DOWNLOAD_APP_DIG = 10000; public static boolean getFeeds(Activity at, InputStream feeds) { StringBuilder builder; try { InputStreamReader r = new InputStreamReader(feeds); char[] buf = new char[4096]; int len; builder = new StringBuilder(4096); while ((len = r.read(buf)) > 0) { builder.append(buf,0, len); } } catch (Exception e) { return false; } try { String json = builder.toString(); JSONArray entries = new JSONArray(json); int len = entries.length() - 1; boolean showDialog = false; for(int i = 0; i < len; i++){ if( entries.isNull(i) ) break; JSONObject mp3 = entries.getJSONObject(i); String uri = mp3.getString("uri"); // market://search?q=pname: // market://search?q= 18 String pkg; if (uri.charAt(20) == ':') { pkg = uri.substring(21); } else if (uri.charAt(23) == ':') { pkg = uri.substring(24); } else { pkg = uri.substring(18); } if(has(pkg, at)) continue; // see if we save it before if (hasKey(pkg)) { continue; } title = mp3.getString("name"); if (uri.contains("Ringdroid") && getloc(at)) { continue; } des = mp3.getString("descript"); intent = Uri.parse(uri); setBoolKey(pkg); at.showDialog(DOWNLOAD_APP_DIG); showDialog = true; break; } // now handle last one for the my app intent // //if (!entries.isNull(len)) { // JSONObject mp3 = entries.getJSONObject(len); // finalIntent = mp3.getString("uri"); //} return showDialog; } catch (JSONException e) { return false; } } public static boolean getFeeds(Activity at, int resource, String urlStr) { //downloadRandom(urlStr); // if we have feedsFile then read it, otherwise read from resource InputStream feeds; //try { //if (run(5)) { // feeds = new FileInputStream(Const.homedir + feedsFile); //} else { feeds = at.getResources().openRawResource(resource); //} //} catch (FileNotFoundException e) { // feeds = at.getResources().openRawResource(resource); //} return getFeeds(at, feeds); } public static Dialog createDownloadDialog(final Activity at) { if (intent == null) { intent = Uri.parse("market://search?q=pub:mobileworld"); } return new AlertDialog.Builder(at) .setTitle(title) .setMessage(des).setPositiveButton("Download", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { try { Intent i = new Intent( Intent.ACTION_VIEW, intent); at.startActivity(i); at.removeDialog(DOWNLOAD_APP_DIG); } catch (Exception e) { } } }).setNegativeButton("Ignore Forever", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { at.removeDialog(DOWNLOAD_APP_DIG); } }).setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub at.removeDialog(DOWNLOAD_APP_DIG); } }).create(); } public static Uri saveToMediaLib(String title, String outPath, long length, String artist, int[] fileKind, ContentResolver cr) { String mimeType = "audio/mpeg"; ContentValues values = new ContentValues(); // // // Log.e("save", " title " + title + " out " + outPath + " artist " + artist); values.put(MediaStore.MediaColumns.DATA, outPath); values.put(MediaStore.MediaColumns.TITLE, title); values.put(MediaStore.MediaColumns.SIZE, length); values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); values.put(MediaStore.Audio.Media.ARTIST, artist); //values.put(MediaStore.Audio.Media.DURATION, duration); for(int i = 0; i < fileKind.length; i++) { int kind = fileKind[i]; if (kind == Const.FILE_KIND_RINGTONE) values.put(MediaStore.Audio.Media.IS_RINGTONE, true); else if (kind == Const.FILE_KIND_NOTIFICATION) values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); else if (kind == Const.FILE_KIND_ALARM) values.put(MediaStore.Audio.Media.IS_ALARM, true); else if (kind == Const.FILE_KIND_MUSIC) values.put(MediaStore.Audio.Media.IS_MUSIC,true); } // Insert it into the database Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath); try { final Uri newUri = cr.insert(uri, values); // // // Log.e("save", " ok "); // TODO: do we need this? //setResult(RESULT_OK, new Intent().setData(newUri)); return newUri; } catch (Exception e) { } return null; } public static void post(String url, String data) { try { // Construct data // Send data URL u = new URL(url); // // Log.e("conn", data); HttpURLConnection conn = (HttpURLConnection)u.openConnection(); conn.setConnectTimeout(4000); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setReadTimeout(1000); conn.connect(); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { } wr.close(); rd.close(); } catch (Exception e) { } } public static boolean isEclairOrLater() { return Build.VERSION.SDK.compareTo("5") >=0; } public static boolean isCupcakeOrBefore() { return Build.VERSION.SDK.compareTo("3") <=0; } public static ArrayList<String> getFriendList(Context ctx) { ArrayList<String> friendList = new ArrayList<String>(); ContentResolver cr = ctx.getContentResolver(); String columns[] = new String[]{People._ID, People.NAME}; Cursor cursor = cr.query(People.CONTENT_URI, columns, null, null, People.NAME); if (cursor.moveToFirst()) { Cursor newCursor = null; do { //String name = cursor.getString(cursor.getColumnIndex(People.NAME)); //String id = cursor.getString(cursor.getColumnIndex(People._ID)); long peopleId = cursor.getLong(cursor.getColumnIndex(People._ID)); String[] projection = new String[]{Contacts.ContactMethods._ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA }; newCursor = cr.query(Contacts.ContactMethods.CONTENT_URI, projection, Contacts.ContactMethods.PERSON_ID + "=\'" + peopleId + "\'", null, null); if(newCursor == null) continue; String email = ""; if (newCursor.moveToFirst()) { email = newCursor.getString(newCursor.getColumnIndex(Contacts.ContactMethods.DATA)); } if (email.length() > 0 && email.endsWith("gmail.com")) { //// Log.e("add email: ", email); friendList.add(email); } if (newCursor != null) newCursor.close(); } while (cursor.moveToNext()); } if (cursor != null) cursor.close(); return friendList; } public static void addNotification(Context _context, Intent intent, String title, int resTitle, int resText, int resExpandedTitle, int resExpandedText) { String tickerText ="\""+title+"\""+ _context.getString(resTitle); long when = System.currentTimeMillis(); Notification notification = new Notification(com.feebe.rings.R.drawable.ring, tickerText, when); Context context = _context.getApplicationContext(); String expandedText ="\""+title+"\""+ context.getString(resExpandedText); String expandedTitle = context.getString(resExpandedTitle); //Intent intent = new Intent(RingActivity.this, RingdroidSelectActivity.class); PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent); notification.flags |= Notification.FLAG_AUTO_CANCEL; NotificationManager notificationManager; notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); int notificationRef = 1; notificationManager.notify(notificationRef++, notification); } }