From 305a40c8c567bd5d5c6ac0b043ee92934e4f0fcf Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Mon, 5 Sep 2016 20:42:57 +0200 Subject: [PATCH] Added some experimental changes to keep the webview on orientation changes --- app/src/main/AndroidManifest.xml | 1 + .../activity/MainActivity.java | 48 +++++++++++++++++-- .../dfa/diaspora_android/util/Helpers.java | 22 +++++++++ app/src/main/res/layout/main__content.xml | 6 +-- app/src/main/res/layout/webview.xml | 13 +++++ 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/layout/webview.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 96e0a844..8a35ba68 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -54,6 +54,7 @@ android:name=".activity.MainActivity" android:launchMode="singleInstance" android:windowSoftInputMode="adjustPan" + android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@style/AppTheme.NoActionBar" android:label="@string/diaspora"> diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index cedaad59..c13255ca 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -31,6 +31,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; @@ -52,6 +53,7 @@ import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -64,6 +66,7 @@ import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.EditText; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; @@ -145,7 +148,9 @@ public class MainActivity extends AppCompatActivity @BindView(R.id.toolbar2) ActionMenuView toolbarBottom; - @BindView(R.id.webView) + @BindView(R.id.placeholder_webview) + FrameLayout webviewPlaceholder; + ContextMenuWebView webView; @BindView(R.id.main__navigaion_view) @@ -169,10 +174,10 @@ public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.d(App.TAG, "onCreate"); // Bind UI setContentView(R.layout.main__activity); - ButterKnife.bind(this); app = (App) getApplication(); appSettings = app.getSettings(); @@ -189,8 +194,23 @@ public class MainActivity extends AppCompatActivity resetProxy(); } - setupWebView(savedInstanceState); + setupUI(savedInstanceState); + } + private void setupUI(Bundle savedInstanceState) { + boolean newWebView = (webView == null); + if(newWebView) { + Log.d(App.TAG, "Webview was null. Create new one."); + View webviewHolder = getLayoutInflater().inflate(R.layout.webview, null); + webView = (ContextMenuWebView) webviewHolder.findViewById(R.id.webView); + ((LinearLayout)webView.getParent()).removeView(webView); + setupWebView(savedInstanceState); + } + ButterKnife.bind(this); + if (webviewPlaceholder.getChildCount() != 0) { + webviewPlaceholder.removeAllViews(); + } + webviewPlaceholder.addView(webView); // Setup toolbar setSupportActionBar(toolbarTop); getMenuInflater().inflate(R.menu.main__menu_bottom, toolbarBottom.getMenu()); @@ -231,8 +251,9 @@ public class MainActivity extends AppCompatActivity progressBar = (ProgressBar) findViewById(R.id.progressBar); String url = urls.getPodUrl(); - if (savedInstanceState == null) { + if (newWebView) { if (WebHelper.isOnline(MainActivity.this)) { + Log.d(App.TAG, "setupUI: reload url"); webView.loadData("", "text/html", null); webView.loadUrlNew(url); } else { @@ -248,6 +269,24 @@ public class MainActivity extends AppCompatActivity handleIntent(getIntent()); } + @Override + public void onConfigurationChanged(Configuration newConfig) + { + if (webView != null) + { + // Remove the WebView from the old placeholder + webviewPlaceholder.removeView(webView); + } + + super.onConfigurationChanged(newConfig); + + // Load the layout resource for the new configuration + setContentView(R.layout.main__activity); + + // Reinitialize the UI + setupUI(null); + } + private void setupWebView(Bundle savedInstanceState) { webSettings = webView.getSettings(); @@ -519,6 +558,7 @@ public class MainActivity extends AppCompatActivity @Override protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { + Helpers.printBundle(savedInstanceState,""); super.onRestoreInstanceState(savedInstanceState); webView.restoreState(savedInstanceState); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java index 8a5988c9..e6202016 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java @@ -22,7 +22,9 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.os.Bundle; import android.os.Environment; +import android.util.Log; import com.github.dfa.diaspora_android.R; @@ -31,8 +33,10 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.Locale; +import java.util.Objects; public class Helpers { @@ -97,4 +101,22 @@ public class Helpers { public static String hexColorFromRessourceColor(Context context, int idColor){ return "#" + Integer.toHexString(context.getResources().getColor(idColor) & 0x00ffffff); } + + public static void printBundle(Bundle savedInstanceState, String k) { + if(savedInstanceState != null) { + for (String key : savedInstanceState.keySet()) { + Log.d("SAVED", key + " is a key in the bundle "+k); + Object bun = savedInstanceState.get(key); + if(bun != null) { + if (bun instanceof Bundle) { + printBundle((Bundle) bun, k + "." + key); + } else if (bun instanceof byte[]) { + Log.d("SAVED", "Key: "+k + "." + key+": "+ Arrays.toString((byte[])bun)); + } else { + Log.d("SAVED", "Key: "+k + "." + key+": "+ bun.toString()); + } + } + } + } + } } diff --git a/app/src/main/res/layout/main__content.xml b/app/src/main/res/layout/main__content.xml index 06dd0e41..a50f9875 100644 --- a/app/src/main/res/layout/main__content.xml +++ b/app/src/main/res/layout/main__content.xml @@ -9,13 +9,13 @@ tools:context=".activity.MainActivity" tools:showIn="@layout/main__app_bar"> - + android:layout_centerVertical="true"/> + + + + \ No newline at end of file