Implementato pulsante di verifica aggiornamenti nelle settings
This commit is contained in:
parent
6105c44dab
commit
1988ad993f
@ -0,0 +1,26 @@
|
||||
package it.integry.integrywmsnative.core.helper;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import it.integry.integrywmsnative.core.utility.UtilityContext;
|
||||
|
||||
public final class ContextHelper {
|
||||
|
||||
public static FragmentManager getFragmentManagerFromContext(Context context) {
|
||||
if (context == null) {
|
||||
throw new IllegalArgumentException("Context cannot be null");
|
||||
}
|
||||
|
||||
if (context instanceof FragmentActivity) {
|
||||
return ((FragmentActivity) context).getSupportFragmentManager();
|
||||
} else if (UtilityContext.getMainActivity() != null) {
|
||||
return UtilityContext.getMainActivity().getSupportFragmentManager();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package it.integry.integrywmsnative.core.update;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
@ -7,15 +8,17 @@ import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import it.integry.integrywmsnative.BuildConfig;
|
||||
import it.integry.integrywmsnative.core.helper.ContextHelper;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.model.system.LatestAppVersionInfoDTO;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
@ -32,16 +35,13 @@ public class UpdatesManager {
|
||||
private final Handler handler;
|
||||
private final SystemRESTConsumer systemRESTConsumer;
|
||||
|
||||
private AppCompatActivity mContext;
|
||||
|
||||
public UpdatesManager(ExecutorService executorService, Handler handler, SystemRESTConsumer systemRESTConsumer) {
|
||||
this.executorService = executorService;
|
||||
this.handler = handler;
|
||||
this.systemRESTConsumer = systemRESTConsumer;
|
||||
}
|
||||
|
||||
public void init(AppCompatActivity activityContext) {
|
||||
this.mContext = activityContext;
|
||||
public void executeCheck(Context context, boolean forceReinstall) {
|
||||
|
||||
executorService.execute(() -> {
|
||||
|
||||
@ -49,7 +49,7 @@ public class UpdatesManager {
|
||||
final String baseEndpoint = SettingsManager.i().getServer().getProtocol() + "://" + SettingsManager.i().getServer().getHost() +
|
||||
(SettingsManager.i().getServer().getPort() > 0 ? ":" + SettingsManager.i().getServer().getPort() : "");
|
||||
|
||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activityContext);
|
||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
var betaEnabled = sharedPreferences.getBoolean(MainSettingsFragment.KEY_BUTTON_ENABLE_BETA, false);
|
||||
|
||||
LatestAppVersionInfoDTO latestData = systemRESTConsumer.retrieveUpdatesInfoSynchronized(betaEnabled);
|
||||
@ -62,34 +62,39 @@ public class UpdatesManager {
|
||||
|
||||
boolean currentVersionIsBeta = BuildConfig.VERSION_NAME.contains("beta");
|
||||
|
||||
if(currentVersionIsBeta == betaEnabled && latestData.getLatestVersionCode() == BuildConfig.VERSION_CODE)
|
||||
if(!forceReinstall && currentVersionIsBeta == betaEnabled && latestData.getLatestVersionCode() == BuildConfig.VERSION_CODE)
|
||||
return;
|
||||
|
||||
//Se sto passando da una beta a una stable e viceversa non obbligo l'utente a fare l'aggiornamento
|
||||
if(currentVersionIsBeta != betaEnabled || forceReinstall) {
|
||||
latestData.setForced(false);
|
||||
}
|
||||
|
||||
String currentDownloadUrl = baseEndpoint + latestData.getUrl();
|
||||
|
||||
showDialog(latestData, () -> {
|
||||
installAPK(currentDownloadUrl);
|
||||
showDialog(context, latestData, () -> {
|
||||
installAPK(context, currentDownloadUrl);
|
||||
});
|
||||
|
||||
} catch (Exception e) {
|
||||
handler.post(() -> {
|
||||
UtilityExceptions.defaultException(mContext, e);
|
||||
UtilityExceptions.defaultException(context, e);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showDialog(LatestAppVersionInfoDTO updatesData, Runnable onUpdateStart) {
|
||||
private void showDialog(Context context, LatestAppVersionInfoDTO updatesData, Runnable onUpdateStart) {
|
||||
DialogUpdateAvailableView.newInstance(updatesData, onUpdateStart)
|
||||
.show(mContext.getSupportFragmentManager(), "dialog-updates");
|
||||
.show(((FragmentActivity)context).getSupportFragmentManager(), "dialog-updates");
|
||||
}
|
||||
|
||||
private void installAPK(String downloadURL) {
|
||||
private void installAPK(Context context, String downloadURL) {
|
||||
|
||||
File destination = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
|
||||
File destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
|
||||
|
||||
var progressDialogBuilder = new DialogProgressView("Download", null, false);
|
||||
progressDialogBuilder.show(mContext.getSupportFragmentManager(), "tag");
|
||||
progressDialogBuilder.show(Objects.requireNonNull(ContextHelper.getFragmentManagerFromContext(context)), "tag");
|
||||
|
||||
|
||||
var fileDownloader = new FileDownloader()
|
||||
@ -106,7 +111,7 @@ public class UpdatesManager {
|
||||
progressDialogBuilder.dismiss();
|
||||
|
||||
if (!destination.exists()) {
|
||||
UtilityExceptions.defaultException(mContext, new Exception("Errore durante il download dell'aggiornamento"));
|
||||
UtilityExceptions.defaultException(context, new Exception("Errore durante il download dell'aggiornamento"));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -115,8 +120,8 @@ public class UpdatesManager {
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
||||
fileLoc = GenericFileProvider.getUriForFile(mContext,
|
||||
mContext.getApplicationContext().getPackageName() + ".core.update.GenericFileProvider",
|
||||
fileLoc = GenericFileProvider.getUriForFile(context,
|
||||
context.getApplicationContext().getPackageName() + ".core.update.GenericFileProvider",
|
||||
destPath);
|
||||
} else {
|
||||
intent = new Intent(Intent.ACTION_VIEW);
|
||||
@ -126,7 +131,7 @@ public class UpdatesManager {
|
||||
intent.setDataAndType(fileLoc, "application/vnd.android.package-archive");
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
||||
mContext.startActivity(intent);
|
||||
context.startActivity(intent);
|
||||
});
|
||||
|
||||
});
|
||||
@ -137,7 +142,7 @@ public class UpdatesManager {
|
||||
fileDownloader.download();
|
||||
} catch (Exception e) {
|
||||
progressDialogBuilder.dismissAllowingStateLoss();
|
||||
UtilityExceptions.defaultException(mContext, e);
|
||||
UtilityExceptions.defaultException(context, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@ package it.integry.integrywmsnative.core.utility;
|
||||
import android.content.Context;
|
||||
import android.text.Html;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
@ -14,6 +13,7 @@ import it.integry.integrywmsnative.BuildConfig;
|
||||
import it.integry.integrywmsnative.core.exception.InvalidConnectionException;
|
||||
import it.integry.integrywmsnative.core.exception.InvalidLUException;
|
||||
import it.integry.integrywmsnative.core.exception.InvalidLUGestioneException;
|
||||
import it.integry.integrywmsnative.core.helper.ContextHelper;
|
||||
import it.integry.integrywmsnative.core.rest.CommonRESTException;
|
||||
import it.integry.integrywmsnative.view.dialogs.base.DialogSimpleMessageView;
|
||||
|
||||
@ -44,13 +44,7 @@ public class UtilityExceptions {
|
||||
if (ex.getCause() != null) errorMessage += "<br />" + ex.getCause().getMessage();
|
||||
}
|
||||
|
||||
FragmentManager fm = null;
|
||||
|
||||
if (context instanceof FragmentActivity) {
|
||||
fm = ((FragmentActivity) context).getSupportFragmentManager();
|
||||
} else if (UtilityContext.getMainActivity() != null) {
|
||||
fm = UtilityContext.getMainActivity().getSupportFragmentManager();
|
||||
}
|
||||
FragmentManager fm = ContextHelper.getFragmentManagerFromContext(context);
|
||||
|
||||
if (fm != null) {
|
||||
DialogSimpleMessageView.makeErrorDialog(Html.fromHtml(errorMessage), null, null)
|
||||
|
||||
@ -89,7 +89,7 @@ public class MainActivity extends BaseActivity
|
||||
mBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_main, null, false);
|
||||
setContentView(mBinding.getRoot());
|
||||
|
||||
updatesManager.init(this);
|
||||
updatesManager.executeCheck(this, false);
|
||||
UtilityContext.initMainActivity(this);
|
||||
|
||||
setSupportActionBar(mBinding.appBarMain.toolbar);
|
||||
|
||||
@ -18,6 +18,7 @@ import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.sqlite.db.SimpleSQLiteQuery;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.harrysoft.androidbluetoothserial.BluetoothManager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -45,6 +46,7 @@ import it.integry.integrywmsnative.core.interfaces.ITitledFragment;
|
||||
import it.integry.integrywmsnative.core.rest.consumers.SystemRESTConsumer;
|
||||
import it.integry.integrywmsnative.core.rest.model.MailAttachmentDTO;
|
||||
import it.integry.integrywmsnative.core.rest.model.MailRequestDTO;
|
||||
import it.integry.integrywmsnative.core.update.UpdatesManager;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
|
||||
import it.integry.integrywmsnative.core.utility.UtilityResources;
|
||||
import it.integry.integrywmsnative.view.dialogs.DialogConsts;
|
||||
@ -68,6 +70,9 @@ public class MainSettingsFragment extends PreferenceFragmentCompat implements IT
|
||||
@Inject
|
||||
public ExecutorService executorService;
|
||||
|
||||
@Inject
|
||||
public UpdatesManager updatesManager;
|
||||
|
||||
|
||||
private boolean progressOpened;
|
||||
|
||||
@ -77,6 +82,7 @@ public class MainSettingsFragment extends PreferenceFragmentCompat implements IT
|
||||
public static final String KEY_TRIGGER_SCAN_MODE = "TRIG_SCAN_MODE";
|
||||
public static final String KEY_TRIGGER_SCAN_DELAY = "TRIG_SCAN_DELAY";
|
||||
public static final String KEY_BUTTON_ENABLE_BETA = "ENABLE_BETA";
|
||||
public static final String KEY_BUTTON_CHECK_UPDATES = "CHECK_UPDATES";
|
||||
public static final String KEY_BUTTON_EXPORT_LOG = "EXPORT_LOG";
|
||||
|
||||
private Collection<BluetoothDevice> btPairedDevices;
|
||||
@ -115,8 +121,14 @@ public class MainSettingsFragment extends PreferenceFragmentCompat implements IT
|
||||
triggerScanModeListPref.setOnPreferenceChangeListener(this);
|
||||
triggerScanModeListPref.setSummary(triggerScanModeListPref.getValue());
|
||||
|
||||
Preference button = findPreference(KEY_BUTTON_EXPORT_LOG);
|
||||
button.setOnPreferenceClickListener(preference -> {
|
||||
Preference checkUpdatesButton = findPreference(KEY_BUTTON_CHECK_UPDATES);
|
||||
checkUpdatesButton.setOnPreferenceClickListener(preference -> {
|
||||
checkUpdates();
|
||||
return true;
|
||||
});
|
||||
|
||||
Preference exportLogButton = findPreference(KEY_BUTTON_EXPORT_LOG);
|
||||
exportLogButton.setOnPreferenceClickListener(preference -> {
|
||||
exportLog();
|
||||
return true;
|
||||
});
|
||||
@ -234,6 +246,12 @@ public class MainSettingsFragment extends PreferenceFragmentCompat implements IT
|
||||
listPref.setSummary(btDeviceName);
|
||||
}
|
||||
|
||||
private void checkUpdates() {
|
||||
Snackbar.make(getView(), R.string.checking_updates, Snackbar.LENGTH_SHORT)
|
||||
.show();
|
||||
updatesManager.executeCheck(getContext(), true);
|
||||
}
|
||||
|
||||
|
||||
private void exportLog() {
|
||||
var handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@ -190,6 +190,8 @@
|
||||
<string name="tare_pckg">Tara collo</string>
|
||||
<string name="tare_art">Tara articolo</string>
|
||||
|
||||
<string name="checking_updates">Controllo gli aggiornamenti</string>
|
||||
|
||||
<string name="saving">Salvataggio</string>
|
||||
<string name="data_saved">Salvataggio completato</string>
|
||||
<string name="alert_print_completed_message">Stampa completata</string>
|
||||
|
||||
@ -189,6 +189,8 @@
|
||||
<string name="tare_art">Tare product</string>
|
||||
<string name="tare_pckg">Tare pckg</string>
|
||||
|
||||
<string name="checking_updates">Checking for updates</string>
|
||||
|
||||
<string name="saving">Saving</string>
|
||||
<string name="data_saved">Data saved</string>
|
||||
<string name="alert_print_completed_message">Printing job completed</string>
|
||||
|
||||
@ -77,6 +77,13 @@
|
||||
app:confirmMessage="Sei sicuro di voler abilitare gli aggiornamenti beta? Se accetti dovrai chiudere e riaprire l'app per scaricare l'aggiornamento."
|
||||
/>
|
||||
|
||||
<Preference
|
||||
android:title="Verifica aggiornamenti"
|
||||
android:key="CHECK_UPDATES"
|
||||
android:summary="Verifica la disponibilità di nuovi aggiornamenti"
|
||||
app:iconSpaceReserved="false"
|
||||
/>
|
||||
|
||||
<Preference
|
||||
android:title="@string/developer_settings_export_log_title"
|
||||
android:key="EXPORT_LOG"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user