android

Fix Xiaomi INSTALL_FAILED_USER_RESTRICTED

If you are developing android and using real device to install your build APK instead of virtual device and you are using Xiaomi phone (Mine is Xiaomi Redmi 4x) probably you will facing this error.

To fix this, what you need to do are just enable these configuration in your device.

  • Developer Mode
  • USB Debugging
  • Install via USB
  • USB debugging (Security Settings)

Reference: https://stackoverflow.com/a/49485273/1936697

Adding SSL Certificate to Retrofit 2

I assume that you already know how to get SSL certificate from your server and you already put it in android resource inside “raw” directory.

SS

SSL Certificate File

Here is how to read in in android and add to your retrofit.

private static SSLContext getSSLConfig(Context context) throws CertificateException, IOException,
            KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

    // Loading CAs from an InputStream
    CertificateFactory cf = null;
    cf = CertificateFactory.getInstance("X.509");

    Certificate ca;
    // I'm using Java7. If you used Java6 close it manually with finally.
    try (InputStream cert = context.getResources().openRawResource(R.raw.your_certificate)) {
        ca = cf.generateCertificate(cert);
    }

    // Creating a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore   = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Creating a TrustManager that trusts the CAs in our KeyStore.
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Creating an SSLSocketFactory that uses our TrustManager
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tmf.getTrustManagers(), null);

    return sslContext;
}

By default Retrofit using OkHttp as the http client library, and here is the last step.

Retrofit.Builder builder = new Retrofit.Builder().baseUrl(Constant.BASE_API_URL);

OkHttpClient okHttp = new OkHttpClient();
okHttp.setSslSocketFactory(getSSLConfig(contex).getSocketFactory());

Retrofit retrofit = builder.client(okHttp).build();
retrofit.create(serviceClass)

References:
http://developer.android.com/training/articles/security-ssl.html
http://stackoverflow.com/a/31436459/1936697

Android ListView Tips & Trick

I thought android listview is not really hard, but after dealing with listview with some advanced UI, listview become the new pain in the ass for me.

These are blog list that I found, easy to understand with good code and case example.
http://cyrilmottier.com/2011/06/20/listview-tips-tricks-1-handle-emptiness/
http://cyrilmottier.com/2011/07/05/listview-tips-tricks-2-section-your-listview/
http://cyrilmottier.com/2011/08/08/listview-tips-tricks-3-create-fancy-listviews/
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
http://cyrilmottier.com/2012/02/16/listview-tips-tricks-5-enlarged-touchable-areas/

Thanks to Cyril Mottier 😀

Resize Android Drawable

Here is a little hack to resize drawable in android.

// Get drawable that you need to resize.
Drawable drwA = getRessources().getDrawable(R.mipmap.my_icon);

// Convert to bitmap and resize to 50x50 px
Bitmap bitmap = ((BitmapDrawable) drwA).getBitmap();
Drawable newDrw = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));

Reference: http://stackoverflow.com/a/8655456/1936697