Skip to content

Commit

Permalink
Merge pull request #5 from Simon-Laux/html_and_main_screen
Browse files Browse the repository at this point in the history
open plain HTML and main screen update
  • Loading branch information
Simon-Laux authored Jun 21, 2019
2 parents 405592b + 8e2bc21 commit 6ac63d0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
This is an Android app.

Opens files with the extention `.html.zip` or `.htmlzip` and displays their content in a webview.
The app can also open plain html files.

It doesn't allow loading resoources from the web. Assets must be inline because it only loads from the index html file for now.
It doesn't allow loading resources from the web. Assets must be inline because it only loads from the index html file for now.

## Zipped HTML format

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "de.simonlaux.ziphtmlviewer"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"
}
signingConfigs {

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<data android:pathPattern=".*\\..*\\.htmlzip" />
<data android:pathPattern=".*\\..*\\..*\\.htmlzip" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.htmlzip" />
<data android:mimeType="text/html"/>
<data android:pathPattern=".*\\.html" />
<data android:pathPattern=".*\\..*\\.html" />
<data android:pathPattern=".*\\..*\\..*\\.html" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.html" />
</intent-filter>
</activity>
</application>
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/de/simonlaux/ziphtmlviewer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package de.simonlaux.ziphtmlviewer;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;

public class MainActivity extends Activity {

TextView trouble;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.trouble = findViewById(R.id.troubleshooting);
this.trouble.setVisibility(View.INVISIBLE);
}

public void toggleTroubleshooting(View view) {
if (this.trouble.getVisibility() == View.VISIBLE)
this.trouble.setVisibility(View.INVISIBLE);
else
this.trouble.setVisibility(View.VISIBLE);
}

}

29 changes: 21 additions & 8 deletions app/src/main/java/de/simonlaux/ziphtmlviewer/OpenActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public class OpenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
try {
this.getActionBar().hide();
} catch (NullPointerException e) {
}
catch (NullPointerException e){}

setContentView(R.layout.activity_reader);

Expand All @@ -43,7 +42,19 @@ private void handleIntent() {
String text = null;
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
text = getStringFromInputStream(inputStream);
if (!uri.getPath().contains("zip")) {
// html mode
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
text = result.toString();
} else {
// zip html mode
text = getStringFromZip(inputStream);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Expand All @@ -58,7 +69,7 @@ private void handleIntent() {
WebView myWebView = findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadDataWithBaseURL("file://index.html", text, "text/html", null, null);
Expand All @@ -68,7 +79,7 @@ private void tellUserThatCouldNotOpenFile() {
Toast.makeText(this, getString(R.string.could_not_open_file), Toast.LENGTH_SHORT).show();
}

public static String getStringFromInputStream(InputStream stream) throws IOException {
public static String getStringFromZip(InputStream stream) throws IOException {
ByteArrayOutputStream fout = new ByteArrayOutputStream();
if (!unpackZip(stream, fout)) {
throw new IOException();
Expand All @@ -84,8 +95,10 @@ private static boolean unpackZip(InputStream is, ByteArrayOutputStream fout) {
byte[] buffer = new byte[1024];
int count;
while ((ze = zis.getNextEntry()) != null) {
if(ze.isDirectory()) continue;
if(!Objects.equals(ze.getName(), "index.html")) continue;
if (ze.isDirectory())
continue;
if (!Objects.equals(ze.getName(), "index.html"))
continue;

while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
Expand Down
54 changes: 50 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,58 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="@string/usage_description"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open '.html.zip' files with this app"
android:onClick="toggleTroubleshooting"
android:text="@string/troubleshooting_btn"
app:layout_constraintBottom_toTopOf="@+id/troubleshooting"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_chainStyle="spread" />


<TextView
android:id="@+id/troubleshooting"
android:layout_width="0dp"
android:layout_height="wrap_content"

android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:autoLink="web"
android:text="@string/troubleshooting"
android:textColor="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_marginBottom="15dp"
android:autoLink="web"
android:text="@string/sources"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="usage_description">Open compressed HTML Files (\'.html.zip\' or \'.htmlzip\') or normal html files with this app</string>
<string name="troubleshooting_btn">Problems?</string>
<string name="troubleshooting">When you have problems: \n 1. Install and Update the WebView app or Chrome \n 2. If the problem persists please open an issue on https://github.com/Simon-Laux/ZHV/issues</string>
<string name="sources">https://github.com/Simon-Laux/ZHV</string>
<string name="could_not_open_file">Could not open file</string>
</resources>

0 comments on commit 6ac63d0

Please sign in to comment.