Download progress and dialog flickering

This commit is contained in:
GaidamakUA 2015-10-07 17:02:39 +03:00
parent fc769ce8db
commit e745937edf
6 changed files with 126 additions and 198 deletions

View file

@ -3,7 +3,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:orientation="vertical"
android:id="@+id/mainLayout">
<net.osmand.plus.views.controls.PagerSlidingTabStrip
android:id="@+id/sliding_tabs"
@ -11,83 +12,10 @@
android:layout_height="@dimen/list_item_height"
android:background="?attr/colorPrimary" />
<LinearLayout
android:id="@+id/ProgressView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical|left"
android:orientation="horizontal" >
<include layout="@layout/free_version_banner"/>
<ProgressBar
android:id="@+id/IndeterminateProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:indeterminate="true"
android:indeterminateOnly="true"
android:visibility="gone"
tools:visibility="visible" />
<TextView
android:id="@+id/ProgressPercent"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right|center_vertical"
android:maxLines="1"
android:text=""
android:textSize="@dimen/default_desc_text_size"
tools:text="@string/lorem_ipsum" />
<TextView
android:id="@+id/ProgressMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_toLeftOf="@id/ProgressPercent"
android:layout_toRightOf="@id/IndeterminateProgressBar"
android:ellipsize="end"
android:gravity="left|center_vertical"
android:singleLine="true"
android:text=""
android:textSize="@dimen/default_desc_text_size"
tools:text="@string/lorem_ipsum" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/memory_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:progress="50"/>
<ImageView
android:id="@+id/Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_remove_dark" />
</LinearLayout>
</LinearLayout>
<include layout="@layout/progress_with_two_texts"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"

View file

@ -2,12 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="?attr/ctx_menu_info_view_bg">
<include layout="@layout/free_version_banner"/>
<include layout="@layout/progress_with_two_texts"/>
android:background="?attr/ctx_menu_info_view_bg"
android:orientation="vertical">
<include layout="@layout/download_items_list"/>
</LinearLayout>

View file

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/freeVersionBanner"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
android:id="@+id/freeVersionBanner"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
@ -12,6 +12,7 @@
tools:showIn="@layout/local_index_fragment_header">
<LinearLayout
android:id="@+id/freeVersionBannerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
@ -47,8 +48,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="10"
tools:progress="5"
android:progressDrawable="@drawable/number_of_downloads_progress_bar_drawable"/>
android:progressDrawable="@drawable/number_of_downloads_progress_bar_drawable"
android:saveEnabled="false"
tools:progress="5"/>
<LinearLayout
android:layout_width="match_parent"
@ -162,16 +164,16 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
osmand:textAllCapsCompat="true"
android:text="@string/get_full_version"/>
android:text="@string/get_full_version"
osmand:textAllCapsCompat="true"/>
<net.osmand.plus.widgets.ButtonEx
android:id="@+id/laterButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
osmand:textAllCapsCompat="true"
android:text="@string/later"/>
android:text="@string/later"
osmand:textAllCapsCompat="true"/>
</LinearLayout>
</LinearLayout>

View file

@ -12,7 +12,6 @@ import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
@ -22,12 +21,12 @@ import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.IndexConstants;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
@ -39,10 +38,13 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.OsmandExpandableListFragment;
import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.items.RegionDialogFragment;
import net.osmand.plus.download.items.WorldItemsFragment;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.plus.views.controls.PagerSlidingTabStrip;
import org.apache.commons.logging.Log;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@ -53,15 +55,7 @@ import java.util.Map;
import java.util.Set;
public class DownloadActivity extends BaseDownloadActivity {
private View progressView;
private ProgressBar indeterminateProgressBar;
private ProgressBar determinateProgressBar;
private TextView progressMessage;
private TextView progressPercent;
private ImageView cancel;
public class DownloadActivity extends BaseDownloadActivity implements RegionDialogFragment.DialogDismissListener {
private List<LocalIndexInfo> localIndexInfos = new ArrayList<LocalIndexInfo>();
private String initialFilter = "";
@ -134,21 +128,6 @@ public class DownloadActivity extends BaseDownloadActivity {
settings = ((OsmandApplication) getApplication()).getSettings();
indeterminateProgressBar = (ProgressBar) findViewById(R.id.IndeterminateProgressBar);
determinateProgressBar = (ProgressBar) findViewById(R.id.memory_progress);
progressView = findViewById(R.id.ProgressView);
progressMessage = (TextView) findViewById(R.id.ProgressMessage);
progressPercent = (TextView) findViewById(R.id.ProgressPercent);
cancel = (ImageView) findViewById(R.id.Cancel);
cancel.setImageDrawable(getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_remove_dark));
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeSureUserCancelDownload();
}
});
findViewById(R.id.downloadButton).setOnClickListener(new View.OnClickListener() {
@Override
@ -220,6 +199,7 @@ public class DownloadActivity extends BaseDownloadActivity {
protected void onResume() {
super.onResume();
getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this);
initFreeVersionBanner(findViewById(R.id.mainLayout));
}
@ -259,53 +239,15 @@ public class DownloadActivity extends BaseDownloadActivity {
@Override
public void updateProgress(boolean updateOnlyProgress) {
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask();
final boolean isFinished = basicProgressAsyncTask == null
|| basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED;
boolean indeterminate = true;
int percent = 0;
String message = "";
if (!isFinished) {
indeterminate = basicProgressAsyncTask.isIndeterminate();
message = basicProgressAsyncTask.getDescription();
if (!indeterminate) {
percent = basicProgressAsyncTask.getProgressPercentage();
}
}
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask =
DownloadActivity.downloadListIndexThread.getCurrentRunningTask();
if (visibleBanner != null) {
visibleBanner.updateProgress(isFinished, indeterminate, percent, message);
final int countedDownloads = DownloadActivity.downloadListIndexThread.getCountedDownloads();
visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask);
}
if (!updateOnlyProgress) {
updateDownloadButton();
}
// TODO delete after refactoring!
//needed when rotation is performed and progress can be null
if (progressView == null) {
return;
}
if (updateOnlyProgress) {
if (basicProgressAsyncTask != null && !basicProgressAsyncTask.isIndeterminate()) {
progressPercent.setText(basicProgressAsyncTask.getProgressPercentage() + "%");
determinateProgressBar.setProgress(basicProgressAsyncTask.getProgressPercentage());
}
} else {
progressView.setVisibility(!isFinished ? View.VISIBLE : View.GONE);
if (!isFinished) {
indeterminateProgressBar.setVisibility(!indeterminate ? View.GONE : View.VISIBLE);
determinateProgressBar.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
cancel.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
progressPercent.setVisibility(indeterminate ? View.GONE : View.VISIBLE);
progressMessage.setText(basicProgressAsyncTask.getDescription());
if (!indeterminate) {
progressPercent.setText(basicProgressAsyncTask.getProgressPercentage() + "%");
determinateProgressBar.setProgress(basicProgressAsyncTask.getProgressPercentage());
}
}
}
}
@Override
@ -628,14 +570,12 @@ public class DownloadActivity extends BaseDownloadActivity {
public void showDialog(FragmentActivity activity, DialogFragment fragment) {
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
Fragment prev = activity.getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
fragment.show(activity.getSupportFragmentManager(), "dialog");
}
fragment.show(ft, "dialog");
@Override
public void onDialogDismissed() {
initFreeVersionBanner(findViewById(R.id.mainLayout));
}
private static class ToggleCollapseFreeVersionBanner implements View.OnClickListener {
@ -666,46 +606,57 @@ public class DownloadActivity extends BaseDownloadActivity {
private final ProgressBar progressBar;
private final TextView leftTextView;
private final TextView rightTextView;
private final Context ctx;
private final boolean shouldShowFreeVersionBanner;
private final ProgressBar downloadsLeftProgressBar;
private final View buttonsLinearLayout;
private final TextView freeVersionDescriptionTextView;
private OsmandApplication application;
private final TextView downloadsLeftTextView;
private final View laterButton;
private final Context ctx;
private final OsmandApplication application;
private final boolean shouldShowFreeVersionBanner;
private final View freeVersionBannerTitle;
public BannerAndDownloadFreeVersion(View view, Context ctx) {
this.ctx = ctx;
application = (OsmandApplication) ctx.getApplicationContext();
freeVersionBanner = view.findViewById(R.id.freeVersionBanner);
downloadProgressLayout = view.findViewById(R.id.downloadProgressLayout);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
leftTextView = (TextView) view.findViewById(R.id.leftTextView);
rightTextView = (TextView) view.findViewById(R.id.rightTextView);
application = (OsmandApplication) ctx.getApplicationContext();
shouldShowFreeVersionBanner = Version.isFreeVersion(application)
|| application.getSettings().SHOULD_SHOW_FREE_VERSION_BANNER.get();
initFreeVersionBanner();
downloadsLeftTextView = (TextView) freeVersionBanner.findViewById(R.id.downloadsLeftTextView);
downloadsLeftProgressBar = (ProgressBar) freeVersionBanner.findViewById(R.id.downloadsLeftProgressBar);
downloadsLeftProgressBar = (ProgressBar) freeVersionBanner.findViewById(R.id.downloadsLeftProgressBar);
buttonsLinearLayout = freeVersionBanner.findViewById(R.id.buttonsLinearLayout);
freeVersionDescriptionTextView = (TextView) freeVersionBanner
.findViewById(R.id.freeVersionDescriptionTextView);
laterButton = freeVersionBanner.findViewById(R.id.laterButton);
freeVersionBannerTitle = freeVersionBanner.findViewById(R.id.freeVersionBannerTitle);
shouldShowFreeVersionBanner = Version.isFreeVersion(application)
|| application.getSettings().SHOULD_SHOW_FREE_VERSION_BANNER.get();
initFreeVersionBanner();
updateFreeVersionBanner();
}
public void updateProgress(boolean isFinished, boolean indeterminate, int percent, String message) {
public void updateProgress(int countedDownloads,
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask) {
final boolean isFinished = basicProgressAsyncTask == null
|| basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED;
if (isFinished) {
downloadProgressLayout.setVisibility(View.GONE);
// TODO
// freeVersionBanner.setVisibility(View.VISIBLE);
updateFreeVersionBanner();
} else {
if (freeVersionBanner.getVisibility() == View.VISIBLE) {
freeVersionBanner.setVisibility(View.GONE);
}
boolean indeterminate = basicProgressAsyncTask.isIndeterminate();
String message = basicProgressAsyncTask.getDescription();
int percent = basicProgressAsyncTask.getProgressPercentage();
setMinimizedFreeVersionBanner(true);
updateAvailableDownloads(countedDownloads);
downloadProgressLayout.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(indeterminate);
if (indeterminate) {
// TODO
leftTextView.setText(message);
} else {
// TODO if only 1 map, show map name
@ -723,6 +674,7 @@ public class DownloadActivity extends BaseDownloadActivity {
freeVersionBanner.setVisibility(View.GONE);
return;
}
freeVersionBanner.setVisibility(View.VISIBLE);
downloadsLeftProgressBar.setMax(BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS);
freeVersionDescriptionTextView.setText(ctx.getString(R.string.free_version_message,
BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS));
@ -738,21 +690,45 @@ public class DownloadActivity extends BaseDownloadActivity {
}
}
});
freeVersionBanner.findViewById(R.id.laterButton).setOnClickListener(
laterButton.setOnClickListener(
new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView, buttonsLinearLayout));
}
private void updateFreeVersionBanner() {
if (!shouldShowFreeVersionBanner) return;
setMinimizedFreeVersionBanner(false);
OsmandSettings settings = application.getSettings();
downloadsLeftProgressBar.setProgress(settings.NUMBER_OF_FREE_DOWNLOADS.get());
final int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS
- settings.NUMBER_OF_FREE_DOWNLOADS.get();
final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get();
downloadsLeftProgressBar.setProgress(mapsDownloaded);
int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS
- mapsDownloaded;
downloadsLeft = Math.max(downloadsLeft, 0);
if (downloadsLeft <= 0) {
laterButton.setVisibility(View.GONE);
}
downloadsLeftTextView.setText(ctx.getString(R.string.downloads_left_template, downloadsLeft));
// TODO review logic
freeVersionBanner.setOnClickListener(new ToggleCollapseFreeVersionBanner(freeVersionDescriptionTextView,
buttonsLinearLayout));
}
private void updateAvailableDownloads(int activeTasks) {
OsmandSettings settings = application.getSettings();
final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get() + activeTasks;
downloadsLeftProgressBar.setProgress(mapsDownloaded);
}
private void setMinimizedFreeVersionBanner(boolean minimize) {
if (minimize) {
freeVersionDescriptionTextView.setVisibility(View.GONE);
buttonsLinearLayout.setVisibility(View.GONE);
freeVersionBannerTitle.setVisibility(View.GONE);
} else {
freeVersionDescriptionTextView.setVisibility(View.VISIBLE);
buttonsLinearLayout.setVisibility(View.VISIBLE);
freeVersionBannerTitle.setVisibility(View.VISIBLE);
}
}
}
}

View file

@ -1,10 +1,6 @@
package net.osmand.plus.download.items;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.WorldRegion;
import net.osmand.plus.download.DownloadActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
@ -13,10 +9,18 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RegionDialogFragment extends DialogFragment {
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.WorldRegion;
import net.osmand.plus.download.DownloadActivity;
public class RegionDialogFragment extends DialogFragment{
public static final String TAG = "RegionDialogFragment";
private static final String REGION_ID_DLG_KEY = "world_region_dialog_key";
private String regionId;
private DialogDismissListener dialogDismissListener;
private DialogDismissListener listener;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -61,7 +65,13 @@ public class RegionDialogFragment extends DialogFragment {
toolbar.setTitle(region.getName());
}
}
((DownloadActivity)getActivity()).initFreeVersionBanner(view);
((DownloadActivity) getActivity()).initFreeVersionBanner(view);
listener = new DialogDismissListener() {
@Override
public void onDialogDismissed() {
((DownloadActivity) getActivity()).initFreeVersionBanner(view);
}
};
return view;
}
@ -76,7 +86,9 @@ public class RegionDialogFragment extends DialogFragment {
}
public void onRegionSelected(String regionId) {
((DownloadActivity)getActivity()).showDialog(getActivity(), createInstance(regionId));
final RegionDialogFragment regionDialogFragment = createInstance(regionId);
regionDialogFragment.setOnDismissListener(listener);
((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment);
}
public static RegionDialogFragment createInstance(String regionId) {
@ -86,4 +98,19 @@ public class RegionDialogFragment extends DialogFragment {
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (dialogDismissListener != null)
dialogDismissListener.onDialogDismissed();
}
public void setOnDismissListener(DialogDismissListener listener) {
this.dialogDismissListener = listener;
}
public interface DialogDismissListener {
void onDialogDismissed();
}
}

View file

@ -70,8 +70,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
onCategorizationFinished();
((DownloadActivity) getActivity()).initFreeVersionBanner(view);
return view;
}
@ -127,7 +125,9 @@ public class WorldItemsFragment extends OsmandExpandableListFragment {
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == worldRegionsIndex) {
WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition);
((DownloadActivity)getActivity()).showDialog(getActivity(), RegionDialogFragment.createInstance(region.getRegionId()));
final RegionDialogFragment regionDialogFragment = RegionDialogFragment.createInstance(region.getRegionId());
regionDialogFragment.setOnDismissListener((DownloadActivity) getActivity());
((DownloadActivity)getActivity()).showDialog(getActivity(), regionDialogFragment);
return true;
} else if (groupPosition == voicePromptsIndex) {
if (childPosition == voicePromptsItemsRecordedSubIndex) {