mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-12-18 08:32:24 +03:00
Fix tests
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
79d417f475
commit
2b3e1efd1c
1 changed files with 37 additions and 16 deletions
|
@ -16,6 +16,9 @@ import android.content.SharedPreferences;
|
||||||
import android.content.pm.ShortcutInfo;
|
import android.content.pm.ShortcutInfo;
|
||||||
import android.content.pm.ShortcutManager;
|
import android.content.pm.ShortcutManager;
|
||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.Network;
|
||||||
|
import android.net.NetworkCapabilities;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.annotation.AnyThread;
|
import androidx.annotation.AnyThread;
|
||||||
|
@ -83,8 +86,7 @@ public class NotesRepository {
|
||||||
private Context context;
|
private Context context;
|
||||||
private final NotesDatabase db;
|
private final NotesDatabase db;
|
||||||
private final String defaultNonEmptyTitle;
|
private final String defaultNonEmptyTitle;
|
||||||
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveDataForSync;
|
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveData;
|
||||||
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveDataForNetworkStatus;
|
|
||||||
private boolean isSyncPossible = false;
|
private boolean isSyncPossible = false;
|
||||||
private boolean networkConnected = false;
|
private boolean networkConnected = false;
|
||||||
private String syncOnlyOnWifiKey;
|
private String syncOnlyOnWifiKey;
|
||||||
|
@ -111,8 +113,6 @@ public class NotesRepository {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final Observer<? super ConnectionLiveData.ConnectionType> networkStatusObserver = (Observer<ConnectionLiveData.ConnectionType>) this::observeNetworkStatus;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see <a href="https://stackoverflow.com/a/3104265">Do not make this a local variable.</a>
|
* @see <a href="https://stackoverflow.com/a/3104265">Do not make this a local variable.</a>
|
||||||
*/
|
*/
|
||||||
|
@ -149,10 +149,9 @@ public class NotesRepository {
|
||||||
this.apiProvider = apiProvider;
|
this.apiProvider = apiProvider;
|
||||||
this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context);
|
this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context);
|
||||||
this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only);
|
this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only);
|
||||||
this.connectionLiveDataForSync = new ConnectionLiveData(context);
|
this.connectionLiveData = new ConnectionLiveData(context);
|
||||||
this.connectionLiveDataForNetworkStatus = new ConnectionLiveData(context);
|
|
||||||
|
|
||||||
connectionLiveDataForSync.observeForever(syncObserver);
|
connectionLiveData.observeForever(syncObserver);
|
||||||
|
|
||||||
final var prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
|
final var prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
|
||||||
prefs.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
|
prefs.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
|
||||||
|
@ -162,23 +161,46 @@ public class NotesRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateNetworkStatus() {
|
public void updateNetworkStatus() {
|
||||||
connectionLiveDataForNetworkStatus.observeForever(networkStatusObserver);
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
|
||||||
|
if (connectivityManager == null) {
|
||||||
|
handleFailedNetworkStatus("ConnectivityManager is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Network network = connectivityManager.getActiveNetwork();
|
||||||
|
if (network == null) {
|
||||||
|
handleFailedNetworkStatus("No network connection");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
|
||||||
|
if (networkCapabilities == null) {
|
||||||
|
handleFailedNetworkStatus("NetworkCapabilities is null");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleNetworkStatus(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void observeNetworkStatus(ConnectionLiveData.ConnectionType connectionType) {
|
private void observeNetworkStatus(ConnectionLiveData.ConnectionType connectionType) {
|
||||||
if (connectionType == ConnectionLiveData.ConnectionType.Lost) {
|
if (connectionType == ConnectionLiveData.ConnectionType.Lost) {
|
||||||
networkConnected = false;
|
handleFailedNetworkStatus("No network connection");
|
||||||
isSyncPossible = false;
|
|
||||||
Log.d(TAG, "No network connection.");
|
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Network connection established with " + connectionType.name());
|
Log.d(TAG, "Network connection established with " + connectionType.name());
|
||||||
handleNetworkStatus();
|
handleNetworkStatus(connectionType == ConnectionLiveData.ConnectionType.WiFi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleNetworkStatus() {
|
private void handleFailedNetworkStatus(String message) {
|
||||||
|
Log.e(TAG, message);
|
||||||
|
networkConnected = false;
|
||||||
|
isSyncPossible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleNetworkStatus(boolean isWifiActive) {
|
||||||
networkConnected = true;
|
networkConnected = true;
|
||||||
isSyncPossible = !syncOnlyOnWifi;
|
isSyncPossible = !syncOnlyOnWifi || isWifiActive;
|
||||||
|
|
||||||
if (isSyncPossible) {
|
if (isSyncPossible) {
|
||||||
Log.d(TAG, "Network connection established.");
|
Log.d(TAG, "Network connection established.");
|
||||||
|
@ -189,8 +211,7 @@ public class NotesRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void finalize() throws Throwable {
|
protected void finalize() throws Throwable {
|
||||||
connectionLiveDataForSync.removeObserver(syncObserver);
|
connectionLiveData.removeObserver(syncObserver);
|
||||||
connectionLiveDataForNetworkStatus.removeObserver(networkStatusObserver);
|
|
||||||
super.finalize();
|
super.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue