From: £ukasz Anwajler Date: Wed, 23 Mar 2011 18:15:48 +0000 (+0100) Subject: Dirty stub. X-Git-Url: https://git.mdrn.pl/wl-mobile.git/commitdiff_plain/c59a709afb0196f8853ec81e9a349e4b4868fe3c Dirty stub. --- diff --git a/gen/pl/org/nowoczesnapolska/R.java b/gen/pl/org/nowoczesnapolska/R.java index 219a09b..e67aa76 100644 --- a/gen/pl/org/nowoczesnapolska/R.java +++ b/gen/pl/org/nowoczesnapolska/R.java @@ -15,16 +15,20 @@ public final class R { } public static final class id { public static final int ScrollView01=0x7f050000; - public static final int addShelf=0x7f050008; - public static final int catalogueBookListView=0x7f050004; + public static final int addShelf=0x7f05000c; + public static final int catalogueBookListView=0x7f050005; public static final int catalogueListView=0x7f050002; + public static final int cataloguePrompt=0x7f050003; public static final int catalogueSearch=0x7f050001; - public static final int catalogueShelvesBookListView=0x7f050009; - public static final int catalogueShelvesListView=0x7f050007; - public static final int putOnShelf=0x7f050006; - public static final int read_online=0x7f050003; - public static final int sign_in=0x7f05000a; - public static final int webview=0x7f050005; + public static final int catalogueShelvesBookListView=0x7f05000d; + public static final int catalogueShelvesListView=0x7f05000b; + public static final int icon=0x7f050007; + public static final int id=0x7f050008; + public static final int name=0x7f050009; + public static final int putOnShelf=0x7f05000a; + public static final int read_online=0x7f050004; + public static final int sign_in=0x7f05000e; + public static final int webview=0x7f050006; } public static final class layout { public static final int about=0x7f030000; @@ -34,11 +38,12 @@ public final class R { public static final int catalogue_item_details=0x7f030004; public static final int catalogue_list_item=0x7f030005; public static final int catalogue_read_online=0x7f030006; - public static final int catalogue_shelves=0x7f030007; - public static final int catalogue_shelves_item=0x7f030008; - public static final int catalogue_shelves_list_item=0x7f030009; - public static final int settings=0x7f03000a; - public static final int signin=0x7f03000b; + public static final int catalogue_row=0x7f030007; + public static final int catalogue_shelves=0x7f030008; + public static final int catalogue_shelves_item=0x7f030009; + public static final int catalogue_shelves_list_item=0x7f03000a; + public static final int settings=0x7f03000b; + public static final int signin=0x7f03000c; } public static final class string { public static final int app_name=0x7f040000; diff --git a/lib/google-gson-stream-1.6.jar b/lib/google-gson-stream-1.6.jar new file mode 100644 index 0000000..2507c65 Binary files /dev/null and b/lib/google-gson-stream-1.6.jar differ diff --git a/res/layout/catalogue.xml b/res/layout/catalogue.xml index f3fccb2..2b6bb30 100644 --- a/res/layout/catalogue.xml +++ b/res/layout/catalogue.xml @@ -15,6 +15,13 @@ + diff --git a/res/layout/catalogue_row.xml b/res/layout/catalogue_row.xml new file mode 100644 index 0000000..b08b7fc --- /dev/null +++ b/res/layout/catalogue_row.xml @@ -0,0 +1,32 @@ + + + + + + + + \ No newline at end of file diff --git a/src/pl/org/nowoczesnapolska/Auth.java b/src/pl/org/nowoczesnapolska/Auth.java index 0fbb0a1..b713cf7 100644 --- a/src/pl/org/nowoczesnapolska/Auth.java +++ b/src/pl/org/nowoczesnapolska/Auth.java @@ -35,7 +35,7 @@ public class Auth extends Activity { private OAuthProvider mProvider; private String mCallbackUrl; private String authURL; - private String apiURL = "http://192.168.148.129/api/"; + private String apiURL = "http://epsilon.nowoczesnapolska.org.pl/api/"; @Override public void onResume(){ @@ -48,11 +48,11 @@ public class Auth extends Activity { Log.d("resume", "token0: "+token[0]); Log.d("resume", "token1: "+token[1]); - String accessToken[] = AuthHelper.getAccessToken(token[1]); + //String accessToken[] = AuthHelper.getAccessToken(token[1]); - Log.d("resume", "token0: " + accessToken[0]); - Log.d("resume", "token1: " + accessToken[1]); - mConsumer = new CommonsHttpOAuthConsumer(accessToken[0], accessToken[1]); + Log.d("resume", "token0: " + token[0]); + Log.d("resume", "token1: " + token[1]); + mConsumer = new CommonsHttpOAuthConsumer(token[0], token[1]); final URL url = new URL(apiURL+"tags/abc.json"); Log.d("url", "token1: "+url.toString()); @@ -65,15 +65,11 @@ public class Auth extends Activity { HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request); - } catch (OAuthMessageSignerException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (OAuthNotAuthorizedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } catch (OAuthExpectationFailedException e) { - // TODO Auto-generated catch block + // TODO Auto-generated catch block11 e.printStackTrace(); } catch (OAuthCommunicationException e) { // TODO Auto-generated catch block diff --git a/src/pl/org/nowoczesnapolska/AuthHelper.java b/src/pl/org/nowoczesnapolska/AuthHelper.java index d531c62..8258c57 100644 --- a/src/pl/org/nowoczesnapolska/AuthHelper.java +++ b/src/pl/org/nowoczesnapolska/AuthHelper.java @@ -20,7 +20,7 @@ public class AuthHelper { private static OAuthConsumer mConsumer; private static OAuthProvider mProvider; private static String mCallbackUrl; - private static String apiURL = "http://192.168.148.129/api/"; + private static String apiURL = "http://epsilon.nowoczesnapolska.org.pl/api/"; public static void OAuthHelper(String consumerKey, String consumerSecret, String callbackUrl) throws UnsupportedEncodingException { @@ -30,7 +30,7 @@ public class AuthHelper { apiURL+"oauth/access_token/", apiURL+"oauth/authorize/"); mProvider.setOAuth10a(true); - mCallbackUrl = (callbackUrl == null ? OAuth.OUT_OF_BAND : callbackUrl); + mCallbackUrl = "wl://"; } diff --git a/src/pl/org/nowoczesnapolska/Catalogue.java b/src/pl/org/nowoczesnapolska/Catalogue.java index cbf329b..a6a72b8 100644 --- a/src/pl/org/nowoczesnapolska/Catalogue.java +++ b/src/pl/org/nowoczesnapolska/Catalogue.java @@ -1,11 +1,36 @@ package pl.org.nowoczesnapolska; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.HashMap; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.content.res.AssetFileDescriptor; +import android.content.res.AssetManager; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.graphics.Paint.Join; +import android.opengl.Visibility; import android.os.Bundle; +import android.os.ParcelFileDescriptor; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -19,93 +44,85 @@ public class Catalogue extends Activity { static final private int MENU_SIGNIN = Menu.FIRST; static final private int MENU_SETTINGS = Menu.FIRST+1; static final private int MENU_ABOUT = Menu.FIRST+2; - - static final String[] AUTHORS = new String[] { - "Władysław Anczyc", - "Hans Christian Andersen", - "Adam Asnyk", - "Autor nieznany", - "Honoré de Balzac", - "Michał Bałucki", - "Charles Baudelaire", - "Władysław Bełza", - "Miłosz Biedrzycki", - "August Bielowski", - "George Gordon Byron", - "Joseph Conrad", - "Anton Czechow", - "Józef Czechowicz", - "Daniel Defoe", - "Casimir Delavigne", - "Antonina Domańska", - "Alojzy Feliński", - "Aleksander Fredro", - "Johann Wolfgang von Goethe", - "Bruno Jasieński", - "Franciszek Karpiński", - "Jan Kasprowicz", - "Rudyard Kipling", - "Jan Kochanowski", - "Maria Konopnicka", - "Franciszek Kowalski", - "Ignacy Krasicki", - "Zygmunt Krasiński", - "Teofil Lenartowicz", - "Bolesław Leśmian", - "Adam Mickiewicz", - "Molière", - "Jan Andrzej Morsztyn", - "Julian Ursyn Niemcewicz", - "Cyprian Kamil Norwid", - "Artur Oppman", - "Eliza Orzeszkowa", - "Edgar Allan Poe", - "Wincenty Pol", - "Ludwik Ksawery Pomian-Łubiński", - "Bolesław Prus", - "Kazimierz Przerwa-Tetmajer", - "Władysław Stanisław Reymont", - "Mikołaj Sęp Szarzyński", - "William Shakespeare", - "Henryk Sienkiewicz", - "Juliusz Słowacki", - "Sofokles", - "Rajnold Suchodolski", - "Wacław Święcicki", - "Władysław Tarnowski", - "Stanisław Ignacy Witkiewicz", - "Stefan Witwicki", - "Józef Wybicki", - "Stanisław Wyspiański", - "Gabriela Zapolska", - "Stefan Å»eromski" - }; + SQLiteDatabase db; + public ArrayList searchList; Context ctx; + public static ListView lv; + public CatalogueAdapter adapter; + int first = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.catalogue); ctx = this; - final ListView lv = (ListView) findViewById(R.id.catalogueListView); + lv = (ListView) findViewById(R.id.catalogueListView); final EditText search = (EditText) findViewById(R.id.catalogueSearch); + final EditText prompt = (EditText) findViewById(R.id.cataloguePrompt); - lv.setAdapter(new ArrayAdapter(this, R.layout.catalogue_list_item, AUTHORS)); + CatalogueDbHelper dbHelper = new CatalogueDbHelper(ctx); + db = dbHelper.getWritableDatabase(); + searchList = new ArrayList(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - startActivity(new Intent(ctx, CatalogueItem.class)); + Log.d("selected id", id+""); + Log.d("selected id", this+""); + Log.d("selected type", searchList.get(position).getType()+""); + Log.d("selected name", searchList.get(position).getName()+""); + Intent selectedIntent = new Intent(ctx, CatalogueItem.class); + selectedIntent.putExtra("id", id); + selectedIntent.putExtra("type", searchList.get(position).getType()); + startActivity(selectedIntent); } }); - + search.addTextChangedListener(new TextWatcher() { - @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - lv.setFilterText(s.toString()); + if(s.toString().length() > 1) { + lv.setVisibility(0); // == VISIBLE + + String[] filter = new String[2]; + String term = s.toString()+"%"; + Log.d("char str:",term); + filter[0] = term; + filter[1] = term; + 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); + cur.moveToFirst(); + int i = 0; + searchList.clear(); + while(i < cur.getCount()){ + try { + Log.d("query result", cur.getString(0)); + searchList.add(new CatalogueEntry(cur.getInt(2), cur.getInt(1), cur.getString(0))); + } catch (Exception e) { + Log.d("query result count", cur.getColumnCount() + " " + cur.getPosition()); + } + cur.moveToNext(); + i++; + Log.d("test listy ctx", ctx.toString()); + Log.d("test listy r.", R.layout.catalogue_list_item+""); + Log.d("test listy list", searchList.size() + ""); + Log.d("test listy lv", lv.toString()); + } + if(searchList.size() > 0) { + if(first == 1) { + adapter = new CatalogueAdapter(ctx, R.layout.catalogue_row, searchList); + lv.setAdapter(adapter); + first = 0; + } + adapter.notifyDataSetChanged(); + } + } else { + searchList.clear(); + if(adapter != null) { + adapter.notifyDataSetChanged(); + } + } } @Override @@ -121,6 +138,81 @@ public class Catalogue extends Activity { } }); + + + Cursor c = db.rawQuery("select id from tags limit 2", null);// do zmiany + Log.d("db", c.getCount()+""); + + /* this is memory consuming and should be optimized. it is first-run only, though */ + if(c.getCount() == 0) { + AssetManager assetManager = getAssets(); + try { + Log.d("assets", assetManager.list("/assets").length+""); + for(String s: assetManager.list("/assets")){ + Log.d("assets", s); + } + InputStream is = assetManager.open("catalogue.jpg", AssetManager.ACCESS_BUFFER); + String catalogueString = readInputStreamAsString(is); + JSONObject jObject = null; + try { + //new JSONOBj + jObject = new JSONObject(catalogueString); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Log.d("assets", catalogueString.length()+""); + JSONObject added = jObject.getJSONObject("added"); + JSONArray books = added.getJSONArray("books"); // books + JSONArray tags = added.getJSONArray("tags"); // tags + + Log.d("json array books length", books.length()+""); + Log.d("json array tags length", tags.length()+""); + Log.d("json array length", added.length()+""); + + for(int i = 0; i < books.length(); i++){ + JSONObject book = books.getJSONObject(i); + Log.d("json-test", book.get("id").toString() + book.get("title").toString() + book.get("slug").toString()); + // JSONException + ContentValues bookMap = new ContentValues(); + bookMap.put("id", book.get("id").toString()); + bookMap.put("title", book.get("title").toString()); + bookMap.put("slug", book.get("slug").toString()); + bookMap.put("epub", "epub"); + bookMap.put("html", "html"); + bookMap.put("mp3", "mp3"); + + long id = db.insert("books", null, bookMap); + Log.d("sql", id+""); + } + + for(int i = 0; i < tags.length(); i++){ + JSONObject tag = tags.getJSONObject(i); + 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()); + // JSONException + + //String bookData[] = {book.get("id").toString(), book.get("title").toString(), book.get("slug").toString()}; + ContentValues tagMap = new ContentValues(); + tagMap.put("id", tag.get("id").toString()); + tagMap.put("name", tag.get("name").toString()); + tagMap.put("slug", tag.get("slug").toString()); + tagMap.put("sort_key", tag.get("sort_key").toString()); + tagMap.put("category", tag.get("category").toString()); + tagMap.put("description", "description"); + long id = db.insert("tags", null, tagMap); + Log.d("sql", id+""); + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } @Override @@ -178,5 +270,29 @@ public class Catalogue extends Activity { return false; } + public static String readInputStreamAsString(InputStream is) + throws IOException { + + if (is != null) { + Writer writer = new StringWriter(); + + /* be careful if tempted to change buffer size, OutOfMemory exception is watching you */ + char[] buffer = new char[1024]; + try { + Reader reader = new BufferedReader( + new InputStreamReader(is, "UTF-8")); + int n; + while ((n = reader.read(buffer)) != -1) { + writer.write(buffer, 0, n); + } + } finally { + is.close(); + } + return writer.toString(); + } else { + return ""; + } + } + } \ No newline at end of file diff --git a/src/pl/org/nowoczesnapolska/CatalogueAdapter.java b/src/pl/org/nowoczesnapolska/CatalogueAdapter.java new file mode 100644 index 0000000..43487ac --- /dev/null +++ b/src/pl/org/nowoczesnapolska/CatalogueAdapter.java @@ -0,0 +1,60 @@ +package pl.org.nowoczesnapolska; + +import java.util.ArrayList; + +import android.app.Activity; +import android.content.Context; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.TextView; + +class CatalogueAdapter extends BaseAdapter { + private ArrayList items; + private Context ctx; + + public CatalogueAdapter(Context context, int textViewResourceId, ArrayList items) { + this.items = items; + this.ctx = context; + } + + @Override + public int getCount() { + // TODO Auto-generated method stub + return items.size(); + } + @Override + public Object getItem(int position) { + // TODO Auto-generated method stub + return items.get(position); + } + @Override + public long getItemId(int position) { + return items.get(position).getId(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View v = convertView; + if (v == null) { + LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = vi.inflate(R.layout.catalogue_row, null); + } + CatalogueEntry o = items.get(position); + if (o != null) { + Log.d("not null", "not null"); + TextView tt = (TextView) v.findViewById(R.id.id); + TextView bt = (TextView) v.findViewById(R.id.name); + if (tt != null) { + tt.setText("Name: "+o.getName()); } + if(bt != null){ + bt.setText("Type: "+ o.getType()); + } + } + Log.d("dalej", "dalej"); + return v; + } +} \ No newline at end of file diff --git a/src/pl/org/nowoczesnapolska/CatalogueDbHelper.java b/src/pl/org/nowoczesnapolska/CatalogueDbHelper.java new file mode 100644 index 0000000..447d1a1 --- /dev/null +++ b/src/pl/org/nowoczesnapolska/CatalogueDbHelper.java @@ -0,0 +1,54 @@ +package pl.org.nowoczesnapolska; +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.database.sqlite.SQLiteOpenHelper; + +public class CatalogueDbHelper extends SQLiteOpenHelper { + + public CatalogueDbHelper(Context context, String name, + CursorFactory factory, int version) { + super(context, name, factory, version); + // TODO Auto-generated constructor stub + } + + private static final int DATABASE_VERSION = 2; + private static final String TAGS_TABLE = "tags"; + private static final String BOOKS_TABLE = "books"; + + private static final String TAGS_TABLE_CREATE = + "CREATE TABLE " + TAGS_TABLE + " (" + + "id INTEGER, " + + "name TEXT, " + + "slug TEXT, " + + "sort_key TEXT, " + + "category TEXT, " + + "description TEXT " + + ");"; + + private static final String BOOKS_TABLE_CREATE = + "CREATE TABLE " + BOOKS_TABLE + " (" + + "id INTEGER, " + + "title TEXT, " + + "slug TEXT, " + + "epub TEXT, " + + "html TEXT, " + + "mp3 TEXT " + + ");"; + private static final String DATABASE_NAME = "wl-mobile"; + + CatalogueDbHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(BOOKS_TABLE_CREATE); + db.execSQL(TAGS_TABLE_CREATE); + } + + @Override + public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { + // TODO Auto-generated method stub + } +} \ No newline at end of file diff --git a/src/pl/org/nowoczesnapolska/CatalogueEntry.java b/src/pl/org/nowoczesnapolska/CatalogueEntry.java new file mode 100644 index 0000000..4f1e337 --- /dev/null +++ b/src/pl/org/nowoczesnapolska/CatalogueEntry.java @@ -0,0 +1,31 @@ +package pl.org.nowoczesnapolska; + +public class CatalogueEntry { + // type: 0 -- book, 1 -- tag + private int type; + private int id; + private String name; + public CatalogueEntry(int type, int id, String name){ + setType(type); + setId(id); + setName(name); + } + public void setType(int type) { + this.type = type; + } + public int getType() { + return type; + } + public void setId(int id) { + this.id = id; + } + public int getId() { + return id; + } + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } +} diff --git a/src/pl/org/nowoczesnapolska/CatalogueItem.java b/src/pl/org/nowoczesnapolska/CatalogueItem.java index fbfa3f2..34ed284 100644 --- a/src/pl/org/nowoczesnapolska/CatalogueItem.java +++ b/src/pl/org/nowoczesnapolska/CatalogueItem.java @@ -3,6 +3,8 @@ package pl.org.nowoczesnapolska; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -27,12 +29,30 @@ public class CatalogueItem extends Activity { static final private int MENU_SETTINGS = Menu.FIRST+2; static final private int MENU_ABOUT = Menu.FIRST+3; + private SQLiteDatabase db; + Context ctx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.catalogue_item_details); ctx = this; + + int id = getIntent().getIntExtra("id", 0); + int type = getIntent().getIntExtra("type", 0); + String[] options = new String[2]; + options[0] = new String(id+""); + options[1] = new String(type+""); + + /*if(type == 0){ + // books + Cursor c = db.rawQuery("select * from books where id = ?", options); + } else { + // tags + }*/ + + CatalogueDbHelper dbHelper = new CatalogueDbHelper(ctx); + db = dbHelper.getWritableDatabase(); final ListView lv = (ListView) findViewById(R.id.catalogueBookListView);