Skip to content

Commit

Permalink
Fix bug #51
Browse files Browse the repository at this point in the history
  • Loading branch information
guolindev committed Sep 26, 2021
1 parent e47cf6c commit b965c2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ internal abstract class BaseTask(@JvmField var pb: PermissionBuilder) : ChainTas
if (pb.requestCallback != null) {
pb.requestCallback!!.onResult(deniedList.isEmpty(), ArrayList(pb.grantedPermissions), deniedList)
}
// Remove the InvisibleFragment from current Activity after request finished.
pb.removeInvisibleFragment()
// Restore the orientation after request finished since it's locked before.
pb.restoreOrientation()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package com.permissionx.guolindev.request

import android.Manifest
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
Expand Down Expand Up @@ -238,9 +241,11 @@ class PermissionBuilder(
*/
fun request(callback: RequestCallback?) {
requestCallback = callback
// Build the request chain.
// RequestNormalPermissions runs first.
// Then RequestBackgroundLocationPermission runs.
// Lock the orientation when requesting permissions, or callback maybe missed due to
// activity destroyed.
lockOrientation()

// Build the request chain. RequestNormalPermissions runs first, then RequestBackgroundLocationPermission runs.
val requestChain = RequestChain()
requestChain.addTaskToChain(RequestNormalPermissions(this))
requestChain.addTaskToChain(RequestBackgroundLocationPermission(this))
Expand Down Expand Up @@ -491,6 +496,26 @@ class PermissionBuilder(
}
}

/**
* Restore the screen orientation. Activity just behave as before locked.
*/
internal fun restoreOrientation() {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}

/**
* Lock the screen orientation. Activity couldn't rotate with sensor.
*/
@SuppressLint("SourceLockedOrientationActivity")
private fun lockOrientation() {
val orientation = activity.resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
}
}

/**
* Go to your app's Settings page to let user turn on the necessary permissions.
*
Expand Down

0 comments on commit b965c2d

Please sign in to comment.