List of usage examples for android.webkit WebView restoreState
@Nullable
public WebBackForwardList restoreState(Bundle inState)
From source file:com.commonsware.android.feedfrags.ContentFragment.java
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); WebView browser = getBrowser(); browser.setWebViewClient(new RedirectFixer()); browser.getSettings().setJavaScriptEnabled(true); if (savedInstanceState != null) { browser.restoreState(savedInstanceState); } else if (urlToLoad != null) { loadUrl(urlToLoad);/*from w ww . ja va 2s .com*/ } }
From source file:android.webkit.cts.WebViewTest.java
@UiThreadTest public void testSaveAndRestoreState() throws Throwable { if (!NullWebViewUtils.isWebViewAvailable()) { return;//w w w .j a v a 2s .com } // nothing to save assertNull(mWebView.saveState(new Bundle())); startWebServer(false); String url1 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL1); String url2 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL2); String url3 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL3); // make a history list mOnUiThread.loadUrlAndWaitForCompletion(url1); pollingCheckWebBackForwardList(url1, 0, 1); mOnUiThread.loadUrlAndWaitForCompletion(url2); pollingCheckWebBackForwardList(url2, 1, 2); mOnUiThread.loadUrlAndWaitForCompletion(url3); pollingCheckWebBackForwardList(url3, 2, 3); // save the list Bundle bundle = new Bundle(); WebBackForwardList saveList = mWebView.saveState(bundle); assertNotNull(saveList); assertEquals(3, saveList.getSize()); assertEquals(2, saveList.getCurrentIndex()); assertEquals(url1, saveList.getItemAtIndex(0).getUrl()); assertEquals(url2, saveList.getItemAtIndex(1).getUrl()); assertEquals(url3, saveList.getItemAtIndex(2).getUrl()); // change the content to a new "blank" web view without history final WebView newWebView = new WebView(getActivity()); WebBackForwardList copyListBeforeRestore = newWebView.copyBackForwardList(); assertNotNull(copyListBeforeRestore); assertEquals(0, copyListBeforeRestore.getSize()); // restore the list final WebBackForwardList restoreList = newWebView.restoreState(bundle); assertNotNull(restoreList); assertEquals(3, restoreList.getSize()); assertEquals(2, saveList.getCurrentIndex()); // wait for the list items to get inflated new PollingCheck(TEST_TIMEOUT) { @Override protected boolean check() { return restoreList.getItemAtIndex(0).getUrl() != null && restoreList.getItemAtIndex(1).getUrl() != null && restoreList.getItemAtIndex(2).getUrl() != null; } }.run(); assertEquals(url1, restoreList.getItemAtIndex(0).getUrl()); assertEquals(url2, restoreList.getItemAtIndex(1).getUrl()); assertEquals(url3, restoreList.getItemAtIndex(2).getUrl()); WebBackForwardList copyListAfterRestore = newWebView.copyBackForwardList(); assertNotNull(copyListAfterRestore); assertEquals(3, copyListAfterRestore.getSize()); assertEquals(2, copyListAfterRestore.getCurrentIndex()); assertEquals(url1, copyListAfterRestore.getItemAtIndex(0).getUrl()); assertEquals(url2, copyListAfterRestore.getItemAtIndex(1).getUrl()); assertEquals(url3, copyListAfterRestore.getItemAtIndex(2).getUrl()); }
From source file:ar.com.tristeslostrestigres.diasporanativewebapp.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = findViewById(R.id.progressBar); pm = new PrefManager(MainActivity.this); SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE); podDomain = config.getString("podDomain", null); fab = findViewById(R.id.multiple_actions); fab.setVisibility(View.GONE); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);//from www . j a va 2 s .com getSupportActionBar().setTitle(null); txtTitle = (TextView) findViewById(R.id.toolbar_title); txtTitle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Helpers.isOnline(MainActivity.this)) { txtTitle.setText(R.string.jb_stream); webView.loadUrl("https://" + podDomain + "/stream"); } else { Snackbar.make(v, R.string.no_internet, Snackbar.LENGTH_SHORT).show(); } } }); webView = findViewById(R.id.webView); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.addJavascriptInterface(new JavaScriptInterface(), "NotificationCounter"); if (savedInstanceState != null) { webView.restoreState(savedInstanceState); } wSettings = webView.getSettings(); wSettings.setJavaScriptEnabled(true); wSettings.setUseWideViewPort(true); wSettings.setLoadWithOverviewMode(true); wSettings.setDomStorageEnabled(true); wSettings.setMinimumFontSize(pm.getMinimumFontSize()); wSettings.setLoadsImagesAutomatically(pm.getLoadImages()); if (android.os.Build.VERSION.SDK_INT >= 21) wSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); /* * WebViewClient */ webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { if (!url.contains(podDomain)) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(i); return true; } return false; } public void onPageFinished(WebView view, String url) { if (url.contains("/new") || url.contains("/sign_in")) { fab.setVisibility(View.GONE); } else { fab.setVisibility(View.VISIBLE); } } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_dialog_alert) .setMessage(description).setPositiveButton("CLOSE", null).show(); } }); /* * WebChromeClient */ webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView wv, int progress) { progressBar.setProgress(progress); if (progress > 0 && progress <= 60) { Helpers.getNotificationCount(wv); } if (progress > 60) { Helpers.hideTopBar(wv); fab.setVisibility(View.VISIBLE); } if (progress == 100) { fab.collapse(); progressBar.setVisibility(View.GONE); } else { progressBar.setVisibility(View.VISIBLE); } } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mFilePathCallback != null) mFilePathCallback.onReceiveValue(null); mFilePathCallback = filePathCallback; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); } catch (IOException ex) { // Error occurred while creating the File Snackbar.make(getWindow().findViewById(R.id.drawer), "Unable to get image", Snackbar.LENGTH_SHORT).show(); } // Continue only if the File was successfully created if (photoFile != null) { mCameraPhotoPath = "file:" + photoFile.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); } else { takePictureIntent = null; } } Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); Intent[] intentArray; if (takePictureIntent != null) { intentArray = new Intent[] { takePictureIntent }; } else { intentArray = new Intent[0]; } Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); return true; } public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } }); /* * NavigationView */ NavigationView navigationView = findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { if (menuItem.isChecked()) menuItem.setChecked(false); else menuItem.setChecked(true); drawerLayout.closeDrawers(); switch (menuItem.getItemId()) { default: Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return true; case R.id.jb_stream: if (Helpers.isOnline(MainActivity.this)) { txtTitle.setText(R.string.jb_stream); webView.loadUrl("https://" + podDomain + "/stream"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_public: setTitle(R.string.jb_public); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/public"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_liked: txtTitle.setText(R.string.jb_liked); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/liked"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_commented: txtTitle.setText(R.string.jb_commented); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/commented"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_contacts: txtTitle.setText(R.string.jb_contacts); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/contacts"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_mentions: txtTitle.setText(R.string.jb_mentions); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/mentions"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_activities: txtTitle.setText(R.string.jb_activities); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/activity"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_followed_tags: txtTitle.setText(R.string.jb_followed_tags); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/followed_tags"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_manage_tags: txtTitle.setText(R.string.jb_manage_tags); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/tag_followings/manage"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_license: txtTitle.setText(R.string.jb_license); new AlertDialog.Builder(MainActivity.this).setTitle(getString(R.string.license_title)) .setMessage(getString(R.string.license_text)) .setPositiveButton(getString(R.string.license_yes), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/mdev88/Diaspora-Native-WebApp")); startActivity(i); dialog.cancel(); } }) .setNegativeButton(getString(R.string.license_no), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }) .show(); return true; case R.id.jb_aspects: txtTitle.setText(R.string.jb_aspects); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/aspects"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_settings: txtTitle.setText(R.string.jb_settings); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/user/edit"); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } case R.id.jb_pod: txtTitle.setText(R.string.jb_pod); if (Helpers.isOnline(MainActivity.this)) { new AlertDialog.Builder(MainActivity.this).setTitle(getString(R.string.confirmation)) .setMessage(getString(R.string.change_pod_warning)) .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() { @TargetApi(11) public void onClick(DialogInterface dialog, int id) { webView.clearCache(true); dialog.cancel(); Intent i = new Intent(MainActivity.this, PodsActivity.class); startActivity(i); finish(); } }).setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() { @TargetApi(11) public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }).show(); return true; } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT).show(); return false; } } } }); /* * DrawerLayout */ drawerLayout = findViewById(R.id.drawer); ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer); drawerLayout.setDrawerListener(actionBarDrawerToggle); //calling sync state is necessary or else your hamburger icon wont show up actionBarDrawerToggle.syncState(); if (savedInstanceState == null) { if (Helpers.isOnline(MainActivity.this)) { webView.loadData("", "text/html", null); webView.loadUrl("https://" + podDomain); } else { Snackbar.make(getWindow().findViewById(R.id.drawer), R.string.no_internet, Snackbar.LENGTH_SHORT) .show(); } } }