Merge pull request #1561 from kiwix/feature/macgills/#1560-crash-opening-files

#1560 Opening a zim file results in a crash
This commit is contained in:
Seán Mac Gillicuddy 2019-10-21 13:33:24 +01:00 committed by GitHub
commit 335e99e140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 62 deletions

View File

@ -7,6 +7,7 @@
android:name=".main.KiwixMainActivity" android:name=".main.KiwixMainActivity"
android:configChanges="orientation|keyboardHidden|screenSize|locale" android:configChanges="orientation|keyboardHidden|screenSize|locale"
android:label="@string/app_name" android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan"> android:windowSoftInputMode="adjustPan">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
@ -115,15 +116,6 @@
android:name=".zim_manager.ZimManageActivity" android:name=".zim_manager.ZimManageActivity"
android:label="@string/choose_file" android:label="@string/choose_file"
android:launchMode="singleTop"> android:launchMode="singleTop">
<!-- TODO -->
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter> <intent-filter>
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.TEXT_CLICKED" /> <action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.TEXT_CLICKED" />
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.ICON_CLICKED" /> <action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.ICON_CLICKED" />

View File

@ -22,12 +22,16 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.core.net.toFile
import androidx.core.net.toUri
import org.json.JSONArray import org.json.JSONArray
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.start
import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.main.WebViewCallback import org.kiwix.kiwixmobile.core.main.WebViewCallback
import org.kiwix.kiwixmobile.core.utils.Constants.REQUEST_FILE_SELECT import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.Constants.EXTRA_ZIM_FILE
import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_ARTICLES import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_ARTICLES
import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_FILE import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_FILE
import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_POSITIONS import org.kiwix.kiwixmobile.core.utils.Constants.TAG_CURRENT_POSITIONS
@ -36,7 +40,6 @@ import org.kiwix.kiwixmobile.core.utils.Constants.TAG_KIWIX
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_KIWIX_MOBILE import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_KIWIX_MOBILE
import org.kiwix.kiwixmobile.core.utils.UpdateUtils.reformatProviderUrl import org.kiwix.kiwixmobile.core.utils.UpdateUtils.reformatProviderUrl
import org.kiwix.kiwixmobile.core.utils.files.FileUtils import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.kiwixActivityComponent import org.kiwix.kiwixmobile.kiwixActivityComponent
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity
import java.io.File import java.io.File
@ -59,7 +62,7 @@ class KiwixMainActivity : CoreMainActivity() {
private fun manageExternalLaunchAndRestoringViewState() { private fun manageExternalLaunchAndRestoringViewState() {
val data = intent.data val data = uriFromIntent()
if (data != null) { if (data != null) {
val filePath = FileUtils.getLocalFilePathByUri(applicationContext, data) val filePath = FileUtils.getLocalFilePathByUri(applicationContext, data)
@ -73,7 +76,7 @@ class KiwixMainActivity : CoreMainActivity() {
filePath + filePath +
" -> open this zim file and load menu_main page" " -> open this zim file and load menu_main page"
) )
openZimFile(File(filePath), false) openZimFile(File(filePath))
} else { } else {
val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0) val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0)
val zimFile = settings.getString(TAG_CURRENT_FILE, null) val zimFile = settings.getString(TAG_CURRENT_FILE, null)
@ -92,6 +95,11 @@ class KiwixMainActivity : CoreMainActivity() {
} }
} }
private fun uriFromIntent() =
intent.data ?: intent.getStringExtra(EXTRA_ZIM_FILE)?.let {
File(FileUtils.getFileName(it)).toUri()
}
private fun restoreTabStates() { private fun restoreTabStates() {
val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0) val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0)
val zimFile = settings.getString(TAG_CURRENT_FILE, null) val zimFile = settings.getString(TAG_CURRENT_FILE, null)
@ -101,7 +109,7 @@ class KiwixMainActivity : CoreMainActivity() {
val currentTab = settings.getInt(TAG_CURRENT_TAB, 0) val currentTab = settings.getInt(TAG_CURRENT_TAB, 0)
if (zimFile != null) { if (zimFile != null) {
openZimFile(File(zimFile), false) openZimFile(File(zimFile))
} else { } else {
Toast.makeText(this, "Unable to open zim file", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Unable to open zim file", Toast.LENGTH_SHORT).show()
} }
@ -125,17 +133,15 @@ class KiwixMainActivity : CoreMainActivity() {
} }
override fun manageZimFiles(tab: Int) { override fun manageZimFiles(tab: Int) {
presenter.loadCurrentZimBookmarksUrl() start<ZimManageActivity> {
val target = Intent(this, ZimManageActivity::class.java) putExtra(ZimManageActivity.TAB_EXTRA, tab)
target.action = Intent.ACTION_GET_CONTENT }
// The MIME data type filter }
target.type = "//"
target.putExtra(ZimManageActivity.TAB_EXTRA, tab)
// Only return URIs that can be opened with ContentResolver
target.addCategory(Intent.CATEGORY_OPENABLE)
// Force use of our file selection component.
// (Note may make sense to just define a custom intent instead)
startActivityForResult(target, REQUEST_FILE_SELECT) override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.data != null) {
openZimFile(intent.data.toFile())
}
} }
} }

