Save tab back/forward state, not just current site when saving/restoring state
This commit is contained in:
parent
f73f82030f
commit
29836bd98a
@ -1157,8 +1157,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
|
||||
|
||||
void saveOpenTabs() {
|
||||
if (mPreferences.getRestoreLostTabsEnabled()) {
|
||||
final String s = mTabsManager.tabsString();
|
||||
mPreferences.setMemoryUrl(s);
|
||||
mTabsManager.saveState();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package acr.browser.lightning.activity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
@ -20,7 +22,7 @@ import javax.inject.Singleton;
|
||||
import acr.browser.lightning.R;
|
||||
import acr.browser.lightning.constant.Constants;
|
||||
import acr.browser.lightning.preference.PreferenceManager;
|
||||
import acr.browser.lightning.utils.Utils;
|
||||
import acr.browser.lightning.utils.FileUtils;
|
||||
import acr.browser.lightning.view.LightningView;
|
||||
|
||||
/**
|
||||
@ -33,9 +35,12 @@ public class TabsManager {
|
||||
private static final String TAG = TabsManager.class.getSimpleName();
|
||||
private final List<LightningView> mWebViewList = new ArrayList<>();
|
||||
private LightningView mCurrentTab;
|
||||
private static final String BUNDLE_KEY = "WEBVIEW_";
|
||||
private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel";
|
||||
|
||||
@Inject PreferenceManager mPreferenceManager;
|
||||
@Inject Bus mEventBus;
|
||||
@Inject Application mApp;
|
||||
|
||||
@Inject
|
||||
public TabsManager() {}
|
||||
@ -65,14 +70,7 @@ public class TabsManager {
|
||||
mWebViewList.clear();
|
||||
mCurrentTab = null;
|
||||
if (mPreferenceManager.getRestoreLostTabsEnabled()) {
|
||||
final String mem = mPreferenceManager.getMemoryUrl();
|
||||
mPreferenceManager.setMemoryUrl("");
|
||||
String[] array = Utils.getArray(mem);
|
||||
for (String urlString : array) {
|
||||
if (!urlString.isEmpty()) {
|
||||
newTab(activity, urlString, false);
|
||||
}
|
||||
}
|
||||
restoreState(activity);
|
||||
}
|
||||
if (url != null) {
|
||||
if (url.startsWith(Constants.FILE)) {
|
||||
@ -228,18 +226,33 @@ public class TabsManager {
|
||||
return mWebViewList.indexOf(tab);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of the currently opened tabs
|
||||
*/
|
||||
public String tabsString() {
|
||||
final StringBuilder builder = new StringBuilder(mWebViewList.size());
|
||||
for (LightningView tab : mWebViewList) {
|
||||
final String url = tab.getUrl();
|
||||
if (!url.isEmpty()) {
|
||||
builder.append(url).append("|$|SEPARATOR|$|");
|
||||
public void saveState() {
|
||||
Bundle outState = new Bundle(ClassLoader.getSystemClassLoader());
|
||||
Log.d(Constants.TAG, "Saving tab state");
|
||||
for (int n = 0; n < mWebViewList.size(); n++) {
|
||||
LightningView tab = mWebViewList.get(n);
|
||||
Bundle state = new Bundle(ClassLoader.getSystemClassLoader());
|
||||
if (tab.getWebView() != null) {
|
||||
tab.getWebView().saveState(state);
|
||||
outState.putBundle(BUNDLE_KEY + n, state);
|
||||
}
|
||||
}
|
||||
FileUtils.writeBundleToStorage(mApp, outState, BUNDLE_STORAGE);
|
||||
}
|
||||
|
||||
private void restoreState(Activity activity) {
|
||||
Bundle savedState = FileUtils.readBundleFromStorage(mApp, BUNDLE_STORAGE);
|
||||
if (savedState != null) {
|
||||
Log.d(Constants.TAG, "Restoring previous WebView state now");
|
||||
for (String key : savedState.keySet()) {
|
||||
if (key.startsWith(BUNDLE_KEY)) {
|
||||
LightningView tab = newTab(activity, "", false);
|
||||
if (tab.getWebView() != null) {
|
||||
tab.getWebView().restoreState(savedState.getBundle(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
package acr.browser.lightning.app;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import com.squareup.otto.Bus;
|
||||
@ -21,6 +22,11 @@ public class AppModule {
|
||||
this.mBus = new Bus();
|
||||
}
|
||||
|
||||
@Provides
|
||||
public Application provideApplication() {
|
||||
return mApp;
|
||||
}
|
||||
|
||||
@Provides
|
||||
public Context provideContext() {
|
||||
return mApp.getApplicationContext();
|
||||
|
@ -33,7 +33,6 @@ public class PreferenceManager {
|
||||
public static final String SEARCH_URL = "searchurl";
|
||||
public static final String TEXT_REFLOW = "textreflow";
|
||||
public static final String TEXT_SIZE = "textsize";
|
||||
public static final String URL_MEMORY = "memory";
|
||||
public static final String USE_WIDE_VIEWPORT = "wideviewport";
|
||||
public static final String USER_AGENT = "agentchoose";
|
||||
public static final String USER_AGENT_STRING = "userAgentString";
|
||||
@ -155,10 +154,6 @@ public class PreferenceManager {
|
||||
return mPrefs.getBoolean(Name.LOCATION, false);
|
||||
}
|
||||
|
||||
public String getMemoryUrl() {
|
||||
return mPrefs.getString(Name.URL_MEMORY, "");
|
||||
}
|
||||
|
||||
public boolean getOverviewModeEnabled() {
|
||||
return mPrefs.getBoolean(Name.OVERVIEW_MODE, true);
|
||||
}
|
||||
@ -367,10 +362,6 @@ public class PreferenceManager {
|
||||
putBoolean(Name.LOCATION, enable);
|
||||
}
|
||||
|
||||
public void setMemoryUrl(String url) {
|
||||
putString(Name.URL_MEMORY, url);
|
||||
}
|
||||
|
||||
public void setOverviewModeEnabled(boolean enable) {
|
||||
putBoolean(Name.OVERVIEW_MODE, enable);
|
||||
}
|
||||
|
90
app/src/main/java/acr/browser/lightning/utils/FileUtils.java
Normal file
90
app/src/main/java/acr/browser/lightning/utils/FileUtils.java
Normal file
@ -0,0 +1,90 @@
|
||||
package acr.browser.lightning.utils;
|
||||
|
||||
import android.app.Application;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import acr.browser.lightning.constant.Constants;
|
||||
|
||||
/**
|
||||
* A utility class containing helpful methods
|
||||
* pertaining to file storage.
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* Writes a bundle to persistent storage in the files directory
|
||||
* using the specified file name. This method is a blocking
|
||||
* operation.
|
||||
*
|
||||
* @param app the application needed to obtain the file directory.
|
||||
* @param bundle the bundle to store in persistent storage.
|
||||
* @param name the name of the file to store the bundle in.
|
||||
*/
|
||||
public static void writeBundleToStorage(Application app, Bundle bundle, String name) {
|
||||
File outputFile = new File(app.getFilesDir(), name);
|
||||
FileOutputStream outputStream = null;
|
||||
try {
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
outputStream = new FileOutputStream(outputFile);
|
||||
Parcel parcel = Parcel.obtain();
|
||||
parcel.writeBundle(bundle);
|
||||
outputStream.write(parcel.marshall());
|
||||
outputStream.flush();
|
||||
parcel.recycle();
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "Unable to write bundle to storage");
|
||||
} finally {
|
||||
Utils.close(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a bundle from the file with the specified
|
||||
* name in the peristent storage files directory.
|
||||
* This method is a blocking operation.
|
||||
*
|
||||
* @param app the application needed to obtain the files directory.
|
||||
* @param name the name of the file to read from.
|
||||
* @return a valid Bundle loaded using the system class loader
|
||||
* or null if the method was unable to read the Bundle from storage.
|
||||
*/
|
||||
@Nullable
|
||||
public static Bundle readBundleFromStorage(Application app, String name) {
|
||||
File inputFile = new File(app.getFilesDir(), name);
|
||||
FileInputStream inputStream = null;
|
||||
try {
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
inputStream = new FileInputStream(inputFile);
|
||||
Parcel parcel = Parcel.obtain();
|
||||
byte[] data = new byte[(int) inputStream.getChannel().size()];
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
inputStream.read(data, 0, data.length);
|
||||
parcel.unmarshall(data, 0, data.length);
|
||||
parcel.setDataPosition(0);
|
||||
Bundle out = parcel.readBundle(ClassLoader.getSystemClassLoader());
|
||||
out.putAll(out);
|
||||
parcel.recycle();
|
||||
return out;
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(Constants.TAG, "Unable to read bundle from storage");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
inputFile.delete();
|
||||
Utils.close(inputStream);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user