Read main page of ZIM File and write on storage

This commit is contained in:
Adeel Zafar 2019-07-28 16:48:44 +05:00
parent bff90479a5
commit ade8940110
2 changed files with 37 additions and 11 deletions

View File

@ -100,6 +100,9 @@ import com.google.android.material.snackbar.Snackbar;
import io.reactivex.android.schedulers.AndroidSchedulers;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -183,6 +186,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
public static final String ACTION_TURN_OFF_BEFORE_O = "Turn_aff_hotspot_before_oreo";
public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo";
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo";
public static final String MAIN_PAGE_STORAGE_PATH =
"/storage/emulated/0/Download/outputDang.html";
public static boolean isFullscreenOpened;
public static boolean refresh;
public static boolean wifiOnly;
@ -957,14 +962,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
openExternalUrl(intentSupportKiwix);
case R.id.menu_wifi_hotspot:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
toggleHotspot();
} else {
if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot.
switchHotspot();
if (readHtmlPage()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
toggleHotspot();
} else {
if (showWritePermissionSettings()) { //request permission and if already granted switch hotspot.
switchHotspot();
}
}
}
default:
break;
}
@ -972,6 +978,28 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
return super.onOptionsItemSelected(item);
}
private boolean readHtmlPage() {
String url =
Uri.parse(ZimContentProvider.CONTENT_URI + ZimContentProvider.getMainPage()).toString();
Uri uri = Uri.parse(url);
try {
InputStream in = getContentResolver().openInputStream(uri);
OutputStream out =
new FileOutputStream(new File(MAIN_PAGE_STORAGE_PATH));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
return true;
} catch (Exception e) {
Log.v("DANG", e.toString());
}
return false;
}
/** Dialog to take user confirmation before deleting all notes */
private void showClearAllNotesDialog() {

View File

@ -5,12 +5,11 @@ import fi.iki.elonen.NanoHTTPD;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
public class WebServer extends NanoHTTPD {
private String selectedFilePath;
public WebServer(int port,String selectedFilePath) {
public WebServer(int port, String selectedFilePath) {
super(port);
this.selectedFilePath = selectedFilePath;
}
@ -26,11 +25,10 @@ public class WebServer extends NanoHTTPD {
answer += line;
}
reader.close();
} catch(IOException ioe) {
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
}