Browse Source

Support setting orientation from application and modals (#834)

Closes #681
Guy Carmeli 7 years ago
parent
commit
2519ff1239

+ 0
- 8
android/app/src/main/AndroidManifest.xml View File

@@ -5,14 +5,6 @@
5 5
         <activity
6 6
             android:name=".controllers.NavigationActivity"
7 7
             android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>
8
-        <activity
9
-            android:name=".controllers.PortraitNavigationActivity"
10
-            android:configChanges="orientation|screenSize|keyboardHidden"
11
-            android:screenOrientation="portrait" />
12
-        <activity
13
-            android:name=".controllers.LandscapeNavigationActivity"
14
-            android:configChanges="orientation|screenSize|keyboardHidden"
15
-            android:screenOrientation="landscape" />
16 8
         <activity
17 9
             android:name="com.facebook.react.devsupport.DevSettingsActivity"
18 10
             android:exported="false" />

+ 1
- 10
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -52,16 +52,7 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
52 52
     public void startApp(final ReadableMap params) {
53 53
         boolean portraitOnlyMode = false;
54 54
         boolean landscapeOnlyMode = false;
55
-
56
-        if (params.hasKey("portraitOnlyMode")) {
57
-            portraitOnlyMode = params.getBoolean("portraitOnlyMode");
58
-        }
59
-
60
-        if (params.hasKey(("landscapeOnlyMode"))) {
61
-            landscapeOnlyMode = params.getBoolean("landscapeOnlyMode");
62
-        }
63
-
64
-        NavigationCommandsHandler.startApp(BundleConverter.toBundle(params), portraitOnlyMode, landscapeOnlyMode);
55
+        NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
65 56
     }
66 57
 
67 58
     @ReactMethod

+ 0
- 4
android/app/src/main/java/com/reactnativenavigation/controllers/LandscapeNavigationActivity.java View File

@@ -1,4 +0,0 @@
1
-package com.reactnativenavigation.controllers;
2
-
3
-public class LandscapeNavigationActivity extends NavigationActivity {
4
-}

+ 9
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/Modal.java View File

@@ -11,7 +11,9 @@ import com.reactnativenavigation.R;
11 11
 import com.reactnativenavigation.layouts.Layout;
12 12
 import com.reactnativenavigation.layouts.ModalScreenLayout;
13 13
 import com.reactnativenavigation.layouts.ScreenStackContainer;
14
+import com.reactnativenavigation.params.AppStyle;
14 15
 import com.reactnativenavigation.params.ContextualMenuParams;
16
+import com.reactnativenavigation.params.Orientation;
15 17
 import com.reactnativenavigation.params.ScreenParams;
16 18
 import com.reactnativenavigation.params.TitleBarButtonParams;
17 19
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
@@ -86,6 +88,7 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
86 88
         requestWindowFeature(Window.FEATURE_NO_TITLE);
87 89
         layout = new ModalScreenLayout(getActivity(), screenParams, this);
88 90
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
91
+        setOrientation(screenParams.styleParams.orientation);
89 92
         setContentView(layout.asView());
90 93
     }
91 94
 
@@ -109,7 +112,7 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
109 112
         layout.newStack(params);
110 113
     }
111 114
 
112
-    public boolean containsNavigator(String navigatorId) {
115
+    boolean containsNavigator(String navigatorId) {
113 116
         return layout.containsNavigator(navigatorId);
114 117
     }
115 118
 
@@ -128,10 +131,15 @@ public class Modal extends Dialog implements DialogInterface.OnDismissListener,
128 131
     @Override
129 132
     public void onDismiss(DialogInterface dialog) {
130 133
         destroy();
134
+        setOrientation(AppStyle.appStyle.orientation);
131 135
         onModalDismissedListener.onModalDismissed(this);
132 136
     }
133 137
 
134 138
     void onModalDismissed() {
135 139
         layout.onModalDismissed();
136 140
     }
141
+
142
+    private void setOrientation(Orientation orientation) {
143
+        getActivity().setRequestedOrientation(orientation.orientationCode);
144
+    }
137 145
 }

+ 7
- 4
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -32,7 +32,7 @@ import com.reactnativenavigation.params.SnackbarParams;
32 32
 import com.reactnativenavigation.params.TitleBarButtonParams;
33 33
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
34 34
 import com.reactnativenavigation.react.ReactGateway;
35
-import com.reactnativenavigation.utils.Orientation;
35
+import com.reactnativenavigation.utils.OrientationHelper;
36 36
 import com.reactnativenavigation.views.SideMenu.Side;
