|
| 1 | +package com.yoti.mobile.android.sdk.sampleapp; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.text.Editable; |
| 6 | +import android.text.TextUtils; |
| 7 | +import android.text.TextWatcher; |
| 8 | +import android.view.View; |
| 9 | +import android.view.View.OnFocusChangeListener; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import androidx.annotation.StringRes; |
| 13 | +import androidx.appcompat.app.AppCompatActivity; |
| 14 | + |
| 15 | +import com.yoti.mobile.android.sdk.YotiSDK; |
| 16 | +import com.yoti.mobile.android.sdk.YotiSDKButton; |
| 17 | +import com.yoti.mobile.android.sdk.exceptions.YotiSDKException; |
| 18 | +import com.yoti.mobile.android.sdk.exceptions.YotiSDKNoYotiAppException; |
| 19 | +import com.yoti.mobile.android.sdk.exceptions.YotiSDKNotValidScenarioException; |
| 20 | +import com.yoti.mobile.android.sdk.model.Scenario; |
| 21 | +import com.yoti.mobile.android.sdk.sampleapp3.R; |
| 22 | + |
| 23 | +public class MainActivity extends AppCompatActivity { |
| 24 | + |
| 25 | + private static final String TAG = MainActivity.class.getSimpleName(); |
| 26 | + |
| 27 | + private YotiSDKButton mYotiSDKButton; |
| 28 | + private View mStatusContainer; |
| 29 | + private TextView mStatusHeader; |
| 30 | + private TextView mStatusMessage; |
| 31 | + private TextView mScenarioEntry; |
| 32 | + private TextView mSdkEntry; |
| 33 | + private TextView mButtonTextEntry; |
| 34 | + private TextView mUseCaseEntry; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onCreate(Bundle savedInstanceState) { |
| 38 | + super.onCreate(savedInstanceState); |
| 39 | + |
| 40 | + setContentView(R.layout.activity_main); |
| 41 | + YotiSDK.enableSDKLogging(true); |
| 42 | + |
| 43 | + mYotiSDKButton = findViewById(R.id.button); |
| 44 | + mStatusContainer = findViewById(R.id.resultContainer); |
| 45 | + mStatusHeader = findViewById(R.id.resultHeader); |
| 46 | + mStatusMessage = findViewById(R.id.resultStatus); |
| 47 | + mScenarioEntry = findViewById(R.id.scenarioIdText); |
| 48 | + mSdkEntry = findViewById(R.id.sdkIdText); |
| 49 | + mButtonTextEntry = findViewById(R.id.buttonLabelText); |
| 50 | + mUseCaseEntry = findViewById(R.id.useCaseIdText); |
| 51 | + createScenario(); |
| 52 | + |
| 53 | + mSdkEntry.setOnFocusChangeListener(scenarioChangeListener); |
| 54 | + mScenarioEntry.setOnFocusChangeListener(scenarioChangeListener); |
| 55 | + mUseCaseEntry.setOnFocusChangeListener(scenarioChangeListener); |
| 56 | + mButtonTextEntry.addTextChangedListener(buttonTextListener); |
| 57 | + |
| 58 | + mYotiSDKButton.setOnYotiButtonClickListener(new YotiSDKButton.OnYotiButtonClickListener() { |
| 59 | + @Override |
| 60 | + public void onStartScenario() { |
| 61 | + mYotiSDKButton.setVisibility(View.GONE); |
| 62 | + showStatus(true, R.string.result_status_startScenario); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onStartScenarioError(YotiSDKException cause) { |
| 67 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 68 | + showStatus(false, R.string.result_status_startScenarioError); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + mYotiSDKButton.setOnYotiAppNotInstalledListener(new YotiSDKButton.OnYotiAppNotInstalledListener() { |
| 73 | + @Override |
| 74 | + public void onYotiAppNotInstalledError(YotiSDKNoYotiAppException cause) { |
| 75 | + //The Yoti app is not installed, let's deal with it |
| 76 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 77 | + showStatus(false, R.string.result_status_appNotInstalled); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + mYotiSDKButton.setOnYotiCalledListener(new YotiSDKButton.OnYotiCalledListener() { |
| 82 | + @Override |
| 83 | + public void onYotiCalled() { |
| 84 | + // Restore the original state |
| 85 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 86 | + showStatus(true, R.string.result_status_openYoti); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void onNewIntent(Intent intent) { |
| 93 | + super.onNewIntent(intent); |
| 94 | + setIntent(intent); |
| 95 | + processExtraData(intent); |
| 96 | + } |
| 97 | + |
| 98 | + private void processExtraData(Intent intent) { |
| 99 | + if (intent.hasExtra(ShareAttributesResultBroadcastReceiver.EXTRA_CANCELLED_BY_USER)) { |
| 100 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 101 | + showStatus(false, R.string.result_status_cancel); |
| 102 | + } |
| 103 | + |
| 104 | + if (intent.hasExtra(ShareAttributesResultBroadcastReceiver.EXTRA_IS_FAILED)) { |
| 105 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 106 | + showStatus(false, R.string.result_status_fail); |
| 107 | + } |
| 108 | + |
| 109 | + if (intent.hasExtra(ShareAttributesResultBroadcastReceiver.EXTRA_RESPONSE)) { |
| 110 | + String response = getIntent().getStringExtra(ShareAttributesResultBroadcastReceiver.EXTRA_RESPONSE); |
| 111 | + if (response != null) { |
| 112 | + showStatus(true, R.string.result_status_success); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (intent.hasExtra(ShareAttributesResultBroadcastReceiver.EXTRA_LOADING)) { |
| 117 | + showStatus(true, R.string.result_status_loading); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private OnFocusChangeListener scenarioChangeListener = new OnFocusChangeListener() { |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onFocusChange(final View v, final boolean hasFocus) { |
| 125 | + if (!hasFocus) { |
| 126 | + createScenario(); |
| 127 | + } |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + private void createScenario() { |
| 132 | + hideStatus(); |
| 133 | + mYotiSDKButton.setVisibility(View.INVISIBLE); |
| 134 | + |
| 135 | + String sdkId = mSdkEntry.getText().toString(); |
| 136 | + String scenarioId = mScenarioEntry.getText().toString(); |
| 137 | + String useCaseId = mUseCaseEntry.getText().toString(); |
| 138 | + |
| 139 | + if (TextUtils.isEmpty(sdkId) || TextUtils.isEmpty(scenarioId) || TextUtils.isEmpty(useCaseId)) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + Scenario scenario = null; |
| 144 | + |
| 145 | + try { |
| 146 | + scenario = new Scenario.Builder() |
| 147 | + .setUseCaseId(useCaseId) |
| 148 | + .setClientSDKId(sdkId) |
| 149 | + .setScenarioId(scenarioId) |
| 150 | + .setCallbackAction("com.test.app.YOTI_CALLBACK") |
| 151 | + .setBackendCallbackAction("com.test.app.BACKEND_CALLBACK") |
| 152 | + .create(); |
| 153 | + } catch (YotiSDKNotValidScenarioException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + |
| 157 | + YotiSDK.addScenario(scenario); |
| 158 | + mYotiSDKButton.setUseCaseId(useCaseId); |
| 159 | + mYotiSDKButton.setVisibility(View.VISIBLE); |
| 160 | + } |
| 161 | + |
| 162 | + private void showStatus(boolean success, @StringRes int message) { |
| 163 | + mStatusContainer.setVisibility(View.VISIBLE); |
| 164 | + if (success) { |
| 165 | + mStatusContainer.setBackgroundColor(getResources().getColor(R.color.YotiGreen)); |
| 166 | + mStatusHeader.setText(R.string.result_header_success); |
| 167 | + }else { |
| 168 | + mStatusContainer.setBackgroundColor(getResources().getColor(R.color.YotiRed)); |
| 169 | + mStatusHeader.setText(R.string.result_header_error); |
| 170 | + } |
| 171 | + |
| 172 | + mStatusMessage.setText(message); |
| 173 | + } |
| 174 | + |
| 175 | + private void hideStatus() { |
| 176 | + mStatusContainer.setVisibility(View.INVISIBLE); |
| 177 | + } |
| 178 | + |
| 179 | + private TextWatcher buttonTextListener = new TextWatcher() { |
| 180 | + |
| 181 | + @Override |
| 182 | + public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void afterTextChanged(final Editable s) { |
| 191 | + mYotiSDKButton.setText(s.toString()); |
| 192 | + } |
| 193 | + }; |
| 194 | +} |
0 commit comments