Refactor classes

This commit is contained in:
Victor Shcherb 2015-11-02 09:53:08 +01:00
parent 98b92608e4
commit d79f86008d
6 changed files with 265 additions and 317 deletions

View file

@ -4,7 +4,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?attr/bottom_menu_view_bg"
tools:context="net.osmand.plus.download.ui.popups.DataStoragePlaceDialogFragment">
tools:context="net.osmand.plus.download.ui.DataStoragePlaceDialogFragment">
<ImageView
android:id="@+id/folderIconImageView"

View file

@ -1,26 +1,18 @@
package net.osmand.plus.download;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.support.annotation.UiThread;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.method.LinkMovementMethod;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.lang.ref.WeakReference;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import net.osmand.IProgress;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast;
import net.osmand.data.LatLon;
import net.osmand.map.WorldRegion;
import net.osmand.map.WorldRegion.RegionParams;
import net.osmand.plus.OsmandApplication;
@ -32,30 +24,46 @@ import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.ActionBarProgressActivity;
import net.osmand.plus.activities.LocalIndexInfo;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.base.BasicProgressAsyncTask;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment;
import net.osmand.plus.download.ui.popups.DataStoragePlaceDialogFragment;
import net.osmand.plus.download.ui.BottomSheetDownloadFragment;
import net.osmand.plus.download.ui.DataStoragePlaceDialogFragment;
import net.osmand.plus.download.ui.DownloadResourceGroupFragment;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.plus.download.ui.UpdatesIndexFragment;
import net.osmand.plus.download.ui.popups.AskMapDownloadFragment;
import net.osmand.plus.download.ui.DownloadResourceGroupFragment;
import net.osmand.plus.download.ui.popups.GoToMapFragment;
import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin;
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.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StatFs;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class DownloadActivity extends ActionBarProgressActivity implements DownloadEvents {
private static final Log LOG = PlatformUtil.getLog(DownloadActivity.class);
@ -555,8 +563,11 @@ public class DownloadActivity extends ActionBarProgressActivity implements Downl
}
}
private void showGoToMap(WorldRegion worldRegion) {
GoToMapFragment.showInstance(worldRegion, this);
private void showGoToMap(WorldRegion region) {
GoToMapFragment fragment = new GoToMapFragment();
fragment.regionCenter = region.getRegionCenter();
fragment.regionName = region.getLocaleName();
fragment.show(getFragmentManager(), GoToMapFragment.TAG);
}
private void showDownloadWorldMapIfNeeded() {
@ -567,7 +578,9 @@ public class DownloadActivity extends ActionBarProgressActivity implements Downl
if(!SUGGESTED_TO_DOWNLOAD_BASEMAP && worldMap != null && (!worldMap.isDownloaded() || worldMap.isOutdated()) &&
!getDownloadThread().isDownloading(worldMap)) {
SUGGESTED_TO_DOWNLOAD_BASEMAP = true;
AskMapDownloadFragment.showInstance(worldMap, this);
AskMapDownloadFragment fragment = new AskMapDownloadFragment();
fragment.indexItem = worldMap;
fragment.show(getFragmentManager(), AskMapDownloadFragment.TAG);
}
}
@ -639,4 +652,150 @@ public class DownloadActivity extends ActionBarProgressActivity implements Downl
srtmNeedsInstallation = srtmPlugin == null || srtmPlugin.needsInstallation();
}
public static class AskMapDownloadFragment extends BottomSheetDownloadFragment {
public static final String TAG = "AskMapDownloadFragment";
private static final String KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME = "key_ask_map_download_item_filename";
private IndexItem indexItem;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
String itemFileName = savedInstanceState.getString(KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME);
if (itemFileName != null) {
indexItem = getMyApplication().getDownloadThread().getIndexes().getIndexItem(itemFileName);
}
}
View view = inflater.inflate(R.layout.ask_map_download_fragment, container, false);
((ImageView) view.findViewById(R.id.titleIconImageView))
.setImageDrawable(getIcon(R.drawable.ic_map, R.color.osmand_orange));
Button actionButtonOk = (Button) view.findViewById(R.id.actionButtonOk);
String titleText = null;
String descriptionText = null;
if (indexItem != null) {
if (indexItem.getBasename().equalsIgnoreCase(WorldRegion.WORLD_BASEMAP)) {
titleText = getString(R.string.index_item_world_basemap);
descriptionText = getString(R.string.world_map_download_descr);
}
actionButtonOk.setText(getString(R.string.shared_string_download) + " (" + indexItem.getSizeDescription(getActivity()) + ")");
}
if (titleText != null) {
((TextView) view.findViewById(R.id.titleTextView))
.setText(titleText);
}
if (descriptionText != null) {
((TextView) view.findViewById(R.id.descriptionTextView))
.setText(descriptionText);
}
final ImageButton closeImageButton = (ImageButton) view.findViewById(R.id.closeImageButton);
closeImageButton.setImageDrawable(getContentIcon(R.drawable.ic_action_remove_dark));
closeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
actionButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (indexItem != null) {
((DownloadActivity) getActivity()).startDownload(indexItem);
dismiss();
}
}
});
view.findViewById(R.id.actionButtonCancel)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (indexItem != null) {
outState.putString(KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME, indexItem.getFileName());
}
}
}
public static class GoToMapFragment extends BottomSheetDownloadFragment {
public static final String TAG = "GoToMapFragment";
private static final String KEY_GOTO_MAP_REGION_CENTER = "key_goto_map_region_center";
private static final String KEY_GOTO_MAP_REGION_NAME = "key_goto_map_region_name";
private LatLon regionCenter;
private String regionName;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
regionName = savedInstanceState.getString(KEY_GOTO_MAP_REGION_NAME, "");
Object rCenterObj = savedInstanceState.getSerializable(KEY_GOTO_MAP_REGION_CENTER);
if (rCenterObj != null) {
regionCenter = (LatLon) rCenterObj;
} else {
regionCenter = new LatLon(0, 0);
}
}
View view = inflater.inflate(R.layout.go_to_map_fragment, container, false);
((ImageView) view.findViewById(R.id.titleIconImageView))
.setImageDrawable(getIcon(R.drawable.ic_map, R.color.osmand_orange));
((TextView) view.findViewById(R.id.descriptionTextView))
.setText(getActivity().getString(R.string.map_downloaded_descr, regionName));
final ImageButton closeImageButton = (ImageButton) view.findViewById(R.id.closeImageButton);
closeImageButton.setImageDrawable(getContentIcon(R.drawable.ic_action_remove_dark));
closeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getActivity() instanceof DownloadActivity) {
((DownloadActivity) getActivity()).setDownloadItem(null);
}
dismiss();
}
});
view.findViewById(R.id.actionButton)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = (OsmandApplication) getActivity().getApplication();
app.getSettings().setMapLocationToShow(regionCenter.getLatitude(), regionCenter.getLongitude(), 5, null);
dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity());
}
});
return view;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(KEY_GOTO_MAP_REGION_NAME, regionName);
outState.putSerializable(KEY_GOTO_MAP_REGION_CENTER, regionCenter);
}
}
}