37 37
 
38 38
 import java.util.List;
@@ -57,20 +57,23 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
57 57
     @Override
58 58
     protected void onCreate(Bundle savedInstanceState) {
59 59
         super.onCreate(savedInstanceState);
60
-
61 60
         if (!NavigationApplication.instance.isReactContextInitialized()) {
62 61
             NavigationApplication.instance.startReactContextOnceInBackgroundAndExecuteJS();
63 62
             return;
64 63
         }
65 64
 
66 65
         activityParams = NavigationCommandsHandler.parseActivityParams(getIntent());
67
-
68 66
         disableActivityShowAnimationIfNeeded();
67
+        setOrientation();
69 68
         createLayout();
70 69
         createModalController();
71 70
         NavigationApplication.instance.getActivityCallbacks().onActivityCreated(this, savedInstanceState);
72 71
     }
73 72
 
73
+    private void setOrientation() {
74
+        OrientationHelper.setOrientation(this, AppStyle.appStyle.orientation);
75
+    }
76
+
74 77
     private void disableActivityShowAnimationIfNeeded() {
75 78
         if (!activityParams.animateShow) {
76 79
             overridePendingTransition(0, 0);
@@ -191,7 +194,7 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
191 194
 
192 195
     @Override
193 196
     public void onConfigurationChanged(Configuration newConfig) {
194
-        Orientation.onConfigurationChanged(newConfig);
197
+        OrientationHelper.onConfigurationChanged(newConfig);
195 198
         NavigationApplication.instance.getActivityCallbacks().onConfigurationChanged(newConfig);
196 199
         super.onConfigurationChanged(newConfig);
197 200
     }

+ 4
- 11
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -16,7 +16,7 @@ import com.reactnativenavigation.params.TitleBarButtonParams;
16 16
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
17 17
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
18 18
 import com.reactnativenavigation.params.parsers.ScreenParamsParser;
19
-import com.reactnativenavigation.utils.Orientation;
19
+import com.reactnativenavigation.utils.OrientationHelper;
20 20
 import com.reactnativenavigation.views.SideMenu.Side;
21 21
 
22 22
 import java.util.List;
@@ -35,15 +35,8 @@ public class NavigationCommandsHandler {
35 35
      * @param params ActivityParams as bundle
36 36
      */
37 37
 
38
-    public static void startApp(Bundle params, boolean portraitOnlyMode, boolean landscapeOnlyMode) {
39
-        Intent intent;
40
-        if (portraitOnlyMode) {
41
-            intent = new Intent(NavigationApplication.instance, PortraitNavigationActivity.class);
42
-        } else if (landscapeOnlyMode) {
43
-            intent = new Intent(NavigationApplication.instance, LandscapeNavigationActivity.class);
44
-        } else {
45
-            intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
46
-        }
38
+    public static void startApp(Bundle params) {
39
+        Intent intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
47 40
         IntentDataHandler.onStartApp(intent);
48 41
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
49 42
         intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
@@ -428,6 +421,6 @@ public class NavigationCommandsHandler {
428 421
         if (currentActivity == null) {
429 422
             return;
430 423
         }
431
-        promise.resolve(Orientation.get(currentActivity));
424
+        promise.resolve(OrientationHelper.getOrientation(currentActivity));
432 425
     }
433 426
 }

+ 0
- 4
android/app/src/main/java/com/reactnativenavigation/controllers/PortraitNavigationActivity.java View File

@@ -1,4 +0,0 @@
1
-package com.reactnativenavigation.controllers;
2
-
3
-public class PortraitNavigationActivity extends NavigationActivity {
4
-}

+ 38
- 0
android/app/src/main/java/com/reactnativenavigation/params/Orientation.java View File

@@ -0,0 +1,38 @@
1
+package com.reactnativenavigation.params;
2
+
3
+import android.content.pm.ActivityInfo;
4
+import android.content.res.Configuration;
5
+
6
+public enum Orientation {
7
+    Portrait("portrait", Configuration.ORIENTATION_PORTRAIT, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT),
8
+    Landscape("landscape", Configuration.ORIENTATION_LANDSCAPE, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE),
9
+    auto("auto", Configuration.ORIENTATION_UNDEFINED, ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
10
+
11
+    public String name;
12
+    public int configurationCode;
13
+    public int orientationCode;
14
+
15
+    Orientation(String name, int configurationCode, int orientationCode) {
16
+        this.name = name;
17
+        this.configurationCode = configurationCode;
18
+        this.orientationCode = orientationCode;
19
+    }
20
+
21
+    public static Orientation fromString(String name) {
22
+        for (Orientation orientation : values()) {
23
+            if (orientation.name.equals(name)) {
24
+                return orientation;
25
+            }
26
+        }
27
+        throw new RuntimeException();
28
+    }
29
+
30
+    public static String fromConfigurationCode(int configurationCode) {
31
+        for (Orientation orientation : values()) {
32
+            if (orientation.configurationCode == configurationCode) {
33
+                return orientation.name;
34
+            }
35
+        }
36
+        throw new RuntimeException();
37
+    }
38
+}

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

@@ -41,6 +41,7 @@ public class StyleParams {
41 41
         }
42 42
     }
43 43
 
44
+    public Orientation orientation;
44 45
     public Color statusBarColor;
45 46
     public Color contextualMenuStatusBarColor;
46 47
     public Color contextualMenuButtonsColor;

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

@@ -4,6 +4,7 @@ import android.graphics.Color;
4 4
 import android.os.Bundle;
5 5
 
6 6
 import com.reactnativenavigation.params.AppStyle;
7
+import com.reactnativenavigation.params.Orientation;
7 8
 import com.reactnativenavigation.params.StyleParams;
8 9
 
9 10
 public class StyleParamsParser {
@@ -19,6 +20,7 @@ public class StyleParamsParser {
19 20
         }
20 21
 
21 22
         StyleParams result = new StyleParams();
23
+        result.orientation = Orientation.fromString(params.getString("orientation", getDefaultOrientation()));
22 24
         result.statusBarColor = getColor("statusBarColor", getDefaultStatusBarColor());
23 25
         result.contextualMenuStatusBarColor = getColor("contextualMenuStatusBarColor", getDefaultContextualMenuStatusBarColor());
24 26
         result.contextualMenuButtonsColor = getColor("contextualMenuButtonsColor", getDefaultContextualMenuButtonsColor());
@@ -74,11 +76,16 @@ public class StyleParamsParser {
74 76
         return result;
75 77
     }
76 78
 
79
+    private String getDefaultOrientation() {
80
+        return AppStyle.appStyle == null ? null : AppStyle.appStyle.orientation.name;
81
+    }
82
+
77 83
     private StyleParams createDefaultStyleParams() {
78 84
         StyleParams result = new StyleParams();
79 85
         result.titleBarDisabledButtonColor = getTitleBarDisabledButtonColor();
80 86
         result.topBarElevationShadowEnabled = true;
81 87
         result.titleBarHideOnScroll = false;
88
+        result.orientation = Orientation.auto;
82 89
         return result;
83 90
     }
84 91
 

+ 0
- 31
android/app/src/main/java/com/reactnativenavigation/utils/Orientation.java View File

@@ -1,31 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import android.content.res.Configuration;
4
-
5
-import com.facebook.react.bridge.Arguments;
6
-import com.facebook.react.bridge.WritableMap;
7
-import com.reactnativenavigation.NavigationApplication;
8
-import com.reactnativenavigation.controllers.NavigationActivity;
9
-
10
-public class Orientation {
11
-    public static String get(NavigationActivity currentActivity) {
12
-        return getOrientation(currentActivity.getResources().getConfiguration().orientation);
13
-    }
14
-
15
-    public static void onConfigurationChanged(Configuration newConfig) {
16
-        WritableMap params = Arguments.createMap();
17
-        params.putString("orientation", getOrientation(newConfig.orientation));
18
-        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("orientationChanged", params);
19
-    }
20
-
21
-    private static String getOrientation(int orientation) {
22
-        switch (orientation) {
23
-            case Configuration.ORIENTATION_PORTRAIT:
24
-                return "PORTRAIT";
25
-            case Configuration.ORIENTATION_LANDSCAPE:
26
-                return "LANDSCAPE";
27
-            default:
28
-                return "UNDEFINED";
29
-        }
30
-    }
31
-}

+ 26
- 0
android/app/src/main/java/com/reactnativenavigation/utils/OrientationHelper.java View File

@@ -0,0 +1,26 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.app.Activity;
4
+import android.content.res.Configuration;
5
+
6
+import com.facebook.react.bridge.Arguments;
7
+import com.facebook.react.bridge.WritableMap;
8
+import com.reactnativenavigation.NavigationApplication;
9
+import com.reactnativenavigation.controllers.NavigationActivity;
10
+import com.reactnativenavigation.params.Orientation;
11
+
12
+public class OrientationHelper {
13
+    public static String getOrientation(NavigationActivity currentActivity) {
14
+        return Orientation.fromConfigurationCode(currentActivity.getResources().getConfiguration().orientation);
15
+    }
16
+
17
+    public static void setOrientation(Activity activity, Orientation orientation) {
18
+        activity.setRequestedOrientation(orientation.orientationCode);
19
+    }
20
+
21
+    public static void onConfigurationChanged(Configuration newConfig) {
22
+        WritableMap params = Arguments.createMap();
23
+        params.putString("orientation", Orientation.fromConfigurationCode(newConfig.orientation));
24
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("orientationChanged", params);
25
+    }
26
+}

+ 0
- 34
android/app/src/main/res/layout/tab_activity.xml View File

@@ -1,34 +0,0 @@
1
-<?xml version="1.0" encoding="utf-8"?>
2
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
-    xmlns:app="http://schemas.android.com/apk/res-auto"
4
-    xmlns:tools="http://schemas.android.com/tools"
5
-    android:layout_width="match_parent"
6
-    android:layout_height="match_parent"
7
-    android:orientation="vertical"
8
-    tools:context=".activities.TabActivity">
9
-
10
-    <android.support.design.widget.AppBarLayout
11
-        android:id="@+id/appbar"
12
-        android:layout_width="match_parent"
13
-        android:layout_height="wrap_content"
14
-        android:fitsSystemWindows="true">
15
-
16
-        <com.reactnativenavigation.views.RnnToolBar
17
-            android:id="@+id/toolbar"
18
-            android:layout_width="match_parent"
19
-            android:layout_height="?attr/actionBarSize"
20
-            app:layout_scrollFlags="scroll|enterAlways" />
21
-
22
-        <com.reactnativenavigation.views.RnnTabLayout
23
-            android:id="@+id/tabLayout"
24
-            android:layout_width="match_parent"
25
-            android:layout_height="?attr/actionBarSize"
26
-            app:layout_scrollFlags="scroll|enterAlways" />
27
-    </android.support.design.widget.AppBarLayout>
28
-
29
-    <android.support.v4.view.ViewPager
30
-        android:id="@+id/viewPager"
31
-        android:layout_width="match_parent"
32
-        android:layout_height="match_parent"
33
-        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
34
-</LinearLayout>

+ 17
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -28,6 +28,9 @@ function startSingleScreenApp(params) {
28 28
   params.screen = adaptNavigationStyleToScreenStyle(screen);
29 29
   params.screen = adaptNavigationParams(screen);
30 30
   params.appStyle = convertStyleParams(params.appStyle);
31
+  if (params.appStyle) {
32
+    params.appStyle.orientation = getOrientation(params);
33
+  }
31 34
   params.sideMenu = convertDrawerParamsToSideMenuParams(params.drawer);
32 35
   params.overrideBackPress = screen.overrideBackPress;
33 36
   params.animateShow = convertAnimationType(params.animationType);
@@ -35,6 +38,16 @@ function startSingleScreenApp(params) {
35 38
   newPlatformSpecific.startApp(params);
36 39
 }
37 40
 
41
+function getOrientation(params) {
42
+  if (params.portraitOnlyMode || _.get(params, 'appStyle.orientation') === 'portrait') {
43
+    return 'portrait';
44
+  }
45
+  if (params.landscaptOnlyMode || _.get(params, 'appStyle.orientation') === 'landscape') {
46
+    return 'landscape';
47
+  }
48
+  return 'auto';
49
+}
50
+
38 51
 function adaptTopTabs(screen, navigatorID) {
39 52
   screen.topTabs = _.cloneDeep(screen.topTabs);
40 53
   _.forEach(_.get(screen, 'topTabs'), (tab) => {
@@ -120,6 +133,7 @@ function convertStyleParams(originalStyleObject) {
120 133
   }
121 134
 
122 135
   let ret = {
136
+    orientation: originalStyleObject.orientation,
123 137
     statusBarColor: processColor(originalStyleObject.statusBarColor),
124 138
     topBarColor: processColor(originalStyleObject.navBarBackgroundColor),
125 139
     topBarTransparent: originalStyleObject.navBarTransparent,
@@ -249,6 +263,9 @@ function startTabBasedApp(params) {
249 263
   params.tabs = newTabs;
250 264
 
251 265
   params.appStyle = convertStyleParams(params.appStyle);
266
+  if (params.appStyle) {
267
+    params.appStyle.orientation = getOrientation(params);
268
+  }
252 269
   params.sideMenu = convertDrawerParamsToSideMenuParams(params.drawer);
253 270
   params.animateShow = convertAnimationType(params.animationType);
254 271