From f3a95c842b84562f009467b5920574d085046861 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 26 Nov 2014 18:01:52 +0200 Subject: [PATCH] Update process now will be visible for Dashboard even if it was started at downloads activity --- .../plus/dashboard/DashUpdatesFragment.java | 60 ++++++++++++- .../plus/dashboard/DashboardActivity.java | 88 +++++++++++++++---- .../plus/download/BaseDownloadActivity.java | 11 ++- .../plus/download/DownloadActivity.java | 6 ++ .../plus/download/DownloadIndexFragment.java | 1 - .../plus/download/DownloadIndexesThread.java | 14 ++- 6 files changed, 153 insertions(+), 27 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java index 78db2e16e9..ddedd551ef 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashUpdatesFragment.java @@ -1,6 +1,7 @@ package net.osmand.plus.dashboard; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; @@ -10,6 +11,7 @@ import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import net.osmand.plus.R; +import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadEntry; @@ -22,6 +24,11 @@ import java.util.List; * Created by Denis on 21.11.2014. */ public class DashUpdatesFragment extends DashBaseFragment { + private ProgressBar currentProgress; + private List progressBars = new ArrayList(); + private List baseNames = new ArrayList(); + private List downloadButtons = new ArrayList(); + @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = getActivity().getLayoutInflater().inflate(R.layout.dash_updates_fragment, container, false); @@ -55,6 +62,9 @@ public class DashUpdatesFragment extends DashBaseFragment { if (mainView == null) { return; } + progressBars.clear(); + baseNames.clear(); + downloadButtons.clear(); if (list.size() > 0) { mainView.setVisibility(View.VISIBLE); } else { @@ -80,17 +90,63 @@ public class DashUpdatesFragment extends DashBaseFragment { ((TextView) view.findViewById(R.id.map_name)).setText(eName); ((TextView) view.findViewById(R.id.map_descr)).setText(d); final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.ProgressBar); - (view.findViewById(R.id.btn_download)).setOnClickListener(new View.OnClickListener() { + View downloadButton = (view.findViewById(R.id.btn_download)); + downloadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - getDownloadActivity().startDownload(progressBar, item); + getDownloadActivity().startDownload(item); + currentProgress = progressBar; } }); + downloadButtons.add(downloadButton); + baseNames.add(item.getBasename()); + progressBars.add(progressBar); updates.addView(view); } + updateProgress(BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask(), true); } private BaseDownloadActivity getDownloadActivity(){ return (BaseDownloadActivity)getActivity(); } + + public void updateProgress(BasicProgressAsyncTask basicProgressAsyncTask, boolean updateOnlyProgress) { + if (basicProgressAsyncTask == null){ + return; + } + //needed when rotation is performed and progress can be null + if (currentProgress == null){ + getProgressIfPossible(basicProgressAsyncTask.getDescription()); + if (currentProgress == null){ + return; + } + } + + if(updateOnlyProgress){ + if(!basicProgressAsyncTask.isIndeterminate()){ + currentProgress.setProgress(basicProgressAsyncTask.getProgressPercentage()); + } + } else { + boolean visible = basicProgressAsyncTask != null && basicProgressAsyncTask.getStatus() != AsyncTask.Status.FINISHED; + if (!visible) { + return; + } + + boolean intermediate = basicProgressAsyncTask.isIndeterminate(); + currentProgress.setVisibility(intermediate ? View.GONE : View.VISIBLE); + if (!intermediate) { + currentProgress.setProgress(basicProgressAsyncTask.getProgressPercentage()); + } + } + } + + private void getProgressIfPossible(String message) { + for (int i =0; i basicProgressAsyncTask = BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask(); - - if(updateOnlyProgress){ - if(!basicProgressAsyncTask.isIndeterminate()){ - progressBar.setProgress(basicProgressAsyncTask.getProgressPercentage()); - } - } else { - boolean visible = basicProgressAsyncTask != null && basicProgressAsyncTask.getStatus() != AsyncTask.Status.FINISHED; - if (!visible) { - return; - } - - boolean intermediate = basicProgressAsyncTask.isIndeterminate(); - progressBar.setVisibility(intermediate ? View.GONE : View.VISIBLE); - if (!intermediate) { - progressBar.setProgress(basicProgressAsyncTask.getProgressPercentage()); + for(WeakReference ref : fragList) { + Fragment f = ref.get(); + if(f instanceof DashUpdatesFragment) { + if(!f.isDetached()) { + ((DashUpdatesFragment) f).updateProgress(basicProgressAsyncTask, updateOnlyProgress); + } } } } @@ -171,4 +174,51 @@ public class DashboardActivity extends BaseDownloadActivity { public boolean onCreateOptionsMenu(Menu menu) { return true; } + + public void checkPreviousRunsForExceptions(boolean firstTime) { + long size = getPreferences(MODE_WORLD_READABLE).getLong(EXCEPTION_FILE_SIZE, 0); + final OsmandApplication app = ((OsmandApplication) getApplication()); + final File file = app.getAppPath(OsmandApplication.EXCEPTION_PATH); + if (file.exists() && file.length() > 0) { + if (size != file.length() && !firstTime) { + String msg = MessageFormat.format(getString(R.string.previous_run_crashed), OsmandApplication.EXCEPTION_PATH); + AlertDialog.Builder builder = new AccessibleAlertBuilder(DashboardActivity.this); + builder.setMessage(msg).setNeutralButton(getString(R.string.close), null); + builder.setPositiveButton(R.string.send_report, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "osmand.app+crash@gmail.com" }); //$NON-NLS-1$ + intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); + intent.setType("vnd.android.cursor.dir/email"); //$NON-NLS-1$ + intent.putExtra(Intent.EXTRA_SUBJECT, "OsmAnd bug"); //$NON-NLS-1$ + StringBuilder text = new StringBuilder(); + text.append("\nDevice : ").append(Build.DEVICE); //$NON-NLS-1$ + text.append("\nBrand : ").append(Build.BRAND); //$NON-NLS-1$ + text.append("\nModel : ").append(Build.MODEL); //$NON-NLS-1$ + text.append("\nProduct : ").append(Build.PRODUCT); //$NON-NLS-1$ + text.append("\nBuild : ").append(Build.DISPLAY); //$NON-NLS-1$ + text.append("\nVersion : ").append(Build.VERSION.RELEASE); //$NON-NLS-1$ + text.append("\nApp Version : ").append(Version.getAppName(app)); //$NON-NLS-1$ + try { + PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0); + if (info != null) { + text.append("\nApk Version : ").append(info.versionName).append(" ").append(info.versionCode); //$NON-NLS-1$ //$NON-NLS-2$ + } + } catch (PackageManager.NameNotFoundException e) { + } + intent.putExtra(Intent.EXTRA_TEXT, text.toString()); + startActivity(Intent.createChooser(intent, getString(R.string.send_report))); + } + + }); + builder.show(); + } + getPreferences(MODE_WORLD_WRITEABLE).edit().putLong(EXCEPTION_FILE_SIZE, file.length()).commit(); + } else { + if (size > 0) { + getPreferences(MODE_WORLD_WRITEABLE).edit().putLong(EXCEPTION_FILE_SIZE, 0).commit(); + } + } + } } diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index 3847241aea..bf08db463e 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -1,5 +1,6 @@ package net.osmand.plus.download; +import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; @@ -27,7 +28,6 @@ public class BaseDownloadActivity extends SherlockFragmentActivity { protected OsmandSettings settings; public static DownloadIndexesThread downloadListIndexThread; protected List> fragList = new ArrayList>(); - protected ProgressBar progressBar; public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; @@ -42,6 +42,12 @@ public class BaseDownloadActivity extends SherlockFragmentActivity { } else { downloadListIndexThread.runReloadIndexFiles(); } + } + + + @Override + protected void onResume() { + super.onResume(); downloadListIndexThread.setUiActivity(this); } @@ -84,14 +90,13 @@ public class BaseDownloadActivity extends SherlockFragmentActivity { } - public void startDownload(ProgressBar progressBar, IndexItem item){ + public void startDownload(IndexItem item){ if (downloadListIndexThread.getCurrentRunningTask() != null){ Toast.makeText(this, "Please wait before previous download is finished", Toast.LENGTH_SHORT).show(); return; } List download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList()); getEntriesToDownload().put(item, download); - this.progressBar = progressBar; downloadFilesCheckFreeVersion(); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 732fa7e9dc..e5af152690 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -58,6 +58,12 @@ public class DownloadActivity extends BaseDownloadActivity { public static final String UPDATES_TAB = "updates"; + @Override + protected void onDestroy() { + super.onDestroy(); + BaseDownloadActivity.downloadListIndexThread.resetUiActivity(DownloadActivity.class); + } + @Override protected void onCreate(Bundle savedInstanceState) { getMyApplication().applyTheme(this); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java index 63195d8e07..71d7cdcc3f 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java @@ -281,7 +281,6 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { EditText filterText = (EditText) getView().findViewById(R.id.search_box); filterText.removeTextChangedListener(textWatcher); } - DownloadActivity.downloadListIndexThread.setUiActivity(null); } public List toString(List t) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 548b8e9637..9746c23f0d 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -72,7 +72,7 @@ public class DownloadIndexesThread { public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; } - + public List flattenDownloadEntries() { List res = new ArrayList(); for(List ens : getEntriesToDownload().values()) { @@ -120,7 +120,17 @@ public class DownloadIndexesThread { } public List getItemsToUpdate() { return itemsToUpdate;} - + + public void resetUiActivity(Class downloadActivityClass) { + if (uiActivity== null){ + return; + } + + if (uiActivity.getClass().equals(downloadActivityClass)){ + uiActivity = null; + } + } + public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask implements DownloadFileShowWarning { private OsmandPreference downloads;