Snippets
Created by
John De la cruz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | @AndroidEntryPoint
open class OAuthActivity :
DaggerAuthenticatorActivity(),
OAuthWebviewHelperEvents,
EventsObserver,
ConnectivityReceiver.OnConnectivityChangeListener {
private val timeoutHandler = Handler(Looper.myLooper()!!)
private var webViewHelper: OAuthWebviewHelper? = null
private lateinit var binding: ActivityOauthBinding
private var wasBackgrounded = false
private var dialogWindowIsVisible = false
@Inject
lateinit var versionChecker: VersionChecker
@Inject
lateinit var analytics: AnalyticsTracker
private var connectivityReceiver: ConnectivityReceiver? = null
private val onBackPressedCallback: OnBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
webViewHelper?.loadLoginPage()
finishAffinity()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityOauthBinding.inflate(layoutInflater)
analytics.logScreen(this, CLASS_NAME)
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
connectivityReceiver = ConnectivityReceiver(this)
registerReceiver(
connectivityReceiver,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
if (intent == null || intent.extras == null) {
Timber.tag("OAuthActivity").e("Bundle missing from OAuthActivity")
return
}
// Getting login options from intent's extras
val loginOptions = ClientManager.LoginOptions.fromBundle(intent.extras)
// Setup content view
setContentView(binding.root)
EventsObservable.get().notifyEvent(
EventsObservable.EventType.AuthWebViewCreateComplete,
binding.activityOauthWebview
)
webViewHelper = OAuthWebviewHelper(
this,
this,
loginOptions,
binding.activityOauthWebview,
savedInstanceState
)
// Let observers know
EventsObservable.get()
.notifyEvent(EventsObservable.EventType.LoginActivityCreateComplete, this)
if (RuntimeConfig.getRuntimeConfig(this)
.getBoolean(RuntimeConfig.ConfigKey.RequireCertAuth)
) {
val alias =
RuntimeConfig.getRuntimeConfig(this)
.getString(RuntimeConfig.ConfigKey.ManagedAppCertAlias)
KeyChain.choosePrivateKeyAlias(this, webViewHelper!!, null, null, null, 0, alias)
} else {
webViewHelper?.loadLoginPage()
}
EventsObservable.get().registerObserver(this)
binding.activityOauthRegister.setOnClickListener {
startActivity(
Intent(
this,
RegistrationActivity::class.java
)
)
}
binding.resetPasswordButton.setOnClickListener {
startActivity(
Intent(
this,
PasswordActivity::class.java
)
)
}
versionChecker.checkForUpdate { updateMessage: String? ->
if (!BuildConfig.DEBUG) {
UpdateVersionActivity.startActivityWithMessage(this, updateMessage)
finish()
}
}
binding.policyLink.setOnClickListener {
openPolicy()
}
}
private fun openPolicy() {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse(
setLink("https://md.com/privacy-policy/")
)
)
)
}
override fun onResume() {
super.onResume()
if (wasBackgrounded) {
webViewHelper?.clearView()
webViewHelper?.loadLoginPage()
wasBackgrounded = false
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// This allows sub classes to override the behavior by returning false.
return if (fixBackButtonBehavior(keyCode)) {
true
} else {
super.onKeyDown(keyCode, event)
}
}
private fun fixBackButtonBehavior(keyCode: Int): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) {
/*
* If there are no accounts signed in, we need the login screen
* to go away, and go back to the home screen. However, if the
* login screen has been brought up from the switcher screen,
* the back button should take the user back to the previous screen.
*/
val accMgr = SalesforceSDKManager.getInstance().userAccountManager
wasBackgrounded = true
if (accMgr.authenticatedUsers == null) {
moveTaskToBack(true)
} else {
finish()
}
return true
}
return false
}
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
super.onSaveInstanceState(outState, outPersistentState)
webViewHelper?.saveState(outState)
}
override fun onDestroy() {
if (connectivityReceiver != null) {
unregisterReceiver(connectivityReceiver)
}
EventsObservable.get().unregisterObserver(this)
timeoutHandler.removeCallbacksAndMessages(null)
super.onDestroy()
}
override fun loadingLoginPage(loginUrl: String) {
timeoutHandler.removeCallbacksAndMessages(null)
timeoutHandler.postDelayed({ webViewHelper?.loadLoginPage() }, OAUTH_REFRESH_TIMEOUT)
}
override fun onAccountAuthenticatorResult(authResult: Bundle) {
setAccountAuthenticatorResult(authResult)
}
override fun finish(userAccount: UserAccount?) {
try {
val am = SalesforceAnalyticsManager.getInstance(userAccount)
am.enableLogging(false)
val remotesField = SalesforceAnalyticsManager::class.java.getDeclaredField("remotes")
remotesField.isAccessible = true
(Objects.requireNonNull(remotesField[am]) as MutableMap<*, *>).clear()
} catch (e: IOException) {
Timber.tag("OAuthActivity").e(e)
} catch (e: URISyntaxException) {
Timber.tag("URISyntaxException").e(e)
}
val userAccountManager = SalesforceSDKManager.getInstance().userAccountManager
val authenticatedUsers = userAccountManager.authenticatedUsers
val numAuthenticatedUsers = authenticatedUsers?.size ?: 0
val userSwitchType: Int = if (numAuthenticatedUsers == 1) {
// We've already authenticated the first user, so there should be one.
UserAccountManager.USER_SWITCH_TYPE_FIRST_LOGIN
} else if (numAuthenticatedUsers > 1) {
// Otherwise we're logging in with an additional user.
UserAccountManager.USER_SWITCH_TYPE_LOGIN
} else {
// This should never happen but if it does, pass in the "unknown" value.
UserAccountManager.USER_SWITCH_TYPE_DEFAULT
}
val preferences: SharedPreferences =
this.getSharedPreferences("data", MODE_PRIVATE)
with(preferences.edit()) {
putBoolean("user_logged_in", true)
apply()
}
userAccountManager.sendUserSwitchIntent(userSwitchType, null)
finish()
}
override fun onEvent(event: EventsObservable.Event) {
if (event.type != EventsObservable.EventType.AuthWebViewPageFinished) {
// Nothing to do
return
}
val url = (event.data as String).lowercase(Locale.getDefault())
if (url.endsWith(URL_SITE_LOGIN)) {
webViewHelper?.loadLoginPage()
}
val showRegister = url.contains(URL_SITE_LOGIN)
val builder = AlertDialog.Builder(this, R.style.DialogTheme)
if (!showRegister &&
!dialogWindowIsVisible &&
url.contains(AUTHORIZE)
) {
builder.setTitle(getString(R.string.login_privacy_dialog_title))
builder.setMessage(getString(R.string.login_privacy_dialog_text))
builder.setPositiveButton(
getString(R.string.see_policy_label)
) { dialogInterface: DialogInterface, _: Int ->
openPolicy()
dialogInterface.dismiss()
dialogWindowIsVisible = false
}
builder.setNegativeButton(
getString(R.string.acknowledge_label)
) { dialogInterface: DialogInterface, _: Int ->
dialogInterface.dismiss()
dialogWindowIsVisible = false
}
builder.show()
dialogWindowIsVisible = true
}
binding.activityOauthRegisterContainer.animate()
.translationY(if (showRegister) 1f else binding.activityOauthRegisterContainer.height.toFloat())
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationCancel(animation: Animator) {
binding.activityOauthRegisterContainer.visibility =
if (showRegister) View.VISIBLE else View.GONE
}
override fun onAnimationEnd(animation: Animator) {
binding.activityOauthRegisterContainer.visibility =
if (showRegister) View.VISIBLE else View.GONE
}
})
}
companion object {
private const val URL_SITE_LOGIN = "sitelogin"
private const val AUTHORIZE = "secur/frontdoor.jsp"
private const val CLASS_NAME = "OAuthActivity"
private val OAUTH_REFRESH_TIMEOUT = TimeUnit.MINUTES.toMillis(1)
}
override fun isConnected(isConnected: Boolean) {
if (isConnected) {
webViewHelper?.loadLoginPage()
}
binding.activityOauthWebview.visibility = if (isConnected) View.VISIBLE else View.INVISIBLE
binding.noConnectionText.visibility = if (!isConnected) View.VISIBLE else View.INVISIBLE
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.