Dirty stub.
[wl-mobile.git] / src / pl / org / nowoczesnapolska / Catalogue.java
1 package pl.org.nowoczesnapolska;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedReader;
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.Reader;
10 import java.io.StringWriter;
11 import java.io.Writer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14
15 import org.json.JSONArray;
16 import org.json.JSONException;
17 import org.json.JSONObject;
18
19 import android.app.Activity;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.res.AssetFileDescriptor;
24 import android.content.res.AssetManager;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.graphics.Paint.Join;
28 import android.opengl.Visibility;
29 import android.os.Bundle;
30 import android.os.ParcelFileDescriptor;
31 import android.text.Editable;
32 import android.text.TextWatcher;
33 import android.util.Log;
34 import android.view.Menu;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.ArrayAdapter;
40 import android.widget.EditText;
41 import android.widget.ListView;
42
43 public class Catalogue extends Activity {
44         static final private int MENU_SIGNIN = Menu.FIRST;
45         static final private int MENU_SETTINGS = Menu.FIRST+1;  
46         static final private int MENU_ABOUT = Menu.FIRST+2;
47         SQLiteDatabase db;
48         public ArrayList<CatalogueEntry> searchList;
49         Context ctx;
50         public static ListView lv;
51         public CatalogueAdapter adapter;
52         int first = 1;
53     @Override
54     public void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         setContentView(R.layout.catalogue);
57         
58         ctx = this;
59         lv = (ListView) findViewById(R.id.catalogueListView);
60         final EditText search = (EditText) findViewById(R.id.catalogueSearch);
61         final EditText prompt = (EditText) findViewById(R.id.cataloguePrompt);
62         
63                 CatalogueDbHelper dbHelper = new CatalogueDbHelper(ctx);
64                 db = dbHelper.getWritableDatabase();
65         searchList = new ArrayList();
66         lv.setTextFilterEnabled(true);
67
68         lv.setOnItemClickListener(new OnItemClickListener() {
69                 @Override
70                 public void onItemClick(AdapterView<?> parent, View view,
71               int position, long id) {
72                         Log.d("selected id", id+"");
73                         Log.d("selected id", this+"");
74                         Log.d("selected type", searchList.get(position).getType()+"");
75                         Log.d("selected name", searchList.get(position).getName()+"");
76                         Intent selectedIntent = new Intent(ctx, CatalogueItem.class);
77                         selectedIntent.putExtra("id", id);
78                         selectedIntent.putExtra("type", searchList.get(position).getType());
79                         startActivity(selectedIntent);
80                 }
81         });
82                 
83         search.addTextChangedListener(new TextWatcher() {
84                         @Override
85                         public void onTextChanged(CharSequence s, int start, int before, int count) {
86                                 if(s.toString().length() > 1) {
87                                         lv.setVisibility(0); // == VISIBLE
88                                         
89                                         String[] filter = new String[2];
90                                         String term = s.toString()+"%";
91                                         Log.d("char str:",term);
92                                         filter[0] = term;
93                                         filter[1] = term;
94                                         Cursor cur = db.rawQuery("select name, id, type from (select title as name, id, 0 as type from books where title like '"+term+"%' UNION ALL select name, id, 1 as type from tags where name like '"+term+"%') order by name", null);
95                                         cur.moveToFirst();
96                                         int i = 0;
97                                         searchList.clear();
98                                         while(i < cur.getCount()){                                              
99                                                 try {
100                                                         Log.d("query result", cur.getString(0));
101                                                         searchList.add(new CatalogueEntry(cur.getInt(2), cur.getInt(1), cur.getString(0)));
102                                                 } catch (Exception e) {
103                                                         Log.d("query result count", cur.getColumnCount() + " " + cur.getPosition());
104                                                 }
105                                                 cur.moveToNext();
106                                                 i++;
107                                                 Log.d("test listy ctx", ctx.toString());
108                                                 Log.d("test listy r.", R.layout.catalogue_list_item+"");
109                                                 Log.d("test listy list", searchList.size() + "");
110                                                 Log.d("test listy lv", lv.toString());
111                                         }
112                                         if(searchList.size() > 0) {
113                                                 if(first == 1) {
114                                                         adapter = new CatalogueAdapter(ctx, R.layout.catalogue_row, searchList);
115                                                         lv.setAdapter(adapter);
116                                                         first = 0;
117                                                 }
118                                                 adapter.notifyDataSetChanged();
119                                         }
120                                 }  else {
121                                         searchList.clear();
122                                         if(adapter != null) {
123                                                 adapter.notifyDataSetChanged();
124                                         }
125                                 }
126                         }
127                         
128                         @Override
129                         public void beforeTextChanged(CharSequence s, int start, int count,
130                                         int after) {
131                                 // TODO Auto-generated method stub
132                                 
133                         }
134                         
135                         @Override
136                         public void afterTextChanged(Editable s) {
137                                 // TODO Auto-generated method stub
138                                 
139                         }
140                 });        
141
142
143                         Cursor c = db.rawQuery("select id from tags limit 2", null);// do zmiany
144                         Log.d("db", c.getCount()+"");
145                         
146                         /* this is memory consuming and should be optimized. it is first-run only, though */
147                         if(c.getCount() == 0) {
148                         AssetManager assetManager = getAssets();
149                         try {
150                                         Log.d("assets", assetManager.list("/assets").length+"");
151                                         for(String s: assetManager.list("/assets")){
152                                                 Log.d("assets", s);
153                                         }
154                                         InputStream is = assetManager.open("catalogue.jpg", AssetManager.ACCESS_BUFFER);
155                                         String catalogueString = readInputStreamAsString(is);
156                                         JSONObject jObject = null;
157                                         try {
158                                                 //new JSONOBj
159                                                 jObject = new JSONObject(catalogueString);
160                                         } catch (JSONException e) {
161                                                 // TODO Auto-generated catch block
162                                                 e.printStackTrace();
163                                         }
164                                         Log.d("assets", catalogueString.length()+"");   
165                                         JSONObject added = jObject.getJSONObject("added");
166                                         JSONArray books = added.getJSONArray("books"); // books
167                                         JSONArray tags = added.getJSONArray("tags"); // tags
168                                         
169                                         Log.d("json array books length", books.length()+"");
170                                         Log.d("json array tags length", tags.length()+"");
171                                         Log.d("json array length", added.length()+"");
172                                         
173                                         for(int i = 0; i < books.length(); i++){
174                                                 JSONObject book = books.getJSONObject(i);
175                                                 Log.d("json-test", book.get("id").toString() + book.get("title").toString() + book.get("slug").toString());                                                             
176                                                                 // JSONException                                                
177                                                 ContentValues bookMap = new ContentValues();
178                                                 bookMap.put("id", book.get("id").toString());
179                                                 bookMap.put("title", book.get("title").toString());
180                                                 bookMap.put("slug", book.get("slug").toString());
181                                                 bookMap.put("epub", "epub");
182                                                 bookMap.put("html", "html");
183                                                 bookMap.put("mp3", "mp3");
184                                                 
185                                                 long id = db.insert("books", null, bookMap);
186                                                 Log.d("sql", id+"");
187                                         }
188
189                                         for(int i = 0; i < tags.length(); i++){
190                                                 JSONObject tag = tags.getJSONObject(i);                                         
191                                                 Log.d("json-test", tag.get("id").toString() + tag.get("name").toString() + tag.get("slug").toString()+ tag.get("sort_key").toString()+ tag.get("category").toString());                                                                 
192                                                                 // JSONException
193
194                                                 //String bookData[] = {book.get("id").toString(), book.get("title").toString(), book.get("slug").toString()};
195                                                 ContentValues tagMap = new ContentValues();
196                                                 tagMap.put("id", tag.get("id").toString());
197                                                 tagMap.put("name", tag.get("name").toString());
198                                                 tagMap.put("slug", tag.get("slug").toString());
199                                                 tagMap.put("sort_key", tag.get("sort_key").toString());
200                                                 tagMap.put("category", tag.get("category").toString());
201                                                 tagMap.put("description", "description");
202                                                 long id = db.insert("tags", null, tagMap);
203                                                 Log.d("sql", id+"");
204                                         }                                       
205                                         
206                                 } catch (IOException e) {
207                                         // TODO Auto-generated catch block
208                                         e.printStackTrace();
209                                 } catch (JSONException e) {
210                                         // TODO Auto-generated catch block
211                                         e.printStackTrace();
212                                 }
213                                 
214                         }
215
216     }
217     
218     @Override
219     public boolean onCreateOptionsMenu(Menu menu) {
220         super.onCreateOptionsMenu(menu);
221
222         int groupId;
223         int menuItemId;
224         int menuItemOrder;
225         int menuItemText;
226
227         // settings
228         groupId = 0;
229         menuItemId = MENU_SIGNIN;
230         menuItemOrder = Menu.NONE;
231         menuItemText = R.string.menu_signin;            
232         
233         menu.add(groupId, menuItemId,
234                         menuItemOrder, menuItemText);
235         
236         // settings
237         groupId = 0;
238         menuItemId = MENU_SETTINGS;
239         menuItemOrder = Menu.NONE;
240         menuItemText = R.string.menu_settings;          
241         
242         menu.add(groupId, menuItemId,
243                         menuItemOrder, menuItemText);           
244         
245         // about
246         groupId = 0;
247         menuItemId = MENU_ABOUT;
248         menuItemOrder = Menu.NONE;
249         menuItemText = R.string.menu_about;
250         
251         menu.add(groupId, menuItemId,
252                         menuItemOrder, menuItemText);
253                 
254         return true;
255     }
256     
257     public boolean onOptionsItemSelected(MenuItem item) {
258         super.onOptionsItemSelected(item);
259         switch (item.getItemId()) {
260                 case (MENU_SIGNIN):
261                         startActivity(new Intent(ctx, SignIn.class));
262                         return true;
263                 case (MENU_SETTINGS):
264                         startActivity(new Intent(ctx, Settings.class));
265                         return true;
266                 case (MENU_ABOUT):
267                         startActivity(new Intent(ctx, About.class));
268                         return true;                    
269         }
270
271         return false;           
272     }
273     public static String readInputStreamAsString(InputStream is) 
274     throws IOException {
275         
276         if (is != null) {
277             Writer writer = new StringWriter();
278
279             /* be careful if tempted to change buffer size, OutOfMemory exception is watching you */
280             char[] buffer = new char[1024];
281             try {
282                 Reader reader = new BufferedReader(
283                         new InputStreamReader(is, "UTF-8"));
284                 int n;
285                 while ((n = reader.read(buffer)) != -1) {
286                     writer.write(buffer, 0, n);
287                 }
288             } finally {
289                 is.close();
290             }
291             return writer.toString();
292         } else {        
293             return "";
294         }       
295     }
296     
297     
298 }