View File

@ -18,10 +18,12 @@
package org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects package org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects
import android.app.Activity import android.app.Activity
import androidx.core.net.toUri
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.start
import org.kiwix.kiwixmobile.core.extensions.toast import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
class OpenFile(private val bookOnDisk: BookOnDisk) : SideEffect<Unit> { class OpenFile(private val bookOnDisk: BookOnDisk) : SideEffect<Unit> {
@ -30,7 +32,10 @@ class OpenFile(private val bookOnDisk: BookOnDisk) : SideEffect<Unit> {
if (!file.canRead()) { if (!file.canRead()) {
activity.toast(R.string.error_file_not_found) activity.toast(R.string.error_file_not_found)
} else { } else {
(activity as ZimManageActivity).finishResult(file.path) activity.finish()
activity.start<KiwixMainActivity> {
data = file.toUri()
}
} }
} }
} }

View File

@ -65,8 +65,12 @@ fun Activity.startActionMode(
}) })
} }
inline fun <reified T : Activity> Activity.start() { inline fun <reified T : Activity> Activity.start(noinline intentFunc: (Intent.() -> Unit)? = null) {
startActivity(Intent(this, T::class.java)) startActivity(
Intent(this, T::class.java).apply {
intentFunc?.invoke(this)
}
)
} }
inline fun <reified T : Activity> Activity.startWithActionFrom() { inline fun <reified T : Activity> Activity.startWithActionFrom() {

View File

@ -82,6 +82,7 @@ import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -94,6 +95,7 @@ import org.kiwix.kiwixmobile.core.BuildConfig;
import org.kiwix.kiwixmobile.core.Intents; import org.kiwix.kiwixmobile.core.Intents;
import org.kiwix.kiwixmobile.core.R; import org.kiwix.kiwixmobile.core.R;
import org.kiwix.kiwixmobile.core.R2; import org.kiwix.kiwixmobile.core.R2;
import org.kiwix.kiwixmobile.core.StorageObserver;
import org.kiwix.kiwixmobile.core.base.BaseActivity; import org.kiwix.kiwixmobile.core.base.BaseActivity;
import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem; import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem;
import org.kiwix.kiwixmobile.core.bookmark.BookmarksActivity; import org.kiwix.kiwixmobile.core.bookmark.BookmarksActivity;
@ -101,6 +103,8 @@ import org.kiwix.kiwixmobile.core.extensions.ContextExtensionsKt;
import org.kiwix.kiwixmobile.core.help.HelpActivity; import org.kiwix.kiwixmobile.core.help.HelpActivity;
import org.kiwix.kiwixmobile.core.history.HistoryActivity; import org.kiwix.kiwixmobile.core.history.HistoryActivity;
import org.kiwix.kiwixmobile.core.history.HistoryListItem; import org.kiwix.kiwixmobile.core.history.HistoryListItem;
import org.kiwix.kiwixmobile.core.reader.ZimFileReader;
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
import org.kiwix.kiwixmobile.core.search.SearchActivity; import org.kiwix.kiwixmobile.core.search.SearchActivity;
import org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity; import org.kiwix.kiwixmobile.core.settings.CoreSettingsActivity;
import org.kiwix.kiwixmobile.core.utils.DimenUtils; import org.kiwix.kiwixmobile.core.utils.DimenUtils;
@ -109,9 +113,6 @@ import org.kiwix.kiwixmobile.core.utils.NetworkUtils;
import org.kiwix.kiwixmobile.core.utils.StyleUtils; import org.kiwix.kiwixmobile.core.utils.StyleUtils;
import org.kiwix.kiwixmobile.core.utils.files.FileUtils; import org.kiwix.kiwixmobile.core.utils.files.FileUtils;
import org.kiwix.kiwixmobile.core.webserver.ZimHostActivity; import org.kiwix.kiwixmobile.core.webserver.ZimHostActivity;
import org.kiwix.kiwixmobile.core.reader.ZimFileReader;
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
import org.kiwix.kiwixmobile.core.StorageObserver;
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate; import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDiskDelegate;
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter; import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter;
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem; import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem;
@ -161,7 +162,7 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
public static boolean wifiOnly; public static boolean wifiOnly;
public static boolean nightMode; public static boolean nightMode;
private final ArrayList<String> bookmarks = new ArrayList<>(); private final ArrayList<String> bookmarks = new ArrayList<>();
private final List<KiwixWebView> webViewList = new ArrayList<>(); protected final List<KiwixWebView> webViewList = new ArrayList<>();
@BindView(R2.id.activity_main_root) @BindView(R2.id.activity_main_root)
ConstraintLayout root; ConstraintLayout root;
@BindView(R2.id.activity_main_toolbar) @BindView(R2.id.activity_main_toolbar)
@ -225,7 +226,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
}; };
private List<DocumentSection> documentSections; private List<DocumentSection> documentSections;
private Menu menu; private Menu menu;
private boolean requestClearHistoryAfterLoad = false;
private boolean requestInitAllMenuItems = false; private boolean requestInitAllMenuItems = false;
private boolean isBackToTopEnabled = false; private boolean isBackToTopEnabled = false;
private boolean wasHideToolbar = true; private boolean wasHideToolbar = true;
@ -399,15 +399,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
newTab(); newTab();
getCurrentWebView().loadUrl(intent.getStringExtra(EXTRA_CHOSE_X_TITLE)); getCurrentWebView().loadUrl(intent.getStringExtra(EXTRA_CHOSE_X_TITLE));
} }
if (intent.hasExtra(EXTRA_ZIM_FILE)) {
File file = new File(FileUtils.getFileName(intent.getStringExtra(EXTRA_ZIM_FILE)));
Uri uri = Uri.fromFile(file);
finish();
Intent zimFile = Intents.internal(CoreMainActivity.class);
zimFile.setData(uri);
startActivity(zimFile);
}
} }
private void setupDocumentParser() { private void setupDocumentParser() {
@ -1040,10 +1031,10 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
.show(); .show();
} }
protected void openZimFile(File file, boolean clearHistory) { protected void openZimFile(File file) {
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) { if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (file.exists()) { if (file.exists()) {
openAndSetInContainer(file, clearHistory); openAndSetInContainer(file);
} else { } else {
Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file.getAbsolutePath()); Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file.getAbsolutePath());
ContextExtensionsKt.toast(this, R.string.error_file_not_found, Toast.LENGTH_LONG); ContextExtensionsKt.toast(this, R.string.error_file_not_found, Toast.LENGTH_LONG);
@ -1072,12 +1063,16 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
} }
} }
private void openAndSetInContainer(File file, boolean clearHistory) { private void openAndSetInContainer(File file) {
try {
if (isNotPreviouslyOpenZim(file.getCanonicalPath())) {
webViewList.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
zimReaderContainer.setZimFile(file); zimReaderContainer.setZimFile(file);
if (zimReaderContainer.getZimFileReader() != null) { if (zimReaderContainer.getZimFileReader() != null) {
if (clearHistory) {
requestClearHistoryAfterLoad = true;
}
if (menu != null) { if (menu != null) {
initAllMenuItems(); initAllMenuItems();
} else { } else {
@ -1093,6 +1088,10 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
} }
} }
private boolean isNotPreviouslyOpenZim(String canonicalPath) {
return canonicalPath != null && !canonicalPath.equals(zimReaderContainer.getZimCanonicalPath());
}
@Override @Override
public void onRequestPermissionsResult(int requestCode, public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) { @NonNull String[] permissions, @NonNull int[] grantResults) {
@ -1765,13 +1764,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
if (checkNull(progressBar)) { if (checkNull(progressBar)) {
progressBar.setProgress(progress); progressBar.setProgress(progress);
if (progress == 100) { if (progress == 100) {
if (requestClearHistoryAfterLoad) {
Log.d(TAG_KIWIX,
"Loading article finished and requestClearHistoryAfterLoad -> clearHistory");
getCurrentWebView().clearHistory();
requestClearHistoryAfterLoad = false;
}
Log.d(TAG_KIWIX, "Loaded URL: " + getCurrentWebView().getUrl()); Log.d(TAG_KIWIX, "Loaded URL: " + getCurrentWebView().getUrl());
} }
} }
@ -1847,11 +1839,7 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
} }
private void open(BooksOnDiskListItem.BookOnDisk bookOnDisk) { private void open(BooksOnDiskListItem.BookOnDisk bookOnDisk) {
File file = bookOnDisk.getFile(); openZimFile(bookOnDisk.getFile());
Intent zimFile = Intents.internal(CoreMainActivity.class);
zimFile.setData(Uri.fromFile(file));
startActivity(zimFile);
finish();
} }
@Override @Override

View File

@ -136,7 +136,7 @@ class CustomMainActivity : CoreMainActivity() {
zimFileMissingDialog.show() zimFileMissingDialog.show()
return false return false
} else { } else {
openZimFile(File(filePath), true) openZimFile(File(filePath))
return true return true
} }
} }