From d327930197bb068b739a5b9ab8faa1440044425e Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Thu, 27 Sep 2018 23:37:10 +0200 Subject: [PATCH] handle orientation changes manually -> screen rotate is ~instantaneous (fixes #1217) --- app/src/main/AndroidManifest.xml | 3 ++- .../streetcomplete/MainActivity.java | 23 ++++++++++++++----- .../data/osmnotes/CreateNoteFragment.java | 16 ++++++++++++- .../quests/AbstractBottomSheetFragment.java | 23 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 1 + .../main/res/layout/marker_create_note.xml | 1 + 6 files changed, 58 insertions(+), 9 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ad4597dd7d..a1a0e97ed5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,7 +25,8 @@ + android:label="@string/app_name" + android:configChanges="orientation|screenSize"> diff --git a/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java b/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java index 206e92fff2..2f60439ce0 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java +++ b/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java @@ -1,6 +1,7 @@ package de.westnordost.streetcomplete; import android.animation.ObjectAnimator; +import android.content.res.Configuration; import android.graphics.Point; import android.graphics.PointF; import android.support.annotation.DrawableRes; @@ -236,13 +237,8 @@ public void onServiceDisconnected(ComponentName className) downloadProgressBar.setMax(1000); mapFragment = (QuestsMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment); - mapFragment.setQuestOffsets(new Rect( - getResources().getDimensionPixelSize(R.dimen.quest_form_leftOffset), - 0, - getResources().getDimensionPixelSize(R.dimen.quest_form_rightOffset), - getResources().getDimensionPixelSize(R.dimen.quest_form_bottomOffset))); - mapFragment.getMapAsync(BuildConfig.MAPZEN_API_KEY); + updateMapQuestOffsets(); } @Override public void onStart() @@ -331,6 +327,21 @@ public void onServiceDisconnected(ComponentName className) questController.onDestroy(); } + @Override public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + findViewById(R.id.main).requestLayout(); + updateMapQuestOffsets(); + } + + private void updateMapQuestOffsets() + { + mapFragment.setQuestOffsets(new Rect( + getResources().getDimensionPixelSize(R.dimen.quest_form_leftOffset), + 0, + getResources().getDimensionPixelSize(R.dimen.quest_form_rightOffset), + getResources().getDimensionPixelSize(R.dimen.quest_form_bottomOffset))); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/osmnotes/CreateNoteFragment.java b/app/src/main/java/de/westnordost/streetcomplete/data/osmnotes/CreateNoteFragment.java index b13c3af666..673d7a8f6d 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/osmnotes/CreateNoteFragment.java +++ b/app/src/main/java/de/westnordost/streetcomplete/data/osmnotes/CreateNoteFragment.java @@ -1,6 +1,7 @@ package de.westnordost.streetcomplete.data.osmnotes; import android.content.Context; +import android.content.res.Configuration; import android.graphics.Point; import android.os.Bundle; import android.support.annotation.NonNull; @@ -24,7 +25,8 @@ public class CreateNoteFragment extends AbstractCreateNoteFragment { private EditText noteInput; - private View markerLayout; + private ViewGroup markerLayout; + private ViewGroup centeredMarkerLayout; private View marker; private CreateNoteListener callbackListener; @@ -39,6 +41,7 @@ public class CreateNoteFragment extends AbstractCreateNoteFragment { markerLayout.startAnimation(createFallDownAnimation()); } + centeredMarkerLayout = view.findViewById(R.id.centered_marker_layout_create_note); marker = view.findViewById(R.id.marker_create_note); @@ -50,6 +53,17 @@ public class CreateNoteFragment extends AbstractCreateNoteFragment return view; } + @Override public void onConfigurationChanged(Configuration newConfig) + { + super.onConfigurationChanged(newConfig); + centeredMarkerLayout.setPadding( + getResources().getDimensionPixelSize(R.dimen.quest_form_leftOffset), + getResources().getDimensionPixelSize(R.dimen.quest_form_topOffset), + getResources().getDimensionPixelSize(R.dimen.quest_form_rightOffset), + getResources().getDimensionPixelSize(R.dimen.quest_form_bottomOffset) + ); + } + private Animation createFallDownAnimation() { AnimationSet a = new AnimationSet(false); diff --git a/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.java b/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.java index bc9d3ff144..e0d96d418b 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.java +++ b/app/src/main/java/de/westnordost/streetcomplete/quests/AbstractBottomSheetFragment.java @@ -9,7 +9,9 @@ import android.support.design.widget.BottomSheetBehavior; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; +import android.util.DisplayMetrics; import android.view.View; +import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.LinearLayout; @@ -22,6 +24,7 @@ public abstract class AbstractBottomSheetFragment extends Fragment { private LinearLayout bottomSheet; + private BottomSheetBehavior bottomSheetBehavior; private View buttonClose; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) @@ -39,7 +42,7 @@ public abstract class AbstractBottomSheetFragment extends Fragment buttonClose = view.findViewById(R.id.close_btn); buttonClose.setOnClickListener(v -> getActivity().onBackPressed()); - BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); + bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); View titleSpeechBubble = view.findViewById(R.id.titleSpeechBubble); titleSpeechBubble.setOnClickListener(v -> { @@ -74,6 +77,24 @@ else if(bottomSheetBehavior.getState() == STATE_COLLAPSED) } } + @Override public void onConfigurationChanged(Configuration newConfig) + { + super.onConfigurationChanged(newConfig); + // I need to do everything myself... (AppCompactActivity only does this after calling this + // method. Genius!) + getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics()); + + bottomSheetBehavior.setPeekHeight( + getResources().getDimensionPixelSize(R.dimen.quest_form_peekHeight)); + View root = getView(); + if(root != null) + { + ViewGroup.LayoutParams params = root.getLayoutParams(); + params.width = getResources().getDimensionPixelSize(R.dimen.quest_form_width); + root.setLayoutParams(params); + } + } + private void updateCloseButtonVisibility() { // this is called asynchronously. It may happen that the activity is already gone when this diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ac74b1216e..6ba7efcd50 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,7 @@