-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): notificaiton with consent details
Implement an example of fetching rich consent record when transaction linking id is present in the notificaiton.
- Loading branch information
1 parent
bc2c19d
commit eb19b12
Showing
5 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
app/src/main/java/com/auth0/guardian/sample/NotificationWithConsentDetailsActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* | ||
* Copyright (c) 2016 Auth0 (http://auth0.com) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.auth0.guardian.sample; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import com.auth0.android.guardian.sdk.Guardian; | ||
import com.auth0.android.guardian.sdk.ParcelableNotification; | ||
import com.auth0.android.guardian.sdk.model.RichConsent; | ||
import com.auth0.android.guardian.sdk.networking.Callback; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.spec.InvalidKeySpecException; | ||
|
||
public class NotificationWithConsentDetailsActivity extends AppCompatActivity { | ||
|
||
private static final String TAG = NotificationWithConsentDetailsActivity.class.getName(); | ||
|
||
private TextView bindingMessageText; | ||
private TextView scopeText; | ||
private TextView dateText; | ||
|
||
private Guardian guardian; | ||
private ParcelableEnrollment enrollment; | ||
private ParcelableNotification notification; | ||
private RichConsent consentDetails; | ||
|
||
static Intent getStartIntent(@NonNull Context context, | ||
@NonNull ParcelableNotification notification, | ||
@NonNull ParcelableEnrollment enrollment) { | ||
if (!enrollment.getId().equals(notification.getEnrollmentId())) { | ||
final String message = String.format("Notification doesn't match enrollment (%s != %s)", | ||
notification.getEnrollmentId(), enrollment.getId()); | ||
throw new IllegalArgumentException(message); | ||
} | ||
|
||
Intent intent = new Intent(context, NotificationWithConsentDetailsActivity.class); | ||
intent.putExtra(Constants.ENROLLMENT, enrollment); | ||
intent.putExtra(Constants.NOTIFICATION, notification); | ||
return intent; | ||
} | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_notification_with_consent_details); | ||
|
||
guardian = new Guardian.Builder() | ||
.url(Uri.parse(getString(R.string.guardian_url))) | ||
.enableLogging() | ||
.build(); | ||
|
||
Intent intent = getIntent(); | ||
enrollment = intent.getParcelableExtra(Constants.ENROLLMENT); | ||
notification = intent.getParcelableExtra(Constants.NOTIFICATION); | ||
|
||
setupUI(); | ||
updateUI(); | ||
|
||
try { | ||
guardian.fetchConsent(notification, enrollment).start(new Callback<RichConsent>() { | ||
@Override | ||
public void onSuccess(RichConsent response) { | ||
consentDetails = response; | ||
updateUI(); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable exception) { | ||
Log.e(TAG, "Error obtaining consent details", exception); | ||
} | ||
}); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
Log.e(TAG, "Error requesting consent details", e); | ||
} | ||
|
||
} | ||
|
||
private void setupUI() { | ||
bindingMessageText = (TextView) findViewById(R.id.bindingMessage); | ||
scopeText = (TextView) findViewById(R.id.scope); | ||
dateText = (TextView) findViewById(R.id.dateText); | ||
|
||
Button rejectButton = (Button) findViewById(R.id.rejectButton); | ||
assert rejectButton != null; | ||
rejectButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
rejectRequested(); | ||
} | ||
}); | ||
|
||
Button allowButton = (Button) findViewById(R.id.allowButton); | ||
assert allowButton != null; | ||
allowButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
allowRequested(); | ||
} | ||
}); | ||
} | ||
|
||
private void updateUI() { | ||
if (consentDetails != null) { | ||
bindingMessageText.setText(consentDetails.requested_details.binding_message); | ||
scopeText.setText(String.join(", ", consentDetails.requested_details.scope)); | ||
} else { | ||
bindingMessageText.setText("N/A"); | ||
scopeText.setText("N/A"); | ||
} | ||
dateText.setText(notification.getDate().toString()); | ||
} | ||
|
||
private void rejectRequested() { | ||
guardian | ||
.reject(notification, enrollment) | ||
.start(new DialogCallback<>(this, | ||
R.string.progress_title_please_wait, | ||
R.string.progress_message_reject, | ||
new Callback<Void>() { | ||
@Override | ||
public void onSuccess(Void response) { | ||
finish(); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable exception) { | ||
|
||
} | ||
})); | ||
} | ||
|
||
private void allowRequested() { | ||
guardian | ||
.allow(notification, enrollment) | ||
.start(new DialogCallback<>(this, | ||
R.string.progress_title_please_wait, | ||
R.string.progress_message_allow, | ||
new Callback<Void>() { | ||
@Override | ||
public void onSuccess(Void response) { | ||
finish(); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable exception) { | ||
|
||
} | ||
})); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
app/src/main/res/layout/activity_notification_with_consent_details.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".NotificationWithConsentDetailsActivity"> | ||
|
||
<com.google.android.material.appbar.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:theme="@style/AppTheme.AppBar"> | ||
|
||
<androidx.appcompat.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:theme="@style/AppTheme.ToolBar" | ||
app:navigationIcon="@mipmap/ic_launcher" | ||
app:popupTheme="@style/AppTheme.Popup" | ||
app:title="@string/title_notification" /> | ||
|
||
</com.google.android.material.appbar.AppBarLayout> | ||
|
||
<RelativeLayout | ||
android:id="@+id/relativeLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
app:layout_behavior="@string/appbar_scrolling_view_behavior"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/textView3" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="30sp" | ||
android:text="Do you approve this transaction?" | ||
android:textSize="34sp" /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="Binding Message" | ||
android:textSize="12sp" | ||
android:theme="@style/Label.Header" /> | ||
|
||
<TextView | ||
android:id="@+id/bindingMessage" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="12dp" | ||
android:textSize="12sp" | ||
android:theme="@style/Label" | ||
tools:text="ABC-123-DEF" /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="Requested scope" | ||
android:textSize="12sp" | ||
android:theme="@style/Label.Header" /> | ||
|
||
<TextView | ||
android:id="@+id/scope" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="12dp" | ||
android:textSize="12sp" | ||
android:theme="@style/Label" | ||
tools:text="read:foo" /> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="Date" | ||
android:textSize="12sp" | ||
android:theme="@style/Label.Header" /> | ||
|
||
<TextView | ||
android:id="@+id/dateText" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="36dp" | ||
android:textSize="12sp" | ||
android:theme="@style/Label" | ||
tools:text="Today at 18:56:21" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentBottom="true"> | ||
|
||
<Button | ||
android:id="@+id/rejectButton" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:text="Deny" /> | ||
|
||
<Button | ||
android:id="@+id/allowButton" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="1" | ||
android:text="Allow" /> | ||
|
||
</LinearLayout> | ||
|
||
</RelativeLayout> | ||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters