public static string GetMD5 (Java.Lang.String s) { try { // Create MD5 Hash var digest = Java.Security.MessageDigest.GetInstance ("MD5"); digest.Update (s.GetBytes ()); var messageDigest = digest.Digest (); // Create Hex String var hexString = new Java.Lang.StringBuffer (); for (int i = 0; i < messageDigest.Length; i++) { var h = Java.Lang.Integer.ToHexString (0xFF & messageDigest [i]); while (h.Length < 2) { h = "0" + h; } hexString.Append (h); } return hexString.ToString (); } catch (Java.Security.NoSuchAlgorithmException ex) { Log.Debug (TAG, ex.StackTrace); } return ""; }
/** * Only call this method from the main (UI) thread. The {@link OnFetchCompleteListener} callback * be invoked on the UI thread, but image fetching will be done in an {@link AsyncTask}. * * @param cookie An arbitrary object that will be passed to the callback. */ public static void FetchImage(Context context, Java.Lang.String url, BitmapFactory.Options decodeOptions, Java.Lang.Object cookie, Action<Bitmap> callback) { Task.Factory.StartNew(() => { if(TextUtils.IsEmpty(url)) { result = null; return; } File cacheFile = null; try { MessageDigest mDigest = MessageDigest.GetInstance("SHA-1"); mDigest.Update(url.GetBytes()); string cacheKey = BytesToHexString(mDigest.Digest()); if(Environment.MediaMounted.Equals(Environment.ExternalStorageState)) { cacheFile = new File(Environment.ExternalStorageDirectory + File.Separator + "Android " + File.Separator + "data" + File.Separator + context.PackageName + File.Separator + "cache" + File.Separator + "bitmap_" + cacheKey + ".tmp"); } } catch (Exception e) { // NoSuchAlgorithmException // Oh well, SHA-1 not available (weird), don't cache bitmaps. } if (cacheFile != null && cacheFile.Exists()) { Bitmap cachedBitmap = BitmapFactory.DecodeFile(cacheFile.ToString(), decodeOptions); if (cachedBitmap != null) { result = cachedBitmap; return; } } try { // TODO: check for HTTP caching headers var client = new System.Net.WebClient(); var image = client.DownloadData(new Uri(url.ToString())); if (image != null) { result = null; return; } // Write response bytes to cache. if (cacheFile != null) { try { cacheFile.ParentFile.Mkdirs(); cacheFile.CreateNewFile(); FileOutputStream fos = new FileOutputStream(cacheFile); fos.Write(image); fos.Close(); } catch (FileNotFoundException e) { Log.Warn(TAG, "Error writing to bitmap cache: " + cacheFile.ToString(), e); } catch (IOException e) { Log.Warn(TAG, "Error writing to bitmap cache: " + cacheFile.ToString(), e); } } // Decode the bytes and return the bitmap. result = (BitmapFactory.DecodeByteArray(image, 0, image.Length, decodeOptions)); return; } catch (Exception e) { Log.Warn(TAG, "Problem while loading image: " + e.ToString(), e); } result = null; }) .ContinueWith(task => callback(result) ); }
/// <summary> /// Sends a message. /// </summary> /// <param name='message'> /// A string of text to send. /// </param> private void SendMessage (Java.Lang.String message) { // Check that we're actually connected before trying anything if (chatService.GetState () != BluetoothChatService.STATE_CONNECTED) { Toast.MakeText (this, Resource.String.not_connected, ToastLength.Short).Show (); return; } // Check that there's actually something to send if (message.Length () > 0) { // Get the message bytes and tell the BluetoothChatService to write byte[] send = message.GetBytes (); chatService.Write (send); // Reset out string buffer to zero and clear the edit text field outStringBuffer.SetLength (0); outEditText.Text = string.Empty; } }