Updated on 2026-08-14

This commit is contained in:
Tangem 2018-07-20 12:26:04 +03:00
parent d6e56d69d5
commit 7130b72624
6 changed files with 155 additions and 35 deletions

View file

@ -0,0 +1,98 @@
package com.tangem.data.network;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* Volley adapter for JSON requests with POST method that will be parsed into Java objects by Gson.
*/
public class GsonRequest<T> extends Request<T> {
public static final String TAG = "api." + GsonRequest.class.getSimpleName();
private Gson mGson = new Gson();
private Class<T> clazz;
private Map<String, String> headers;
private Map<String, String> params;
private Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
*/
public GsonRequest(int method,
String url,
Class<T> clazz,
Listener<T> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.listener = listener;
mGson = new Gson();
}
/**
* Make a POST request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
*/
public GsonRequest(int method,
String url,
Class<T> clazz,
Map<String, String> params,
Listener<T> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.params = params;
this.listener = listener;
this.headers = null;
mGson = new Gson();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
Log.i(TAG, (json));
return Response.success(mGson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "UnsupportedEncodingException");
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
Log.e(TAG, "JsonSyntaxException");
return Response.error(new ParseError(e));
}
}
}

View file

@ -3,18 +3,21 @@ package com.tangem.data.network
import android.content.Context
import android.content.Intent
import android.support.v7.app.AlertDialog
import android.util.Log
import com.android.volley.AuthFailureError
import com.android.volley.DefaultRetryPolicy
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import com.tangem.AppController
import com.tangem.presentation.fragment.LoadedWallet
import com.tangem.wallet.R
class VolleyHelper {
fun doRequest(context: Context, url: String, params: Map<String, String>) {
fun doRequestDebug(context: Context, url: String, params: Map<String, String>) {
val stringRequest = object : StringRequest(Request.Method.POST, url,
{ response ->
val data = GsonBuilder().setPrettyPrinting().create().toJson(JsonParser().parse(response))
@ -51,4 +54,28 @@ class VolleyHelper {
AppController.getInstance().addToRequestQueue(stringRequest)
}
fun doRequestString(context: Context, url: String, params: Map<String, String>) {
val stringRequest = object : StringRequest(Request.Method.POST, url,
Response.Listener<String> { response ->
// val json = String(response)
Log.i(LoadedWallet.TAG, response)
},
Response.ErrorListener { "That didn't work!" }) {
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
return Server.getHeader()
}
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
return params
}
}
stringRequest.retryPolicy = DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
AppController.getInstance().addToRequestQueue(stringRequest)
}
}

View file

@ -1,29 +0,0 @@
package com.tangem.presentation.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.tangem.presentation.fragment.VerifyCard;
import com.tangem.wallet.R;
public class VerifyCardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify_card);
MainActivity.commonInit(getApplicationContext());
}
@Override
public void onBackPressed() {
super.onBackPressed();
VerifyCard verifyCard = (VerifyCard) getSupportFragmentManager().findFragmentById(R.id.verify_card_fragment);
Intent data = verifyCard.prepareResultIntent();
data.putExtra("modification", "update");
finish();
}
}

View file

@ -0,0 +1,25 @@
package com.tangem.presentation.activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.tangem.presentation.fragment.VerifyCard
import com.tangem.wallet.R
class VerifyCardActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_verify_card)
MainActivity.commonInit(applicationContext)
}
override fun onBackPressed() {
super.onBackPressed()
val verifyCard = supportFragmentManager.findFragmentById(R.id.verify_card_fragment) as VerifyCard
val data = verifyCard.prepareResultIntent()
data.putExtra("modification", "update")
finish()
}
}

View file

@ -104,7 +104,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
val params = HashMap<String, String>()
params["CID"] = Util.bytesToHex(mCard!!.cid)
params["publicKey"] = Util.bytesToHex(mCard!!.cardPublicKey)
VolleyHelper().doRequest(context!!, URL_CARD_VALIDATE, params)
VolleyHelper().doRequestString(context!!, URL_CARD_VALIDATE, params)
}
override fun onCreate(savedInstanceState: Bundle?) {
@ -112,11 +112,9 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
mNfcManager = NfcManager(activity, this)
mCard = TangemCard(activity!!.intent.getStringExtra(TangemCard.EXTRA_CARD))
mCard!!.LoadFromBundle(activity!!.intent.extras!!.getBundle(TangemCard.EXTRA_CARD))
mCard!!.LoadFromBundle(activity!!.intent.extras.getBundle(TangemCard.EXTRA_CARD))
lastTag = activity!!.intent.getParcelableExtra(Main.EXTRA_LAST_DISCOVERED_TAG)
// requestCardValidate();
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -186,7 +184,7 @@ class LoadedWallet : Fragment(), NfcAdapter.ReaderCallback, CardProtocol.Notific
intent.addCategory(Intent.CATEGORY_DEFAULT)
startActivity(intent)
} catch (e: ActivityNotFoundException) {
e.printStackTrace()
Toast.makeText(context, R.string.no_compatible_wallet, Toast.LENGTH_LONG).show()
}
}

View file

@ -89,6 +89,7 @@
<!-- LoadedWalletActivity, LoadedWallet -->
<string name="wallet_empty">The wallet is empty</string>
<string name="no_compatible_wallet">No compatible wallet installed</string>
<string name="not_enough_funds">Not enough funds for transaction fee (gas)!</string>
<string name="please_wait_while_previous">Please wait while previous transaction is confirmed in blockchain</string>
<string name="please_wait_for_confirmation">Could not obtain all inputs. Swipe down to refresh.</string>