Refactored downloads activity to be able to use update functionality in dashboard.

This commit is contained in:
Denis 2014-11-25 17:28:33 +02:00
parent d020889409
commit 0a4a26be91
8 changed files with 218 additions and 102 deletions

View file

@ -8,6 +8,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/map_update"
@ -24,4 +25,9 @@
style="@style/DashboardGeneralButton"/>
</LinearLayout>
<LinearLayout android:id="@+id/updates_items"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

View file

@ -63,6 +63,12 @@ public class DashFavoritesFragment extends DashBaseFragment {
View mainView = getView();
final FavouritesDbHelper helper = getMyApplication().getFavorites();
final List<FavouritePoint> points = new ArrayList<FavouritePoint>(helper.getFavouritePoints());
if (points.size() == 0){
(mainView.findViewById(R.id.main_fav)).setVisibility(View.GONE);
return;
} else {
(mainView.findViewById(R.id.main_fav)).setVisibility(View.VISIBLE);
}
Collections.sort(points, new Comparator<FavouritePoint>() {
@Override
public int compare(FavouritePoint point, FavouritePoint point2) {
@ -76,12 +82,7 @@ public class DashFavoritesFragment extends DashBaseFragment {
});
LinearLayout favorites = (LinearLayout) mainView.findViewById(R.id.favorites);
favorites.removeAllViews();
if (points.size() == 0){
(mainView.findViewById(R.id.main_fav)).setVisibility(View.GONE);
return;
} else {
(mainView.findViewById(R.id.main_fav)).setVisibility(View.VISIBLE);
}
if (points.size() > 3){
while (points.size() != 3){

View file

@ -72,8 +72,5 @@ public class DashPluginsFragment extends DashBaseFragment {
view.setLayoutParams(lp);
layout.addView(view);
}
}
}

View file

@ -5,7 +5,15 @@ import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.download.BaseDownloadActivity;
import net.osmand.plus.download.DownloadEntry;
import net.osmand.plus.download.IndexItem;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Denis on 21.11.2014.
@ -21,4 +29,44 @@ public class DashUpdatesFragment extends DashBaseFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void updatedDownloadsList(List<IndexItem> list) {
View mainView = getView();
if (mainView == null) {
return;
}
if (list.size() > 0) {
mainView.setVisibility(View.VISIBLE);
} else {
mainView.setVisibility(View.GONE);
return;
}
LinearLayout updates = (LinearLayout) mainView.findViewById(R.id.updates_items);
updates.removeAllViews();
for (int i = 0; i < list.size(); i++) {
final IndexItem item = list.get(i);
if (i > 2) {
break;
}
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dash_updates_item, null, false);
String eName = item.getVisibleDescription(getMyApplication()) + "\n"
+ item.getVisibleName(getMyApplication(), getMyApplication().getResourceManager().getOsmandRegions());
String d = item.getDate(getMyApplication().getResourceManager().getDateFormat()) + "\n" + item.getSizeDescription(getMyApplication());
((TextView) view.findViewById(R.id.map_name)).setText(eName);
((TextView) view.findViewById(R.id.map_descr)).setText(d);
(view.findViewById(R.id.btn_download)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<DownloadEntry> download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList<DownloadEntry>());
getDownloadActivity().getEntriesToDownload().put(item, download);
}
});
}
}
private BaseDownloadActivity getDownloadActivity(){
return (BaseDownloadActivity)getActivity();
}
}

View file

