1 package pl.org.nowoczesnapolska;
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;
10 import java.io.StringWriter;
11 import java.io.Writer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
15 import org.json.JSONArray;
16 import org.json.JSONException;
17 import org.json.JSONObject;
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;
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;
48 public ArrayList<CatalogueEntry> searchList;
50 public static ListView lv;
51 public CatalogueAdapter adapter;
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.catalogue);
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);
63 CatalogueDbHelper dbHelper = new CatalogueDbHelper(ctx);
64 db = dbHelper.getWritableDatabase();
65 searchList = new ArrayList();
66 lv.setTextFilterEnabled(true);
68 lv.setOnItemClickListener(new OnItemClickListener() {
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);
83 search.addTextChangedListener(new TextWatcher() {
85 public void onTextChanged(CharSequence s, int start, int before, int count) {
86 if(s.toString().length() > 1) {
87 lv.setVisibility(0); // == VISIBLE
89 String[] filter = new String[2];
90 String term = s.toString()+"%";
91 Log.d("char str:",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);
98 while(i < cur.getCount()){
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());
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());
112 if(searchList.size() > 0) {
114 adapter = new CatalogueAdapter(ctx, R.layout.catalogue_row, searchList);
115 lv.setAdapter(adapter);
118 adapter.notifyDataSetChanged();
122 if(adapter != null) {
123 adapter.notifyDataSetChanged();
129 public void beforeTextChanged(CharSequence s, int start, int count,
131 // TODO Auto-generated method stub
136 public void afterTextChanged(Editable s) {
137 // TODO Auto-generated method stub
143 Cursor c = db.rawQuery("select id from tags limit 2", null);// do zmiany
144 Log.d("db", c.getCount()+"");
146 /* this is memory consuming and should be optimized. it is first-run only, though */
147 if(c.getCount() == 0) {
148 AssetManager assetManager = getAssets();
150 Log.d("assets", assetManager.list("/assets").length+"");
151 for(String s: assetManager.list("/assets")){
154 InputStream is = assetManager.open("catalogue.jpg", AssetManager.ACCESS_BUFFER);
155 String catalogueString = readInputStreamAsString(is);
156 JSONObject jObject = null;
159 jObject = new JSONObject(catalogueString);
160 } catch (JSONException e) {
161 // TODO Auto-generated catch block
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
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()+"");
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());
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");
185 long id = db.insert("books", null, bookMap);
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());
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);
206 } catch (IOException e) {
207 // TODO Auto-generated catch block
209 } catch (JSONException e) {
210 // TODO Auto-generated catch block
219 public boolean onCreateOptionsMenu(Menu menu) {
220 super.onCreateOptionsMenu(menu);
229 menuItemId = MENU_SIGNIN;
230 menuItemOrder = Menu.NONE;
231 menuItemText = R.string.menu_signin;
233 menu.add(groupId, menuItemId,
234 menuItemOrder, menuItemText);
238 menuItemId = MENU_SETTINGS;
239 menuItemOrder = Menu.NONE;
240 menuItemText = R.string.menu_settings;
242 menu.add(groupId, menuItemId,
243 menuItemOrder, menuItemText);
247 menuItemId = MENU_ABOUT;
248 menuItemOrder = Menu.NONE;
249 menuItemText = R.string.menu_about;
251 menu.add(groupId, menuItemId,
252 menuItemOrder, menuItemText);
257 public boolean onOptionsItemSelected(MenuItem item) {
258 super.onOptionsItemSelected(item);
259 switch (item.getItemId()) {
261 startActivity(new Intent(ctx, SignIn.class));
263 case (MENU_SETTINGS):
264 startActivity(new Intent(ctx, Settings.class));
267 startActivity(new Intent(ctx, About.class));
273 public static String readInputStreamAsString(InputStream is)
277 Writer writer = new StringWriter();
279 /* be careful if tempted to change buffer size, OutOfMemory exception is watching you */
280 char[] buffer = new char[1024];
282 Reader reader = new BufferedReader(
283 new InputStreamReader(is, "UTF-8"));
285 while ((n = reader.read(buffer)) != -1) {
286 writer.write(buffer, 0, n);
291 return writer.toString();