33import android .Manifest ;
44import android .content .ContentResolver ;
55import android .content .Intent ;
6+ import android .content .SharedPreferences ;
67import android .content .pm .PackageManager ;
78import android .net .Uri ;
89import android .os .Bundle ;
10+ import androidx .preference .PreferenceManager ;
11+
12+ import android .os .Handler ;
913import android .text .TextUtils ;
1014import android .util .Log ;
1115import android .view .MenuItem ;
16+ import android .view .MotionEvent ;
1217import android .widget .Toast ;
1318
1419import androidx .annotation .NonNull ;
20+ import androidx .appcompat .app .ActionBar ;
1521import androidx .appcompat .app .AppCompatActivity ;
1622import androidx .core .app .ActivityCompat ;
17- import androidx .fragment .app .Fragment ;
23+ import androidx .core .view .WindowCompat ;
24+ import androidx .core .view .WindowInsetsCompat ;
25+ import androidx .core .view .WindowInsetsControllerCompat ;
1826import androidx .lifecycle .ViewModelProvider ;
1927
2028import org .nitri .opentopo .model .GpxViewModel ;
@@ -41,6 +49,9 @@ public class MainActivity extends AppCompatActivity implements MapFragment.OnFra
4149 protected static final String WAY_POINT_DETAIL_FRAGMENT_TAG = "way_point_detail_fragment" ;
4250 protected static final String NEARBY_FRAGMENT_TAG = "nearby_fragment" ;
4351
52+ private static final String PREF_FULLSCREEN = "fullscreen" ;
53+ private static final String PREF_FULLSCREEN_ON_MAP_TAP = "fullscreen_on_map_tap" ;
54+
4455 private static final int REQUEST_LOCATION_PERMISSION = 1 ;
4556 private static final int REQUEST_STORAGE_PERMISSION = 2 ;
4657 private static final int READ_REQUEST_CODE = 69 ;
@@ -51,8 +62,14 @@ public class MainActivity extends AppCompatActivity implements MapFragment.OnFra
5162 private Uri mGpxUri ;
5263 private boolean mZoomToGpx ;
5364 private NearbyItem mSelectedNearbyPlace ;
54- private Fragment mMapFragment ;
65+ private MapFragment mMapFragment ;
5566 private GpxViewModel mGpxViewModel ;
67+ boolean mFullscreen = false ;
68+ private WindowInsetsControllerCompat windowInsetsController ;
69+ private ActionBar actionBar ;
70+ private boolean mFullscreenOnMapTap ;
71+ private SharedPreferences mPrefs ;
72+ private Handler handler ;
5673
5774 @ Override
5875 protected void onCreate (Bundle savedInstanceState ) {
@@ -62,6 +79,12 @@ protected void onCreate(Bundle savedInstanceState) {
6279 mGpxUriString = savedInstanceState .getString (GPX_URI_STATE );
6380 }
6481
82+ handler = new Handler (getMainLooper ());
83+
84+ mPrefs = PreferenceManager .getDefaultSharedPreferences (this );
85+
86+ mFullscreenOnMapTap = mPrefs .getBoolean (PREF_FULLSCREEN_ON_MAP_TAP , false );
87+
6588 mGpxViewModel = new ViewModelProvider (this ).get (GpxViewModel .class );
6689
6790 Intent intent = getIntent ();
@@ -77,8 +100,24 @@ protected void onCreate(Bundle savedInstanceState) {
77100 }
78101
79102 if (savedInstanceState != null ) {
80- mMapFragment = getSupportFragmentManager ().getFragment (savedInstanceState , MAP_FRAGMENT_TAG );
103+ mMapFragment = ( MapFragment ) getSupportFragmentManager ().getFragment (savedInstanceState , MAP_FRAGMENT_TAG );
81104 }
105+
106+ windowInsetsController =
107+ WindowCompat .getInsetsController (getWindow (), getWindow ().getDecorView ());
108+ windowInsetsController .setSystemBarsBehavior (
109+ WindowInsetsControllerCompat .BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
110+ );
111+
112+ actionBar = getSupportActionBar ();
113+
114+ setFullscreen (mPrefs .getBoolean (PREF_FULLSCREEN , false ));
115+ }
116+
117+ @ Override
118+ public boolean dispatchTouchEvent (MotionEvent ev ) {
119+ //tapDetector.onTouchEvent(ev);
120+ return super .dispatchTouchEvent (ev );
82121 }
83122
84123 private void handleIntent (Intent intent ) {
@@ -101,13 +140,48 @@ private void handleIntent(Intent intent) {
101140 }
102141 }
103142
143+ private void setFullscreen (boolean fullscreen ) {
144+ handler .removeCallbacksAndMessages (null );
145+ if (windowInsetsController == null )
146+ return ;
147+ if (fullscreen ) {
148+ windowInsetsController .hide (WindowInsetsCompat .Type .systemBars ());
149+ if (actionBar != null ) {
150+ actionBar .hide ();
151+ }
152+ if (mMapFragment != null ) {
153+ handler .postDelayed ( () ->
154+ mMapFragment .showZoomControls (false ), 3000 );
155+ }
156+ } else {
157+ windowInsetsController .show (WindowInsetsCompat .Type .systemBars ());
158+ if (actionBar != null ) {
159+ actionBar .show ();
160+ }
161+ if (mMapFragment != null ) {
162+ mMapFragment .showZoomControls (true );
163+ }
164+ }
165+ mFullscreen = fullscreen ;
166+
167+ SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences (this );
168+ SharedPreferences .Editor editor = preferences .edit ();
169+ editor .putBoolean (PREF_FULLSCREEN , mFullscreen );
170+ editor .apply ();
171+
172+ }
173+ private void toggleFullscreen () {
174+ mFullscreen = !mFullscreen ;
175+ setFullscreen (mFullscreen );
176+ }
177+
104178
105179 private void addMapFragment () {
106180 if (mapFragmentAdded ()) {
107181 return ;
108182 }
109183 if (mGeoPointFromIntent == null ) {
110- mMapFragment = getSupportFragmentManager ().findFragmentByTag (MAP_FRAGMENT_TAG );
184+ mMapFragment = ( MapFragment ) getSupportFragmentManager ().findFragmentByTag (MAP_FRAGMENT_TAG );
111185 if (mMapFragment == null ) {
112186 mMapFragment = MapFragment .newInstance ();
113187 }
@@ -145,7 +219,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
145219 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
146220 if (requestCode == REQUEST_LOCATION_PERMISSION ) {
147221 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
148- addMapFragment ();
222+ addMapFragment ();
149223 } else {
150224 finish ();
151225 }
@@ -273,4 +347,31 @@ public NearbyItem getSelectedNearbyPlace() {
273347 public void clearSelectedNearbyPlace () {
274348 mSelectedNearbyPlace = null ;
275349 }
350+
351+ @ Override
352+ public void onMapTap () {
353+ if (mFullscreenOnMapTap ) {
354+ toggleFullscreen ();
355+ }
356+ }
357+
358+ @ Override
359+ public void onMapLongPress () {
360+
361+ }
362+
363+ @ Override
364+ public void setFullscreenOnMapTap (boolean fullscreenOnMapTap ) {
365+ mFullscreenOnMapTap = fullscreenOnMapTap ;
366+ mPrefs .edit ().putBoolean (PREF_FULLSCREEN_ON_MAP_TAP , fullscreenOnMapTap ).apply ();
367+ }
368+
369+ public boolean isFullscreenOnMapTap () {
370+ return mFullscreenOnMapTap ;
371+ }
372+
373+ @ Override
374+ public boolean isFullscreen () {
375+ return mFullscreen ;
376+ }
276377}
0 commit comments