Skip to content

Commit

Permalink
Fix #450: NPE on initializing OkHttpClient, also remove Async behavio…
Browse files Browse the repository at this point in the history
…r on controller init.
  • Loading branch information
nilsbraden committed May 28, 2021
1 parent 292299e commit 9578020
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 59 deletions.
107 changes: 49 additions & 58 deletions ttrssreader/src/main/java/org/ttrssreader/controllers/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,72 +219,63 @@ public void initialize(final Context context) {
return;

synchronized (lockInitialize) {

if (initialized)
return;

initialized = true;

/* Attempt to initialize some stuff in a background-thread to reduce loading time. Start a login-request
separately because this takes some time. Also initialize SSL-Stuff since the login needs this. */
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

if (Controller.getInstance().useKeystore()) {
try {
Log.i(TAG, "initialize SSL: Trust certificates from keystore");
SSLUtils.initPrivateKeystore(Controller.getInstance().getKeystorePassword());
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_Keystore);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else if (Controller.getInstance().trustAllSsl()) {
try {
Log.i(TAG, "initialize SSL: Trust all certificates");
SSLUtils.trustAllCert();
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_TrustAllHosts);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else if (useClientCertificate()) {
try {
Log.i(TAG, "initialize SSL: Trust only client certificates");
SSLUtils.trustClientCert();
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_TrustClientCerts);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else {
try {
Log.i(TAG, "initialize SSL: Normal certificate-checks");
SSLUtils.initSslSocketFactory(null, null);
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_SocketFactory);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}
if (Controller.getInstance().useKeystore()) {
try {
Log.i(TAG, "initialize SSL: Trust certificates from keystore");
SSLUtils.initPrivateKeystore(Controller.getInstance().getKeystorePassword());
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_Keystore);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else if (Controller.getInstance().trustAllSsl()) {
try {
Log.i(TAG, "initialize SSL: Trust all certificates");
SSLUtils.trustAllCert();
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_TrustAllHosts);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else if (useClientCertificate()) {
try {
Log.i(TAG, "initialize SSL: Trust only client certificates");
SSLUtils.trustClientCert();
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_TrustClientCerts);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
} else {
try {
Log.i(TAG, "initialize SSL: Normal certificate-checks");
SSLUtils.initSslSocketFactory(null, null);
} catch (GeneralSecurityException e) {
String msg = context.getString(R.string.Error_SSL_SocketFactory);
Log.e(TAG, msg, e);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
}

if (Controller.getInstance().trustAllHosts()) {
Log.i(TAG, "initialize SSL: Ignore if Certificate matches host");
SSLUtils.trustAllHost();
}
if (Controller.getInstance().trustAllHosts()) {
Log.i(TAG, "initialize SSL: Ignore if Certificate matches host");
SSLUtils.trustAllHost();
}

// Only need this once we are displaying the feed-list or an article...
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
refreshDisplayMetrics(wm.getDefaultDisplay());
}
// Only need this once we are displaying the feed-list or an article...
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
refreshDisplayMetrics(wm.getDefaultDisplay());
}

enableHttpResponseCache(context.getCacheDir());
enableHttpResponseCache(context.getCacheDir());

return null;
}
}.execute();
initialized = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private void initHttpClient() {

// Build Client-Object:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
clientBuilder.sslSocketFactory(SSLUtils.factory);
if (SSLUtils.factory != null)
clientBuilder.sslSocketFactory(SSLUtils.factory);
clientBuilder.proxy(getProxy());
clientBuilder.readTimeout(10, timeoutUnit);
this.client = clientBuilder.build();
Expand Down

0 comments on commit 9578020

Please sign in to comment.