|
46 | 46 | import net.bierbaumer.otp_authenticator.TOTPHelper;
|
47 | 47 |
|
48 | 48 | import org.apache.commons.codec.binary.Base32;
|
| 49 | +import org.json.JSONException; |
| 50 | +import org.json.JSONObject; |
49 | 51 |
|
50 | 52 | import butterknife.BindView;
|
51 | 53 | import butterknife.ButterKnife;
|
@@ -132,22 +134,28 @@ public void onCreate(Bundle savedInstanceState) {
|
132 | 134 |
|
133 | 135 | if (credential != null) {
|
134 | 136 | handler = new Handler();
|
135 |
| - if (credential.getOtp().length() > 4) { |
136 |
| - otp_refresh = new Runnable() { |
137 |
| - @Override |
138 |
| - public void run() { |
139 |
| - int progress = (int) (System.currentTimeMillis() / 1000) % 30; |
140 |
| - otp_progress.setProgress(progress * 100); |
141 |
| - |
142 |
| - ObjectAnimator animation = ObjectAnimator.ofInt(otp_progress, "progress", (progress + 1) * 100); |
143 |
| - animation.setDuration(1000); |
144 |
| - animation.setInterpolator(new LinearInterpolator()); |
145 |
| - animation.start(); |
146 |
| - |
147 |
| - otp.setText(TOTPHelper.generate(new Base32().decode(credential.getOtp()))); |
148 |
| - handler.postDelayed(this, 1000); |
149 |
| - } |
150 |
| - }; |
| 137 | + try { |
| 138 | + JSONObject otpObj = new JSONObject(credential.getOtp()); |
| 139 | + if (otpObj.has("secret") && otpObj.getString("secret").length() > 4) { |
| 140 | + String otpSecret = otpObj.getString("secret"); |
| 141 | + otp_refresh = new Runnable() { |
| 142 | + @Override |
| 143 | + public void run() { |
| 144 | + int progress = (int) (System.currentTimeMillis() / 1000) % 30; |
| 145 | + otp_progress.setProgress(progress * 100); |
| 146 | + |
| 147 | + ObjectAnimator animation = ObjectAnimator.ofInt(otp_progress, "progress", (progress + 1) * 100); |
| 148 | + animation.setDuration(1000); |
| 149 | + animation.setInterpolator(new LinearInterpolator()); |
| 150 | + animation.start(); |
| 151 | + |
| 152 | + otp.setText(TOTPHelper.generate(new Base32().decode(otpSecret))); |
| 153 | + handler.postDelayed(this, 1000); |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | + } catch (JSONException e) { |
| 158 | + e.printStackTrace(); |
151 | 159 | }
|
152 | 160 | } else {
|
153 | 161 | Toast.makeText(getContext(), getString(R.string.error_occurred), Toast.LENGTH_LONG).show();
|
|
0 commit comments