Update process now will be visible for Dashboard even if it was started at downloads activity

This commit is contained in:
Denis 2014-11-26 18:01:52 +02:00
parent 284e6953c8
commit f3a95c842b
6 changed files with 153 additions and 27 deletions

View file

@ -1,6 +1,7 @@
package net.osmand.plus.dashboard; package net.osmand.plus.dashboard;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -10,6 +11,7 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.BaseDownloadActivity;
import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadEntry; import net.osmand.plus.download.DownloadEntry;
@ -22,6 +24,11 @@ import java.util.List;
* Created by Denis on 21.11.2014. * Created by Denis on 21.11.2014.
*/ */
public class DashUpdatesFragment extends DashBaseFragment { public class DashUpdatesFragment extends DashBaseFragment {
private ProgressBar currentProgress;
private List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
private List<String> baseNames = new ArrayList<String>();
private List<View> downloadButtons = new ArrayList<View>();
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.dash_updates_fragment, container, false); View view = getActivity().getLayoutInflater().inflate(R.layout.dash_updates_fragment, container, false);
@ -55,6 +62,9 @@ public class DashUpdatesFragment extends DashBaseFragment {
if (mainView == null) { if (mainView == null) {
return; return;
} }
progressBars.clear();
baseNames.clear();
downloadButtons.clear();
if (list.size() > 0) { if (list.size() > 0) {
mainView.setVisibility(View.VISIBLE); mainView.setVisibility(View.VISIBLE);
} else { } 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_name)).setText(eName);
((TextView) view.findViewById(R.id.map_descr)).setText(d); ((TextView) view.findViewById(R.id.map_descr)).setText(d);
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.ProgressBar); 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 @Override
public void onClick(View view) { 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); updates.addView(view);
} }
updateProgress(BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask(), true);
} }
private BaseDownloadActivity getDownloadActivity(){ private BaseDownloadActivity getDownloadActivity(){
return (BaseDownloadActivity)getActivity(); 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<baseNames.size(); i++){
if (message.equals(getActivity().getString(R.string.downloading_file_new) + " " + baseNames.get(i))){
currentProgress = progressBars.get(i);
currentProgress.setVisibility(View.VISIBLE);
return;
}
}
}
} }

View file

@ -1,13 +1,19 @@
package net.osmand.plus.dashboard; package net.osmand.plus.dashboard;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.SpannableString; import android.text.SpannableString;
@ -20,6 +26,7 @@ import android.widget.*;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import net.osmand.access.AccessibleAlertBuilder;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.plus.*; import net.osmand.plus.*;
@ -29,7 +36,9 @@ import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.download.*; import net.osmand.plus.download.*;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.MessageFormat;
import java.util.*; import java.util.*;
/** /**
@ -38,6 +47,7 @@ import java.util.*;
public class DashboardActivity extends BaseDownloadActivity { public class DashboardActivity extends BaseDownloadActivity {
private static final String CONTRIBUTION_VERSION_FLAG = "CONTRIBUTION_VERSION_FLAG"; private static final String CONTRIBUTION_VERSION_FLAG = "CONTRIBUTION_VERSION_FLAG";
private static final String EXCEPTION_FILE_SIZE = "EXCEPTION_FS"; //$NON-NLS-1$
public static final boolean TIPS_AND_TRICKS = false; public static final boolean TIPS_AND_TRICKS = false;
@ -141,28 +151,21 @@ public class DashboardActivity extends BaseDownloadActivity {
} }
} }
@Override
protected void onDestroy() {
super.onDestroy();
downloadListIndexThread.resetUiActivity(DashboardActivity.class);
}
@Override @Override
public void updateProgress(boolean updateOnlyProgress) { public void updateProgress(boolean updateOnlyProgress) {
//needed when rotation is performed and progress can be null
if (progressBar == null){
return;
}
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask(); BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask();
for(WeakReference<Fragment> ref : fragList) {
if(updateOnlyProgress){ Fragment f = ref.get();
if(!basicProgressAsyncTask.isIndeterminate()){ if(f instanceof DashUpdatesFragment) {
progressBar.setProgress(basicProgressAsyncTask.getProgressPercentage()); if(!f.isDetached()) {
} ((DashUpdatesFragment) f).updateProgress(basicProgressAsyncTask, updateOnlyProgress);
} 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());
} }
} }
} }
@ -171,4 +174,51 @@ public class DashboardActivity extends BaseDownloadActivity {
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
return true; 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();
}
}
}
} }

View file

@ -1,5 +1,6 @@
package net.osmand.plus.download; package net.osmand.plus.download;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
@ -27,7 +28,6 @@ public class BaseDownloadActivity extends SherlockFragmentActivity {
protected OsmandSettings settings; protected OsmandSettings settings;
public static DownloadIndexesThread downloadListIndexThread; public static DownloadIndexesThread downloadListIndexThread;
protected List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>(); protected List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>();
protected ProgressBar progressBar;
public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10;
@ -42,6 +42,12 @@ public class BaseDownloadActivity extends SherlockFragmentActivity {
} else { } else {
downloadListIndexThread.runReloadIndexFiles(); downloadListIndexThread.runReloadIndexFiles();
} }
}
@Override
protected void onResume() {
super.onResume();
downloadListIndexThread.setUiActivity(this); 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){ if (downloadListIndexThread.getCurrentRunningTask() != null){
Toast.makeText(this, "Please wait before previous download is finished", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Please wait before previous download is finished", Toast.LENGTH_SHORT).show();
return; return;
} }
List<DownloadEntry> download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList<DownloadEntry>()); List<DownloadEntry> download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList<DownloadEntry>());
getEntriesToDownload().put(item, download); getEntriesToDownload().put(item, download);
this.progressBar = progressBar;
downloadFilesCheckFreeVersion(); downloadFilesCheckFreeVersion();
} }

View file

@ -58,6 +58,12 @@ public class DownloadActivity extends BaseDownloadActivity {
public static final String UPDATES_TAB = "updates"; public static final String UPDATES_TAB = "updates";
@Override
protected void onDestroy() {
super.onDestroy();
BaseDownloadActivity.downloadListIndexThread.resetUiActivity(DownloadActivity.class);
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
getMyApplication().applyTheme(this); getMyApplication().applyTheme(this);

View file

@ -281,7 +281,6 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment {
EditText filterText = (EditText) getView().findViewById(R.id.search_box); EditText filterText = (EditText) getView().findViewById(R.id.search_box);
filterText.removeTextChangedListener(textWatcher); filterText.removeTextChangedListener(textWatcher);
} }
DownloadActivity.downloadListIndexThread.setUiActivity(null);
} }
public List<String> toString(List<DownloadActivityType> t) { public List<String> toString(List<DownloadActivityType> t) {

View file

@ -121,6 +121,16 @@ public class DownloadIndexesThread {
public List<IndexItem> getItemsToUpdate() { return itemsToUpdate;} public List<IndexItem> getItemsToUpdate() { return itemsToUpdate;}
public void resetUiActivity(Class<?> downloadActivityClass) {
if (uiActivity== null){
return;
}
if (uiActivity.getClass().equals(downloadActivityClass)){
uiActivity = null;
}
}
public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning { public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, Object, String> implements DownloadFileShowWarning {
private OsmandPreference<Integer> downloads; private OsmandPreference<Integer> downloads;