@ -22,6 +22,9 @@ import net.osmand.plus.activities.MainMenuActivity;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TipsAndTricksActivity;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.download.BaseDownloadActivity;
import net.osmand.plus.download.DownloadActivityType;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.util.MapUtils;
import java.util.*;
@ -29,9 +32,11 @@ import java.util.*;
/**
* Created by Denis on 05.11.2014.
*/
public class DashboardActivity extends SherlockFragmentActivity {
public class DashboardActivity extends BaseDownloadActivity {
public static final boolean TIPS_AND_TRICKS = false;
public static DownloadIndexesThread downloadListIndexThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -41,6 +46,16 @@ public class DashboardActivity extends SherlockFragmentActivity {
getSupportActionBar().setBackgroundDrawable(color);
getSupportActionBar().setIcon(android.R.color.transparent);
if(downloadListIndexThread == null) {
downloadListIndexThread = new DownloadIndexesThread(this);
}
if (downloadListIndexThread.getCachedIndexFiles() != null && downloadListIndexThread.isDownloadedFromInternet()) {
downloadListIndexThread.runCategorization(DownloadActivityType.NORMAL_FILE);
} else {
downloadListIndexThread.runReloadIndexFiles();
}
downloadListIndexThread.setUiActivity(this);
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
@ -92,10 +107,6 @@ public class DashboardActivity extends SherlockFragmentActivity {
return true;
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getApplication();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;

View file

@ -0,0 +1,133 @@
package net.osmand.plus.download;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Denis on 25.11.2014.
*/
public class BaseDownloadActivity extends SherlockFragmentActivity {
protected DownloadActivityType type = DownloadActivityType.NORMAL_FILE;
protected OsmandSettings settings;
public static DownloadIndexesThread downloadListIndexThread;
public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10;
public void updateDownloadList(List<IndexItem> list){
}
public void updateProgress(boolean updateOnlyProgress) {
}
public DownloadActivityType getDownloadType(){
return type;
}
public Map<IndexItem, List<DownloadEntry>> getEntriesToDownload() {
if(downloadListIndexThread == null) {
return new LinkedHashMap<IndexItem, List<DownloadEntry>>();
}
return downloadListIndexThread.getEntriesToDownload();
}
public void downloadedIndexes() {
}
public void updateDownloadButton(boolean scroll) {
}
public void downloadListUpdated(){
}
public OsmandApplication getMyApplication(){
return (OsmandApplication)getApplication();
}
public void categorizationFinished(List<IndexItem> filtered, List<IndexItemCategory> cats){
}
public void downloadFilesPreCheckSpace() {
double sz = 0;
List<DownloadEntry> list = downloadListIndexThread.flattenDownloadEntries();
for (DownloadEntry es : list) {
sz += es.sizeMB;
}
// get availabile space
double asz = downloadListIndexThread.getAvailableSpace();
if (asz != -1 && asz > 0 && sz / asz > 0.4) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadListIndexThread.runDownloadFiles();
}
});
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show();
} else {
downloadListIndexThread.runDownloadFiles();
}
}
protected void downloadFilesCheckFreeVersion() {
if (Version.isFreeVersion(getMyApplication()) ) {
int total = settings.NUMBER_OF_FREE_DOWNLOADS.get();
boolean wiki = false;
for (IndexItem es : DownloadActivity.downloadListIndexThread.getEntriesToDownload().keySet()) {
if (es.getBasename() != null && es.getBasename().contains("_wiki")) {
wiki = true;
break;
} else if (DownloadActivityType.isCountedInDownloads(es.getType())) {
total++;
}
}
if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) {
String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "");
AlertDialog.Builder msg = new AlertDialog.Builder(this);
msg.setTitle(R.string.free_version_title);
msg.setMessage(msgTx);
msg.setPositiveButton(R.string.default_buttons_ok, null);
msg.show();
} else {
downloadFilesCheckInternet();
}
} else {
downloadFilesCheckInternet();
}
}
protected void downloadFilesCheckInternet() {
if(!getMyApplication().getSettings().isWifiConnected()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.download_using_mobile_internet));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadFilesPreCheckSpace();
}
});
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show();
} else {
downloadFilesPreCheckSpace();
}
}
}

View file

