Commit a3f3d9648d82b4114052313d048cf97c1b7cd9d0
1 parent
1daf1864
- load javascript popup for input values
Showing
7 changed files
with
204 additions
and
134 deletions
app/src/main/java/net/devfac/userstory/FSM/Action.java
app/src/main/java/net/devfac/userstory/FSM/StateContext.java
... | ... | @@ -17,6 +17,7 @@ import net.devfac.userstory.FSM.state.StateAdd; |
17 | 17 | import net.devfac.userstory.FSM.state.StateBookSearchToAdd; |
18 | 18 | import net.devfac.userstory.FSM.state.StateFinish; |
19 | 19 | import net.devfac.userstory.FSM.state.StateIdle; |
20 | +import net.devfac.userstory.FSM.state.StateInputInfoToAdd; | |
20 | 21 | import net.devfac.userstory.FSM.state.StateLoginRequested; |
21 | 22 | import net.devfac.userstory.FSM.state.StateLogoutRequested; |
22 | 23 | import net.devfac.userstory.FSM.state.StateSync; |
... | ... | @@ -76,6 +77,7 @@ public class StateContext { |
76 | 77 | public State mStateLogoutRequested = new StateLogoutRequested(this); |
77 | 78 | public State mStateFinish = new StateFinish(this); |
78 | 79 | public State mStateBookSearchToAdd = new StateBookSearchToAdd(this); |
80 | + public State mStateInputInfoToAdd = new StateInputInfoToAdd(this); | |
79 | 81 | |
80 | 82 | //For special use for WebView debug |
81 | 83 | public void setWebView(WebView webView){ |
... | ... | @@ -268,7 +270,7 @@ public class StateContext { |
268 | 270 | }); |
269 | 271 | } |
270 | 272 | |
271 | - private void runJavaScript(WebView view, String script){ | |
273 | + public void runJavaScript(WebView view, String script){ | |
272 | 274 | if( Build.VERSION.SDK_INT >= 19 ){ |
273 | 275 | view.evaluateJavascript(script, null); |
274 | 276 | } |
... | ... | @@ -443,6 +445,8 @@ public class StateContext { |
443 | 445 | |
444 | 446 | for( Element elements : book.getAllElements(HTMLElementName.DIV)){ |
445 | 447 | if( elements.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_INFO)){ |
448 | + mBookSearchResult.isbn = mBookSearchResult.id.split("_")[mBookSearchResult.id.split("_").length-1]; | |
449 | + | |
446 | 450 | for( Element bookInfo:elements.getAllElements(HTMLElementName.P)){ |
447 | 451 | if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_TITLE)){ |
448 | 452 | // Logger.i("BOOK TITLE : " + bookInfo.getContent() ); | ... | ... |
app/src/main/java/net/devfac/userstory/FSM/state/StateBookSearchToAdd.java
app/src/main/java/net/devfac/userstory/FSM/state/StateIdle.java
... | ... | @@ -40,6 +40,9 @@ public class StateIdle implements State { |
40 | 40 | case REQ_BOOK_SEARCH_TO_ADD: |
41 | 41 | returnState = mStateContext.mStateBookSearchToAdd; |
42 | 42 | break; |
43 | + case REQ_INPUT_INFO_TO_ADD: | |
44 | + returnState = mStateContext.mStateInputInfoToAdd; | |
45 | + break; | |
43 | 46 | } |
44 | 47 | |
45 | 48 | return returnState; | ... | ... |
app/src/main/java/net/devfac/userstory/FSM/state/StateInputInfoToAdd.java
0 → 100644
1 | +package net.devfac.userstory.FSM.state; | |
2 | + | |
3 | +import net.devfac.userstory.FSM.Action; | |
4 | +import net.devfac.userstory.FSM.State; | |
5 | +import net.devfac.userstory.FSM.StateContext; | |
6 | +import net.devfac.userstory.FragmentAddBook; | |
7 | +import net.devfac.userstory.MainActivity; | |
8 | +import net.devfac.userstory.Utils.Logger; | |
9 | +import net.devfac.userstory.Utils.UrlGenerator; | |
10 | + | |
11 | +/** | |
12 | + * Created by Onether on 15. 11. 7.. | |
13 | + */ | |
14 | +public class StateInputInfoToAdd implements State { | |
15 | + private StateContext mStateContext; | |
16 | + public StateInputInfoToAdd(StateContext stateContext){ | |
17 | + mStateContext = stateContext; | |
18 | + } | |
19 | + | |
20 | + @Override | |
21 | + public State act(Action action) { | |
22 | + State returnState = null; | |
23 | + | |
24 | + switch(action){ | |
25 | + case RES_INPUT_INFO_TO_ADD: | |
26 | + returnState = mStateContext.mStateIdle; | |
27 | + break; | |
28 | + } | |
29 | + | |
30 | + return returnState; | |
31 | + } | |
32 | + | |
33 | + @Override | |
34 | + public void onEntry(Object input) { | |
35 | + FragmentAddBook.BookSearchResult mBookSearchResult = (FragmentAddBook.BookSearchResult)input; | |
36 | + final String strUrl = UrlGenerator.getInstance(mStateContext.mContext).getJavascriptLoadAddpopup( | |
37 | + mBookSearchResult.isbn | |
38 | + ); | |
39 | + | |
40 | + Logger.i("Add popup url : " + strUrl); | |
41 | + | |
42 | + ((MainActivity)mStateContext.mContext).runOnUiThread(new Runnable() { | |
43 | + @Override | |
44 | + public void run() { | |
45 | + mStateContext.runJavaScript(mStateContext.mWebView, strUrl); | |
46 | +// mStateContext.mWebView.loadUrl(strUrl); | |
47 | + } | |
48 | + }); | |
49 | + | |
50 | + } | |
51 | + | |
52 | + @Override | |
53 | + public void onExit(Object output, StateContext.STATE_RESULT stateResult) { | |
54 | + | |
55 | + } | |
56 | +} | ... | ... |
app/src/main/java/net/devfac/userstory/FragmentAddBook.java
... | ... | @@ -123,146 +123,147 @@ public class FragmentAddBook extends Fragment implements View.OnClickListener, S |
123 | 123 | |
124 | 124 | } |
125 | 125 | |
126 | - private class ProcessSearchBookTask extends AsyncTask<String, Void, ArrayList<BookSearchResult>>{ | |
127 | -// StringBuffer mResultStringBuffer; | |
128 | - Activity mUiActivity; | |
129 | - | |
130 | - ProcessSearchBookTask(Activity uiActivity){ | |
131 | - mUiActivity = uiActivity; | |
132 | - } | |
133 | - | |
134 | - @Override | |
135 | - protected ArrayList<BookSearchResult> doInBackground(String... params) { | |
136 | - String keyword = params[0]; | |
137 | -// mResultStringBuffer = new StringBuffer(""); | |
138 | - ArrayList<BookSearchResult> mBookSearchResultArray = new ArrayList<>(); | |
139 | - | |
140 | - String strUrl = UrlGenerator.getInstance(getActivity()).getSearchBooksAtAladin(keyword); | |
141 | - URL url = null; | |
142 | - try { | |
143 | - url = new URL(strUrl); | |
144 | - | |
145 | - Source source = new Source(url); | |
146 | - source.fullSequentialParse(); | |
147 | - | |
148 | - List<Element> books = source.getAllElements(HTMLElementName.LI); | |
149 | - | |
150 | - for( Element book:books){ | |
151 | - BookSearchResult mBookSearchResult = new BookSearchResult(); | |
152 | - | |
153 | -// String book_id = book.getAttributeValue("id"); | |
154 | -// Logger.i("BOOK ID : " + book_id); | |
155 | - mBookSearchResult.id = book.getAttributeValue("id"); | |
156 | - | |
157 | - for( Element elements : book.getAllElements(HTMLElementName.DIV)){ | |
158 | - if( elements.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_INFO)){ | |
159 | - for( Element bookInfo:elements.getAllElements(HTMLElementName.P)){ | |
160 | - if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_TITLE)){ | |
161 | -// Logger.i("BOOK TITLE : " + bookInfo.getContent() ); | |
162 | - mBookSearchResult.title = bookInfo.getContent().toString(); | |
163 | - } | |
164 | - else if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_DESC)){ | |
165 | -// Logger.i("BOOK DESC : " + bookInfo.getContent() ); | |
166 | - mBookSearchResult.desc = bookInfo.getContent().toString(); | |
167 | - } | |
168 | - else if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_GENRE)){ | |
169 | -// Logger.i("BOOK GENRE : " + bookInfo.getContent() ); | |
170 | - mBookSearchResult.genre = bookInfo.getContent().toString(); | |
171 | - } | |
172 | - } | |
173 | - } | |
174 | - else if( elements.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_IMAGE)){ | |
175 | - for( Element bookImage:elements.getAllElements(HTMLElementName.IMG)){ | |
176 | -// Logger.i("COVER SRC : " + bookImage.getAttributeValue("src")); | |
177 | - | |
178 | - mBookSearchResult.imageUrl = bookImage.getAttributeValue("src"); | |
179 | - | |
180 | - String[] splited_string = mBookSearchResult.imageUrl.split("/"); | |
181 | - mBookSearchResult.imageFileName = splited_string[splited_string.length-1]; | |
182 | - | |
183 | - URL coverUrl = new URL(mBookSearchResult.imageUrl); | |
184 | - Bitmap coverBitmap = BitmapFactory.decodeStream(coverUrl.openConnection().getInputStream()); | |
185 | - | |
186 | - saveBookCover(coverBitmap, mBookSearchResult.imageFileName); | |
187 | - } | |
188 | - } | |
189 | - } | |
190 | - | |
191 | - mBookSearchResultArray.add(mBookSearchResult); | |
192 | - } | |
193 | - | |
194 | - | |
195 | - | |
196 | -// HttpURLConnection connection = (HttpURLConnection)url.openConnection(); | |
126 | +// private class ProcessSearchBookTask extends AsyncTask<String, Void, ArrayList<BookSearchResult>>{ | |
127 | +//// StringBuffer mResultStringBuffer; | |
128 | +// Activity mUiActivity; | |
129 | +// | |
130 | +// ProcessSearchBookTask(Activity uiActivity){ | |
131 | +// mUiActivity = uiActivity; | |
132 | +// } | |
133 | +// | |
134 | +// @Override | |
135 | +// protected ArrayList<BookSearchResult> doInBackground(String... params) { | |
136 | +// String keyword = params[0]; | |
137 | +//// mResultStringBuffer = new StringBuffer(""); | |
138 | +// ArrayList<BookSearchResult> mBookSearchResultArray = new ArrayList<>(); | |
139 | +// | |
140 | +// String strUrl = UrlGenerator.getInstance(getActivity()).getSearchBooksAtAladin(keyword); | |
141 | +// URL url = null; | |
142 | +// try { | |
143 | +// url = new URL(strUrl); | |
144 | +// | |
145 | +// Source source = new Source(url); | |
146 | +// source.fullSequentialParse(); | |
197 | 147 | // |
198 | -// connection.setRequestProperty("User-Agent", ""); | |
199 | -// connection.setRequestMethod("GET"); | |
200 | -// connection.setDoOutput(true); | |
201 | -// connection.connect(); | |
148 | +// List<Element> books = source.getAllElements(HTMLElementName.LI); | |
202 | 149 | // |
203 | -// InputStream inputStream = connection.getInputStream(); | |
150 | +// for( Element book:books){ | |
151 | +// BookSearchResult mBookSearchResult = new BookSearchResult(); | |
204 | 152 | // |
205 | -// BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); | |
153 | +//// String book_id = book.getAttributeValue("id"); | |
154 | +//// Logger.i("BOOK ID : " + book_id); | |
155 | +// mBookSearchResult.id = book.getAttributeValue("id"); | |
206 | 156 | // |
207 | -// String line = ""; | |
208 | -// while( (line = rd.readLine()) != null ){ | |
209 | -// Logger.i("LINE : " + line); | |
210 | -// mResultStringBuffer.append(line); | |
157 | +// for( Element elements : book.getAllElements(HTMLElementName.DIV)){ | |
158 | +// if( elements.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_INFO)){ | |
159 | +// for( Element bookInfo:elements.getAllElements(HTMLElementName.P)){ | |
160 | +// if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_TITLE)){ | |
161 | +//// Logger.i("BOOK TITLE : " + bookInfo.getContent() ); | |
162 | +// mBookSearchResult.title = bookInfo.getContent().toString(); | |
163 | +// } | |
164 | +// else if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_DESC)){ | |
165 | +//// Logger.i("BOOK DESC : " + bookInfo.getContent() ); | |
166 | +// mBookSearchResult.desc = bookInfo.getContent().toString(); | |
167 | +// } | |
168 | +// else if( bookInfo.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_GENRE)){ | |
169 | +//// Logger.i("BOOK GENRE : " + bookInfo.getContent() ); | |
170 | +// mBookSearchResult.genre = bookInfo.getContent().toString(); | |
171 | +// } | |
172 | +// } | |
173 | +// } | |
174 | +// else if( elements.getAttributeValue("class").equals(Constants.BOOK_SEARCH_CLASS_BOOK_IMAGE)){ | |
175 | +// for( Element bookImage:elements.getAllElements(HTMLElementName.IMG)){ | |
176 | +//// Logger.i("COVER SRC : " + bookImage.getAttributeValue("src")); | |
177 | +// | |
178 | +// mBookSearchResult.imageUrl = bookImage.getAttributeValue("src"); | |
179 | +// | |
180 | +// String[] splited_string = mBookSearchResult.imageUrl.split("/"); | |
181 | +// mBookSearchResult.imageFileName = splited_string[splited_string.length-1]; | |
182 | +// | |
183 | +// URL coverUrl = new URL(mBookSearchResult.imageUrl); | |
184 | +// Bitmap coverBitmap = BitmapFactory.decodeStream(coverUrl.openConnection().getInputStream()); | |
185 | +// | |
186 | +// saveBookCover(coverBitmap, mBookSearchResult.imageFileName); | |
187 | +// } | |
188 | +// } | |
189 | +// } | |
190 | +// | |
191 | +// mBookSearchResultArray.add(mBookSearchResult); | |
211 | 192 | // } |
212 | - } catch (MalformedURLException e) { | |
213 | - e.printStackTrace(); | |
214 | - } catch (IOException e) { | |
215 | - e.printStackTrace(); | |
216 | - } | |
217 | - | |
218 | - return mBookSearchResultArray; | |
219 | - } | |
220 | - | |
221 | - @Override | |
222 | - protected void onPostExecute(ArrayList<BookSearchResult> bookSearchResultArrayList) { | |
223 | - final ArrayList<BookSearchResult> resultBookSearchResultArrayList = bookSearchResultArrayList; | |
224 | - if(!resultBookSearchResultArrayList.isEmpty()){ | |
225 | -// super.onPostExecute(bookSearchResultArrayList); | |
226 | - mUiActivity.runOnUiThread(new Runnable() { | |
227 | - @Override | |
228 | - public void run() { | |
229 | - mArrayAdapterBookSearchResult = new ArrayAdapterBookSearchResult(mUiActivity, R.layout.listview_searched_result_item, resultBookSearchResultArrayList ); | |
230 | - mListSearchedResult.setAdapter(mArrayAdapterBookSearchResult); | |
231 | - } | |
232 | - }); | |
233 | - | |
234 | - } | |
235 | - } | |
236 | - | |
237 | - private void saveBookCover(Bitmap bitmap, String coverFileName){ | |
238 | - OutputStream outStream; | |
239 | - | |
240 | - File dir = new File(Constants.IMAGE_FILE_SAVE_PATH); | |
241 | - if( !dir.exists() ){ | |
242 | - if( !dir.mkdirs() ){ | |
243 | - Logger.e("Fail to create dir :" + dir.toString()); | |
244 | - return; | |
245 | - } | |
246 | - } | |
247 | - | |
248 | - File file = new File(Constants.IMAGE_FILE_SAVE_PATH, coverFileName); | |
249 | - | |
250 | - if( file.exists() ) return; | |
251 | - | |
252 | - try { | |
253 | - outStream = new FileOutputStream(file); | |
254 | - bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); | |
255 | - outStream.flush(); | |
256 | - outStream.close(); | |
257 | - } catch (IOException e) { | |
258 | - e.printStackTrace(); | |
259 | - } | |
260 | - | |
261 | - } | |
262 | - } | |
193 | +// | |
194 | +// | |
195 | +// | |
196 | +//// HttpURLConnection connection = (HttpURLConnection)url.openConnection(); | |
197 | +//// | |
198 | +//// connection.setRequestProperty("User-Agent", ""); | |
199 | +//// connection.setRequestMethod("GET"); | |
200 | +//// connection.setDoOutput(true); | |
201 | +//// connection.connect(); | |
202 | +//// | |
203 | +//// InputStream inputStream = connection.getInputStream(); | |
204 | +//// | |
205 | +//// BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); | |
206 | +//// | |
207 | +//// String line = ""; | |
208 | +//// while( (line = rd.readLine()) != null ){ | |
209 | +//// Logger.i("LINE : " + line); | |
210 | +//// mResultStringBuffer.append(line); | |
211 | +//// } | |
212 | +// } catch (MalformedURLException e) { | |
213 | +// e.printStackTrace(); | |
214 | +// } catch (IOException e) { | |
215 | +// e.printStackTrace(); | |
216 | +// } | |
217 | +// | |
218 | +// return mBookSearchResultArray; | |
219 | +// } | |
220 | +// | |
221 | +// @Override | |
222 | +// protected void onPostExecute(ArrayList<BookSearchResult> bookSearchResultArrayList) { | |
223 | +// final ArrayList<BookSearchResult> resultBookSearchResultArrayList = bookSearchResultArrayList; | |
224 | +// if(!resultBookSearchResultArrayList.isEmpty()){ | |
225 | +//// super.onPostExecute(bookSearchResultArrayList); | |
226 | +// mUiActivity.runOnUiThread(new Runnable() { | |
227 | +// @Override | |
228 | +// public void run() { | |
229 | +// mArrayAdapterBookSearchResult = new ArrayAdapterBookSearchResult(mUiActivity, R.layout.listview_searched_result_item, resultBookSearchResultArrayList ); | |
230 | +// mListSearchedResult.setAdapter(mArrayAdapterBookSearchResult); | |
231 | +// } | |
232 | +// }); | |
233 | +// | |
234 | +// } | |
235 | +// } | |
236 | +// | |
237 | +// private void saveBookCover(Bitmap bitmap, String coverFileName){ | |
238 | +// OutputStream outStream; | |
239 | +// | |
240 | +// File dir = new File(Constants.IMAGE_FILE_SAVE_PATH); | |
241 | +// if( !dir.exists() ){ | |
242 | +// if( !dir.mkdirs() ){ | |
243 | +// Logger.e("Fail to create dir :" + dir.toString()); | |
244 | +// return; | |
245 | +// } | |
246 | +// } | |
247 | +// | |
248 | +// File file = new File(Constants.IMAGE_FILE_SAVE_PATH, coverFileName); | |
249 | +// | |
250 | +// if( file.exists() ) return; | |
251 | +// | |
252 | +// try { | |
253 | +// outStream = new FileOutputStream(file); | |
254 | +// bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); | |
255 | +// outStream.flush(); | |
256 | +// outStream.close(); | |
257 | +// } catch (IOException e) { | |
258 | +// e.printStackTrace(); | |
259 | +// } | |
260 | +// | |
261 | +// } | |
262 | +// } | |
263 | 263 | |
264 | 264 | public static class BookSearchResult{ |
265 | 265 | public String id; |
266 | + public String isbn; | |
266 | 267 | public String title; |
267 | 268 | public String desc; |
268 | 269 | public String genre; |
... | ... | @@ -275,6 +276,9 @@ public class FragmentAddBook extends Fragment implements View.OnClickListener, S |
275 | 276 | @Override |
276 | 277 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
277 | 278 | Logger.i("Clicked : " + ((BookSearchResult)parent.getItemAtPosition(position)).title); |
279 | + StateContext.getInstance(getActivity()).processEvent(Action.REQ_INPUT_INFO_TO_ADD, | |
280 | + parent.getItemAtPosition(position)); | |
281 | + | |
278 | 282 | return false; |
279 | 283 | } |
280 | 284 | } | ... | ... |
app/src/main/java/net/devfac/userstory/Utils/UrlGenerator.java
... | ... | @@ -52,7 +52,7 @@ public class UrlGenerator { |
52 | 52 | return "http://userstorybook.net/bookapi/?keyword=" + keyword; |
53 | 53 | } |
54 | 54 | public String getJavascriptLoadAddpopup(String bookid){ |
55 | - return "javascript:recommend('" + bookid + "');return false;"; | |
55 | + return "javascript:recommend('" + bookid + "');"; | |
56 | 56 | } |
57 | 57 | |
58 | 58 | public String getLogin(){ | ... | ... |