Back to project page gnucash-android.
The source code is released under:
Apache License
If you think the Android project gnucash-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (c) 2012 - 2014 Ngewi Fet <ngewif@gmail.com> */*from ww w . j a v a 2s . c o m*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.gnucash.android.receivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import org.gnucash.android.db.TransactionsDbAdapter; import org.gnucash.android.model.*; import org.gnucash.android.ui.widget.WidgetConfigurationActivity; import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.math.BigDecimal; import java.util.Currency; /** * Broadcast receiver responsible for creating transactions received through {@link Intent}s * In order to create a transaction through Intents, broadcast an intent with the arguments needed to * create the transaction. Transactions are strongly bound to {@link Account}s and it is recommended to * create an Account for your transaction splits. * <p>Remember to declare the appropriate permissions in order to create transactions with Intents. * The required permission is "org.gnucash.android.permission.RECORD_TRANSACTION"</p> * @author Ngewi Fet <ngewif@gmail.com> * @see AccountCreator * @see org.gnucash.android.model.Transaction#createIntent(org.gnucash.android.model.Transaction) */ public class TransactionRecorder extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i(this.getClass().getName(), "Received transaction recording intent"); Bundle args = intent.getExtras(); String name = args.getString(Intent.EXTRA_TITLE); String note = args.getString(Intent.EXTRA_TEXT); String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE); if (currencyCode == null) currencyCode = Money.DEFAULT_CURRENCY_CODE; Transaction transaction = new Transaction(name); transaction.setTime(System.currentTimeMillis()); transaction.setNote(note); transaction.setCurrencyCode(currencyCode); //Parse deprecated args for compatibility. Transactions were bound to accounts, now only splits are String accountUID = args.getString(Transaction.EXTRA_ACCOUNT_UID); if (accountUID != null) { TransactionType type = TransactionType.valueOf(args.getString(Transaction.EXTRA_TRANSACTION_TYPE)); BigDecimal amountBigDecimal = (BigDecimal) args.getSerializable(Transaction.EXTRA_AMOUNT); Money amount = new Money(amountBigDecimal, Currency.getInstance(currencyCode)); Split split = new Split(amount.absolute(), accountUID); split.setType(type); transaction.addSplit(split); String transferAccountUID = args.getString(Transaction.EXTRA_DOUBLE_ACCOUNT_UID); if (transferAccountUID != null) { transaction.addSplit(split.createPair(transferAccountUID)); } } String splits = args.getString(Transaction.EXTRA_SPLITS); if (splits != null) { StringReader stringReader = new StringReader(splits); BufferedReader bufferedReader = new BufferedReader(stringReader); String line = null; try { while ((line = bufferedReader.readLine()) != null){ Split split = Split.parseSplit(line); transaction.addSplit(split); } } catch (IOException e) { e.printStackTrace(); } } TransactionsDbAdapter transactionsDbAdapter = new TransactionsDbAdapter(context); transactionsDbAdapter.addTransaction(transaction); WidgetConfigurationActivity.updateAllWidgets(context); transactionsDbAdapter.close(); } }