Browse Source

Make StartPage an AsyncTask, makes it simpler to use

master
Anthony Restaino 9 years ago
parent
commit
24385c4334
  1. 63
      app/src/main/java/acr/browser/lightning/constant/StartPage.java
  2. 13
      app/src/main/java/acr/browser/lightning/view/LightningView.java

63
app/src/main/java/acr/browser/lightning/constant/StartPage.java

@ -3,29 +3,32 @@
*/ */
package acr.browser.lightning.constant; package acr.browser.lightning.constant;
import android.app.Activity; import android.app.Application;
import android.os.AsyncTask;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
public class StartPage { public class StartPage extends AsyncTask<Void, Void, Void> {
public static final String FILENAME = "homepage.html"; public static final String FILENAME = "homepage.html";
private static final String HEAD = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\">" private static final String HEAD_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head>" + "<head>"
+ "<meta content=\"en-us\" http-equiv=\"Content-Language\" />" + "<meta content=\"en-us\" http-equiv=\"Content-Language\" />"
+ "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />" + "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />"
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">"
+ "<title>" + "<title>";
+ BrowserApp.getContext().getString(R.string.home)
+ "</title>" private static final String HEAD_2 = "</title>"
+ "</head>" + "</head>"
+ "<style>body{background:#f2f2f2;text-align:center;margin:0px;}#search_input{height:35px; " + "<style>body{background:#f2f2f2;text-align:center;margin:0px;}#search_input{height:35px; "
+ "width:100%;outline:none;border:none;font-size: 16px;background-color:transparent;}" + "width:100%;outline:none;border:none;font-size: 16px;background-color:transparent;}"
@ -47,14 +50,41 @@ public class StartPage {
private static final String END = "\" + document.getElementById(\"search_input\").value;document.getElementById(\"search_input\").value = \"\";}return false;}</script></body></html>"; private static final String END = "\" + document.getElementById(\"search_input\").value;document.getElementById(\"search_input\").value = \"\";}return false;}</script></body></html>";
private final String mTitle;
private final File mFilesDir;
private final WeakReference<LightningView> mTabReference;
private String mStartpageUrl;
public StartPage(LightningView tab, Application app) {
mTitle = app.getString(R.string.home);
mFilesDir = app.getFilesDir();
mTabReference = new WeakReference<>(tab);
}
@Override
protected Void doInBackground(Void... params) {
mStartpageUrl = getHomepage();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null) {
tab.loadUrl(mStartpageUrl);
}
}
/** /**
* This method builds the homepage and returns the local URL to be loaded * This method builds the homepage and returns the local URL to be loaded
* when it finishes building. * when it finishes building.
* *
* @return the URL to load * @return the URL to load
*/ */
public static String getHomepage(Activity activity) { private String getHomepage() {
StringBuilder homepageBuilder = new StringBuilder(StartPage.HEAD); StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle + HEAD_2);
String icon; String icon;
String searchUrl; String searchUrl;
final PreferenceManager preferenceManager = BrowserApp.getPreferenceManager(); final PreferenceManager preferenceManager = BrowserApp.getPreferenceManager();
@ -89,14 +119,14 @@ public class StartPage {
break; break;
case 5: case 5:
// STARTPAGE_SEARCH; // STARTPAGE_SEARCH;
icon = "file:///android_asset/startpage.png"; icon = "file:///android_asset/png";
// "https://startpage.com/graphics/startp_logo.gif"; // "https://com/graphics/startp_logo.gif";
searchUrl = Constants.STARTPAGE_SEARCH; searchUrl = Constants.STARTPAGE_SEARCH;
break; break;
case 6: case 6:
// STARTPAGE_MOBILE // STARTPAGE_MOBILE
icon = "file:///android_asset/startpage.png"; icon = "file:///android_asset/png";
// "https://startpage.com/graphics/startp_logo.gif"; // "https://com/graphics/startp_logo.gif";
searchUrl = Constants.STARTPAGE_MOBILE_SEARCH; searchUrl = Constants.STARTPAGE_MOBILE_SEARCH;
break; break;
case 7: case 7:
@ -132,11 +162,11 @@ public class StartPage {
} }
homepageBuilder.append(icon); homepageBuilder.append(icon);
homepageBuilder.append(StartPage.MIDDLE); homepageBuilder.append(MIDDLE);
homepageBuilder.append(searchUrl); homepageBuilder.append(searchUrl);
homepageBuilder.append(StartPage.END); homepageBuilder.append(END);
File homepage = new File(activity.getFilesDir(), StartPage.FILENAME); File homepage = new File(mFilesDir, FILENAME);
FileWriter hWriter = null; FileWriter hWriter = null;
try { try {
//noinspection IOResourceOpenedButNotSafelyClosed //noinspection IOResourceOpenedButNotSafelyClosed
@ -150,4 +180,5 @@ public class StartPage {
return Constants.FILE + homepage; return Constants.FILE + homepage;
} }
} }

13
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -177,18 +177,7 @@ public class LightningView {
* UI thread. * UI thread.
*/ */
private void loadStartpage() { private void loadStartpage() {
BrowserApp.getIOThread().execute(new Runnable() { new StartPage(this, BrowserApp.get(mActivity)).executeOnExecutor(BrowserApp.getIOThread());
@Override
public void run() {
final String homepage = StartPage.getHomepage(mActivity);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(homepage, mRequestHeaders);
}
});
}
});
} }
/** /**

Loading…
Cancel
Save