2525import android .content .SharedPreferences ;
2626import android .content .SharedPreferences .Editor ;
2727import android .database .Cursor ;
28+ import android .database .sqlite .SQLiteDatabase ;
2829import android .os .Bundle ;
29- import android .preference .PreferenceManager ;
3030import android .support .v4 .widget .SimpleCursorAdapter ;
3131import android .util .Log ;
3232import android .view .View ;
33+ import android .widget .AdapterView ;
3334import android .widget .Button ;
3435import android .widget .RemoteViews ;
3536import android .widget .Spinner ;
3637import android .widget .Toast ;
3738
3839import org .gnucash .android .R ;
40+ import org .gnucash .android .db .BookDbHelper ;
41+ import org .gnucash .android .db .DatabaseHelper ;
42+ import org .gnucash .android .db .DatabaseSchema ;
3943import org .gnucash .android .db .adapter .AccountsDbAdapter ;
44+ import org .gnucash .android .db .adapter .BooksDbAdapter ;
4045import org .gnucash .android .model .Account ;
46+ import org .gnucash .android .model .Book ;
4147import org .gnucash .android .model .Money ;
4248import org .gnucash .android .receivers .TransactionAppWidgetProvider ;
4349import org .gnucash .android .ui .account .AccountsActivity ;
4450import org .gnucash .android .ui .common .FormActivity ;
4551import org .gnucash .android .ui .common .UxArgument ;
52+ import org .gnucash .android .ui .settings .PreferenceActivity ;
4653import org .gnucash .android .ui .transaction .TransactionsActivity ;
4754import org .gnucash .android .util .QualifiedAccountNameCursorAdapter ;
4855
4956import java .util .Locale ;
5057
58+ import butterknife .Bind ;
59+ import butterknife .ButterKnife ;
60+
5161/**
5262 * Activity for configuration which account to display on a widget.
5363 * The activity is opened each time a widget is added to the homescreen
@@ -57,19 +67,42 @@ public class WidgetConfigurationActivity extends Activity {
5767 private AccountsDbAdapter mAccountsDbAdapter ;
5868 private int mAppWidgetId ;
5969
60- private Spinner mAccountsSpinner ;
61- private Button mOkButton ;
62- private Button mCancelButton ;
63-
70+ @ Bind (R .id .input_accounts_spinner ) Spinner mAccountsSpinner ;
71+ @ Bind (R .id .input_books_spinner ) Spinner mBooksSpinner ;
72+
73+ @ Bind (R .id .btn_save ) Button mOkButton ;
74+ @ Bind (R .id .btn_cancel ) Button mCancelButton ;
75+ private SimpleCursorAdapter mAccountsCursorAdapter ;
76+
77+
6478 @ Override
6579 public void onCreate (Bundle savedInstanceState ) {
6680 super .onCreate (savedInstanceState );
6781 setContentView (R .layout .widget_configuration );
6882 setResult (RESULT_CANCELED );
69-
70- mAccountsSpinner = (Spinner ) findViewById (R .id .input_accounts_spinner );
71- mOkButton = (Button ) findViewById (R .id .btn_save );
72- mCancelButton = (Button ) findViewById (R .id .btn_cancel );
83+
84+ ButterKnife .bind (this );
85+
86+ BooksDbAdapter booksDbAdapter = BooksDbAdapter .getInstance ();
87+ Cursor booksCursor = booksDbAdapter .fetchAllRecords ();
88+ String currentBookUID = booksDbAdapter .getActiveBookUID ();
89+
90+ //determine the position of the currently active book in the cursor
91+ int position = 0 ;
92+ while (booksCursor .moveToNext ()){
93+ String bookUID = booksCursor .getString (booksCursor .getColumnIndexOrThrow (DatabaseSchema .BookEntry .COLUMN_UID ));
94+ if (bookUID .equals (currentBookUID ))
95+ break ;
96+ ++position ;
97+ }
98+
99+ SimpleCursorAdapter booksCursorAdapter = new SimpleCursorAdapter (this ,
100+ android .R .layout .simple_spinner_item , booksCursor ,
101+ new String []{DatabaseSchema .BookEntry .COLUMN_DISPLAY_NAME },
102+ new int []{android .R .id .text1 }, 0 );
103+ booksCursorAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
104+ mBooksSpinner .setAdapter (booksCursorAdapter );
105+ mBooksSpinner .setSelection (position );
73106
74107 mAccountsDbAdapter = AccountsDbAdapter .getInstance ();
75108 Cursor cursor = mAccountsDbAdapter .fetchAllRecordsOrderedByFullName ();
@@ -79,10 +112,10 @@ public void onCreate(Bundle savedInstanceState) {
79112 finish ();
80113 }
81114
82- SimpleCursorAdapter cursorAdapter = new QualifiedAccountNameCursorAdapter (this , cursor );
115+ mAccountsCursorAdapter = new QualifiedAccountNameCursorAdapter (this , cursor );
83116 //without this line, the app crashes when a user tries to select an account
84- cursorAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
85- mAccountsSpinner .setAdapter (cursorAdapter );
117+ mAccountsCursorAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
118+ mAccountsSpinner .setAdapter (mAccountsCursorAdapter );
86119
87120 bindListeners ();
88121 }
@@ -91,6 +124,24 @@ public void onCreate(Bundle savedInstanceState) {
91124 * Sets click listeners for the buttons in the dialog
92125 */
93126 private void bindListeners () {
127+ mBooksSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
128+ @ Override
129+ public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
130+ Book book = BooksDbAdapter .getInstance ().getRecord (id );
131+ SQLiteDatabase db = new DatabaseHelper (WidgetConfigurationActivity .this , book .getUID ()).getWritableDatabase ();
132+ mAccountsDbAdapter = new AccountsDbAdapter (db );
133+
134+ Cursor cursor = mAccountsDbAdapter .fetchAllRecordsOrderedByFullName ();
135+ mAccountsCursorAdapter .swapCursor (cursor );
136+ mAccountsCursorAdapter .notifyDataSetChanged ();
137+ }
138+
139+ @ Override
140+ public void onNothingSelected (AdapterView <?> parent ) {
141+ //nothing to see here, move along
142+ }
143+ });
144+
94145 mOkButton .setOnClickListener (new View .OnClickListener () {
95146
96147 @ Override
@@ -110,12 +161,17 @@ public void onClick(View v) {
110161
111162 long accountId = mAccountsSpinner .getSelectedItemId ();
112163 String accountUID = mAccountsDbAdapter .getUID (accountId );
113- SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (WidgetConfigurationActivity .this );
164+
165+ long bookId = mBooksSpinner .getSelectedItemId ();
166+ String bookUID = BooksDbAdapter .getInstance ().getUID (bookId );
167+
168+ SharedPreferences prefs = PreferenceActivity .getBookSharedPreferences (bookUID );
169+ //PreferenceManager.getDefaultSharedPreferences(WidgetConfigurationActivity.this);
114170 Editor editor = prefs .edit ();
115171 editor .putString (UxArgument .SELECTED_ACCOUNT_UID + mAppWidgetId , accountUID );
116- editor .commit ();
172+ editor .apply ();
117173
118- updateWidget (WidgetConfigurationActivity .this , mAppWidgetId , accountUID );
174+ updateWidget (WidgetConfigurationActivity .this , mAppWidgetId , accountUID , bookUID );
119175
120176 Intent resultValue = new Intent ();
121177 resultValue .putExtra (AppWidgetManager .EXTRA_APPWIDGET_ID , mAppWidgetId );
@@ -137,14 +193,16 @@ public void onClick(View v) {
137193 * Updates the widget with id <code>appWidgetId</code> with information from the
138194 * account with record ID <code>accountId</code>
139195 * If the account has been deleted, then a notice is posted in the widget
140- * @param appWidgetId ID of the widget to be updated
196+ * @param appWidgetId ID of the widget to be updated
141197 * @param accountUID GUID of the account tied to the widget
198+ * @param bookUID GUID of the book with the relevant account
142199 */
143- public static void updateWidget (final Context context , int appWidgetId , String accountUID ) {
200+ public static void updateWidget (final Context context , int appWidgetId , String accountUID , String bookUID ) {
144201 Log .i ("WidgetConfiguration" , "Updating widget: " + appWidgetId );
145202 AppWidgetManager appWidgetManager = AppWidgetManager .getInstance (context );
146203
147- AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter .getInstance ();
204+ AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter (BookDbHelper .getDatabase (bookUID ));
205+
148206 final Account account ;
149207 try {
150208 account = accountsDbAdapter .getRecord (accountUID );
@@ -161,9 +219,9 @@ public static void updateWidget(final Context context, int appWidgetId, String a
161219 views .setOnClickPendingIntent (R .id .widget_layout , pendingIntent );
162220 views .setOnClickPendingIntent (R .id .btn_new_transaction , pendingIntent );
163221 appWidgetManager .updateAppWidget (appWidgetId , views );
164- Editor editor = PreferenceManager .getDefaultSharedPreferences (context ).edit ();
222+ Editor editor = PreferenceActivity . getActiveBookSharedPreferences (). edit (); // PreferenceManager.getDefaultSharedPreferences(context).edit();
165223 editor .remove (UxArgument .SELECTED_ACCOUNT_UID + appWidgetId );
166- editor .commit ();
224+ editor .apply ();
167225 return ;
168226 }
169227
@@ -183,6 +241,7 @@ public static void updateWidget(final Context context, int appWidgetId, String a
183241 accountViewIntent .setAction (Intent .ACTION_VIEW );
184242 accountViewIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
185243 accountViewIntent .putExtra (UxArgument .SELECTED_ACCOUNT_UID , accountUID );
244+ accountViewIntent .putExtra (UxArgument .BOOK_UID , bookUID );
186245 PendingIntent accountPendingIntent = PendingIntent
187246 .getActivity (context , appWidgetId , accountViewIntent , 0 );
188247 views .setOnClickPendingIntent (R .id .widget_layout , accountPendingIntent );
@@ -191,6 +250,7 @@ public static void updateWidget(final Context context, int appWidgetId, String a
191250 newTransactionIntent .setAction (Intent .ACTION_INSERT_OR_EDIT );
192251 newTransactionIntent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
193252 newTransactionIntent .putExtra (UxArgument .FORM_TYPE , FormActivity .FormType .TRANSACTION .name ());
253+ newTransactionIntent .putExtra (UxArgument .BOOK_UID , bookUID );
194254 newTransactionIntent .putExtra (UxArgument .SELECTED_ACCOUNT_UID , accountUID );
195255 PendingIntent pendingIntent = PendingIntent
196256 .getActivity (context , appWidgetId , newTransactionIntent , 0 );
@@ -212,7 +272,8 @@ public static void updateAllWidgets(final Context context){
212272 //update widgets asynchronously so as not to block method which called the update
213273 //inside the computation of the account balance
214274 new Thread (new Runnable () {
215- SharedPreferences defaultSharedPrefs = PreferenceManager .getDefaultSharedPreferences (context );
275+ SharedPreferences defaultSharedPrefs = PreferenceActivity .getActiveBookSharedPreferences ();
276+ //PreferenceManager.getDefaultSharedPreferences(context);
216277
217278 @ Override
218279 public void run () {
@@ -223,7 +284,7 @@ public void run() {
223284 if (accountUID == null )
224285 continue ;
225286
226- updateWidget (context , widgetId , accountUID );
287+ updateWidget (context , widgetId , accountUID , BooksDbAdapter . getInstance (). getActiveBookUID () );
227288 }
228289 }
229290 }).start ();
0 commit comments