Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1.3 discord 1 #1

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ project.xcworkspace
node_modules/
npm-debug.log
*.iml
package-lock.json
yarn.lock

# AS
.idea
local.properties
android/build/**

# Gradle
.gradle
gradle.properties
gradle-wrapper.jar
gradle-wrapper.properties
gradlew
gradlew.bat
46 changes: 40 additions & 6 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
allprojects {
repositories {
google()
mavenCentral()
}
}

repositories {
google()
mavenCentral()
}

buildscript {
ext.kotlin_version = '1.6.0'
repositories {
google()
mavenCentral()
}
dependencies {
//noinspection GradleDynamicVersion
classpath 'com.android.tools.build:gradle:4+'
//noinspection GradleDynamicVersion,DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1+"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

def _ext = rootProject.ext

def _compileSdkVersion = _ext.has('compileSdkVersion') ? _ext.compileSdkVersion : 31
def _minSdkVersion = _ext.has('minSdkVersion') ? _ext.minSdkVersion : 16
def _targetSdkVersion = _ext.has('targetSdkVersion') ? _ext.targetSdkVersion : 31

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion _compileSdkVersion

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
minSdkVersion _minSdkVersion
targetSdkVersion _targetSdkVersion
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -16,5 +48,7 @@ android {
}

dependencies {
compile "com.facebook.react:react-native:+"
}
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.github.yamill.orientation

import android.annotation.SuppressLint
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.github.yamill.orientation.listeners.OrientationAutoRotateListener
import com.github.yamill.orientation.listeners.OrientationConfigListener


class OrientationModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

private var autoRotateEnabled = false
private var autoRotateIgnored = false

override fun getName() = "Orientation"

override fun getConstants(): Map<String, Any?> {
val orientationInt = reactApplicationContext.resources.configuration.orientation
val orientation = getOrientationString(orientationInt)

return mapOf("initialOrientation" to orientation)
}

init {
reactContext.addLifecycleEventListener(
OrientationAutoRotateListener(reactContext) { autoRotateEnabled ->
this.autoRotateEnabled = autoRotateEnabled
this.maybeLockToPortrait()
}
)
reactContext.addLifecycleEventListener(
OrientationConfigListener(reactContext) {
currentActivity
}
)
}

@Suppress("unused")
@ReactMethod
fun getOrientation(callback: Callback) {
val orientationInt = reactApplicationContext.resources.configuration.orientation
val orientation = getOrientationString(orientationInt)
if (orientation === "null") {
callback.invoke(orientationInt, null)
} else {
callback.invoke(null, orientation)
}
}

@Suppress("unused")
@ReactMethod
fun ignoreAutoRotate(ignoreAutoRotate: Boolean) {
this.autoRotateIgnored = ignoreAutoRotate
this.maybeLockToPortrait()
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New API surface to disable the default behavior of respecting the auto-rotate setting.


@SuppressLint("SourceLockedOrientationActivity")
@Suppress("unused")
@ReactMethod
fun lockToPortrait() {
currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

@Suppress("unused")
@ReactMethod
fun lockToLandscape() {
if (autoRotateEnabled || autoRotateIgnored) {
currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
}

@Suppress("unused")
@ReactMethod
fun lockToLandscapeLeft() {
if (autoRotateEnabled || autoRotateIgnored) {
currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}

@Suppress("unused")
@ReactMethod
fun lockToLandscapeRight() {
if (autoRotateEnabled || autoRotateIgnored) {
currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
}
}

@Suppress("unused")
@ReactMethod
fun unlockAllOrientations() {
if (autoRotateEnabled || autoRotateIgnored) {
currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}

private fun maybeLockToPortrait() {
if (autoRotateEnabled || autoRotateIgnored) {
return
}

lockToPortrait()
}

private fun getOrientationString(orientation: Int) =
when (orientation) {
Configuration.ORIENTATION_LANDSCAPE ->
"LANDSCAPE"
Configuration.ORIENTATION_PORTRAIT ->
"PORTRAIT"
Configuration.ORIENTATION_UNDEFINED ->
"UNKNOWN"
else ->
null
}
}
Loading