Skip to content

Commit

Permalink
available balance on sendfragment
Browse files Browse the repository at this point in the history
change update data logic
start working with NFC
  • Loading branch information
KirillVolkov committed Apr 5, 2017
1 parent 1675afe commit 22a31c1
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 62 deletions.
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

<application
android:allowBackup="true"
Expand All @@ -26,6 +28,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="text/plain" />
</intent-filter>

</activity>
<meta-data
android:name="io.fabric.ApiKey"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.qtum.mromanovsky.qtum.dataprovider;


public interface BalanceChangeListener {
void onChangeBalance(String balance, String unconfirmedBalance);
boolean getVisibility();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import org.qtum.mromanovsky.qtum.dataprovider.RestAPI.gsonmodels.History.History;

public interface UpdateServiceListener {
public interface TransactionListener {
void onNewHistory(History history);
void onChangeBalance(String balance, String unconfirmedBalance);
boolean getVisibility();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.google.gson.Gson;

Expand All @@ -25,7 +24,6 @@
import org.qtum.mromanovsky.qtum.datastorage.KeyStorage;
import org.qtum.mromanovsky.qtum.ui.activity.MainActivity.MainActivity;

import java.math.BigDecimal;
import java.net.URISyntaxException;

import io.socket.client.IO;
Expand All @@ -40,7 +38,8 @@ public class UpdateService extends Service {

public final int DEFAULT_NOTIFICATION_ID = 101;
private NotificationManager notificationManager;
private UpdateServiceListener mListener = null;
private TransactionListener mTransactionListener = null;
private BalanceChangeListener mBalanceChangeListener;
private Notification notification;
private Socket socket;
private int totalTransaction = 0;
Expand All @@ -66,18 +65,18 @@ public void call(Object... args) {
arr.put(address);
}
socket.emit("subscribe","balance_subscribe", arr);
sendNotification("Default","Default","Defaul",null);
sendNotification("Default","Default","Default",null);

}
}).on("balance_changed", new Emitter.Listener() {
@Override
public void call(Object... args) {
if(mListener!=null) {
if(mBalanceChangeListener !=null) {
JSONObject data = (JSONObject) args[0];
try {
double unconfirmedBalance = data.getDouble("unconfirmedBalance") / 100000000;
double balance = data.getDouble("balance") / 100000000;
mListener.onChangeBalance(String.valueOf(balance), String.valueOf(unconfirmedBalance));
mBalanceChangeListener.onChangeBalance(String.valueOf(balance), String.valueOf(unconfirmedBalance));
} catch (JSONException e) {
e.printStackTrace();
}
Expand All @@ -89,9 +88,9 @@ public void call(Object... args) {
Gson gson = new Gson();
JSONObject data = (JSONObject) args[0];
History history = gson.fromJson(data.toString(),History.class);
if(mListener != null) {
mListener.onNewHistory(history);
if(!mListener.getVisibility()){
if(mTransactionListener != null) {
mTransactionListener.onNewHistory(history);
if(!mTransactionListener.getVisibility()){
if(history.getBlockTime()!=null) {
totalTransaction++;
sendNotification("New confirmed transaction", totalTransaction + " new confirmed transaction",
Expand Down Expand Up @@ -202,8 +201,20 @@ public IBinder onBind(Intent intent) {
return mUpdateBinder;
}

public void registerListener(UpdateServiceListener updateServiceListener){
mListener = updateServiceListener;
public void addTransactionListener(TransactionListener updateServiceListener){
mTransactionListener = updateServiceListener;
}

public void removeTransactionListener(){
mTransactionListener = null;
}

public void addBalanceChangeListener(BalanceChangeListener balanceChangeListener){
mBalanceChangeListener = balanceChangeListener;
}

public void removeBalanceChangeListener(){
mBalanceChangeListener = null;
}

public void clearNotification(){
Expand All @@ -213,10 +224,6 @@ public void clearNotification(){
}
}

public void removeListener(){
mListener = null;
}

public class UpdateBinder extends Binder {
public UpdateService getService() {
return UpdateService.this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.qtum.mromanovsky.qtum.ui.activity.MainActivity;

import android.Manifest;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.internal.BottomNavigationItemView;
Expand All @@ -21,6 +27,7 @@
import org.qtum.mromanovsky.qtum.ui.activity.BaseActivity.BaseActivity;
import org.qtum.mromanovsky.qtum.ui.fragment.BaseFragment.BaseFragment;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import butterknife.BindView;
Expand All @@ -31,6 +38,7 @@ public class MainActivity extends BaseActivity implements MainActivityView {
private static final int LAYOUT = R.layout.activity_main;
private MainActivityPresenterImpl mMainActivityPresenterImpl;
private static final int REQUEST_CAMERA = 0;
private NfcAdapter mNfcAdapter;

@BindView(R.id.bottom_navigation_view)
BottomNavigationView mBottomNavigationView;
Expand All @@ -53,7 +61,77 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
//Fabric.with(this, new Crashlytics());
loadPermissions(Manifest.permission.CAMERA, REQUEST_CAMERA);

//TODO: NFC
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
boolean b = mNfcAdapter.isEnabled();
String s = getIntent().getAction();

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(s)) {
Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}

private void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) return;

String text = "";
// String tagId = new String(msgs[0].getRecords()[0].getType());
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
// String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

try {
// Get the Text
text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}

int i = 2;
}


@Override
protected void onResume() {
super.onResume();

Intent intent = new Intent(getContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

final PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};

// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType("text/plain");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("Check your mime type.");
}

mNfcAdapter.enableForegroundDispatch(this,pendingIntent,filters,techList);
}

@Override
protected void onPause() {
super.onPause();

mNfcAdapter.disableForegroundDispatch(this);
}
//TODO: NFC END

@Override
protected void onNewIntent(Intent intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void setRootFragment(Fragment fragment) {
@Override
public void processIntent(Intent intent) {
if(intent.getBooleanExtra("notification_action", false)){
WalletFragment walletFragment = WalletFragment.newInstance();
getView().openRootFragment(walletFragment);
mRootFragment = WalletFragment.newInstance();
getView().openRootFragment(mRootFragment);
getView().setIconChecked(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -37,7 +38,8 @@ public class SendBaseFragment extends BaseFragment implements SendBaseFragmentVi
ImageButton mImageButtonBack;
@BindView(R.id.tv_toolbar_send)
TextView mTextViewToolBar;

@BindView(R.id.progress_bar)
ProgressBar mProgressBar;
@BindView(R.id.tv_total_balance_number)
TextView mTextViewTotalBalanceNumber;

Expand Down Expand Up @@ -158,8 +160,16 @@ public void clearError() {
}

@Override
public void setBalance(double balance) {
mTextViewTotalBalanceNumber.setText(String.valueOf(balance) + " QTUM");
public void setProgressBar() {
mProgressBar.setVisibility(View.VISIBLE);
mTextViewTotalBalanceNumber.setVisibility(View.GONE);
}

@Override
public void updateAvailableBalance(String balance) {
mProgressBar.setVisibility(View.GONE);
mTextViewTotalBalanceNumber.setVisibility(View.VISIBLE);
mTextViewTotalBalanceNumber.setText(balance);
}

public void onResponse(String pubAddress, Double amount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.qtum.mromanovsky.qtum.ui.fragment.SendBaseFragment;


import java.util.List;

interface SendBaseFragmentInteractor {
void getUnspentOutputs(SendBaseFragmentInteractorImpl.GetUnspentListCallBack callBack);
void sendTx(String address, String amount, SendBaseFragmentInteractorImpl.SendTxCallBack callBack);
void createTx(String address, String amount, SendBaseFragmentInteractorImpl.CreateTxCallBack callBack);
int getPassword();
double getBalance();
List<String> getAddresses();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.qtum.mromanovsky.qtum.ui.fragment.SendBaseFragment;

import android.content.Context;
import android.support.v4.content.res.TypedArrayUtils;
import android.util.Log;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.AddressFormatException;
Expand Down Expand Up @@ -224,4 +222,9 @@ public int getPassword() {
public double getBalance() {
return HistoryList.getInstance().getBalance() * QtumSharedPreference.getInstance().getExchangeRates(mContext);
}

@Override
public List<String> getAddresses() {
return KeyStorage.getInstance().getAddresses();
}
}
Loading

0 comments on commit 22a31c1

Please sign in to comment.