forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstRunActivity.kt
More file actions
242 lines (206 loc) · 8.85 KB
/
FirstRunActivity.kt
File metadata and controls
242 lines (206 loc) · 8.85 KB
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
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2023 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2018 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.nextcloud.client.onboarding
import android.accounts.AccountManager
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.ViewGroup.MarginLayoutParams
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.viewpager.widget.ViewPager
import com.nextcloud.android.common.ui.theme.utils.ColorRole
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.appinfo.AppInfo
import com.nextcloud.client.di.Injectable
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.utils.mdm.MDMConfig
import com.nmc.android.helper.OnBoardingPagerAdapter
import com.nmc.android.helper.OnBoardingUtils.Companion.getOnBoardingItems
import com.nmc.android.utils.DisplayUtils.isLandscapeOrientation
import com.owncloud.android.BuildConfig
import com.owncloud.android.R
import com.owncloud.android.authentication.AuthenticatorActivity
import com.owncloud.android.databinding.FirstRunActivityBinding
import com.owncloud.android.ui.activity.BaseActivity
import com.owncloud.android.ui.activity.FileDisplayActivity
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.theme.ViewThemeUtils
import javax.inject.Inject
/**
* Activity displaying general feature after a fresh install.
*/
class FirstRunActivity : BaseActivity(), ViewPager.OnPageChangeListener, Injectable {
@JvmField
@Inject
var userAccountManager: UserAccountManager? = null
@JvmField
@Inject
var preferences: AppPreferences? = null
@JvmField
@Inject
var appInfo: AppInfo? = null
@JvmField
@Inject
var onboarding: OnboardingService? = null
@JvmField
@Inject
var viewThemeUtilsFactory: ViewThemeUtils.Factory? = null
private var activityResult: ActivityResultLauncher<Intent>? = null
private lateinit var binding: FirstRunActivityBinding
private var defaultViewThemeUtils: ViewThemeUtils? = null
private var selectedPosition = 0
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
enableAccountHandling = false
super.onCreate(savedInstanceState)
applyDefaultTheme()
// NMC Customization
// if device is not tablet then we have to lock it to Portrait mode
// as we don't have images for that
if (!com.nmc.android.utils.DisplayUtils.isTablet()) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
binding = FirstRunActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
registerActivityResult()
setupLoginButton()
deleteAccountAtFirstLaunch()
updateLoginButtonMargin()
updateOnBoardingPager(selectedPosition)
handleOnBackPressed()
}
private fun applyDefaultTheme() {
defaultViewThemeUtils = viewThemeUtilsFactory?.withPrimaryAsBackground()
defaultViewThemeUtils?.platform?.colorStatusBar(this, resources.getColor(R.color.primary))
}
private fun registerActivityResult() {
activityResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (RESULT_OK == result.resultCode) {
val data = result.data
val accountName = data?.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)
val account = userAccountManager?.getAccountByName(accountName)
if (account == null) {
DisplayUtils.showSnackMessage(this, R.string.account_creation_failed)
return@registerForActivityResult
}
userAccountManager?.setCurrentOwnCloudAccount(account.name)
val i = Intent(this, FileDisplayActivity::class.java)
i.action = FileDisplayActivity.RESTART
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i)
finish()
}
}
}
private fun setupLoginButton() {
defaultViewThemeUtils?.material?.colorMaterialButtonFilledOnPrimary(binding.login)
binding.login.setOnClickListener {
if (intent.getBooleanExtra(EXTRA_ALLOW_CLOSE, false)) {
val authenticatorActivityIntent = getAuthenticatorActivityIntent(false)
activityResult?.launch(authenticatorActivityIntent)
} else {
preferences?.onBoardingComplete = true
finish()
}
}
}
private fun getAuthenticatorActivityIntent(extraUseProviderAsWebLogin: Boolean): Intent {
val intent = Intent(this, AuthenticatorActivity::class.java)
intent.putExtra(AuthenticatorActivity.EXTRA_USE_PROVIDER_AS_WEBLOGIN, extraUseProviderAsWebLogin)
return intent
}
// Sometimes, accounts are not deleted when you uninstall the application so we'll do it now
private fun deleteAccountAtFirstLaunch() {
if (onboarding?.isFirstRun == true) {
userAccountManager?.removeAllAccounts()
}
}
private fun updateLoginButtonMargin() {
if (isLandscapeOrientation()) {
if (binding.login.layoutParams is MarginLayoutParams) {
(binding.login.layoutParams as MarginLayoutParams).setMargins(
0, 0, 0, resources.getDimensionPixelOffset(
R.dimen.login_btn_bottom_margin_land
)
)
binding.login.requestLayout()
}
} else {
if (binding.login.layoutParams is MarginLayoutParams) {
(binding.login.layoutParams as MarginLayoutParams).setMargins(
0, 0, 0, resources.getDimensionPixelOffset(
R.dimen.login_btn_bottom_margin
)
)
binding.login.requestLayout()
}
}
}
private fun updateOnBoardingPager(selectedPosition: Int) {
val featuresViewAdapter = OnBoardingPagerAdapter(this, getOnBoardingItems())
binding.progressIndicator.setNumberOfSteps(featuresViewAdapter.count)
binding.contentPanel.adapter = featuresViewAdapter
binding.contentPanel.currentItem = selectedPosition
binding.contentPanel.addOnPageChangeListener(this)
}
private fun handleOnBackPressed() {
onBackPressedDispatcher.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val isFromAddAccount = intent.getBooleanExtra(EXTRA_ALLOW_CLOSE, false)
// NMC Customization -> Modified the condition for readability
if (isFromAddAccount) {
val destination = Intent(applicationContext, FileDisplayActivity::class.java)
destination.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(destination)
finish()
} else {
// NMC Customization -> No redirection to AuthenticatorActivity is required
// just close the app
finishAffinity()
}
}
}
)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
updateLoginButtonMargin()
updateOnBoardingPager(selectedPosition)
}
private fun onFinish() {
preferences?.lastSeenVersionCode = BuildConfig.VERSION_CODE
}
override fun onStop() {
onFinish()
super.onStop()
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// unused but to be implemented due to abstract parent
}
override fun onPageSelected(position: Int) {
//-1 to position because this position doesn't start from 0
selectedPosition = position - 1
//pass directly the position here because this position will doesn't start from 0
binding.progressIndicator.animateToStep(position)
}
override fun onPageScrollStateChanged(state: Int) {
// unused but to be implemented due to abstract parent
}
companion object {
const val EXTRA_ALLOW_CLOSE = "ALLOW_CLOSE"
const val EXTRA_EXIT = "EXIT"
}
}