Skip to content

Commit

Permalink
Feature: Enable creation of accounts and recording of transactions th…
Browse files Browse the repository at this point in the history
…rough intents

Apps which do so will need to declare the necessary permissions
  • Loading branch information
codinguser committed Jul 20, 2012
1 parent e73c3b0 commit 7838865
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 6 deletions.
29 changes: 28 additions & 1 deletion GnucashMobile/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
<permission android:name="org.gnucash.android.permission.CREATE_ACCOUNT"
android:protectionLevel="dangerous" />
<permission android:name="org.gnucash.android.permission.RECORD_TRANSACTION"
android:protectionLevel="dangerous" />

<uses-permission android:label="@string/permission_record_transactions"
android:name="org.gnucash.android.permission.RECORD_TRANSACTION" />
<uses-permission android:label="@string/permission_create_accounts"
android:name="org.gnucash.android.permission.CREATE_ACCOUNT" />
<uses-permission android:label="@string/permission_access_sdcard"
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand All @@ -46,6 +55,24 @@
</intent-filter>
</activity>
<activity android:name=".ui.settings.SettingsActivity"></activity>
<receiver android:name=".receivers.TransactionRecorder"
android:label="Records transactions received through intents"
android:permission="org.gnucash.android.permission.RECORD_TRANSACTION">
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.org.gnucash.android.transaction"/>
</intent-filter>
</receiver>
<receiver android:name=".receivers.AccountCreator"
android:label="Creates new accounts"
android:permission="org.gnucash.android.permission.CREATE_ACCOUNT" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.org.gnucash.android.account"/>
</intent-filter>
</receiver>
</application>

</manifest>
5 changes: 4 additions & 1 deletion GnucashMobile/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<string name="choose_currency">Choose default currency</string>
<string name="title_default_currency">Default currency</string>
<string name="summary_default_currency">Default currency to assign to new accounts</string>
<string name="permission_record_transactions">Enables recording transactions in Gnucash for Android</string><string name="permission_create_accounts">Enables creation of accounts in Gnucash for Android</string>
<string-array name="currency_names">
<item>Afghani</item>
<item>Algerian Dinar</item>
Expand Down Expand Up @@ -437,5 +438,7 @@
<item>ZMK</item>
<item>ZWL</item>
<item>PLN</item>
</string-array>
</string-array>


</resources>
5 changes: 4 additions & 1 deletion GnucashMobile/src/org/gnucash/android/data/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import org.gnucash.android.ui.MainActivity;
Expand All @@ -42,6 +41,8 @@
*/
public class Account {

public static final String MIME_TYPE = "vnd.android.cursor.item/vnd.org.gnucash.android.account";

/**
* The type of account
*
Expand All @@ -66,6 +67,8 @@ public enum AccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE};
* List of transactions in this account
*/
private List<Transaction> mTransactionsList = new ArrayList<Transaction>();

public static final String EXTRA_CURRENCY_CODE = "org.gnucash.extra.currency_code";

/**
* Constructor
Expand Down
2 changes: 1 addition & 1 deletion GnucashMobile/src/org/gnucash/android/data/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public int compareTo(Money another) {
}

public static String parse(String formattedAmount){
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance();
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.getDefault());
String result = formattedAmount;
try {
result = formatter.parse(formattedAmount).toString();
Expand Down
4 changes: 4 additions & 0 deletions GnucashMobile/src/org/gnucash/android/data/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class Transaction {
*/
public enum TransactionType {DEBIT, CREDIT};

public static final String MIME_TYPE = "vnd.android.cursor.item/vnd.org.gnucash.android.transaction";
public static final String EXTRA_ACCOUNT_UID = "org.gnucash.android.extra.account_uid";
public static final String EXTRA_AMOUNT = "org.gnucash.android.extra.amount";

private Money mAmount;
private String mTransactionUID;
private String mName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,16 @@ public String getAccountUID(long acountRowID){
return uid;
}

public long getAccountID(String accountUID){
long id = -1;
Cursor c = mDb.query(DatabaseHelper.ACCOUNTS_TABLE_NAME,
new String[]{DatabaseHelper.KEY_ROW_ID},
DatabaseHelper.KEY_UID + "='" + accountUID + "'",
null, null, null, null);
if (c != null && c.moveToFirst()){
id = c.getLong(0);
c.close();
}
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Written By: Ngewi Fet <ngewif@gmail.com>
* Copyright (c) 2012 Ngewi Fet
*
* This file is part of Gnucash for Android
*
* Gnucash for Android is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/

package org.gnucash.android.receivers;

import java.util.Currency;

import org.gnucash.android.data.Account;
import org.gnucash.android.db.AccountsDbAdapter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class AccountCreator extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.i("Gnucash", "Received account creation intent");
Bundle args = intent.getExtras();

String uid = args.getString(Intent.EXTRA_UID);

Account account = new Account(args.getString(Intent.EXTRA_TITLE));
String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);

if (currencyCode != null){
Currency currency = Currency.getInstance(currencyCode);
account.setCurrency(currency);
}

if (uid != null)
account.setUID(uid);

AccountsDbAdapter accountsAdapter = new AccountsDbAdapter(context);
accountsAdapter.addAccount(account);
accountsAdapter.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Written By: Ngewi Fet <ngewif@gmail.com>
* Copyright (c) 2012 Ngewi Fet
*
* This file is part of Gnucash for Android
*
* Gnucash for Android is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/

package org.gnucash.android.receivers;

import java.math.BigDecimal;
import java.util.Currency;

import org.gnucash.android.data.Account;
import org.gnucash.android.data.Money;
import org.gnucash.android.data.Transaction;
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.ui.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class TransactionRecorder extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.i("Gnucash", "Received transaction recording intent");
Bundle args = intent.getExtras();
String name = args.getString(Intent.EXTRA_TITLE);
String note = args.getString(Intent.EXTRA_TEXT);
double amountDouble = args.getDouble(Transaction.EXTRA_AMOUNT, 0);
String currencyCode = args.getString(Account.EXTRA_CURRENCY_CODE);
if (currencyCode == null)
currencyCode = MainActivity.DEFAULT_CURRENCY_CODE;

String accountUID = args.getString(Transaction.EXTRA_ACCOUNT_UID);
if (accountUID == null)
accountUID = "uncategorized";

Money amount = new Money(new BigDecimal(amountDouble), Currency.getInstance(currencyCode));
Transaction transaction = new Transaction(amount, name);
transaction.setTime(System.currentTimeMillis());
transaction.setDescription(note);
transaction.setAccountUID(accountUID);

TransactionsDbAdapter transacionsDbAdapter = new TransactionsDbAdapter(context);
transacionsDbAdapter.addTransaction(transaction);
transacionsDbAdapter.close();
}

}
3 changes: 3 additions & 0 deletions GnucashMobileTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

