Skip to content

Instantly share code, notes, and snippets.

@mpuz
mpuz / gist:81ff82e858776ec47b4255817f5f0a87
Created March 29, 2017 17:26
Android, Java: Get Your OWN IP ADDRESS
WifiManager wifiMan = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
int ipAddress = wifiInf.getIpAddress();
String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
@mpuz
mpuz / gist:6c99ab8bab30d567a04a7cef0265231c
Created March 29, 2017 17:28
Android, Java: Attenuate audio in decibels
public void AttenuateAudio(float[] data, int decibels)
{
float gain = (float)Math.Pow(10, (double)-decibels / 20.0);
for (int i = 0; i < data.Length; i++)
{
data[i] *= gain;
}
}
@mpuz
mpuz / gist:361ff33715849f58dd4d460661865cc5
Created March 29, 2017 17:30
Android, Java: JSON to String to Map to JSON
//Map to JSON:
Map map = new HashMap();
map.put("name", "foo");
map.put("nickname", "bar");
String jsonText = JSONValue.toJSONString(map);
//JSON to List/Map:
String s = yourJsonString;
List list = (JSONArray) JSONValue.parse(s);
@mpuz
mpuz / gist:aec73da6148f0c3049ccbb96544825d5
Created March 29, 2017 20:02
Android, Java: Base64 encode decode
// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
@mpuz
mpuz / gist:62ba1c934c3e959c64c28c6b3c16dbde
Created March 29, 2017 20:05
Android, Java: Heavy while loops threading
//While loop you are running is running on a default Thread termed as the ui thread so in short
//run while loops on separate threads.. eg..
new Thread(new Runnable() {
@Override
public void run() {
// Your hard while loop here
//get whatever you want and update your ui with ui communication methods.
}
).start();
@mpuz
mpuz / gist:4df99c57528725dbc8604bb67f4b2929
Created March 29, 2017 20:07
Android, Java: Gradients, banding
//Add the following override to your activity,
//This ought to fix the color banding issue you are seeing
//by setting the window format to support more colors.
@Override
public void onAttachedToWindow() {
getWindow().setFormat(PixelFormat.RGBA_8888);
}
//More info : http://developer.android.com/reference/android/view/Window.html#setFormat(int)
@mpuz
mpuz / gist:3d946ae784ed485476171b3e1b1467dd
Created March 29, 2017 20:09
Android, xml: Roboto type family
// From android 4.1 / 4.2, the following Roboto font families are available:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium" // roboto medium (android 5.0)
// in combination with this
android:textStyle="normal|bold|italic"
@mpuz
mpuz / gist:6a2fc5ce8d88a0a53a1766d66c493413
Created March 29, 2017 20:11
Android, Java: Multicast check
/** 
* Ensures that the caller receives an usable multicast socket. 
* In case the network configuration does not have a default gateway 
* set, the multicast socket might not work. This is the case when 
* the device is connected to a Wireless Direct printer. In that 
* cases, force a network interface into the socket. 
* @return A ready-to-use multicast socket. 
*/ 
public static MulticastSocket createMulticastSocket(Context context)
@mpuz
mpuz / gist:78e9e875df646698243affe1870dda58
Created March 29, 2017 20:14
Android, Java: mix 2 PCM audio files
private void mixSound() throws IOException {
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, 44100, AudioTrack.MODE_STREAM);
InputStream in1=getResources().openRawResource(R.raw.track1);
InputStream in2=getResources().openRawResource(R.raw.track2);
byte[] music1 = null;
music1= new byte[in1.available()];
music1=convertStreamToByteArray(in1);
in1.close();
@mpuz
mpuz / gist:1ca81ecfbbe39379f81db6a93ba0b8b5
Created March 29, 2017 20:16
Android, Java: Encrypt and decrypt strings using AES128
//Simple helper class to encrypt and decrypt strings using AES128.
//The result is Ascii-encoded (actually hex, no base64), so no byte[] has to be stored.
//A SEED value is used as a shared secret (“Master-Password”).
//Only with the same SEED the stored values can be decrypted.
package net.sf.andhsli.hotspotlogin;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;