Moved loation listners from fragment to activity

This commit is contained in:
Denis 2014-12-16 17:21:51 +02:00
parent d0da057585
commit bdf161bbb8
2 changed files with 128 additions and 102 deletions

View file

@ -9,6 +9,7 @@ import java.util.Random;
import android.graphics.Color;
import android.os.Build;
import android.view.Gravity;
import net.osmand.Location;
import net.osmand.access.AccessibleAlertBuilder;
import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmAndLocationProvider;
@ -50,36 +51,44 @@ import net.osmand.plus.views.controls.FloatingActionButton;
/**
*/
public class MainMenuActivity extends BaseDownloadActivity {
public class MainMenuActivity extends BaseDownloadActivity implements OsmAndLocationProvider.OsmAndCompassListener, OsmAndLocationProvider.OsmAndLocationListener {
private static final String LATEST_CHANGES_URL = "changes-1.9.html";
public static final boolean TIPS_AND_TRICKS = false;
public static final int APP_EXIT_CODE = 4;
public static final String APP_EXIT_KEY = "APP_EXIT_KEY";
private static final String FIRST_TIME_APP_RUN = "FIRST_TIME_APP_RUN"; //$NON-NLS-1$
private static final String VECTOR_INDEXES_CHECK = "VECTOR_INDEXES_CHECK"; //$NON-NLS-1$
private static final String TIPS_SHOW = "TIPS_SHOW"; //$NON-NLS-1$
private static final String VERSION_INSTALLED = "VERSION_INSTALLED"; //$NON-NLS-1$
private static final String EXCEPTION_FILE_SIZE = "EXCEPTION_FS"; //$NON-NLS-1$
private static final String CONTRIBUTION_VERSION_FLAG = "CONTRIBUTION_VERSION_FLAG";
private static final int HELP_ID = 0;
private static final int SETTINGS_ID = 1;
private static final int EXIT_ID = 2;
private OsmAndLocationProvider lp;
@Override
public void onPause() {
super.onPause();
getMyApplication().getLocationProvider().removeCompassListener(this);
getMyApplication().getLocationProvider().removeLocationListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
getMyApplication().applyTheme(this);
super.onCreate(savedInstanceState);
if(getIntent() != null){
if (getIntent() != null) {
Intent intent = getIntent();
if(intent.getExtras() != null && intent.getExtras().containsKey(APP_EXIT_KEY)){
if (intent.getExtras() != null && intent.getExtras().containsKey(APP_EXIT_KEY)) {
getMyApplication().closeApplication(this);
return;
}
}
if(Version.isSherpafy(getMyApplication())) {
if (Version.isSherpafy(getMyApplication())) {
final Intent mapIntent = new Intent(this, TourViewActivity.class);
startActivity(mapIntent);
finish();
@ -87,18 +96,18 @@ public class MainMenuActivity extends BaseDownloadActivity {
}
setContentView(R.layout.dashboard);
lp = getMyApplication().getLocationProvider();
String textVersion = Version.getFullVersion(getMyApplication());
if(textVersion.indexOf("#") != -1) {
if (textVersion.indexOf("#") != -1) {
textVersion = textVersion.substring(0, textVersion.indexOf("#") + 1);
}
getSupportActionBar().setTitle(textVersion);
ColorDrawable color = new ColorDrawable(getResources().getColor(R.color.actionbar_color));
getSupportActionBar().setBackgroundDrawable(color);
getSupportActionBar().setIcon(android.R.color.transparent);
boolean firstTime = initApp(this, getMyApplication());
if(getMyApplication().getAppCustomization().checkExceptionsOnStart()){
if (getMyApplication().getAppCustomization().checkExceptionsOnStart()) {
checkPreviousRunsForExceptions(firstTime);
}
setupContributionVersion();
@ -118,15 +127,26 @@ public class MainMenuActivity extends BaseDownloadActivity {
});
}
}
@Override
protected void onResume() {
super.onResume();
if (getMyApplication().getFavorites().getFavouritePoints().size() > 0) {
getLocationProvider().addLocationListener(this);
getLocationProvider().addCompassListener(this);
getLocationProvider().registerOrUnregisterCompassListener(true);
getLocationProvider().resumeAllUpdates();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == APP_EXIT_CODE){
if (resultCode == APP_EXIT_CODE) {
getMyApplication().closeApplication(this);
}
}
protected void setupContributionVersion() {
findViewById(R.id.credentials).setVisibility(View.VISIBLE);
//Copyright notes and links have been put on the 'About' screen
@ -155,80 +175,80 @@ public class MainMenuActivity extends BaseDownloadActivity {
final String aboutString = getString(R.string.about_settings);
SpannableString ss = new SpannableString(aboutString);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
showAboutDialog(MainMenuActivity.this, getMyApplication());
}
@Override
public void onClick(View textView) {
showAboutDialog(MainMenuActivity.this, getMyApplication());
}
};
ss.setSpan(clickableSpan, 0, aboutString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
about.setText(ss);
about.setMovementMethod(LinkMovementMethod.getInstance());
}
private void addErrorFragment(){
private void addErrorFragment() {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
if (manager.findFragmentByTag(DashErrorFragment.TAG) == null){
if (manager.findFragmentByTag(DashErrorFragment.TAG) == null) {
DashErrorFragment errorFragment = new DashErrorFragment();
fragmentTransaction.add(R.id.content, errorFragment, DashErrorFragment.TAG).commit();
}
}
public static void showAboutDialog(final Activity activity, final OsmandApplication app) {
Builder bld = new AlertDialog.Builder(activity);
bld.setTitle(R.string.about_settings);
ScrollView sv = new ScrollView(activity);
TextView tv = new TextView(activity);
sv.addView(tv);
ScrollView sv = new ScrollView(activity);
TextView tv = new TextView(activity);
sv.addView(tv);
String version = Version.getFullVersion(app);
String vt = activity.getString(R.string.about_version) +"\t";
String vt = activity.getString(R.string.about_version) + "\t";
int st = vt.length();
String edition = "";
if (!activity.getString(R.string.app_edition).equals("")) {
edition = activity.getString(R.string.local_index_installed) + " : \t" + activity.getString(R.string.app_edition);
}
SharedPreferences prefs = app.getSharedPreferences("net.osmand.settings", MODE_WORLD_READABLE);
if (prefs.contains(CONTRIBUTION_VERSION_FLAG) && Version.isDeveloperVersion(app)) {
//Next 7 lines produced bogus Edition dates in many situtations, let us try (see above) to use the BUILD_ID as delivered from builder
//try {
//PackageManager pm = activity.getPackageManager();
//ApplicationInfo appInfo = pm.getApplicationInfo(OsmandApplication.class.getPackage().getName(), 0);
//Date date = new Date(new File(appInfo.sourceDir).lastModified());
//edition = activity.getString(R.string.local_index_installed) + " : \t" + DateFormat.getDateFormat(app).format(date);
//} catch (Exception e) {
//}
SpannableString content = new SpannableString(vt + version + "\n" +
edition + "\n\n" +
activity.getString(R.string.about_content));
content.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
final Intent mapIntent = new Intent(activity, ContributionVersionActivity.class);
activity.startActivityForResult(mapIntent, 0);
}
}, st, st + version.length(), 0);
tv.setText(content);
} else {
tv.setText(vt + version + "\n" +
edition + "\n\n" +
activity.getString(R.string.about_content));
}
SharedPreferences prefs = app.getSharedPreferences("net.osmand.settings", MODE_WORLD_READABLE);
if (prefs.contains(CONTRIBUTION_VERSION_FLAG) && Version.isDeveloperVersion(app)) {
//Next 7 lines produced bogus Edition dates in many situtations, let us try (see above) to use the BUILD_ID as delivered from builder
//try {
//PackageManager pm = activity.getPackageManager();
//ApplicationInfo appInfo = pm.getApplicationInfo(OsmandApplication.class.getPackage().getName(), 0);
//Date date = new Date(new File(appInfo.sourceDir).lastModified());
//edition = activity.getString(R.string.local_index_installed) + " : \t" + DateFormat.getDateFormat(app).format(date);
//} catch (Exception e) {
//}
SpannableString content = new SpannableString(vt + version + "\n" +
edition + "\n\n" +
activity.getString(R.string.about_content));
content.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
final Intent mapIntent = new Intent(activity, ContributionVersionActivity.class);
activity.startActivityForResult(mapIntent, 0);
}
}, st, st + version.length(), 0);
tv.setText(content);
} else {
tv.setText(vt + version + "\n" +
edition + "\n\n" +
activity.getString(R.string.about_content));
}
tv.setPadding(5, 0, 5, 5);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
tv.setMovementMethod(LinkMovementMethod.getInstance());
bld.setView(sv);
bld.setPositiveButton(R.string.default_buttons_ok, null);
bld.show();
}
protected boolean initApp(final Activity activity, OsmandApplication app) {
final OsmAndAppCustomization appCustomization = app.getAppCustomization();
// restore follow route mode
if(app.getSettings().FOLLOW_THE_ROUTE.get() && !app.getRoutingHelper().isRouteCalculated()){
if (app.getSettings().FOLLOW_THE_ROUTE.get() && !app.getRoutingHelper().isRouteCalculated()) {
startMapActivity();
return false;
}
@ -284,8 +304,8 @@ public class MainMenuActivity extends BaseDownloadActivity {
} catch (NameNotFoundException e) {
netOsmandWasInstalled = false;
}
if(netOsmandWasInstalled){
if (netOsmandWasInstalled) {
// Builder builder = new AccessibleAlertBuilder(this);
// builder.setMessage(R.string.osmand_net_previously_installed);
// builder.setPositiveButton(R.string.default_buttons_ok, null);
@ -305,7 +325,7 @@ public class MainMenuActivity extends BaseDownloadActivity {
builder.show();
}
}
public void checkPreviousRunsForExceptions(boolean firstTime) {
long size = getPreferences(MODE_WORLD_READABLE).getLong(EXCEPTION_FILE_SIZE, 0);
final OsmandApplication app = ((OsmandApplication) getApplication());
@ -323,7 +343,6 @@ public class MainMenuActivity extends BaseDownloadActivity {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, HELP_ID, 0, R.string.tips_and_tricks).setIcon(R.drawable.ic_ac_help)
@ -345,7 +364,7 @@ public class MainMenuActivity extends BaseDownloadActivity {
public boolean onOptionsItemSelected(MenuItem item) {
OsmAndAppCustomization appCustomization = getMyApplication().getAppCustomization();
if (item.getItemId() == HELP_ID) {
if(TIPS_AND_TRICKS) {
if (TIPS_AND_TRICKS) {
TipsAndTricksActivity activity = new TipsAndTricksActivity(this);
Dialog dlg = activity.getDialogToShowTips(false, true);
dlg.show();
@ -353,10 +372,10 @@ public class MainMenuActivity extends BaseDownloadActivity {
final Intent helpIntent = new Intent(this, HelpActivity.class);
startActivity(helpIntent);
}
} else if (item.getItemId() == SETTINGS_ID){
} else if (item.getItemId() == SETTINGS_ID) {
final Intent settings = new Intent(this, appCustomization.getSettingsActivity());
startActivity(settings);
} else if (item.getItemId() == EXIT_ID){
} else if (item.getItemId() == EXIT_ID) {
getMyApplication().closeApplication(this);
}
return true;
@ -369,9 +388,9 @@ public class MainMenuActivity extends BaseDownloadActivity {
// do not show each time
if (check && new Random().nextInt() % 5 == 1) {
Builder builder = new AccessibleAlertBuilder(this);
if(maps.isEmpty()){
if (maps.isEmpty()) {
builder.setMessage(R.string.vector_data_missing);
} else if(!maps.basemapExists()){
} else if (!maps.basemapExists()) {
builder.setMessage(R.string.basemap_missing);
} else {
return;
@ -393,9 +412,9 @@ public class MainMenuActivity extends BaseDownloadActivity {
builder.setNegativeButton(R.string.first_time_continue, null);
builder.show();
}
}
// private static void enableLink(final Activity activity, String textVersion, TextView textVersionView) {
// SpannableString content = new SpannableString(textVersion);
// content.setSpan(new ClickableSpan() {
@ -413,37 +432,57 @@ public class MainMenuActivity extends BaseDownloadActivity {
@Override
public void updateProgress(boolean updateOnlyProgress) {
BasicProgressAsyncTask<?, ?, ?> basicProgressAsyncTask = BaseDownloadActivity.downloadListIndexThread.getCurrentRunningTask();
for(WeakReference<Fragment> ref : fragList) {
for (WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if(f instanceof DashUpdatesFragment) {
if(!f.isDetached()) {
((DashUpdatesFragment) f).updateProgress(basicProgressAsyncTask, updateOnlyProgress);
}
if (f instanceof DashUpdatesFragment && !f.isDetached()) {
((DashUpdatesFragment) f).updateProgress(basicProgressAsyncTask, updateOnlyProgress);
}
}
}
@Override
public void updateDownloadList(List<IndexItem> list){
for(WeakReference<Fragment> ref : fragList) {
public void updateDownloadList(List<IndexItem> list) {
for (WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if(f instanceof DashUpdatesFragment) {
if(!f.isDetached()) {
if (downloadQueue.size() > 0){
startDownload(downloadQueue.get(0));
downloadQueue.remove(0);
}
((DashUpdatesFragment) f).updatedDownloadsList(list);
if (f instanceof DashUpdatesFragment && !f.isDetached()) {
if (downloadQueue.size() > 0) {
startDownload(downloadQueue.get(0));
downloadQueue.remove(0);
}
((DashUpdatesFragment) f).updatedDownloadsList(list);
}
if(f instanceof DashDownloadMapsFragment) {
if(!f.isDetached()) {
((DashDownloadMapsFragment) f).refreshData();
}
if (f instanceof DashDownloadMapsFragment && !f.isDetached()) {
((DashDownloadMapsFragment) f).refreshData();
}
if(f instanceof DashAudioVideoNotesFragment && !f.isDetached()){
if (f instanceof DashAudioVideoNotesFragment && !f.isDetached()) {
((DashAudioVideoNotesFragment) f).setupNotes();
}
}
}
@Override
public void updateCompassValue(float value) {
for (WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if (f instanceof DashFavoritesFragment && !f.isDetached()) {
((DashFavoritesFragment) f).updateCompassValue(value);
}
}
}
@Override
public void updateLocation(Location location) {
for (WeakReference<Fragment> ref : fragList) {
Fragment f = ref.get();
if (f instanceof DashFavoritesFragment && !f.isDetached()) {
((DashFavoritesFragment) f).updateLocation(location);
}
}
}
private OsmAndLocationProvider getLocationProvider() {
return getMyApplication().getLocationProvider();
}
}

View file

@ -34,7 +34,7 @@ import java.util.List;
/**
* Created by Denis on 24.11.2014.
*/
public class DashFavoritesFragment extends DashBaseFragment implements OsmAndLocationProvider.OsmAndCompassListener, OsmAndLocationProvider.OsmAndLocationListener {
public class DashFavoritesFragment extends DashBaseFragment {
public static final String TAG = "DASH_FAVORITES_FRAGMENT";
private net.osmand.Location location = null;
private Float heading = null;
@ -62,18 +62,12 @@ public class DashFavoritesFragment extends DashBaseFragment implements OsmAndLoc
return view;
}
@Override
public void onPause() {
super.onPause();
getLocationProvider().removeCompassListener(this);
getLocationProvider().removeLocationListener(this);
}
@Override
public void onResume() {
super.onResume();
if (getMyApplication().getFavorites().getFavouritePoints().size() > 0) {
registerListeners();
if(!getMyApplication().getSettings().isLastKnownMapLocation()) {
// show first time when application ran
location = getMyApplication().getLocationProvider().getFirstTimeRunDefaultLocation();
@ -88,12 +82,7 @@ public class DashFavoritesFragment extends DashBaseFragment implements OsmAndLoc
}
private void registerListeners() {
getLocationProvider().addLocationListener(this);
getLocationProvider().addCompassListener(this);
getLocationProvider().registerOrUnregisterCompassListener(true);
getLocationProvider().resumeAllUpdates();
}
private void setupFavorites(){
View mainView = getView();
@ -189,13 +178,11 @@ public class DashFavoritesFragment extends DashBaseFragment implements OsmAndLoc
direction.setImageDrawable(draw);
}
@Override
public void updateCompassValue(float value) {
heading = value;
updateArrows();
}
@Override
public void updateLocation(Location location) {
if (location == null){
return;