<uses-permission android:name="org.gnucash.android.permission.RECORD_TRANSACTION" />
<uses-permission android:name="org.gnucash.android.permission.CREATE_ACCOUNT" />

<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="org.gnucash.android" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.gnucash.android.ui.MainActivity;
import org.gnucash.android.ui.accounts.AccountsListFragment;

import android.content.Intent;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
Expand Down Expand Up @@ -174,6 +175,33 @@ public void testDeleteAccount(){
transDbAdapter.close();
}

public void testIntentAccountCreation(){
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.putExtra(Intent.EXTRA_TITLE, "Intent Account");
intent.putExtra(Intent.EXTRA_UID, "intent-account");
intent.putExtra(Account.EXTRA_CURRENCY_CODE, "EUR");
intent.setType(Account.MIME_TYPE);
getActivity().sendBroadcast(intent);

//give time for the account to be created
synchronized (mSolo) {
try {
mSolo.wait(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

AccountsDbAdapter dbAdapter = new AccountsDbAdapter(getActivity());
Account account = dbAdapter.getAccount("intent-account");
dbAdapter.close();
assertNotNull(account);
assertEquals("Intent Account", account.getName());
assertEquals("intent-account", account.getUID());
assertEquals("EUR", account.getCurrency().getCurrencyCode());
}

protected void tearDown() throws Exception {
AccountsDbAdapter adapter = new AccountsDbAdapter(getActivity());
adapter.deleteAllAccounts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.gnucash.android.test;

import java.util.Date;
import java.util.List;

import org.gnucash.android.R;
import org.gnucash.android.data.Account;
Expand All @@ -37,6 +38,7 @@
import org.gnucash.android.ui.accounts.AccountsListFragment;
import org.gnucash.android.ui.transactions.NewTransactionFragment;

import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.Fragment;
import android.test.ActivityInstrumentationTestCase2;
Expand Down Expand Up @@ -122,8 +124,9 @@ public void testAddTransaction(){
mSolo.enterText(0, "Lunch");
mSolo.enterText(1, "899");
//check that the amount is correctly converted in the input field
String actualValue = mSolo.getEditText(1).getText().toString();
assertEquals(new Money("-8.99").toPlainString(), actualValue);
String value = mSolo.getEditText(1).getText().toString();
double actualValue = Double.parseDouble(Money.parse(value));
assertEquals(-8.99, actualValue);

int transactionsCount = getTranscationCount();

Expand Down Expand Up @@ -200,6 +203,42 @@ public void testDeleteTransaction(){

}

public void testIntentTransactionRecording(){
TransactionsDbAdapter trxnAdapter = new TransactionsDbAdapter(getActivity());
int beforeCount = trxnAdapter.getTransactionsCount(trxnAdapter.getAccountID(DUMMY_ACCOUNT_UID));
Intent transactionIntent = new Intent(Intent.ACTION_INSERT);
transactionIntent.setType(Transaction.MIME_TYPE);
transactionIntent.putExtra(Intent.EXTRA_TITLE, "Power intents");
transactionIntent.putExtra(Intent.EXTRA_TEXT, "Intents for sale");
transactionIntent.putExtra(Transaction.EXTRA_AMOUNT, 4.99);
transactionIntent.putExtra(Transaction.EXTRA_ACCOUNT_UID, DUMMY_ACCOUNT_UID);

getActivity().sendBroadcast(transactionIntent);

synchronized (mSolo) {
try {
mSolo.wait(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

int afterCount = trxnAdapter.getTransactionsCount(trxnAdapter.getAccountID(DUMMY_ACCOUNT_UID));

assertEquals(beforeCount + 1, afterCount);

List<Transaction> transactions = trxnAdapter.getAllTransactionsForAccount(DUMMY_ACCOUNT_UID);

for (Transaction transaction : transactions) {
if (transaction.getName().equals("Power intents")){
assertEquals("Intents for sale", transaction.getDescription());
assertEquals(4.99, transaction.getAmount().asDouble());
}
}

trxnAdapter.close();
}

private void refreshAccountsList(){
Fragment fragment = getActivity()
.getSupportFragmentManager()
Expand Down

0 comments on commit 7838865

Please sign in to comment.