@ -35,15 +35,10 @@ import java.util.*;
/**
* Created by Denis on 08.09.2014.
*/
public class DownloadActivity extends SherlockFragmentActivity {
public class DownloadActivity extends BaseDownloadActivity {
private TabHost tabHost;
private FavouritesActivity.TabsAdapter mTabsAdapter;
public static DownloadIndexesThread downloadListIndexThread;
private DownloadActivityType type = DownloadActivityType.NORMAL_FILE;
public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10;
private OsmandSettings settings;
private View progressView;
private ProgressBar indeterminateProgressBar;
@ -241,8 +236,6 @@ public class DownloadActivity extends SherlockFragmentActivity {
return localIndexInfos;
}
public DownloadActivityType getDownloadType() { return type;}
public void setType(DownloadActivityType type) { this.type = type;}
public void changeType(final DownloadActivityType tp) {
@ -253,90 +246,12 @@ public class DownloadActivity extends SherlockFragmentActivity {
}
}
public void downloadFilesPreCheckSpace() {
double sz = 0;
List<DownloadEntry> list = downloadListIndexThread.flattenDownloadEntries();
for (DownloadEntry es : list) {
sz += es.sizeMB;
}
// get availabile space
double asz = downloadListIndexThread.getAvailableSpace();
if (asz != -1 && asz > 0 && sz / asz > 0.4) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadListIndexThread.runDownloadFiles();
}
});
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show();
} else {
downloadListIndexThread.runDownloadFiles();
}
}
public Map<IndexItem, List<DownloadEntry>> getEntriesToDownload() {
if(downloadListIndexThread == null) {
return new LinkedHashMap<IndexItem, List<DownloadEntry>>();
}
return downloadListIndexThread.getEntriesToDownload();
}
@Override
public void onPause() {
super.onPause();
(getMyApplication()).setDownloadActivity(null);
}
protected void downloadFilesCheckFreeVersion() {
if (Version.isFreeVersion(getMyApplication()) ) {
int total = settings.NUMBER_OF_FREE_DOWNLOADS.get();
boolean wiki = false;
for (IndexItem es : DownloadActivity.downloadListIndexThread.getEntriesToDownload().keySet()) {
if (es.getBasename() != null && es.getBasename().contains("_wiki")) {
wiki = true;
break;
} else if (DownloadActivityType.isCountedInDownloads(es.getType())) {
total++;
}
}
if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS || wiki) {
String msgTx = getString(R.string.free_version_message, MAXIMUM_AVAILABLE_FREE_DOWNLOADS + "");
AlertDialog.Builder msg = new AlertDialog.Builder(this);
msg.setTitle(R.string.free_version_title);
msg.setMessage(msgTx);
msg.setPositiveButton(R.string.default_buttons_ok, null);
msg.show();
} else {
downloadFilesCheckInternet();
}
} else {
downloadFilesCheckInternet();
}
}
protected void downloadFilesCheckInternet() {
if(!getMyApplication().getSettings().isWifiConnected()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.download_using_mobile_internet));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
downloadFilesPreCheckSpace();
}
});
builder.setNegativeButton(R.string.default_buttons_no, null);
builder.show();
} else {
downloadFilesPreCheckSpace();
}
}
public OsmandApplication getMyApplication(){ return (OsmandApplication)getApplication();}
public void showDialogOfFreeDownloadsIfNeeded() {
if (Version.isFreeVersion(getMyApplication())) {
AlertDialog.Builder msg = new AlertDialog.Builder(this);
@ -361,6 +276,7 @@ public class DownloadActivity extends SherlockFragmentActivity {
}
}
@Override
public void updateProgress(boolean updateOnlyProgress) {
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask();
//needed when rotation is performed and progress can be null
@ -411,6 +327,7 @@ public class DownloadActivity extends SherlockFragmentActivity {
bld.show();
}
@Override
public void updateDownloadList(List<IndexItem> list){
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
@ -422,6 +339,7 @@ public class DownloadActivity extends SherlockFragmentActivity {
}
}
@Override
public void categorizationFinished(List<IndexItem> filtered, List<IndexItemCategory> cats){
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
@ -444,6 +362,7 @@ public class DownloadActivity extends SherlockFragmentActivity {
}
}
@Override
public void downloadedIndexes(){
for(WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
@ -464,6 +383,7 @@ public class DownloadActivity extends SherlockFragmentActivity {
}
@Override
public void updateDownloadButton(boolean scroll) {
// View view = getView();
// if (view == null || getExpandableListView() == null){

View file

@ -43,7 +43,7 @@ import android.view.View;
import android.widget.Toast;
public class DownloadIndexesThread {
private DownloadActivity uiActivity = null;
private BaseDownloadActivity uiActivity = null;
private IndexFileList indexFiles = null;
private Map<IndexItem, List<DownloadEntry>> entriesToDownload = new ConcurrentHashMap<IndexItem, List<DownloadEntry>>();
private Set<DownloadEntry> currentDownloads = new HashSet<DownloadEntry>();
@ -69,7 +69,7 @@ public class DownloadIndexesThread {
indexFiles = null;
}
public void setUiActivity(DownloadActivity uiActivity) {
public void setUiActivity(BaseDownloadActivity uiActivity) {
this.uiActivity = uiActivity;
}