Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3247 from vector-im/feature/im_3225
Browse files Browse the repository at this point in the history
Handle Terms and condition approval for integration manager
  • Loading branch information
bmarty authored Aug 12, 2019
2 parents 83d18d3 + 6790a56 commit f23e9d8
Show file tree
Hide file tree
Showing 24 changed files with 949 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MatrixSdk:

Features:
- Make Widget/Integration manager optional (#3224)
- Handle terms and condition approval for IntegrationManager (#3225)

Improvements:
-
Expand Down
4 changes: 4 additions & 0 deletions vector/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@
android:name=".activity.SASVerificationActivity"
android:label="@string/title_activity_verify_device"
android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
android:name=".activity.ReviewTermsActivity"
android:label="@string/review_terms"
android:windowSoftInputMode="stateHidden|adjustResize" />

<!-- FileProvider -->
<provider
Expand Down
5 changes: 5 additions & 0 deletions vector/src/main/java/im/vector/VectorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public void onCreate() {
Log.d(LOG_TAG, "onCreate");
super.onCreate();

PreferencesManager.setIntegrationManagerUrls(this,
getString(R.string.integrations_ui_url),
getString(R.string.integrations_rest_url),
getString(R.string.integrations_jitsi_widget_url));

mLifeCycleListener = new VectorLifeCycleObserver();
ProcessLifecycleOwner.get().getLifecycle().addObserver(mLifeCycleListener);

Expand Down
36 changes: 34 additions & 2 deletions vector/src/main/java/im/vector/activity/AbstractWidgetActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package im.vector.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
Expand All @@ -30,6 +31,7 @@ import com.google.gson.reflect.TypeToken
import im.vector.Matrix
import im.vector.R
import im.vector.activity.util.INTEGRATION_MANAGER_ACTIVITY_REQUEST_CODE
import im.vector.activity.util.TERMS_REQUEST_CODE
import im.vector.types.JsonDict
import im.vector.types.WidgetEventData
import im.vector.util.AssetReader
Expand All @@ -42,6 +44,8 @@ import org.matrix.androidsdk.core.Log
import org.matrix.androidsdk.core.callback.ApiCallback
import org.matrix.androidsdk.core.model.MatrixError
import org.matrix.androidsdk.data.Room
import org.matrix.androidsdk.features.terms.TermsManager
import org.matrix.androidsdk.features.terms.TermsNotSignedException
import java.util.*
import javax.net.ssl.HttpsURLConnection

Expand Down Expand Up @@ -77,7 +81,7 @@ abstract class AbstractWidgetActivity : VectorAppCompatActivity() {
private var mHistoryAlreadyCleared = false


lateinit var widgetManager : WidgetsManager
lateinit var widgetManager: WidgetsManager
/* ==========================================================================================
* LIFE CYCLE
* ========================================================================================== */
Expand Down Expand Up @@ -129,7 +133,13 @@ abstract class AbstractWidgetActivity : VectorAppCompatActivity() {
}

override fun onUnexpectedError(e: Exception) {
onError(e.localizedMessage)
if (e is TermsNotSignedException) {
mIsRefreshingToken = false
hideWaitingView()
presentTermsForServices(e.token)
} else {
onError(e.localizedMessage)
}
}
})
} else {
Expand All @@ -138,6 +148,17 @@ abstract class AbstractWidgetActivity : VectorAppCompatActivity() {
}
}

private fun presentTermsForServices(token: String?) {
val wm = WidgetManagerProvider.getWidgetManager(this)
if (wm == null) { // should not happen
finish()
return
}
startActivityForResult(ReviewTermsActivity.intent(this,
TermsManager.ServiceType.IntegrationManager, wm.uiUrl, token),
TERMS_REQUEST_CODE)
}

/* ==========================================================================================
* UI Events
* ========================================================================================== */
Expand All @@ -150,6 +171,17 @@ abstract class AbstractWidgetActivity : VectorAppCompatActivity() {
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == TERMS_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
getScalarTokenAndLoadUrl()
} else {
finish()
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
/* ==========================================================================================
* PRIVATE
* ========================================================================================== */
Expand Down
56 changes: 56 additions & 0 deletions vector/src/main/java/im/vector/activity/ReviewTermsActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.activity

import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.content.Intent
import im.vector.R
import im.vector.fragments.terms.AcceptTermsFragment
import im.vector.fragments.terms.AcceptTermsViewModel
import im.vector.fragments.terms.ServiceTermsArgs
import org.matrix.androidsdk.features.terms.TermsManager


class ReviewTermsActivity : SimpleFragmentActivity() {

override fun initUiAndData() {
super.initUiAndData()
if (supportFragmentManager.fragments.isEmpty()) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, AcceptTermsFragment.newInstance())
.commitNow()
}

val viewModel = ViewModelProviders.of(this).get(AcceptTermsViewModel::class.java)
viewModel.termsArgs = intent.getParcelableExtra(EXTRA_INFO)

mSession = getSession(intent)

viewModel.initSession(session)
}

companion object {

private const val EXTRA_INFO = "EXTRA_INFO"

fun intent(context: Context, serviceType: TermsManager.ServiceType, baseUrl: String, token: String?): Intent {
return Intent(context, ReviewTermsActivity::class.java).also {
it.putExtra(EXTRA_INFO, ServiceTermsArgs(serviceType, baseUrl, token))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,12 @@ private void openIntegrationManagerActivity(@Nullable String screenId) {
return;
}

WidgetsManager wm = WidgetManagerProvider.INSTANCE.getWidgetManager(this);
if (wm == null) {
//Should not happen this action is not activated if no wm
return;
}

final Intent intent = IntegrationManagerActivity.Companion.getIntent(this, mMyUserId, mRoom.getRoomId(), null, screenId);
startActivity(intent);
}
Expand Down Expand Up @@ -2325,9 +2331,9 @@ public void onClick(DialogInterface dialog, int which) {
}

Intent intent = StickerPickerActivity.Companion.getIntent(this, mMyUserId, mRoom.getRoomId(), stickerWidgetUrl, stickerWidgetId);

startActivityForResult(intent, RequestCodesKt.STICKER_PICKER_ACTIVITY_REQUEST_CODE);
}

}

/**
Expand Down
2 changes: 2 additions & 0 deletions vector/src/main/java/im/vector/activity/util/RequestCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ const val INTEGRATION_MANAGER_ACTIVITY_REQUEST_CODE = 13000
//const val BATTERY_OPTIMIZATION_FCM_REQUEST_CODE = 14000
const val BATTERY_OPTIMIZATION_FDROID_REQUEST_CODE = 14001

const val TERMS_REQUEST_CODE = 15000

149 changes: 149 additions & 0 deletions vector/src/main/java/im/vector/fragments/terms/AcceptTermsFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright 2019 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.fragments.terms

import android.app.Activity
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import androidx.core.view.isVisible
import butterknife.BindView
import butterknife.OnClick
import com.airbnb.epoxy.EpoxyRecyclerView
import im.vector.R
import im.vector.fragments.VectorBaseFragment
import im.vector.util.openUrlInExternalBrowser
import im.vector.util.state.MxAsync
import org.matrix.androidsdk.features.terms.TermsManager

class AcceptTermsFragment : VectorBaseFragment(), TermsController.Listener {


lateinit var viewModel: AcceptTermsViewModel

override fun getLayoutResId(): Int = R.layout.fragment_accept_terms

private lateinit var termsController: TermsController

@BindView(R.id.terms_recycler_view)
lateinit var termsList: EpoxyRecyclerView

@BindView(R.id.terms_bottom_accept)
lateinit var acceptButton: Button

@BindView(R.id.termsLoadingIndicator)
lateinit var progressBar: ProgressBar

@BindView(R.id.termsBottomBar)
lateinit var bottomBar: ViewGroup

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

viewModel = ViewModelProviders.of(requireActivity()).get(AcceptTermsViewModel::class.java)

val description = when (viewModel.termsArgs.type) {
TermsManager.ServiceType.IdentityService -> getString(R.string.terms_description_for_identity_server)
TermsManager.ServiceType.IntegrationManager -> getString(R.string.terms_description_for_integration_manager)
}

termsController = TermsController(description, this)
termsList.setController(termsController)

viewModel.loadTerms(getString(R.string.resources_language))

viewModel.termsList.observe(this, Observer { terms ->
when (terms) {
is MxAsync.Loading -> {
bottomBar.isVisible = false
progressBar.isVisible = true
}
is MxAsync.Error -> {
progressBar.isVisible = false
terms.stringResId.let { stringRes ->
AlertDialog.Builder(requireActivity())
.setMessage(stringRes)
.setPositiveButton(R.string.ok) { dialog, which ->
activity?.finish()
}
.show()
}
}
is MxAsync.Success -> {
updateState(terms.value)
progressBar.isVisible = false
bottomBar.isVisible = true
acceptButton.isEnabled = terms.value.all { it.accepted }
}
}
})

viewModel.acceptTerms.observe(this, Observer { request ->
when (request) {
is MxAsync.Loading -> {
progressBar.isVisible = true
}
is MxAsync.Error -> {
progressBar.isVisible = false
request.stringResId.let { stringRes ->
AlertDialog.Builder(requireActivity())
.setMessage(stringRes)
.setPositiveButton(R.string.ok, null)
.show()
}
}
is MxAsync.Success -> {
activity?.setResult(Activity.RESULT_OK)
activity?.finish()
}
}
})
}

private fun updateState(terms: List<Term>) {
termsController.setData(terms)
}

companion object {
fun newInstance(): AcceptTermsFragment {
return AcceptTermsFragment()
}
}


override fun setChecked(term: Term, isChecked: Boolean) {
viewModel.markTermAsAccepted(term.url, isChecked)
}

override fun review(term: Term) {
openUrlInExternalBrowser(this.requireContext(), term.url)
}

@OnClick(R.id.terms_bottom_accept)
fun onAcceptButton() {
viewModel.acceptTerms()
}


@OnClick(R.id.terms_bottom_decline)
fun onDeclineButton() {
activity?.finish()
}
}
Loading

0 comments on commit f23e9d8

Please sign in to comment.