Make singletons threadsafe. (#838)

Merge branch 'master' into threadsafe-singletons

Make singletons threadsafe.

Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/838
Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
Co-Authored-By: opyale <opyale@noreply.codeberg.org>
Co-Committed-By: opyale <opyale@noreply.codeberg.org>
This commit is contained in:
opyale 2021-03-03 15:10:23 +01:00 committed by M M Arif
parent 77e39952c1
commit ff915f1813
7 changed files with 51 additions and 26 deletions

View File

@ -150,7 +150,7 @@ public class CreateFileActivity extends BaseActivity {
disableProcessButton(); disableProcessButton();
NetworkStatusObserver networkStatusObserver = NetworkStatusObserver.get(ctx); NetworkStatusObserver networkStatusObserver = NetworkStatusObserver.getInstance(ctx);
networkStatusObserver.registerNetworkStatusListener(hasNetworkConnection -> newFileCreate.setEnabled(hasNetworkConnection)); networkStatusObserver.registerNetworkStatusListener(hasNetworkConnection -> newFileCreate.setEnabled(hasNetworkConnection));
newFileCreate.setOnClickListener(createFileListener); newFileCreate.setOnClickListener(createFileListener);

View File

@ -61,7 +61,7 @@ public class LoginActivity extends BaseActivity {
ActivityLoginBinding activityLoginBinding = ActivityLoginBinding.inflate(getLayoutInflater()); ActivityLoginBinding activityLoginBinding = ActivityLoginBinding.inflate(getLayoutInflater());
setContentView(activityLoginBinding.getRoot()); setContentView(activityLoginBinding.getRoot());
NetworkStatusObserver networkStatusObserver = NetworkStatusObserver.get(ctx); NetworkStatusObserver networkStatusObserver = NetworkStatusObserver.getInstance(ctx);
loginButton = activityLoginBinding.loginButton; loginButton = activityLoginBinding.loginButton;
instanceUrlET = activityLoginBinding.instanceUrl; instanceUrlET = activityLoginBinding.instanceUrl;

View File

@ -19,7 +19,7 @@ import okhttp3.OkHttpClient;
public class PicassoService { public class PicassoService {
private static PicassoService picassoService; private static volatile PicassoService picassoService;
private final File cachePath; private final File cachePath;
private Picasso picasso; private Picasso picasso;
@ -40,12 +40,12 @@ public class PicassoService {
.hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier())); .hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()));
builder.downloader(new OkHttp3Downloader(okHttpClient.build())); builder.downloader(new OkHttp3Downloader(okHttpClient.build()));
builder.listener((picasso, uri, exception) -> { // builder.listener((picasso, uri, exception) -> {
//
//Log.e("PicassoService", Objects.requireNonNull(uri.toString())); // Log.e("PicassoService", Objects.requireNonNull(uri.toString()));
//Log.e("PicassoService", exception.toString()); // Log.e("PicassoService", exception.toString());
//
}); // });
picasso = builder.memoryCache(new PicassoCache(cachePath, context)).build(); picasso = builder.memoryCache(new PicassoCache(cachePath, context)).build();
@ -66,10 +66,15 @@ public class PicassoService {
public static synchronized PicassoService getInstance(Context context) { public static synchronized PicassoService getInstance(Context context) {
if(picassoService == null) { if(picassoService == null) {
picassoService = new PicassoService(context); synchronized(PicassoService.class) {
if(picassoService == null) {
picassoService = new PicassoService(context);
}
}
} }
return picassoService; return picassoService;
} }
} }

View File

@ -96,30 +96,38 @@ public class RetrofitClient {
} }
public static ApiInterface getApiInterface(Context context, String url) { public static ApiInterface getApiInterface(Context context, String url) {
if(!apiInterfaces.containsKey(url)) { if(!apiInterfaces.containsKey(url)) {
synchronized(RetrofitClient.class) {
if(!apiInterfaces.containsKey(url)) {
ApiInterface apiInterface = createRetrofit(context, url) ApiInterface apiInterface = createRetrofit(context, url).create(ApiInterface.class);
.create(ApiInterface.class); apiInterfaces.put(url, apiInterface);
apiInterfaces.put(url, apiInterface);
return apiInterface;
return apiInterface;
}
}
} }
return apiInterfaces.get(url); return apiInterfaces.get(url);
} }
public static WebInterface getWebInterface(Context context, String url) { public static WebInterface getWebInterface(Context context, String url) {
if(!webInterfaces.containsKey(url)) { if(!webInterfaces.containsKey(url)) {
synchronized(RetrofitClient.class) {
if(!webInterfaces.containsKey(url)) {
WebInterface webInterface = createRetrofit(context, url) WebInterface webInterface = createRetrofit(context, url).create(WebInterface.class);
.create(WebInterface.class); webInterfaces.put(url, webInterface);
webInterfaces.put(url, webInterface);
return webInterface;
return webInterface;
}
}
} }
return webInterfaces.get(url); return webInterfaces.get(url);
} }
} }

View File

@ -57,7 +57,7 @@ public class AppUtil {
public static boolean hasNetworkConnection(Context context) { public static boolean hasNetworkConnection(Context context) {
return NetworkStatusObserver.get(context).hasNetworkConnection(); return NetworkStatusObserver.getInstance(context).hasNetworkConnection();
} }
public static int getAppBuildNo(Context context) { public static int getAppBuildNo(Context context) {

View File

@ -15,7 +15,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/ */
public class NetworkStatusObserver { public class NetworkStatusObserver {
private static NetworkStatusObserver networkStatusObserver; private static volatile NetworkStatusObserver networkStatusObserver;
private final AtomicBoolean hasNetworkConnection = new AtomicBoolean(false); private final AtomicBoolean hasNetworkConnection = new AtomicBoolean(false);
private final List<NetworkStatusListener> networkStatusListeners = new ArrayList<>(); private final List<NetworkStatusListener> networkStatusListeners = new ArrayList<>();
@ -98,12 +98,18 @@ public class NetworkStatusObserver {
public interface NetworkStatusListener { void onNetworkStatusChanged(boolean hasNetworkConnection); } public interface NetworkStatusListener { void onNetworkStatusChanged(boolean hasNetworkConnection); }
public static NetworkStatusObserver get(Context context) { public static NetworkStatusObserver getInstance(Context context) {
if(networkStatusObserver == null) { if(networkStatusObserver == null) {
networkStatusObserver = new NetworkStatusObserver(context); synchronized(NetworkStatusObserver.class) {
if(networkStatusObserver == null) {
networkStatusObserver = new NetworkStatusObserver(context);
}
}
} }
return networkStatusObserver; return networkStatusObserver;
} }
} }

View File

@ -15,7 +15,7 @@ import java.util.Map;
public class TinyDB { public class TinyDB {
private static TinyDB tinyDB; private static volatile TinyDB tinyDB;
private final SharedPreferences preferences; private final SharedPreferences preferences;
@ -24,11 +24,17 @@ public class TinyDB {
} }
public static synchronized TinyDB getInstance(Context context) { public static synchronized TinyDB getInstance(Context context) {
if(tinyDB == null) { if(tinyDB == null) {
tinyDB = new TinyDB(context); synchronized(TinyDB.class) {
if(tinyDB == null) {
tinyDB = new TinyDB(context);
}
}
} }
return tinyDB; return tinyDB;
} }
/** /**