allow update cancel

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/284/head
androidacy-user 1 year ago
parent cb562c7aa1
commit df41a04c15

@ -24,6 +24,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import io.noties.markwon.Markwon;
import timber.log.Timber;
@SuppressWarnings("UnnecessaryReturnStatement")
@ -36,6 +37,8 @@ public class UpdateActivity extends FoxActivity {
// Get the progress bar and make it indeterminate for now
LinearProgressIndicator progressIndicator = findViewById(R.id.update_progress);
progressIndicator.setIndeterminate(true);
// get update_cancel button
MaterialButton updateCancel = findViewById(R.id.update_cancel_button);
// get status text view
MaterialTextView statusTextView = findViewById(R.id.update_progress_text);
// set status text to please wait
@ -48,71 +51,29 @@ public class UpdateActivity extends FoxActivity {
statusTextView.setVisibility(MaterialTextView.INVISIBLE);
return;
}*/
new Thread(() -> {
// Now, parse the intent
String extras = getIntent().getAction();
// if extras is null, then we are in a bad state or user launched the activity manually
if (extras == null) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.error_no_extras);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
return;
}
// get action
ACTIONS action = ACTIONS.valueOf(extras);
// if action is null, then we are in a bad state or user launched the activity manually
if (Objects.isNull(action)) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.error_no_action);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
// return
return;
}
// For check action, we need to check if there is an update using the AppUpdateManager.peekShouldUpdate()
if (action == ACTIONS.CHECK) {
checkForUpdate();
} else if (action == ACTIONS.DOWNLOAD) {
try {
downloadUpdate();
} catch (
JSONException e) {
e.printStackTrace();
Thread updateThread = new Thread() {
public void run() {
// Now, parse the intent
String extras = getIntent().getAction();
// if extras is null, then we are in a bad state or user launched the activity manually
if (extras == null) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.error_download_update);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(100, false);
});
}
} else if (action == ACTIONS.INSTALL) {
// ensure path was passed and points to a file within our cache directory
String path = getIntent().getStringExtra("path");
if (path == null) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.no_file_found);
statusTextView.setText(R.string.error_no_extras);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
return;
}
File file = new File(path);
if (!file.exists()) {
// get action
ACTIONS action = ACTIONS.valueOf(extras);
// if action is null, then we are in a bad state or user launched the activity manually
if (Objects.isNull(action)) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.no_file_found);
statusTextView.setText(R.string.error_no_action);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
@ -120,26 +81,77 @@ public class UpdateActivity extends FoxActivity {
// return
return;
}
if (!Objects.equals(file.getParentFile(), getCacheDir())) {
// set status text to error
runOnUiThread(() -> {
statusTextView.setText(R.string.no_file_found);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
// return
return;
// For check action, we need to check if there is an update using the AppUpdateManager.peekShouldUpdate()
if (action == ACTIONS.CHECK) {
checkForUpdate();
} else if (action == ACTIONS.DOWNLOAD) {
try {
downloadUpdate();
} catch (
JSONException e) {
e.printStackTrace();
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.error_download_update);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(100, false);
});
}
} else if (action == ACTIONS.INSTALL) {
// ensure path was passed and points to a file within our cache directory
String path = getIntent().getStringExtra("path");
if (path == null) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.no_file_found);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
return;
}
File file = new File(path);
if (!file.exists()) {
runOnUiThread(() -> {
// set status text to error
statusTextView.setText(R.string.no_file_found);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
// return
return;
}
if (!Objects.equals(file.getParentFile(), getCacheDir())) {
// set status text to error
runOnUiThread(() -> {
statusTextView.setText(R.string.no_file_found);
// set progress bar to error
progressIndicator.setIndeterminate(false);
progressIndicator.setProgressCompat(0, false);
});
// return
return;
}
// set status text to installing
statusTextView.setText(R.string.installing_update);
// set progress bar to indeterminate
progressIndicator.setIndeterminate(true);
// install update
installUpdate(file);
}
// set status text to installing
statusTextView.setText(R.string.installing_update);
// set progress bar to indeterminate
progressIndicator.setIndeterminate(true);
// install update
installUpdate(file);
}
}).start();
};
// on click, finish the activity and anything running in it
updateCancel.setOnClickListener(v -> {
// end any download
updateThread.interrupt();
forceBackPressed();
finish();
});
updateThread.start();
}
public void checkForUpdate() {
@ -193,6 +205,11 @@ public class UpdateActivity extends FoxActivity {
}
// convert to JSON
JSONObject latestJSON = new JSONObject(new String(lastestJSON));
String changelog = latestJSON.getString("body");
// set changelog text. changelog could be markdown, so we need to convert it to HTML
MaterialTextView changelogTextView = findViewById(R.id.update_changelog);
final Markwon markwon = Markwon.builder(this).build();
runOnUiThread(() -> markwon.setMarkdown(changelogTextView, changelog));
// we already know that there is an update, so we can get the latest version of our architecture. We're going to have to iterate through the assets to find the one we want
JSONArray assets = latestJSON.getJSONArray("assets");
// get the asset we want

@ -7,22 +7,22 @@
android:orientation="vertical" tools:context=".UpdateActivity">
<!-- Activity used to download and install app updates -->
<!-- first, upgrade icon -->
<ImageView
android:id="@+id/updat_icon"
android:layout_width="101dp"
android:layout_height="93dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:contentDescription="@string/crash_icon"
android:src="@drawable/baseline_system_update_24" />
<!-- first, upgrade icon -->
<ImageView
android:id="@+id/updat_icon"
android:layout_width="101dp"
android:layout_height="93dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:contentDescription="@string/crash_icon"
android:src="@drawable/baseline_system_update_24" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/update_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_gravity="center"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:text="@string/update_title"
android:textAppearance="?attr/textAppearanceHeadline6" />
@ -31,14 +31,15 @@
android:id="@+id/update_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="8dp"
android:layout_gravity="center"
android:text="@string/update_message"
android:textAppearance="?attr/textAppearanceBody2" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/update_progress"
style="@style/Widget.Material3.LinearProgressIndicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
@ -52,16 +53,33 @@
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceBody2" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="4dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/update_button"
android:layout_width="wrap_content"
android:text="@string/update_button"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:text="@string/update_button"
android:visibility="invisible" />
<!-- Invisible warning for debug builds -->
<com.google.android.material.button.MaterialButton
android:id="@+id/update_cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:text="@string/update_cancel_button"
android:visibility="visible" />
</LinearLayout>
<!-- Invisible warning for debug builds -->
<com.google.android.material.textview.MaterialTextView
android:id="@+id/update_debug_warning"
android:layout_width="wrap_content"
@ -73,5 +91,31 @@
android:visibility="gone" />
<!-- Changelog view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/changelog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:text="@string/changelog"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/update_changelog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:scrollbars="vertical"
android:text="@string/changelog_none"
android:textAppearance="?attr/textAppearanceBody2" />
</LinearLayout>
</LinearLayout>

@ -329,7 +329,7 @@
<string name="background_update_check_excludes">Modules to exclude from automatic update checks</string>
<string name="title_activity_update">In-app Updater</string>
<string name="update_title">Update app</string>
<string name="update_message">An update may be available for FoxMMM. Please wait while we download and install it.</string>
<string name="update_message">Please wait while we check for and install updates to FoxMMM. This may take a few minutes</string>
<string name="update_button">Please wait...</string>
<string name="error_no_extras">ERROR: Invalid data received on launch</string>
<string name="update_debug_warning">You appear to be running a debug build. Debug builds must be updated manually, and do not support in-app updates</string>
@ -340,5 +340,5 @@
<string name="error_download_update">An error occurred downloading the update information.</string>
<string name="error_no_asset">ERROR: Failed to parse update information</string>
<string name="downloading_update">Downloading update… %1$d%%</string>
<string name="installing_update">Installing update…</string><string name="no_file_found">ERROR: Could not find update package.</string><string name="check_for_updates">Check for app updates</string><string name="update_debug_download_pref">Test update download mechanism</string>
<string name="installing_update">Installing update…</string><string name="no_file_found">ERROR: Could not find update package.</string><string name="check_for_updates">Check for app updates</string><string name="update_debug_download_pref">Test update download mechanism</string><string name="changelog_none">No changes yet!</string><string name="update_cancel_button">Cancel update</string>
</resources>

Loading…
Cancel
Save