View file

@ -0,0 +1,72 @@
package net.osmand.plus.download.ui;
import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.IndexItem;
public abstract class BottomSheetDownloadFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
boolean isLightTheme = getMyApplication()
.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME;
int themeId = isLightTheme ? R.style.OsmandLightTheme_BottomSheet
: R.style.OsmandDarkTheme_BottomSheet;
final Dialog dialog = new Dialog(getActivity(), themeId);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_PopUpMenu_Bottom;
return dialog;
}
@Nullable
@Override
public abstract View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
@Override
public void onStart() {
super.onStart();
final Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
protected OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
protected Drawable getIcon(@DrawableRes int drawableRes, @ColorRes int color) {
return getMyApplication().getIconsCache().getIcon(drawableRes, color);
}
protected Drawable getContentIcon(@DrawableRes int drawableRes) {
return getMyApplication().getIconsCache().getContentIcon(drawableRes);
}
}

View file

@ -1,4 +1,4 @@
package net.osmand.plus.download.ui.popups;
package net.osmand.plus.download.ui;
import android.app.Activity;
import android.app.Dialog;

View file

@ -1,151 +0,0 @@
package net.osmand.plus.download.ui.popups;
import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.IndexItem;
public class AskMapDownloadFragment extends DialogFragment {
public static final String TAG = "AskMapDownloadFragment";
private static final String KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME = "key_ask_map_download_item_filename";
private IndexItem indexItem;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
boolean isLightTheme = getMyApplication()
.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME;
int themeId = isLightTheme ? R.style.OsmandLightTheme_BottomSheet
: R.style.OsmandDarkTheme_BottomSheet;
final Dialog dialog = new Dialog(getActivity(), themeId);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_PopUpMenu_Bottom;
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
String itemFileName = savedInstanceState.getString(KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME);
if (itemFileName != null) {
indexItem = getMyApplication().getDownloadThread().getIndexes().getIndexItem(itemFileName);
}
}
View view = inflater.inflate(R.layout.ask_map_download_fragment, container, false);
((ImageView) view.findViewById(R.id.titleIconImageView))
.setImageDrawable(getIcon(R.drawable.ic_map, R.color.osmand_orange));
Button actionButtonOk = (Button) view.findViewById(R.id.actionButtonOk);
String titleText = null;
String descriptionText = null;
if (indexItem != null) {
if (indexItem.getBasename().equalsIgnoreCase(WorldRegion.WORLD_BASEMAP)) {
titleText = getString(R.string.index_item_world_basemap);
descriptionText = getString(R.string.world_map_download_descr);
}
actionButtonOk.setText(getString(R.string.shared_string_download) + " (" + indexItem.getSizeDescription(getActivity()) + ")");
}
if (titleText != null) {
((TextView) view.findViewById(R.id.titleTextView))
.setText(titleText);
}
if (descriptionText != null) {
((TextView) view.findViewById(R.id.descriptionTextView))
.setText(descriptionText);
}
final ImageButton closeImageButton = (ImageButton) view.findViewById(R.id.closeImageButton);
closeImageButton.setImageDrawable(getContentIcon(R.drawable.ic_action_remove_dark));
closeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
actionButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (indexItem != null) {
((DownloadActivity) getActivity()).startDownload(indexItem);
dismiss();
}
}
});
view.findViewById(R.id.actionButtonCancel)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
final Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (indexItem != null) {
outState.putString(KEY_ASK_MAP_DOWNLOAD_ITEM_FILENAME, indexItem.getFileName());
}
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
private Drawable getIcon(@DrawableRes int drawableRes, @ColorRes int color) {
return getMyApplication().getIconsCache().getIcon(drawableRes, color);
}
private Drawable getContentIcon(@DrawableRes int drawableRes) {
return getMyApplication().getIconsCache().getContentIcon(drawableRes);
}
public static void showInstance(IndexItem indexItem, DownloadActivity activity) {
AskMapDownloadFragment fragment = new AskMapDownloadFragment();
fragment.indexItem = indexItem;
fragment.show(activity.getFragmentManager(), TAG);
}
}

