From e48e1aef9f7b05edff896947b2a0951a472efbea Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 27 Apr 2015 18:09:33 +0200 Subject: [PATCH 1/2] Fix warnings --- OsmAnd/res/values/strings.xml | 6 +- .../osmand/plus/activities/MapActivity.java | 4 + .../plus/activities/SettingsActivity.java | 10 -- .../activities/SettingsGeneralActivity.java | 86 +++++++++++----- .../dashboard/DashChooseAppDirFragment.java | 99 +++++++++++++------ 5 files changed, 138 insertions(+), 67 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index f71aff7f48..08d70ed591 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,6 +9,7 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> + Do you want OsmAnd to also copy its data files to the new destination? Maps could not be created in specified directory Copying files failed External storage @@ -16,6 +17,7 @@ Internal application memory Manually specified Internal memory + Data storage folder Map Storage Copy Filter by name @@ -241,7 +243,7 @@ Manage Map language Transport stops - Do you want OsmAnd to also copy its data files to the new destination? Select \'No\' for OsmAnd to just create and use the new folder. + Zone Northing Easting @@ -1799,7 +1801,7 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A Use OsmAnd offline navigation applies only to routes > 20 km (experimental) OsmAnd offline navigation is an experimental feature and it does not work for distances of more than about 20 km.\n\nNavigation service is temporarily switched to online CloudMade. Can not find the specified folder. - Data storage folder + A previous OsmAnd version is installed. All offline data will be supported by the new application. But Favorite points should be exported in the old application and later imported by the new one. Build {0} successfully installed ({1}). diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index b973536f19..1308f51cc5 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -64,6 +64,7 @@ import android.content.Intent; import android.media.AudioManager; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -802,6 +803,9 @@ public class MapActivity extends AccessibleActivity { } public void checkExternalStorage() { + if(Build.VERSION.SDK_INT >= 19) { + return; + } String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // ok diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index 32b6611f4c..670756a2b1 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -1,9 +1,6 @@ package net.osmand.plus.activities; -import java.io.File; - -import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; @@ -13,21 +10,14 @@ import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.graphics.Color; -import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceScreen; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.Toolbar; -import android.text.method.LinkMovementMethod; import android.util.DisplayMetrics; import android.util.TypedValue; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.ScrollView; diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java index c6af41dbbe..3b71335422 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java @@ -7,7 +7,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; -import net.osmand.CallbackWithObject; import net.osmand.IProgress; import net.osmand.IndexConstants; import net.osmand.access.AccessibleToast; @@ -27,8 +26,11 @@ import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.render.NativeOsmandLibrary; import net.osmand.plus.voice.CommandPlayer; import net.osmand.render.RenderingRulesStorage; +import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; @@ -36,6 +38,7 @@ import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.media.AudioManager; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; @@ -304,7 +307,63 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { } }); } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public void showAppDirDialog(){ + if(Build.VERSION.SDK_INT >= 19) { + ChooseAppDirDialogFragment newFragment = ChooseAppDirDialogFragment.newInstance(); + newFragment.show(getFragmentManager(), "dialog"); + } + AlertDialog.Builder editalert = new AlertDialog.Builder(SettingsGeneralActivity.this); + editalert.setTitle(R.string.application_dir); + final EditText input = new EditText(SettingsGeneralActivity.this); + input.setText(settings.getExternalStorageDirectory().getAbsolutePath()); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + lp.leftMargin = lp.rightMargin = 5; + lp.bottomMargin = lp.topMargin = 5; + input.setLayoutParams(lp); + settings.getExternalStorageDirectory().getAbsolutePath(); + editalert.setView(input); + editalert.setNegativeButton(R.string.shared_string_cancel, null); + editalert.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + warnAboutChangingStorage(input.getText().toString()); + } + }); + editalert.show(); + + } + + @SuppressLint("NewApi") + public static class ChooseAppDirDialogFragment extends android.app.DialogFragment { + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle args = getArguments(); + final DashChooseAppDirFragment ssf = new DashChooseAppDirFragment(); + ssf.setArguments(args); + ssf.onAttach(getActivity()); + AlertDialog dlg = new AlertDialog.Builder(getActivity()) + .setView(ssf.onCreateView(getActivity().getLayoutInflater(), null, savedInstanceState)) + .setNegativeButton(R.string.shared_string_cancel, null) + .setPositiveButton(R.string.shared_string_ok, new OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + android.view.View.OnClickListener cf = ssf.getConfirmListener(); + cf.onClick(null); + } + }) + .create(); + return dlg; + } + public static ChooseAppDirDialogFragment newInstance() { + return new ChooseAppDirDialogFragment(); + } + } private void addMiscPreferences(PreferenceGroup misc) { @@ -314,30 +373,9 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { applicationDir.setKey("external_storage_dir"); applicationDir.setOnPreferenceClickListener(new OnPreferenceClickListener() { - public void showOtherDialog(){ - AlertDialog.Builder editalert = new AlertDialog.Builder(SettingsGeneralActivity.this); - editalert.setTitle(R.string.application_dir); - final EditText input = new EditText(SettingsGeneralActivity.this); - input.setText(settings.getExternalStorageDirectory().getAbsolutePath()); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT); - lp.leftMargin = lp.rightMargin = 5; - lp.bottomMargin = lp.topMargin = 5; - input.setLayoutParams(lp); - settings.getExternalStorageDirectory().getAbsolutePath(); - editalert.setView(input); - editalert.setNegativeButton(R.string.shared_string_cancel, null); - editalert.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - warnAboutChangingStorage(input.getText().toString()); - } - }); - editalert.show(); - } @Override public boolean onPreferenceClick(Preference preference) { - showOtherDialog(); + showAppDirDialog(); return false; } }); @@ -444,7 +482,7 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { return; } Builder builder = new AlertDialog.Builder(this); - builder.setMessage(getString(R.string.application_dir_change_warning2)); + builder.setMessage(getString(R.string.application_dir_change_warning3)); builder.setPositiveButton(R.string.shared_string_yes, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java index ed77758f7d..f13a598e20 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java @@ -22,6 +22,7 @@ import net.osmand.plus.ProgressImplementation; import net.osmand.plus.R; import net.osmand.util.Algorithms; import android.annotation.TargetApi; +import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; @@ -31,9 +32,10 @@ import android.os.Build; import android.os.Bundle; import android.os.StatFs; import android.support.annotation.Nullable; -import android.support.v7.app.ActionBarActivity; +import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; @@ -59,11 +61,25 @@ public class DashChooseAppDirFragment extends DashBaseFragment { private File selectedFile = new File("/"); private File currentAppFile; private OsmandSettings settings; + private Activity activity; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + this.activity = activity; + } + @Override public void onOpenDash() { } + + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return initView(inflater, container); + } + private String getFreeSpace(File dir) { if(dir.canRead()){ StatFs fs = new StatFs(dir.getAbsolutePath()); @@ -85,30 +101,38 @@ public class DashChooseAppDirFragment extends DashBaseFragment { locationPath.setText(R.string.storage_directory_manual); } locationDesc.setText(selectedFile.getAbsolutePath() + " \u2022 " + getFreeSpace(selectedFile)); - boolean readOnlyAndFileExist = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()) && - !OsmandSettings.isWritable(currentAppFile) && !mapsCopied; - if (readOnlyAndFileExist) { - readOnlyAndFileExist = false; + boolean copyFiles = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()) && + !mapsCopied; + if (copyFiles) { + copyFiles = false; File[] lf = currentAppFile.listFiles(); if (lf != null) { for (File f : lf) { if (f != null) { if (f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { - readOnlyAndFileExist = true; + copyFiles = true; break; } } } } } - warningReadonly.setVisibility(readOnlyAndFileExist ? View.VISIBLE : View.GONE); - warningReadonly.setText(getString(R.string.android_19_location_disabled, currentAppFile.getAbsolutePath())); - copyMapsBtn.setVisibility(readOnlyAndFileExist ? View.VISIBLE : View.GONE); + warningReadonly.setVisibility(copyFiles ? View.VISIBLE : View.GONE); + if(copyFiles) { + if(OsmandSettings.isWritable(currentAppFile)) { + warningReadonly.setText(getString(R.string.android_19_location_disabled, currentAppFile.getAbsolutePath())); + } else { + warningReadonly.setText(getString(R.string.application_dir_change_warning3)); + } + } + + copyMapsBtn.setVisibility(copyFiles ? View.VISIBLE : View.GONE); } - @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - View view = getActivity().getLayoutInflater().inflate(R.layout.dash_storage_type_fragment, container, false); + + + public View initView(LayoutInflater inflater, ViewGroup container) { + View view = inflater.inflate(R.layout.dash_storage_type_fragment, container, false); settings = getMyApplication().getSettings(); locationPath = (TextView) view.findViewById(R.id.location_path); locationDesc = (TextView) view.findViewById(R.id.location_desc); @@ -136,7 +160,7 @@ public class DashChooseAppDirFragment extends DashBaseFragment { @TargetApi(Build.VERSION_CODES.KITKAT) protected void showSelectDialog19() { - AlertDialog.Builder editalert = new AlertDialog.Builder(getActivity()); + AlertDialog.Builder editalert = new AlertDialog.Builder(activity); editalert.setTitle(R.string.application_dir); final List items = new ArrayList(); final List paths = new ArrayList(); @@ -224,9 +248,9 @@ public class DashChooseAppDirFragment extends DashBaseFragment { } public void showOtherDialog(){ - AlertDialog.Builder editalert = new AlertDialog.Builder(getActivity()); + AlertDialog.Builder editalert = new AlertDialog.Builder(activity); editalert.setTitle(R.string.application_dir); - final EditText input = new EditText(getActivity()); + final EditText input = new EditText(activity); input.setText(selectedFile.getAbsolutePath()); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, @@ -262,51 +286,62 @@ public class DashChooseAppDirFragment extends DashBaseFragment { copyMapsBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - MoveFilesToDifferentDirectory task = new MoveFilesToDifferentDirectory(getActivity(), currentAppFile, + MoveFilesToDifferentDirectory task = new MoveFilesToDifferentDirectory(activity, currentAppFile, selectedFile) { - protected Boolean doInBackground(Void[] params) { - Boolean result = super.doInBackground(params); + + @Override + protected void onPostExecute(Boolean result) { + super.onPostExecute(result); if (result) { mapsCopied = true; getMyApplication().getResourceManager().resetStoreDirectory(); } else { - AccessibleToast.makeText(getActivity(), R.string.copying_osmand_file_failed, - Toast.LENGTH_SHORT).show(); + AccessibleToast.makeText(activity, R.string.copying_osmand_file_failed, Toast.LENGTH_SHORT) + .show(); } updateView(); - return result; } }; task.execute(); } }); - confirmBtn.setOnClickListener(new View.OnClickListener() { + confirmBtn.setOnClickListener(getConfirmListener()); + + } + + public OnClickListener getConfirmListener() { + return new View.OnClickListener() { @Override public void onClick(View v) { boolean wr = OsmandSettings.isWritable(selectedFile); if(wr) { boolean changed = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()); - getMyApplication().setExternalStorageDirectory(type, currentAppFile.getAbsolutePath()); + getMyApplication().setExternalStorageDirectory(type, selectedFile.getAbsolutePath()); if(changed) { reloadData(); } - ActionBarActivity dashboardActivity = ((ActionBarActivity) getActivity()); - if (dashboardActivity != null) { - dashboardActivity.getSupportFragmentManager().beginTransaction().remove(DashChooseAppDirFragment.this) + if (activity instanceof FragmentActivity) { + ((FragmentActivity)activity).getSupportFragmentManager().beginTransaction().remove(DashChooseAppDirFragment.this) .commit(); } } else { - AccessibleToast.makeText(getActivity(), R.string.specified_directiory_not_writeable, + AccessibleToast.makeText(activity, R.string.specified_directiory_not_writeable, Toast.LENGTH_LONG).show(); } } - }); - + }; } protected void reloadData() { - new ReloadData(getActivity(), getMyApplication()).execute((Void)null ); + new ReloadData(activity, getMyApplication()).execute((Void)null ); + } + + public OsmandApplication getMyApplication(){ + if (activity == null){ + return null; + } + return (OsmandApplication) activity.getApplication(); } public static class ReloadData extends AsyncTask { @@ -343,7 +378,9 @@ public class DashChooseAppDirFragment extends DashBaseFragment { if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return false; } - return !settings.isExternalStorageDirectorySpecifiedV19(); + // TODO + return true; +// return !settings.isExternalStorageDirectorySpecifiedV19(); } public static HashSet getExternalMounts() { From ccd966c50bbc5d12c845a3ebd68d753627503935 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 27 Apr 2015 22:37:57 +0200 Subject: [PATCH 2/2] Update application dir --- .../activities/SettingsGeneralActivity.java | 43 +- .../dashboard/DashChooseAppDirFragment.java | 719 +++++++++--------- 2 files changed, 383 insertions(+), 379 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java index 3b71335422..dede7a1ff7 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java @@ -19,6 +19,7 @@ import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.dashboard.DashChooseAppDirFragment; +import net.osmand.plus.dashboard.DashChooseAppDirFragment.ChooseAppDirFragment; import net.osmand.plus.dashboard.DashChooseAppDirFragment.MoveFilesToDifferentDirectory; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; @@ -26,8 +27,6 @@ import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.render.NativeOsmandLibrary; import net.osmand.plus.voice.CommandPlayer; import net.osmand.render.RenderingRulesStorage; -import android.annotation.SuppressLint; -import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; @@ -308,11 +307,11 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { }); } - @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public void showAppDirDialog(){ if(Build.VERSION.SDK_INT >= 19) { - ChooseAppDirDialogFragment newFragment = ChooseAppDirDialogFragment.newInstance(); - newFragment.show(getFragmentManager(), "dialog"); + showAppDirDialogV19(); + return; } AlertDialog.Builder editalert = new AlertDialog.Builder(SettingsGeneralActivity.this); editalert.setTitle(R.string.application_dir); @@ -336,34 +335,14 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { } - @SuppressLint("NewApi") - public static class ChooseAppDirDialogFragment extends android.app.DialogFragment { - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle args = getArguments(); - final DashChooseAppDirFragment ssf = new DashChooseAppDirFragment(); - ssf.setArguments(args); - ssf.onAttach(getActivity()); - AlertDialog dlg = new AlertDialog.Builder(getActivity()) - .setView(ssf.onCreateView(getActivity().getLayoutInflater(), null, savedInstanceState)) - .setNegativeButton(R.string.shared_string_cancel, null) - .setPositiveButton(R.string.shared_string_ok, new OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - android.view.View.OnClickListener cf = ssf.getConfirmListener(); - cf.onClick(null); - } - }) - .create(); - return dlg; - } + private void showAppDirDialogV19() { + Builder bld = new AlertDialog.Builder(this); + ChooseAppDirFragment frg = new DashChooseAppDirFragment.ChooseAppDirFragment(this, (Dialog) null); + bld.setView(frg.initView(getLayoutInflater(), null)); + AlertDialog dlg = bld.show(); + frg.setDialog(dlg); + } - public static ChooseAppDirDialogFragment newInstance() { - return new ChooseAppDirDialogFragment(); - } - } private void addMiscPreferences(PreferenceGroup misc) { diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java index f13a598e20..237a3474c3 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashChooseAppDirFragment.java @@ -24,6 +24,7 @@ import net.osmand.util.Algorithms; import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; +import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -32,6 +33,7 @@ import android.os.Build; import android.os.Bundle; import android.os.StatFs; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; @@ -48,25 +50,12 @@ import android.widget.Toast; public class DashChooseAppDirFragment extends DashBaseFragment { public static final String TAG = "DASH_CHOOSE_APP_DIR_FRAGMENT"; - public static final int VERSION_DEFAULTLOCATION_CHANGED = 19; - private TextView locationPath; - private TextView locationDesc; - MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); - private View copyMapsBtn; - private ImageView editBtn; - private View confirmBtn; - private boolean mapsCopied = false; - private TextView warningReadonly; - private int type = -1; - private File selectedFile = new File("/"); - private File currentAppFile; - private OsmandSettings settings; - private Activity activity; + private ChooseAppDirFragment fragment; @Override public void onAttach(Activity activity) { super.onAttach(activity); - this.activity = activity; + fragment = new ChooseAppDirFragment(activity, this); } @@ -77,348 +66,355 @@ public class DashChooseAppDirFragment extends DashBaseFragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - return initView(inflater, container); + return fragment.initView(inflater, container); } - private String getFreeSpace(File dir) { - if(dir.canRead()){ - StatFs fs = new StatFs(dir.getAbsolutePath()); - return formatGb.format(new Object[]{(float) (fs.getAvailableBlocks()) * fs.getBlockSize() / (1 << 30) }); - } - return ""; - } - public void updateView() { - if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT ) { - locationPath.setText(R.string.storage_directory_default); - } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_EXTERNAL_FILE) { - locationPath.setText(R.string.storage_directory_external); - } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_OBB) { - locationPath.setText(R.string.storage_directory_multiuser); - } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { - locationPath.setText(R.string.storage_directory_manual); - } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { - locationPath.setText(R.string.storage_directory_manual); - } - locationDesc.setText(selectedFile.getAbsolutePath() + " \u2022 " + getFreeSpace(selectedFile)); - boolean copyFiles = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()) && - !mapsCopied; - if (copyFiles) { - copyFiles = false; - File[] lf = currentAppFile.listFiles(); - if (lf != null) { - for (File f : lf) { - if (f != null) { - if (f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { - copyFiles = true; - break; - } - } - } - } - } - warningReadonly.setVisibility(copyFiles ? View.VISIBLE : View.GONE); - if(copyFiles) { - if(OsmandSettings.isWritable(currentAppFile)) { - warningReadonly.setText(getString(R.string.android_19_location_disabled, currentAppFile.getAbsolutePath())); - } else { - warningReadonly.setText(getString(R.string.application_dir_change_warning3)); - } - } - - copyMapsBtn.setVisibility(copyFiles ? View.VISIBLE : View.GONE); - } - - - - public View initView(LayoutInflater inflater, ViewGroup container) { - View view = inflater.inflate(R.layout.dash_storage_type_fragment, container, false); - settings = getMyApplication().getSettings(); - locationPath = (TextView) view.findViewById(R.id.location_path); - locationDesc = (TextView) view.findViewById(R.id.location_desc); - warningReadonly = (TextView) view.findViewById(R.id.android_19_location_changed); - currentAppFile = settings.getExternalStorageDirectory(); - selectedFile = currentAppFile; - if (settings.getExternalStorageDirectoryTypeV19() >= 0) { - type = settings.getExternalStorageDirectoryTypeV19(); - } else { - ValueHolder vh = new ValueHolder(); - settings.getExternalStorageDirectory(vh); - if (vh.value != null && vh.value >= 0) { - type = vh.value; - } else { - type = 0; - } - } - editBtn = (ImageView) view.findViewById(R.id.edit_icon); - copyMapsBtn = view.findViewById(R.id.copy_maps); - confirmBtn = view.findViewById(R.id.confirm); - addListeners(); - updateView(); - return view; - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - protected void showSelectDialog19() { - AlertDialog.Builder editalert = new AlertDialog.Builder(activity); - editalert.setTitle(R.string.application_dir); - final List items = new ArrayList(); - final List paths = new ArrayList(); - final TIntArrayList types = new TIntArrayList(); - int selected = -1; - if(type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { - items.add(getString(R.string.storage_directory_manual)); - paths.add(selectedFile.getAbsolutePath()); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED); - } - File df = settings.getDefaultInternalStorage(); - if(type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT || - OsmandSettings.isWritable(df)) { - if(type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) { - selected = items.size(); - } - items.add(getString(R.string.storage_directory_default)); - paths.add(df.getAbsolutePath()); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT); - } - - - File[] externals = getMyApplication().getExternalFilesDirs(null); - if(externals != null) { - int i = 1; - for(File external : externals) { - if(external != null) { - if(selectedFile.getAbsolutePath().equals(external.getAbsolutePath()) ) { - selected = items.size(); - } - items.add(getString(R.string.storage_directory_external) + " " + (i++)); - paths.add(external.getAbsolutePath()); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_EXTERNAL_FILE); - } - } - } - - File[] obbDirs = getMyApplication().getObbDirs(); - if(obbDirs != null) { - int i = 1; - for(File obb : obbDirs) { - if(obb != null) { - if(selectedFile.getAbsolutePath().equals(obb.getAbsolutePath()) ) { - selected = items.size(); - } - items.add(getString(R.string.storage_directory_multiuser) + " " + (i++)); - paths.add(obb.getAbsolutePath()); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_OBB); - } - } - } - - String pth = settings.getInternalAppPath().getAbsolutePath(); - if(selectedFile.getAbsolutePath().equals(pth) ) { - selected = items.size(); - } - items.add(getString(R.string.storage_directory_internal_app)); - paths.add(pth); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_INTERNAL_FILE); - - items.add(getString(R.string.storage_directory_manual) + getString(R.string.shared_string_ellipsis)); - paths.add(""); - types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED); - - editalert.setSingleChoiceItems(items.toArray(new String[items.size()]), - selected, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - if (which == items.size() - 1) { - dialog.dismiss(); - showOtherDialog(); - } else { - mapsCopied = false; - type = types.get(which); - selectedFile = new File(paths.get(which)); - dialog.dismiss(); - updateView(); - - } - } - }); - editalert.setNegativeButton(R.string.shared_string_dismiss, null); - editalert.show(); - } - - public void showOtherDialog(){ - AlertDialog.Builder editalert = new AlertDialog.Builder(activity); - editalert.setTitle(R.string.application_dir); - final EditText input = new EditText(activity); - input.setText(selectedFile.getAbsolutePath()); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT); - lp.leftMargin = lp.rightMargin = 5; - lp.bottomMargin = lp.topMargin = 5; - input.setLayoutParams(lp); - settings.getExternalStorageDirectory().getAbsolutePath(); - editalert.setView(input); - editalert.setNegativeButton(R.string.shared_string_cancel, null); - editalert.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - selectedFile = new File(input.getText().toString()); - mapsCopied = false; - updateView(); - } - }); - editalert.show(); - } - - private void addListeners() { - editBtn.setOnClickListener(new View.OnClickListener() { - - @Override - public void onClick(View v) { - if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - showOtherDialog(); - } else { - showSelectDialog19(); - } - } - }); - copyMapsBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - MoveFilesToDifferentDirectory task = new MoveFilesToDifferentDirectory(activity, currentAppFile, - selectedFile) { - - @Override - protected void onPostExecute(Boolean result) { - super.onPostExecute(result); - if (result) { - mapsCopied = true; - getMyApplication().getResourceManager().resetStoreDirectory(); - } else { - AccessibleToast.makeText(activity, R.string.copying_osmand_file_failed, Toast.LENGTH_SHORT) - .show(); - } - updateView(); - } - }; - task.execute(); - } - }); - confirmBtn.setOnClickListener(getConfirmListener()); - - } - - public OnClickListener getConfirmListener() { - return new View.OnClickListener() { - - @Override - public void onClick(View v) { - boolean wr = OsmandSettings.isWritable(selectedFile); - if(wr) { - boolean changed = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()); - getMyApplication().setExternalStorageDirectory(type, selectedFile.getAbsolutePath()); - if(changed) { - reloadData(); - } - if (activity instanceof FragmentActivity) { - ((FragmentActivity)activity).getSupportFragmentManager().beginTransaction().remove(DashChooseAppDirFragment.this) - .commit(); - } - } else { - AccessibleToast.makeText(activity, R.string.specified_directiory_not_writeable, - Toast.LENGTH_LONG).show(); - } - } - }; - } - - protected void reloadData() { - new ReloadData(activity, getMyApplication()).execute((Void)null ); - } - - public OsmandApplication getMyApplication(){ - if (activity == null){ - return null; - } - return (OsmandApplication) activity.getApplication(); - } - - public static class ReloadData extends AsyncTask { - private Context ctx; - protected ProgressImplementation progress; - private OsmandApplication app; - - public ReloadData(Context ctx, OsmandApplication app) { - this.ctx = ctx; - this.app = app; - } - - @Override - protected void onPreExecute() { - progress = ProgressImplementation.createProgressDialog(ctx, ctx.getString(R.string.loading_data), - ctx.getString(R.string.loading_data), ProgressDialog.STYLE_HORIZONTAL); - } - - @Override - protected void onPostExecute(Boolean result) { - if (progress.getDialog().isShowing()) { - progress.getDialog().dismiss(); - } - } - - @Override - protected Boolean doInBackground(Void... params) { - app.getResourceManager().reloadIndexes(progress, new ArrayList()); - return true; - } - } - public static boolean isDashNeeded(OsmandSettings settings) { - if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return false; } - // TODO - return true; -// return !settings.isExternalStorageDirectorySpecifiedV19(); + return !settings.isExternalStorageDirectorySpecifiedV19(); } - public static HashSet getExternalMounts() { - final HashSet out = new HashSet(); - String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*"; - String s = ""; - try { - final Process process = new ProcessBuilder().command("mount") - .redirectErrorStream(true).start(); - process.waitFor(); - final InputStream is = process.getInputStream(); - final byte[] buffer = new byte[1024]; - while (is.read(buffer) != -1) { - s = s + new String(buffer); - } - is.close(); - } catch (final Exception e) { - e.printStackTrace(); - } + + public static class ChooseAppDirFragment { + public static final int VERSION_DEFAULTLOCATION_CHANGED = 19; + private TextView locationPath; + private TextView locationDesc; + MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); + private View copyMapsBtn; + private ImageView editBtn; + private View confirmBtn; + private boolean mapsCopied = false; + private TextView warningReadonly; + private int type = -1; + private File selectedFile = new File("/"); + private File currentAppFile; + private OsmandSettings settings; + private Activity activity; + private Fragment fragment; + private Dialog dlg; + + public ChooseAppDirFragment(Activity activity, Fragment f) { + this.activity = activity; + this.fragment = f; + } + + public ChooseAppDirFragment(Activity activity, Dialog dlg) { + this.activity = activity; + this.dlg = dlg; + } + + private String getFreeSpace(File dir) { + if (dir.canRead()) { + StatFs fs = new StatFs(dir.getAbsolutePath()); + return formatGb + .format(new Object[] { (float) (fs.getAvailableBlocks()) * fs.getBlockSize() / (1 << 30) }); + } + return ""; + } + + public void updateView() { + if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) { + locationPath.setText(R.string.storage_directory_default); + } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_EXTERNAL_FILE) { + locationPath.setText(R.string.storage_directory_external); + } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_OBB) { + locationPath.setText(R.string.storage_directory_multiuser); + } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { + locationPath.setText(R.string.storage_directory_manual); + } else if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { + locationPath.setText(R.string.storage_directory_manual); + } + locationDesc.setText(selectedFile.getAbsolutePath() + " \u2022 " + getFreeSpace(selectedFile)); + boolean copyFiles = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()) && !mapsCopied; + if (copyFiles) { + copyFiles = false; + File[] lf = currentAppFile.listFiles(); + if (lf != null) { + for (File f : lf) { + if (f != null) { + if (f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { + copyFiles = true; + break; + } + } + } + } + } + warningReadonly.setVisibility(copyFiles ? View.VISIBLE : View.GONE); + if (copyFiles) { + if (!OsmandSettings.isWritable(currentAppFile)) { + warningReadonly.setText(activity.getString(R.string.android_19_location_disabled, + currentAppFile.getAbsolutePath())); + } else { + warningReadonly.setText(getString(R.string.application_dir_change_warning3)); + } + } + + copyMapsBtn.setVisibility(copyFiles ? View.VISIBLE : View.GONE); + } + + public View initView(LayoutInflater inflater, ViewGroup container) { + View view = inflater.inflate(R.layout.dash_storage_type_fragment, container, false); + settings = getMyApplication().getSettings(); + locationPath = (TextView) view.findViewById(R.id.location_path); + locationDesc = (TextView) view.findViewById(R.id.location_desc); + warningReadonly = (TextView) view.findViewById(R.id.android_19_location_changed); + currentAppFile = settings.getExternalStorageDirectory(); + selectedFile = currentAppFile; + if (settings.getExternalStorageDirectoryTypeV19() >= 0) { + type = settings.getExternalStorageDirectoryTypeV19(); + } else { + ValueHolder vh = new ValueHolder(); + settings.getExternalStorageDirectory(vh); + if (vh.value != null && vh.value >= 0) { + type = vh.value; + } else { + type = 0; + } + } + editBtn = (ImageView) view.findViewById(R.id.edit_icon); + copyMapsBtn = view.findViewById(R.id.copy_maps); + confirmBtn = view.findViewById(R.id.confirm); + addListeners(); + updateView(); + return view; + } + + public String getString(int string) { + return activity.getString(string); + } + + @TargetApi(Build.VERSION_CODES.KITKAT) + protected void showSelectDialog19() { + AlertDialog.Builder editalert = new AlertDialog.Builder(activity); + editalert.setTitle(R.string.application_dir); + final List items = new ArrayList(); + final List paths = new ArrayList(); + final TIntArrayList types = new TIntArrayList(); + int selected = -1; + if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED) { + items.add(getString(R.string.storage_directory_manual)); + paths.add(selectedFile.getAbsolutePath()); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED); + } + File df = settings.getDefaultInternalStorage(); + if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT || OsmandSettings.isWritable(df)) { + if (type == OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT) { + selected = items.size(); + } + items.add(getString(R.string.storage_directory_default)); + paths.add(df.getAbsolutePath()); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_DEFAULT); + } + + File[] externals = getMyApplication().getExternalFilesDirs(null); + if (externals != null) { + int i = 1; + for (File external : externals) { + if (external != null) { + if (selectedFile.getAbsolutePath().equals(external.getAbsolutePath())) { + selected = items.size(); + } + items.add(getString(R.string.storage_directory_external) + " " + (i++)); + paths.add(external.getAbsolutePath()); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_EXTERNAL_FILE); + } + } + } + + File[] obbDirs = getMyApplication().getObbDirs(); + if (obbDirs != null) { + int i = 1; + for (File obb : obbDirs) { + if (obb != null) { + if (selectedFile.getAbsolutePath().equals(obb.getAbsolutePath())) { + selected = items.size(); + } + items.add(getString(R.string.storage_directory_multiuser) + " " + (i++)); + paths.add(obb.getAbsolutePath()); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_OBB); + } + } + } + + String pth = settings.getInternalAppPath().getAbsolutePath(); + if (selectedFile.getAbsolutePath().equals(pth)) { + selected = items.size(); + } + items.add(getString(R.string.storage_directory_internal_app)); + paths.add(pth); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_INTERNAL_FILE); + + items.add(getString(R.string.storage_directory_manual) + getString(R.string.shared_string_ellipsis)); + paths.add(""); + types.add(OsmandSettings.EXTERNAL_STORAGE_TYPE_SPECIFIED); + + editalert.setSingleChoiceItems(items.toArray(new String[items.size()]), selected, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == items.size() - 1) { + dialog.dismiss(); + showOtherDialog(); + } else { + mapsCopied = false; + type = types.get(which); + selectedFile = new File(paths.get(which)); + dialog.dismiss(); + updateView(); + + } + } + }); + editalert.setNegativeButton(R.string.shared_string_dismiss, null); + editalert.show(); + } + + public void showOtherDialog() { + AlertDialog.Builder editalert = new AlertDialog.Builder(activity); + editalert.setTitle(R.string.application_dir); + final EditText input = new EditText(activity); + input.setText(selectedFile.getAbsolutePath()); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + lp.leftMargin = lp.rightMargin = 5; + lp.bottomMargin = lp.topMargin = 5; + input.setLayoutParams(lp); + settings.getExternalStorageDirectory().getAbsolutePath(); + editalert.setView(input); + editalert.setNegativeButton(R.string.shared_string_cancel, null); + editalert.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + selectedFile = new File(input.getText().toString()); + mapsCopied = false; + updateView(); + } + }); + editalert.show(); + } + + private void addListeners() { + editBtn.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { + showOtherDialog(); + } else { + showSelectDialog19(); + } + } + }); + copyMapsBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + MoveFilesToDifferentDirectory task = new MoveFilesToDifferentDirectory(activity, currentAppFile, + selectedFile) { + + @Override + protected void onPostExecute(Boolean result) { + super.onPostExecute(result); + if (result) { + mapsCopied = true; + getMyApplication().getResourceManager().resetStoreDirectory(); + } else { + AccessibleToast.makeText(activity, R.string.copying_osmand_file_failed, + Toast.LENGTH_SHORT).show(); + } + updateView(); + } + }; + task.execute(); + } + }); + confirmBtn.setOnClickListener(getConfirmListener()); + + } + + public OnClickListener getConfirmListener() { + return new View.OnClickListener() { + + @Override + public void onClick(View v) { + boolean wr = OsmandSettings.isWritable(selectedFile); + if (wr) { + boolean changed = !currentAppFile.getAbsolutePath().equals(selectedFile.getAbsolutePath()); + getMyApplication().setExternalStorageDirectory(type, selectedFile.getAbsolutePath()); + if (changed) { + reloadData(); + } + if (fragment != null && activity instanceof FragmentActivity) { + ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction() + .remove(fragment).commit(); + } + } else { + AccessibleToast.makeText(activity, R.string.specified_directiory_not_writeable, + Toast.LENGTH_LONG).show(); + } + if(dlg != null) { + dlg.dismiss(); + } + } + }; + } + + protected void reloadData() { + new ReloadData(activity, getMyApplication()).execute((Void) null); + } + + public OsmandApplication getMyApplication() { + if (activity == null) { + return null; + } + return (OsmandApplication) activity.getApplication(); + } + + + + + public static HashSet getExternalMounts() { + final HashSet out = new HashSet(); + String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*"; + String s = ""; + try { + final Process process = new ProcessBuilder().command("mount").redirectErrorStream(true).start(); + process.waitFor(); + final InputStream is = process.getInputStream(); + final byte[] buffer = new byte[1024]; + while (is.read(buffer) != -1) { + s = s + new String(buffer); + } + is.close(); + } catch (final Exception e) { + e.printStackTrace(); + } + + // parse output + final String[] lines = s.split("\n"); + for (String line : lines) { + if (!line.toLowerCase(Locale.US).contains("asec")) { + if (line.matches(reg)) { + String[] parts = line.split(" "); + for (String part : parts) { + if (part.startsWith("/")) + if (!part.toLowerCase(Locale.US).contains("vold")) + out.add(part); + } + } + } + } + return out; + } + + public void setDialog(Dialog dlg) { + this.dlg = dlg; + } - // parse output - final String[] lines = s.split("\n"); - for (String line : lines) { - if (!line.toLowerCase(Locale.US).contains("asec")) { - if (line.matches(reg)) { - String[] parts = line.split(" "); - for (String part : parts) { - if (part.startsWith("/")) - if (!part.toLowerCase(Locale.US).contains("vold")) - out.add(part); - } - } - } - } - return out; } - - public static class MoveFilesToDifferentDirectory extends AsyncTask { @@ -513,5 +509,34 @@ public class DashChooseAppDirFragment extends DashBaseFragment { } } + + public static class ReloadData extends AsyncTask { + private Context ctx; + protected ProgressImplementation progress; + private OsmandApplication app; + public ReloadData(Context ctx, OsmandApplication app) { + this.ctx = ctx; + this.app = app; + } + + @Override + protected void onPreExecute() { + progress = ProgressImplementation.createProgressDialog(ctx, ctx.getString(R.string.loading_data), + ctx.getString(R.string.loading_data), ProgressDialog.STYLE_HORIZONTAL); + } + + @Override + protected void onPostExecute(Boolean result) { + if (progress.getDialog().isShowing()) { + progress.getDialog().dismiss(); + } + } + + @Override + protected Boolean doInBackground(Void... params) { + app.getResourceManager().reloadIndexes(progress, new ArrayList()); + return true; + } + } }