private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
private final MutableLiveData<HledgerVersion> detectedVersion = new MutableLiveData<>(null);
+ private final MutableLiveData<Boolean> detectingHledgerVersion = new MutableLiveData<>(false);
public int initialThemeHue = Colors.DEFAULT_HUE_DEG;
private VersionDetectionThread versionDetectionThread;
public ProfileDetailModel() {
void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
themeId.observe(lfo, o);
}
+ void observeDetectingHledgerVersion(LifecycleOwner lfo, Observer<Boolean> o) {
+ detectingHledgerVersion.observe(lfo, o);
+ }
void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
final int profileThemeId;
if (mProfile != null) {
versionDetectionThread.start();
}
static class VersionDetectionThread extends Thread {
+ static final int TARGET_PROCESS_DURATION = 1000;
private final Pattern versionPattern =
Pattern.compile("^\"(\\d+)\\.(\\d+)(?:\\.(\\d+))?\"$");
private final ProfileDetailModel model;
public VersionDetectionThread(ProfileDetailModel model) {
this.model = model;
}
- @Override
- public void run() {
+ private HledgerVersion detectVersion() {
+ HttpURLConnection http = null;
try {
- HttpURLConnection http = NetworkUtil.prepareConnection(model.getUrl(), "version",
+ http = NetworkUtil.prepareConnection(model.getUrl(), "version",
model.getUseAuthentication());
switch (http.getResponseCode()) {
case 200:
break;
case 404:
- model.detectedVersion.postValue(new HledgerVersion(true));
- return;
+ return new HledgerVersion(true);
default:
Logger.debug("profile", String.format(Locale.US,
"HTTP error detecting hledger-web version: [%d] %s",
http.getResponseCode(), http.getResponseMessage()));
- model.detectedVersion.postValue(null);
- return;
+ return null;
}
InputStream stream = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
final boolean hasPatch = m.groupCount() >= 3;
int patch = hasPatch ? Integer.parseInt(Objects.requireNonNull(m.group(3))) : 0;
- model.detectedVersion.postValue(
- hasPatch ? new HledgerVersion(major, minor, patch)
- : new HledgerVersion(major, minor));
+ return hasPatch ? new HledgerVersion(major, minor, patch)
+ : new HledgerVersion(major, minor);
}
else {
Logger.debug("profile",
String.format("Unrecognised version string '%s'", version));
+ return null;
}
}
catch (IOException e) {
e.printStackTrace();
+ return null;
+ }
+ }
+ @Override
+ public void run() {
+ model.detectingHledgerVersion.postValue(true);
+ try {
+ long startTime = System.currentTimeMillis();
+
+ final HledgerVersion version = detectVersion();
+
+ long elapsed = System.currentTimeMillis() - startTime;
+ Logger.debug("profile", "Detection duration " + elapsed);
+ if (elapsed < TARGET_PROCESS_DURATION) {
+ try {
+ Thread.sleep(TARGET_PROCESS_DURATION - elapsed);
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ model.detectedVersion.postValue(version);
+ }
+ finally {
+ model.detectingHledgerVersion.postValue(false);
}
}
}
<TextView
android:id="@+id/api_version_label"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/profile_api_version_title"
android:textAppearance="?android:textAppearanceListItem"
+ android:layout_marginEnd="24dp"
+ app:layout_constraintEnd_toStartOf="@id/detected_version_text"
+ app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
android:textAppearance="?android:textAppearanceListItemSecondary"
android:textColor="?attr/textColor"
app:layout_constraintEnd_toStartOf="@id/detected_version_text"
+ android:layout_marginEnd="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/api_version_label"
/>
- <TextView
- android:id="@+id/detected_version_label"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:gravity="end"
- android:text="@string/detected_version_label"
- android:textAppearance="?android:textAppearanceListItemSecondary"
- app:layout_constraintEnd_toStartOf="@id/detected_version_text"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@id/api_version_text"
- />
<TextView
android:id="@+id/detected_version_text"
android:layout_width="wrap_content"
- android:layout_height="wrap_content"
+ android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:text="@string/api_version_unknown_label"
android:textAppearance="?android:textAppearanceListItemSecondary"
android:textColor="?attr/textColor"
app:layout_constraintEnd_toStartOf="@id/api_version_detect_button"
- app:layout_constraintTop_toBottomOf="@id/api_version_text"
+ android:gravity="bottom"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintTop_toTopOf="parent"
/>
- <TextView
+ <ProgressBar
+ android:layout_height="24dp"
android:id="@+id/api_version_detect_button"
android:layout_width="24dp"
- android:layout_height="0dp"
- android:drawableBottom="@drawable/ic_refresh_primary_24dp"
+ android:indeterminate="true"
android:foregroundGravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toBottomOf="@id/api_version_label"
+ android:visibility="invisible"
/>
</androidx.constraintlayout.widget.ConstraintLayout>