mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-22 03:54:18 -04:00
Merge pull request #1365 from kiwix/julianharty/fix-issue-1364
Fix for a bug that affects custom apps missing their contents.
This commit is contained in:
commit
c3f7e6f0ad
@ -34,6 +34,7 @@ import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
|||||||
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil;
|
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil;
|
||||||
|
|
||||||
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_EXTERNAL_LINK;
|
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_EXTERNAL_LINK;
|
||||||
|
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||||
|
|
||||||
public class KiwixWebViewClient extends WebViewClient {
|
public class KiwixWebViewClient extends WebViewClient {
|
||||||
|
|
||||||
@ -91,11 +92,19 @@ public class KiwixWebViewClient extends WebViewClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPageFinished(WebView view, String url) {
|
public void onPageFinished(WebView view, String url) {
|
||||||
if ((url.equals("content://" + BuildConfig.APPLICATION_ID + ".zim.base/null"))
|
boolean invalidUrl = url.equals("content://" + BuildConfig.APPLICATION_ID + ".zim.base/null");
|
||||||
&& !BuildConfig.IS_CUSTOM_APP) {
|
Log.d(TAG_KIWIX, "invalidUrl = " + invalidUrl);
|
||||||
inflateHomeView(view);
|
|
||||||
return;
|
if (invalidUrl) {
|
||||||
|
if (BuildConfig.IS_CUSTOM_APP) {
|
||||||
|
Log.e(TAG_KIWIX, "Abandoning WebView as there's a problem getting the content for this custom app.");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
inflateHomeView(view);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!url.equals("file:///android_asset/home.html")) {
|
if (!url.equals("file:///android_asset/home.html")) {
|
||||||
view.removeView(home);
|
view.removeView(home);
|
||||||
} else if (!BuildConfig.IS_CUSTOM_APP) {
|
} else if (!BuildConfig.IS_CUSTOM_APP) {
|
||||||
|
@ -361,7 +361,24 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
|||||||
|
|
||||||
setupDocumentParser();
|
setupDocumentParser();
|
||||||
|
|
||||||
manageExternalLaunchAndRestoringViewState();
|
if (BuildConfig.IS_CUSTOM_APP) {
|
||||||
|
Log.d(TAG_KIWIX, "This is a custom app:" +BuildConfig.APPLICATION_ID);
|
||||||
|
if (loadCustomAppContent()) {
|
||||||
|
Log.d(TAG_KIWIX, "Found custom content, continuing...");
|
||||||
|
// Continue
|
||||||
|
} else {
|
||||||
|
Log.e(TAG_KIWIX, "Problem finding the content, no more OnCreate() code");
|
||||||
|
// What should we do here? exit? I'll investigate empirically.
|
||||||
|
// It seems unpredictable behaviour if the code returns at this point as yesterday
|
||||||
|
// it didn't crash yet today the app crashes because it tries to load books
|
||||||
|
// in onResume();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
manageExternalLaunchAndRestoringViewState();
|
||||||
|
}
|
||||||
|
|
||||||
loadPrefs();
|
loadPrefs();
|
||||||
updateTitle();
|
updateTitle();
|
||||||
|
|
||||||
@ -1876,6 +1893,74 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* loadCustomAppContent Return true if all's well, else false.
|
||||||
|
*/
|
||||||
|
private boolean loadCustomAppContent() {
|
||||||
|
Log.d(TAG_KIWIX, "Kiwix Custom App starting for the first time. Checking Companion ZIM: "
|
||||||
|
+ BuildConfig.ZIM_FILE_NAME);
|
||||||
|
|
||||||
|
String currentLocaleCode = Locale.getDefault().toString();
|
||||||
|
// Custom App recommends to start off a specific language
|
||||||
|
if (BuildConfig.ENFORCED_LANG.length() > 0 && !BuildConfig.ENFORCED_LANG
|
||||||
|
.equals(currentLocaleCode)) {
|
||||||
|
|
||||||
|
// change the locale machinery
|
||||||
|
LanguageUtils.handleLocaleChange(this, BuildConfig.ENFORCED_LANG);
|
||||||
|
|
||||||
|
// save new locale into preferences for next startup
|
||||||
|
sharedPreferenceUtil.putPrefLanguage(BuildConfig.ENFORCED_LANG);
|
||||||
|
|
||||||
|
// restart activity for new locale to take effect
|
||||||
|
this.setResult(1236);
|
||||||
|
this.finish();
|
||||||
|
this.startActivity(new Intent(this, this.getClass()));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String filePath = "";
|
||||||
|
if (BuildConfig.HAS_EMBEDDED_ZIM) {
|
||||||
|
String appPath = getPackageResourcePath();
|
||||||
|
File libDir = new File(appPath.substring(0, appPath.lastIndexOf("/")) + "/lib/");
|
||||||
|
if (libDir.exists() && libDir.listFiles().length > 0) {
|
||||||
|
filePath = libDir.listFiles()[0].getPath() + "/" + BuildConfig.ZIM_FILE_NAME;
|
||||||
|
}
|
||||||
|
if (filePath.isEmpty() || !new File(filePath).exists()) {
|
||||||
|
filePath = String.format("/data/data/%s/lib/%s", BuildConfig.APPLICATION_ID,
|
||||||
|
BuildConfig.ZIM_FILE_NAME);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String fileName = FileUtils.getExpansionAPKFileName(true);
|
||||||
|
filePath = FileUtils.generateSaveFileName(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG_KIWIX, "BuildConfig.ZIM_FILE_SIZE = " + BuildConfig.ZIM_FILE_SIZE);
|
||||||
|
if (!FileUtils.doesFileExist(filePath, BuildConfig.ZIM_FILE_SIZE, false)) {
|
||||||
|
|
||||||
|
AlertDialog.Builder zimFileMissingBuilder =
|
||||||
|
new AlertDialog.Builder(this, dialogStyle());
|
||||||
|
zimFileMissingBuilder.setTitle(R.string.app_name);
|
||||||
|
zimFileMissingBuilder.setMessage(R.string.customapp_missing_content);
|
||||||
|
zimFileMissingBuilder.setIcon(R.mipmap.kiwix_icon);
|
||||||
|
final Activity activity = this;
|
||||||
|
zimFileMissingBuilder.setPositiveButton(getString(R.string.go_to_play_store),
|
||||||
|
(dialog, which) -> {
|
||||||
|
String market_uri = "market://details?id=" + BuildConfig.APPLICATION_ID;
|
||||||
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
|
intent.setData(Uri.parse(market_uri));
|
||||||
|
startActivity(intent);
|
||||||
|
activity.finish();
|
||||||
|
});
|
||||||
|
zimFileMissingBuilder.setCancelable(false);
|
||||||
|
AlertDialog zimFileMissingDialog = zimFileMissingBuilder.create();
|
||||||
|
zimFileMissingDialog.show();
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
openZimFile(new File(filePath), true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void manageExternalLaunchAndRestoringViewState() {
|
private void manageExternalLaunchAndRestoringViewState() {
|
||||||
|
|
||||||
if (getIntent().getData() != null) {
|
if (getIntent().getData() != null) {
|
||||||
@ -1902,71 +1987,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
|||||||
// Alternative would be to restore webView state. But more effort to implement, and actually
|
// Alternative would be to restore webView state. But more effort to implement, and actually
|
||||||
// fits better normal android behavior if after closing app ("back" button) state is not maintained.
|
// fits better normal android behavior if after closing app ("back" button) state is not maintained.
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (BuildConfig.IS_CUSTOM_APP) {
|
|
||||||
Log.d(TAG_KIWIX, "Kiwix Custom App starting for the first time. Checking Companion ZIM: "
|
|
||||||
+ BuildConfig.ZIM_FILE_NAME);
|
|
||||||
|
|
||||||
String currentLocaleCode = Locale.getDefault().toString();
|
|
||||||
// Custom App recommends to start off a specific language
|
|
||||||
if (BuildConfig.ENFORCED_LANG.length() > 0 && !BuildConfig.ENFORCED_LANG
|
|
||||||
.equals(currentLocaleCode)) {
|
|
||||||
|
|
||||||
// change the locale machinery
|
|
||||||
LanguageUtils.handleLocaleChange(this, BuildConfig.ENFORCED_LANG);
|
|
||||||
|
|
||||||
// save new locale into preferences for next startup
|
|
||||||
sharedPreferenceUtil.putPrefLanguage(BuildConfig.ENFORCED_LANG);
|
|
||||||
|
|
||||||
// restart activity for new locale to take effect
|
|
||||||
this.setResult(1236);
|
|
||||||
this.finish();
|
|
||||||
this.startActivity(new Intent(this, this.getClass()));
|
|
||||||
}
|
|
||||||
|
|
||||||
String filePath = "";
|
|
||||||
if (BuildConfig.HAS_EMBEDDED_ZIM) {
|
|
||||||
String appPath = getPackageResourcePath();
|
|
||||||
File libDir = new File(appPath.substring(0, appPath.lastIndexOf("/")) + "/lib/");
|
|
||||||
if (libDir.exists() && libDir.listFiles().length > 0) {
|
|
||||||
filePath = libDir.listFiles()[0].getPath() + "/" + BuildConfig.ZIM_FILE_NAME;
|
|
||||||
}
|
|
||||||
if (filePath.isEmpty() || !new File(filePath).exists()) {
|
|
||||||
filePath = String.format("/data/data/%s/lib/%s", BuildConfig.APPLICATION_ID,
|
|
||||||
BuildConfig.ZIM_FILE_NAME);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String fileName = FileUtils.getExpansionAPKFileName(true);
|
|
||||||
filePath = FileUtils.generateSaveFileName(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!FileUtils.doesFileExist(filePath, BuildConfig.ZIM_FILE_SIZE, false)) {
|
|
||||||
|
|
||||||
AlertDialog.Builder zimFileMissingBuilder =
|
|
||||||
new AlertDialog.Builder(this, dialogStyle());
|
|
||||||
zimFileMissingBuilder.setTitle(R.string.app_name);
|
|
||||||
zimFileMissingBuilder.setMessage(R.string.customapp_missing_content);
|
|
||||||
zimFileMissingBuilder.setIcon(R.mipmap.kiwix_icon);
|
|
||||||
final Activity activity = this;
|
|
||||||
zimFileMissingBuilder.setPositiveButton(getString(R.string.go_to_play_store),
|
|
||||||
(dialog, which) -> {
|
|
||||||
String market_uri = "market://details?id=" + BuildConfig.APPLICATION_ID;
|
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
||||||
intent.setData(Uri.parse(market_uri));
|
|
||||||
startActivity(intent);
|
|
||||||
activity.finish();
|
|
||||||
});
|
|
||||||
zimFileMissingBuilder.setCancelable(false);
|
|
||||||
AlertDialog zimFileMissingDialog = zimFileMissingBuilder.create();
|
|
||||||
zimFileMissingDialog.show();
|
|
||||||
} else {
|
|
||||||
openZimFile(new File(filePath), true);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Log.d(TAG_KIWIX, "Kiwix normal start, no zimFile loaded last time -> display home page");
|
Log.d(TAG_KIWIX, "Kiwix normal start, no zimFile loaded last time -> display home page");
|
||||||
showHomePage();
|
showHomePage();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user