2013-04-05

Bypass exception for developing purpose "Trust anchor for certification path not found"

Original: http://stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection

Dùng bypass exception verify SSL (develop only) trong trường hợp gặp lỗi do cert ko match. Gọi trước khi execute HTTP request


/**
 * Trust every server - dont check for any certificate
 */
private static void trustAllHosts() {
       // Create a trust manager that does not validate certificate chains
       TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                     return new java.security.cert.X509Certificate[] {};
              }

              public void checkClientTrusted(X509Certificate[] chain,
                           String authType) throws CertificateException {
              }

              public void checkServerTrusted(X509Certificate[] chain,
                           String authType) throws CertificateException {
              }
       } };

       // Install the all-trusting trust manager
       try {
              SSLContext sc = SSLContext.getInstance("TLS");
              sc.init(null, trustAllCerts, new java.security.SecureRandom());
              HttpsURLConnection
                           .setDefaultSSLSocketFactory(sc.getSocketFactory());

              HttpsURLConnection
                           .setDefaultHostnameVerifier(new HostnameVerifier() {
                                  @Override
                                  public boolean verify(String hostname,
                                                SSLSession session) {
                                         return true;
                                  }
                           });
       } catch (Exception e) {
              e.printStackTrace();
       }
}

2013-04-02

ActionBar one-line with tabs

Dùng tabs embedded để ép ActionBar hiển thị trên 1 dòng

Bài viết trên stackoverflow
http://stackoverflow.com/questions/12392541/replicate-actionbar-tabs-with-custom-view/12703960#12703960

Trên Google Group
https://groups.google.com/forum/#!topic/actionbarsherlock/hmmB1JqDeCk

Code dùng reflection


// Hide the home title
ActionBar bar = getSupportActionBar();
bar.setDisplayShowTitleEnabled(false);
// bar.setDisplayShowHomeEnabled(false);

// pre-ICS
if (bar instanceof ActionBarImpl) {
       enableEmbeddedTabs(bar);

       // ICS and forward
} else if (bar instanceof ActionBarWrapper) {
       try {
              Field actionBarField = bar.getClass().getDeclaredField(
                           "mActionBar");
              actionBarField.setAccessible(true);
              enableEmbeddedTabs(actionBarField.get(bar));
       } catch (Exception e) {
              LogUtils.e(TAG, "Error enabling embedded tabs", e);
       }
}