2
2
3
3
import android .annotation .SuppressLint ;
4
4
import android .app .Activity ;
5
- import android .content .Intent ;
6
5
import android .net .Uri ;
7
6
import android .os .Bundle ;
8
- import android .util .Log ;
9
- import android .view .ContextMenu ;
10
7
import android .view .Menu ;
11
8
import android .view .View ;
12
9
import android .view .inputmethod .EditorInfo ;
21
18
import java .io .ByteArrayOutputStream ;
22
19
import java .io .IOException ;
23
20
import java .io .InputStream ;
24
- import java .util .Objects ;
25
21
import java .util .zip .ZipEntry ;
26
22
import java .util .zip .ZipInputStream ;
27
23
24
+ import de .simonlaux .ziphtmlviewer .R ;
25
+
28
26
public class OpenActivity extends Activity implements View .OnClickListener {
27
+ private static final String MD_TEXT = "MD_TEXT" ;
29
28
private static final String FIRST_TIME_INIT = "FirstTimeInit" ;
30
29
private static final String IS_SEARCHING = "Searching" ;
31
- private static final String ONE_TIME = "OneTime" ;
32
- private static final String TITLEWEB = "TitleWeb" ;
30
+ private static final String TITLE_WEB = "TitleWeb" ;
33
31
private boolean firstTimeInit = true ;
34
- private boolean isSearching = true ;
35
- private boolean onetime = false ;
32
+ private boolean isSearching = false ;
33
+ private JsObject jsObject ;
36
34
private String titleWEB ="ZHV" ;
37
-
35
+
38
36
@ Override
39
37
protected void onCreate (Bundle savedInstanceState ) {
40
38
super .onCreate (savedInstanceState );
41
- if (onetime ) {
42
- Toast .makeText (this , R .string .image_click_explain , Toast .LENGTH_SHORT ).show ();
43
- }
44
39
if (savedInstanceState != null ) {
45
40
firstTimeInit = savedInstanceState .getBoolean (FIRST_TIME_INIT , true );
46
41
isSearching = savedInstanceState .getBoolean (IS_SEARCHING , false );
47
- onetime = savedInstanceState .getBoolean (ONE_TIME , false );
48
- titleWEB =savedInstanceState .getString (TITLEWEB , titleWEB );
42
+ titleWEB =savedInstanceState .getString (TITLE_WEB , titleWEB );
43
+ String text =savedInstanceState .getString (MD_TEXT ,"" );
44
+ if (text =="" ){
45
+ jsObject =new JsObject ("" ,false );
46
+ }else {
47
+ jsObject =new JsObject (text ,true );
48
+ }
49
49
}
50
50
/*Don't hide the bar for use it
51
51
try {
@@ -63,22 +63,32 @@ protected void onCreate(Bundle savedInstanceState) {
63
63
this .setTitle (titleWEB );
64
64
}
65
65
}
66
-
66
+
67
67
@ SuppressLint ("SetJavaScriptEnabled" )
68
68
public void configureWebView (WebView web ) {
69
69
WebSettings webSettings = web .getSettings ();
70
70
webSettings .setJavaScriptEnabled (true );
71
71
webSettings .setSupportZoom (true );
72
72
webSettings .setBuiltInZoomControls (true );
73
73
webSettings .setDomStorageEnabled (true );
74
+ web .addJavascriptInterface (jsObject , "zhv" );
75
+ web .setWebChromeClient (new WebChromeClient () {
76
+
77
+ @ Override
78
+ public void onReceivedTitle (WebView view , String title ) {
79
+ super .onReceivedTitle (view , title );
80
+ OpenActivity .this .titleWEB = title ;
81
+ OpenActivity .this .setTitle (title );
82
+ }
83
+ });
74
84
}
75
-
85
+
76
86
@ Override
77
87
public boolean onCreateOptionsMenu (Menu menu ) {
78
88
getMenuInflater ().inflate (R .menu .search_menu , menu );
79
89
final SearchView searchView = (SearchView ) menu .findItem (R .id .search_menu_item ).getActionView ();
80
90
searchView .setIconifiedByDefault (true );
81
- searchView .setIconified (true );
91
+ // searchView.setIconified(true);
82
92
searchView .setImeOptions (EditorInfo .IME_FLAG_NAVIGATE_PREVIOUS | EditorInfo .IME_ACTION_NEXT | EditorInfo .IME_FLAG_NAVIGATE_NEXT );
83
93
searchView .setOnSearchClickListener (this );
84
94
searchView .setSubmitButtonEnabled (true );
@@ -93,10 +103,10 @@ public boolean onClose() {
93
103
@ Override
94
104
public boolean onQueryTextSubmit (String query ) {
95
105
WebView myWebView = findViewById (R .id .webview );
96
- myWebView .findAllAsync ( query );
106
+ myWebView .findNext ( true );
97
107
return false ;
98
108
}
99
-
109
+
100
110
@ Override
101
111
public boolean onQueryTextChange (String newText ) {
102
112
WebView myWebView = findViewById (R .id .webview );
@@ -105,9 +115,9 @@ public boolean onQueryTextChange(String newText) {
105
115
}
106
116
});
107
117
return super .onCreateOptionsMenu (menu );
108
-
118
+
109
119
}
110
-
120
+
111
121
@ Override
112
122
public void onBackPressed () {
113
123
if (isSearching ) {
@@ -119,9 +129,9 @@ public void onBackPressed() {
119
129
} else {
120
130
super .onBackPressed ();
121
131
}
122
-
132
+
123
133
}
124
-
134
+
125
135
private void handleIntent () {
126
136
Uri uri = getIntent ().getData ();
127
137
if (uri == null ) {
@@ -155,91 +165,41 @@ private void handleIntent() {
155
165
} catch (IOException e ) {
156
166
e .printStackTrace ();
157
167
}
158
-
168
+
159
169
if (text == null ) {
160
170
tellUserThatCouldNotOpenFile ();
161
171
return ;
162
172
}
163
-
173
+
164
174
WebView myWebView = findViewById (R .id .webview );
165
-
175
+ jsObject = new JsObject ( text , isMarkdown );
166
176
configureWebView (myWebView );
167
-
168
- registerForContextMenu (myWebView );
169
-
177
+
170
178
// myWebView.setWebContentsDebuggingEnabled(true);
171
-
172
- final boolean isMarkdownReader = isMarkdown ;
173
- final String contentText = text ;
174
- class JsObject {
175
- @ JavascriptInterface
176
- public int getVersion () {
177
- return BuildConfig .VERSION_CODE ;
178
- }
179
-
180
- @ JavascriptInterface
181
- public String toString () {
182
- return "[ZippedHTMLViewer Object]" ;
183
- }
184
-
185
- @ JavascriptInterface
186
- public String getMarkdown () {
187
- if (isMarkdownReader ) {
188
- return contentText ;
189
- } else {
190
- return "" ;
191
- }
192
- }
193
- }
194
- myWebView .addJavascriptInterface (new JsObject (), "zhv" );
195
- myWebView .setWebChromeClient (new WebChromeClient () {
196
- @ Override
197
- public void onReceivedTitle (WebView view , String title ) {
198
- super .onReceivedTitle (view , title );
199
- OpenActivity .this .titleWEB = title ;
200
- OpenActivity .this .setTitle (title );
201
- }
202
- });
179
+
203
180
myWebView .loadData ("" , "text/html" , null );
204
- if (isMarkdown ) {
181
+ if (isMarkdown ){
205
182
// markdown mode
206
183
myWebView .loadUrl ("file:///android_asset/markdown-reader.html" );
207
184
} else {
208
185
// html mode
209
- myWebView .loadDataWithBaseURL ("file://index.html" , contentText , "text/html" , null , null );
186
+ myWebView .loadDataWithBaseURL ("file://index.html" , text , "text/html" , null , null );
210
187
}
211
188
firstTimeInit = false ;
212
189
}
213
-
214
- //on Long touch Not Seen Images send to bot
215
- @ Override
216
- public void onCreateContextMenu (ContextMenu menu , View v , ContextMenu .ContextMenuInfo menuInfo ) {
217
- super .onCreateContextMenu (menu , v , menuInfo );
218
- Log .d ("TAG" , "onCreateContextMenu: " + v .toString ());
219
- WebView myWebView = findViewById (R .id .webview );
220
- WebView .HitTestResult hitTestResult = myWebView .getHitTestResult ();
221
- if (hitTestResult .getType () == WebView .HitTestResult .IMAGE_TYPE ) {
222
- Intent intent = new Intent (Intent .ACTION_SEND );
223
- intent .putExtra (Intent .EXTRA_EMAIL , "" );
224
- intent .putExtra (Intent .EXTRA_HTML_TEXT , "/web " + hitTestResult .getExtra ());
225
- intent .putExtra (Intent .EXTRA_TEXT , "/web " + hitTestResult .getExtra ());
226
- intent .setType ("text/html" );
227
- startActivity (Intent .createChooser (intent , getString (R .string .open_delta )));
228
- }
229
- }
230
-
190
+
231
191
private void tellUserThatCouldNotOpenFile () {
232
192
Toast .makeText (this , getString (R .string .could_not_open_file ), Toast .LENGTH_SHORT ).show ();
233
193
}
234
-
194
+
235
195
public static String getStringFromZip (InputStream stream ) throws IOException {
236
196
ByteArrayOutputStream fout = new ByteArrayOutputStream ();
237
197
if (!unpackZip (stream , fout )) {
238
198
throw new IOException ();
239
199
}
240
200
return fout .toString ();
241
201
}
242
-
202
+
243
203
private static boolean unpackZip (InputStream is , ByteArrayOutputStream fout ) {
244
204
ZipInputStream zis ;
245
205
try {
@@ -250,9 +210,10 @@ private static boolean unpackZip(InputStream is, ByteArrayOutputStream fout) {
250
210
while ((ze = zis .getNextEntry ()) != null ) {
251
211
if (ze .isDirectory ())
252
212
continue ;
253
- if (!Objects .equals (ze .getName (), "index.html" ))
213
+ //Objects esta del api 19 en adelante
214
+ if (!ze .getName ().contentEquals ("index.html" ))
254
215
continue ;
255
-
216
+
256
217
while ((count = zis .read (buffer )) != -1 ) {
257
218
fout .write (buffer , 0 , count );
258
219
}
@@ -265,19 +226,43 @@ private static boolean unpackZip(InputStream is, ByteArrayOutputStream fout) {
265
226
}
266
227
return true ;
267
228
}
268
-
229
+
269
230
@ Override
270
231
protected void onSaveInstanceState (Bundle outState ) {
271
232
WebView myWebView = findViewById (R .id .webview );
272
233
myWebView .saveState (outState );
273
234
outState .putBoolean (IS_SEARCHING , isSearching );
274
235
outState .putBoolean (FIRST_TIME_INIT , firstTimeInit );
275
- outState .putString (TITLEWEB ,titleWEB );
236
+ outState .putString (TITLE_WEB ,titleWEB );
237
+ outState .putString (MD_TEXT ,jsObject .getMarkdown ());
276
238
super .onSaveInstanceState (outState );
277
239
}
278
-
240
+
279
241
@ Override
280
242
public void onClick (View v ) {
281
243
isSearching = true ;
282
244
}
245
+
246
+
247
+ class JsObject {
248
+ String contentText ="" ;
249
+ boolean isMarkdownReader =false ;
250
+ public JsObject (String contentText ,boolean isMarkdownReader ){
251
+ this .contentText =contentText ;
252
+ this .isMarkdownReader =isMarkdownReader ;
253
+ }
254
+
255
+ @ JavascriptInterface
256
+ public int getVersion () { return BuildConfig .VERSION_CODE ; }
257
+ @ JavascriptInterface
258
+ public String toString () { return "[ZippedHTMLViewer Object]" ; }
259
+ @ JavascriptInterface
260
+ public String getMarkdown () {
261
+ if (isMarkdownReader ){
262
+ return contentText ;
263
+ } else {
264
+ return "" ;
265
+ }
266
+ }
267
+ }
283
268
}
0 commit comments