mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 04:33:54 -04:00
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:
commit
335e99e140
@ -7,6 +7,7 @@
|
||||
android:name=".main.KiwixMainActivity"
|
||||
android:configChanges="orientation|keyboardHidden|screenSize|locale"
|
||||
android:label="@string/app_name"
|
||||
android:launchMode="singleTop"
|
||||
android:windowSoftInputMode="adjustPan">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
@ -115,15 +116,6 @@
|
||||
android:name=".zim_manager.ZimManageActivity"
|
||||
android:label="@string/choose_file"
|
||||
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>
|
||||
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.TEXT_CLICKED" />
|
||||
<action android:name="org.kiwix.kiwixmobile.utils.KiwixSearchWidget.ICON_CLICKED" />
|
||||
|
@ -22,12 +22,16 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.core.net.toFile
|
||||
import androidx.core.net.toUri
|
||||
import org.json.JSONArray
|
||||
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.main.CoreMainActivity
|
||||
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_FILE
|
||||
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.UpdateUtils.reformatProviderUrl
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
|
||||
import org.kiwix.kiwixmobile.kiwixActivityComponent
|
||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity
|
||||
import java.io.File
|
||||
@ -59,7 +62,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
||||
|
||||
private fun manageExternalLaunchAndRestoringViewState() {
|
||||
|
||||
val data = intent.data
|
||||
val data = uriFromIntent()
|
||||
if (data != null) {
|
||||
val filePath = FileUtils.getLocalFilePathByUri(applicationContext, data)
|
||||
|
||||
@ -73,7 +76,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
||||
filePath +
|
||||
" -> open this zim file and load menu_main page"
|
||||
)
|
||||
openZimFile(File(filePath), false)
|
||||
openZimFile(File(filePath))
|
||||
} else {
|
||||
val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0)
|
||||
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() {
|
||||
val settings = getSharedPreferences(PREF_KIWIX_MOBILE, 0)
|
||||
val zimFile = settings.getString(TAG_CURRENT_FILE, null)
|
||||
@ -101,7 +109,7 @@ class KiwixMainActivity : CoreMainActivity() {
|
||||
val currentTab = settings.getInt(TAG_CURRENT_TAB, 0)
|
||||
|
||||
if (zimFile != null) {
|
||||
openZimFile(File(zimFile), false)
|
||||
openZimFile(File(zimFile))
|
||||
} else {
|
||||
Toast.makeText(this, "Unable to open zim file", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
@ -125,17 +133,15 @@ class KiwixMainActivity : CoreMainActivity() {
|
||||
}
|
||||
|
||||
override fun manageZimFiles(tab: Int) {
|
||||
presenter.loadCurrentZimBookmarksUrl()
|
||||
val target = Intent(this, ZimManageActivity::class.java)
|
||||
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)
|
||||
start<ZimManageActivity> {
|
||||
putExtra(ZimManageActivity.TAB_EXTRA, tab)
|
||||
}
|
||||
}
|
||||
|
||||
startActivityForResult(target, REQUEST_FILE_SELECT)
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
if (intent?.data != null) {
|
||||
openZimFile(intent.data.toFile())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,10 +18,12 @@
|
||||
package org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.core.net.toUri
|
||||
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.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> {
|
||||
|
||||
@ -30,7 +32,10 @@ class OpenFile(private val bookOnDisk: BookOnDisk) : SideEffect<Unit> {
|
||||
if (!file.canRead()) {
|
||||
activity.toast(R.string.error_file_not_found)
|
||||
} else {
|
||||
(activity as ZimManageActivity).finishResult(file.path)
|
||||
activity.finish()
|
||||
activity.start<KiwixMainActivity> {
|
||||
data = file.toUri()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,8 +65,12 @@ fun Activity.startActionMode(
|
||||
})
|
||||
}
|
||||
|
||||
inline fun <reified T : Activity> Activity.start() {
|
||||
startActivity(Intent(this, T::class.java))
|
||||
inline fun <reified T : Activity> Activity.start(noinline intentFunc: (Intent.() -> Unit)? = null) {
|
||||
startActivity(
|
||||
Intent(this, T::class.java).apply {
|
||||
intentFunc?.invoke(this)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <reified T : Activity> Activity.startWithActionFrom() {
|
||||
|
@ -82,6 +82,7 @@ import com.google.android.material.navigation.NavigationView;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
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.R;
|
||||
import org.kiwix.kiwixmobile.core.R2;
|
||||
import org.kiwix.kiwixmobile.core.StorageObserver;
|
||||
import org.kiwix.kiwixmobile.core.base.BaseActivity;
|
||||
import org.kiwix.kiwixmobile.core.bookmark.BookmarkItem;
|
||||
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.history.HistoryActivity;
|
||||
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.settings.CoreSettingsActivity;
|
||||
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.files.FileUtils;
|
||||
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.BooksOnDiskAdapter;
|
||||
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 nightMode;
|
||||
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)
|
||||
ConstraintLayout root;
|
||||
@BindView(R2.id.activity_main_toolbar)
|
||||
@ -225,7 +226,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
|
||||
};
|
||||
private List<DocumentSection> documentSections;
|
||||
private Menu menu;
|
||||
private boolean requestClearHistoryAfterLoad = false;
|
||||
private boolean requestInitAllMenuItems = false;
|
||||
private boolean isBackToTopEnabled = false;
|
||||
private boolean wasHideToolbar = true;
|
||||
@ -399,15 +399,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
|
||||
newTab();
|
||||
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() {
|
||||
@ -1040,10 +1031,10 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
|
||||
.show();
|
||||
}
|
||||
|
||||
protected void openZimFile(File file, boolean clearHistory) {
|
||||
protected void openZimFile(File file) {
|
||||
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||
if (file.exists()) {
|
||||
openAndSetInContainer(file, clearHistory);
|
||||
openAndSetInContainer(file);
|
||||
} else {
|
||||
Log.w(TAG_KIWIX, "ZIM file doesn't exist at " + file.getAbsolutePath());
|
||||
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);
|
||||
if (zimReaderContainer.getZimFileReader() != null) {
|
||||
if (clearHistory) {
|
||||
requestClearHistoryAfterLoad = true;
|
||||
}
|
||||
if (menu != null) {
|
||||
initAllMenuItems();
|
||||
} 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
|
||||
public void onRequestPermissionsResult(int requestCode,
|
||||
@NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
@ -1765,13 +1764,6 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
|
||||
if (checkNull(progressBar)) {
|
||||
progressBar.setProgress(progress);
|
||||
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());
|
||||
}
|
||||
}
|
||||
@ -1847,11 +1839,7 @@ public abstract class CoreMainActivity extends BaseActivity implements WebViewCa
|
||||
}
|
||||
|
||||
private void open(BooksOnDiskListItem.BookOnDisk bookOnDisk) {
|
||||
File file = bookOnDisk.getFile();
|
||||
Intent zimFile = Intents.internal(CoreMainActivity.class);
|
||||
zimFile.setData(Uri.fromFile(file));
|
||||
startActivity(zimFile);
|
||||
finish();
|
||||
openZimFile(bookOnDisk.getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -136,7 +136,7 @@ class CustomMainActivity : CoreMainActivity() {
|
||||
zimFileMissingDialog.show()
|
||||
return false
|
||||
} else {
|
||||
openZimFile(File(filePath), true)
|
||||
openZimFile(File(filePath))
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user