View file

@ -1,132 +0,0 @@
package net.osmand.plus.download.ui.popups;
import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.data.LatLon;
import net.osmand.map.WorldRegion;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.download.DownloadActivity;
public class GoToMapFragment extends DialogFragment {
public static final String TAG = "GoToMapFragment";
private static final String KEY_GOTO_MAP_REGION_CENTER = "key_goto_map_region_center";
private static final String KEY_GOTO_MAP_REGION_NAME = "key_goto_map_region_name";
private LatLon regionCenter;
private String regionName;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
boolean isLightTheme = getMyApplication()
.getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME;
int themeId = isLightTheme ? R.style.OsmandLightTheme_BottomSheet
: R.style.OsmandDarkTheme_BottomSheet;
final Dialog dialog = new Dialog(getActivity(), themeId);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_PopUpMenu_Bottom;
return dialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
regionName = savedInstanceState.getString(KEY_GOTO_MAP_REGION_NAME, "");
Object rCenterObj = savedInstanceState.getSerializable(KEY_GOTO_MAP_REGION_CENTER);
if (rCenterObj != null) {
regionCenter = (LatLon) rCenterObj;
} else {
regionCenter = new LatLon(0, 0);
}
}
View view = inflater.inflate(R.layout.go_to_map_fragment, container, false);
((ImageView) view.findViewById(R.id.titleIconImageView))
.setImageDrawable(getIcon(R.drawable.ic_map, R.color.osmand_orange));
((TextView) view.findViewById(R.id.descriptionTextView))
.setText(getActivity().getString(R.string.map_downloaded_descr, regionName));
final ImageButton closeImageButton = (ImageButton) view.findViewById(R.id.closeImageButton);
closeImageButton.setImageDrawable(getContentIcon(R.drawable.ic_action_remove_dark));
closeImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(getActivity() instanceof DownloadActivity) {
((DownloadActivity) getActivity()).setDownloadItem(null);
}
dismiss();
}
});
view.findViewById(R.id.actionButton)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OsmandApplication app = (OsmandApplication) getActivity().getApplication();
app.getSettings().setMapLocationToShow(regionCenter.getLatitude(), regionCenter.getLongitude(), 5, null);
dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity());
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
final Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(KEY_GOTO_MAP_REGION_NAME, regionName);
outState.putSerializable(KEY_GOTO_MAP_REGION_CENTER, regionCenter);
}
private OsmandApplication getMyApplication() {
return (OsmandApplication) getActivity().getApplication();
}
private Drawable getIcon(@DrawableRes int drawableRes, @ColorRes int color) {
return getMyApplication().getIconsCache().getIcon(drawableRes, color);
}
private Drawable getContentIcon(@DrawableRes int drawableRes) {
return getMyApplication().getIconsCache().getContentIcon(drawableRes);
}
public static void showInstance(WorldRegion region, DownloadActivity activity) {
GoToMapFragment fragment = new GoToMapFragment();
fragment.regionCenter = region.getRegionCenter();
fragment.regionName = region.getLocaleName();
fragment.show(activity.getFragmentManager(), TAG);
}
}