List of usage examples for android.webkit DownloadListener DownloadListener
DownloadListener
From source file:net.granoeste.scaffold.app.ScaffoldWebViewFragment.java
@SuppressLint("SetJavaScriptEnabled") @Override/* www.j a va 2s.c o m*/ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); FrameLayout root = new FrameLayout(getActivity()); // ------------------ // Progress content // ------------------ mPFrame = new LinearLayout(getActivity()); mPFrame.setId(INTERNAL_PROGRESS_CONTAINER_ID); mPFrame.setOrientation(LinearLayout.VERTICAL); mPFrame.setGravity(Gravity.CENTER); ProgressBar progress = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyleLarge); mPFrame.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); // ---------------- // WebView content // ---------------- mCFrame = new FrameLayout(getActivity()); mCFrame.setId(INTERNAL_CONTENT_CONTAINER_ID); mWebView = new InternalWebView(getActivity()); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); mWebView.getSettings().setJavaScriptEnabled(true); // Flash Support if (UIUtils.hasKitkat()) { // Flash(4.4?????) } else if (UIUtils.hasJellyBeanMr2()) { mWebView.getSettings().setPluginState(WebSettings.PluginState.ON); } else if (UIUtils.hasFroyo()) { // Deprecated Since API level 9, and removed in API level 18 (JellyBeanMr2) // mWebView.getSettings().setPluginEnabled(true); } // mWebView.getSettings().setVerticalScrollbarOverlay(true); mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // ? mWebView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setType(mimetype); intent.setData(Uri.parse(url)); startActivity(intent); } }); mWebView.setWebViewClient(new InternalWebViewClient(root, mCallbacks)); mCFrame.addView(mWebView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); // ------------------------------------------------------------------ root.addView(mCFrame, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); root.addView(mPFrame, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return root; }
From source file:com.directsiding.android.WebActivity.java
/** * Configuracin del WebView/*from www. ja v a2 s. com*/ */ private void webViewConfig() { webView.getSettings().setBuiltInZoomControls(true); // Para HoneyComb o mayor, sacamos los controles del zoom if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { webView.getSettings().setDisplayZoomControls(false); } webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { mProgressBar.setProgress(progress); } }); webView.setWebViewClient(new DirectSidingWebViewClient()); webView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { String filename = getFileName(contentDisposition); Toast.makeText(WebActivity.this, "Iniciando descarga ...", Toast.LENGTH_SHORT).show(); new DownloadFile(WebActivity.actualNotifyId++, filename).execute(url); } }); }
From source file:de.baumann.hhsmoodle.fragmentsMain.FragmentBrowser.java
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressLint("SetJavaScriptEnabled") @Override/*w w w . j a v a 2 s.c om*/ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_screen_browser, container, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { WebView.enableSlowWholeDocumentDraw(); } PreferenceManager.setDefaultValues(getActivity(), R.xml.user_settings, false); sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); sharedPref.edit().putString("browserStarted", "true").apply(); customViewContainer = (FrameLayout) rootView.findViewById(R.id.customViewContainer); progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar); viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager); SwipeRefreshLayout swipeView = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe); assert swipeView != null; swipeView.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mWebView.reload(); } }); mWebView = (WebView) rootView.findViewById(R.id.webView); mWebChromeClient = new myWebChromeClient(); mWebView.setWebChromeClient(mWebChromeClient); imageButton_left = (ImageButton) rootView.findViewById(R.id.imageButton_left); imageButton_right = (ImageButton) rootView.findViewById(R.id.imageButton_right); if (sharedPref.getBoolean("arrow", false)) { imageButton_left.setVisibility(View.VISIBLE); imageButton_right.setVisibility(View.VISIBLE); } else { imageButton_left.setVisibility(View.INVISIBLE); imageButton_right.setVisibility(View.INVISIBLE); } helper_webView.webView_Settings(getActivity(), mWebView); helper_webView.webView_WebViewClient(getActivity(), swipeView, mWebView); mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(final String url, String userAgent, final String contentDisposition, final String mimetype, long contentLength) { final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); Snackbar snackbar = Snackbar .make(mWebView, getString(R.string.toast_download_1) + " " + filename, Snackbar.LENGTH_INDEFINITE) .setAction(getString(R.string.toast_yes), new View.OnClickListener() { @Override public void onClick(View view) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(newFileDest(), filename); DownloadManager dm = (DownloadManager) getActivity() .getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.toast_download) + " " + filename, Snackbar.LENGTH_LONG).show(); getActivity().registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } }); snackbar.show(); } }); String URLtoOpen = sharedPref.getString("loadURL", ""); if (URLtoOpen.isEmpty()) { mWebView.loadUrl( sharedPref.getString("favoriteURL", "https://moodle.huebsch.ka.schule-bw.de/moodle/my/")); } else { mWebView.loadUrl(URLtoOpen); } setHasOptionsMenu(true); return rootView; }
From source file:at.ac.uniklu.mobile.sportal.WebViewActivity.java
@SuppressLint("SetJavaScriptEnabled") @Override//from w ww. jav a 2 s . c o m protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String targetUrl = getIntent().getStringExtra(URL); if (targetUrl == null || targetUrl.length() == 0) { // if there's no URL, close the activity finish(); } setContentView(R.layout.webview); mActionBar = new ActionBarHelper(this).setupHeader().addActionRefresh(); // set header title or hide header if no title is given String title = getIntent().getStringExtra(TITLE); if (title != null) { ((TextView) findViewById(R.id.view_title)).setText(title); } else { findViewById(R.id.actionbar).setVisibility(View.GONE); } // Moodle 2.0 uses SSO/CAS authentication mSSO = getIntent().getBooleanExtra(SSO, false); // the moodle hack is only needed until Android 2.3 (or maybe 3.x? - not tested) // Android 4.0 has the redirect history entry problem solved if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) { mExecuteMoodleHack = getIntent().getBooleanExtra(MOODLE_HACK, false); } mWebView = (WebView) findViewById(R.id.web_view); //mWebView.setBackgroundColor(Color.BLACK); // black color messes up the CAS login page mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // http://stackoverflow.com/questions/3998916/android-webview-leaves-space-for-scrollbar mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setLightTouchEnabled(true); mWebView.getSettings().setLoadWithOverviewMode(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.getSettings().setUseWideViewPort(true); // setup custom webview client that shows a progress dialog while loading mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("https://sso.uni-klu.ac.at") || url.startsWith("https://sso.aau.at") || url.startsWith("https://campus.aau.at") || url.startsWith("https://moodle.aau.at")) { return false; } else if (url.startsWith("http://campus-gis.aau.at/")) { Log.d(TAG, "REDIRECT TO MAP"); String roomParameter = "curRouteTo="; int index = url.indexOf(roomParameter); if (index > -1) { MapUtils.openMapAndShowRoom(WebViewActivity.this, url.substring(index + roomParameter.length())); } return true; } // open external websites in browser Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { progressNotificationOn(); super.onPageStarted(view, url, favicon); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); progressNotificationOff(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); progressNotificationOff(); /* * the first page of moodle opens through a redirect so we need to clear the first history * entry to avoid execution of the redirect when going back through the history with the back button */ if (mExecuteMoodleHack && mWebView.canGoBack()) { mWebView.clearHistory(); mExecuteMoodleHack = false; } else { mIsFirstPage = false; } } }); mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Analytics.onEvent(Analytics.EVENT_WEB_COURSEMOODLE_DOWNLOAD); Uri uri = Uri.parse(url); MoodleDownloadHelper.download(WebViewActivity.this, uri, Utils.getContentDispositionOrUrlFilename(contentDisposition, uri)); } }); // set session cookie // http://stackoverflow.com/questions/1652850/android-webview-cookie-problem // http://android.joao.jp/2010/11/cookiemanager-and-removeallcookie.html if (Studentportal.getSportalClient().isSessionCookieAvailable()) { CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); //cookieManager.removeSessionCookie(); // NOTE when calling this method the cookies get removed after the next setCookie() gets called Cookie sessionCookie = Studentportal.getSportalClient().getSessionCookie(); cookieManager.setCookie(sessionCookie.getDomain(), Utils.cookieHeaderString(sessionCookie)); if (mSSO) { // set SSO/CAS cookie Cookie ssoCookie = Studentportal.getSportalClient().getCookie("CASTGC"); if (ssoCookie != null) { cookieManager.setCookie(ssoCookie.getDomain(), Utils.cookieHeaderString(ssoCookie)); } } CookieSyncManager.getInstance().sync(); } mIsFirstPage = true; mWebView.loadUrl(targetUrl); }
From source file:org.zirco.ui.components.CustomPagerAdapter.java
private CustomWebView initializeWebView(CustomWebView webView) { webView.setOnTouchListener(mParentActivity); webView.setWebViewClient(new CustomWebViewClient(mParentActivity)); webView.setWebChromeClient(new CustomWebChromeClient(mParentActivity, webView)); webView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { @Override//w ww. j a v a2s .co m public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { HitTestResult result = ((WebView) v).getHitTestResult(); int resultType = result.getType(); if ((resultType == HitTestResult.ANCHOR_TYPE) || (resultType == HitTestResult.IMAGE_ANCHOR_TYPE) || (resultType == HitTestResult.SRC_ANCHOR_TYPE) || (resultType == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)) { Intent i = new Intent(); i.putExtra(Constants.EXTRA_ID_URL, result.getExtra()); MenuItem item = menu.add(0, MainActivity.CONTEXT_MENU_OPEN, 0, R.string.Main_MenuOpen); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_OPEN_IN_NEW_TAB, 0, R.string.Main_MenuOpenNewTab); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_COPY, 0, R.string.Main_MenuCopyLinkUrl); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_DOWNLOAD, 0, R.string.Main_MenuDownload); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_SHARE, 0, R.string.Main_MenuShareLinkUrl); item.setIntent(i); menu.setHeaderTitle(result.getExtra()); } else if (resultType == HitTestResult.IMAGE_TYPE) { Intent i = new Intent(); i.putExtra(Constants.EXTRA_ID_URL, result.getExtra()); MenuItem item = menu.add(0, MainActivity.CONTEXT_MENU_OPEN, 0, R.string.Main_MenuViewImage); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_COPY, 0, R.string.Main_MenuCopyImageUrl); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_DOWNLOAD, 0, R.string.Main_MenuDownloadImage); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_SHARE, 0, R.string.Main_MenuShareImageUrl); item.setIntent(i); menu.setHeaderTitle(result.getExtra()); } else if (resultType == HitTestResult.EMAIL_TYPE) { Intent sendMail = new Intent(Intent.ACTION_VIEW, Uri.parse(WebView.SCHEME_MAILTO + result.getExtra())); MenuItem item = menu.add(0, MainActivity.CONTEXT_MENU_SEND_MAIL, 0, R.string.Main_MenuSendEmail); item.setIntent(sendMail); Intent i = new Intent(); i.putExtra(Constants.EXTRA_ID_URL, result.getExtra()); item = menu.add(0, MainActivity.CONTEXT_MENU_COPY, 0, R.string.Main_MenuCopyEmailUrl); item.setIntent(i); item = menu.add(0, MainActivity.CONTEXT_MENU_SHARE, 0, R.string.Main_MenuShareEmailUrl); item.setIntent(i); menu.setHeaderTitle(result.getExtra()); } } }); webView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { mParentActivity.doDownloadStart(url, userAgent, contentDisposition, mimetype, contentLength); } }); return webView; }
From source file:com.nbplus.hybrid.BasicWebViewClient.java
/** * ??./* w ww . j a va 2 s. c o m*/ * @param activity : context * @param view : ?? */ public BasicWebViewClient(Activity activity, WebView view, String alertTitleString, String confirmTitleString) { mWebView = view; mContext = activity; // This will handle downloading. It requires Gingerbread, though mDownloadManager = (DownloadManager) mContext.getSystemService(mContext.DOWNLOAD_SERVICE); mWebChromeClient = new BroadcastWebChromeClient(); // Enable remote debugging via chrome://inspect if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mWebView.setWebContentsDebuggingEnabled(true); } mWebView.setWebChromeClient(mWebChromeClient); WebSettings webSettings = mWebView.getSettings(); webSettings.setGeolocationEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setDatabaseEnabled(true); // Use WideViewport and Zoom out if there is no viewport defined webSettings.setUseWideViewPort(true); webSettings.setLoadWithOverviewMode(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { webSettings.setMediaPlaybackRequiresUserGesture(false); } // Enable pinch to zoom without the zoom buttons webSettings.setBuiltInZoomControls(true); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) { // Hide the zoom controls for HONEYCOMB+ webSettings.setDisplayZoomControls(false); } webSettings.setAppCacheEnabled(true); mWebView.clearCache(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Sets whether the WebView should allow third party cookies to be set. // Allowing third party cookies is a per WebView policy and can be set differently on different WebView instances. // Apps that target KITKAT or below default to allowing third party cookies. // Apps targeting LOLLIPOP or later default to disallowing third party cookies. CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true); cookieManager.setAcceptThirdPartyCookies(mWebView, true); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { mWebView.getSettings().setTextZoom(100); } if (StringUtils.isEmptyString(alertTitleString)) { mAlertTitleString = activity.getString(R.string.default_webview_alert_title); } else { mAlertTitleString = alertTitleString; } if (StringUtils.isEmptyString(confirmTitleString)) { mConfirmTitleString = activity.getString(R.string.default_webview_confirm_title); } else { mConfirmTitleString = confirmTitleString; } mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); mContext.startActivity(intent); } }); Log.d(TAG, ">> user agent = " + mWebView.getSettings().getUserAgentString()); }
From source file:de.baumann.browser.Browser.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WebView.enableSlowWholeDocumentDraw(); setContentView(R.layout.activity_browser); helper_main.onStart(Browser.this); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);//from w w w .j a v a2 s .c o m PreferenceManager.setDefaultValues(this, R.xml.user_settings, false); PreferenceManager.setDefaultValues(this, R.xml.user_settings_search, false); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); sharedPref.edit().putInt("keyboard", 0).apply(); sharedPref.getInt("keyboard", 0); customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); progressBar = (ProgressBar) findViewById(R.id.progressBar); editText = (EditText) findViewById(R.id.editText); editText.setHint(R.string.app_search_hint); editText.clearFocus(); imageButton = (ImageButton) findViewById(R.id.imageButton); imageButton.setVisibility(View.INVISIBLE); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mWebView.scrollTo(0, 0); imageButton.setVisibility(View.INVISIBLE); actionBar = getSupportActionBar(); assert actionBar != null; if (!actionBar.isShowing()) { editText.setText(mWebView.getTitle()); actionBar.show(); } setNavArrows(); } }); SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe); assert swipeView != null; swipeView.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mWebView.reload(); } }); mWebView = (ObservableWebView) findViewById(R.id.webView); if (sharedPref.getString("fullscreen", "2").equals("2") || sharedPref.getString("fullscreen", "2").equals("3")) { mWebView.setScrollViewCallbacks(this); } mWebChromeClient = new myWebChromeClient(); mWebView.setWebChromeClient(mWebChromeClient); imageButton_left = (ImageButton) findViewById(R.id.imageButton_left); imageButton_right = (ImageButton) findViewById(R.id.imageButton_right); if (sharedPref.getBoolean("arrow", false)) { imageButton_left.setVisibility(View.VISIBLE); imageButton_right.setVisibility(View.VISIBLE); } else { imageButton_left.setVisibility(View.INVISIBLE); imageButton_right.setVisibility(View.INVISIBLE); } helper_webView.webView_Settings(Browser.this, mWebView); helper_webView.webView_WebViewClient(Browser.this, swipeView, mWebView, editText); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); if (isNetworkUnAvailable()) { if (sharedPref.getBoolean("offline", false)) { if (isNetworkUnAvailable()) { // loading offline mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); Snackbar.make(mWebView, R.string.toast_cache, Snackbar.LENGTH_SHORT).show(); } } else { Snackbar.make(mWebView, R.string.toast_noInternet, Snackbar.LENGTH_SHORT).show(); } } mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(final String url, String userAgent, final String contentDisposition, final String mimetype, long contentLength) { registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); Snackbar snackbar = Snackbar .make(mWebView, getString(R.string.toast_download_1) + " " + filename, Snackbar.LENGTH_LONG) .setAction(getString(R.string.toast_yes), new View.OnClickListener() { @Override public void onClick(View view) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.toast_download) + " " + filename, Snackbar.LENGTH_SHORT).show(); } }); snackbar.show(); } }); helper_editText.editText_Touch(editText, Browser.this, mWebView); helper_editText.editText_EditorAction(editText, Browser.this, mWebView); helper_editText.editText_FocusChange(editText, Browser.this); onNewIntent(getIntent()); helper_main.grantPermissionsStorage(Browser.this); }
From source file:de.baumann.browser.Browser_right.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(ContextCompat.getColor(Browser_right.this, R.color.colorTwoDark)); WebView.enableSlowWholeDocumentDraw(); setContentView(R.layout.activity_browser); helper_main.onStart(Browser_right.this); PreferenceManager.setDefaultValues(this, R.xml.user_settings, false); PreferenceManager.setDefaultValues(this, R.xml.user_settings_search, false); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); sharedPref.edit()/*from w ww . j av a 2 s . c om*/ .putString("openURL", sharedPref.getString("startURL", "https://github.com/scoute-dich/browser/")) .apply(); sharedPref.edit().putInt("keyboard", 0).apply(); sharedPref.getInt("keyboard", 0); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setBackgroundColor(ContextCompat.getColor(Browser_right.this, R.color.colorTwo)); setSupportActionBar(toolbar); actionBar = getSupportActionBar(); customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); progressBar = (ProgressBar) findViewById(R.id.progressBar); editText = (EditText) findViewById(R.id.editText); editText.setVisibility(View.GONE); editText.setHint(R.string.app_search_hint); editText.clearFocus(); urlBar = (TextView) findViewById(R.id.urlBar); imageButton = (ImageButton) findViewById(R.id.imageButton); imageButton.setVisibility(View.INVISIBLE); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mWebView.scrollTo(0, 0); imageButton.setVisibility(View.INVISIBLE); if (!actionBar.isShowing()) { urlBar.setText(mWebView.getTitle()); actionBar.show(); } helper_browser.setNavArrows(mWebView, imageButton_left, imageButton_right); } }); SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe); assert swipeView != null; swipeView.setColorSchemeResources(R.color.colorTwo, R.color.colorAccent); swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mWebView.reload(); } }); mWebView = (ObservableWebView) findViewById(R.id.webView); if (sharedPref.getString("fullscreen", "2").equals("2") || sharedPref.getString("fullscreen", "2").equals("3")) { mWebView.setScrollViewCallbacks(this); } mWebChromeClient = new myWebChromeClient(); mWebView.setWebChromeClient(mWebChromeClient); imageButton_left = (ImageButton) findViewById(R.id.imageButton_left); imageButton_right = (ImageButton) findViewById(R.id.imageButton_right); if (sharedPref.getBoolean("arrow", false)) { imageButton_left.setVisibility(View.VISIBLE); imageButton_right.setVisibility(View.VISIBLE); } else { imageButton_left.setVisibility(View.INVISIBLE); imageButton_right.setVisibility(View.INVISIBLE); } helper_webView.webView_Settings(Browser_right.this, mWebView); helper_webView.webView_WebViewClient(Browser_right.this, swipeView, mWebView, urlBar); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); if (isNetworkUnAvailable()) { if (sharedPref.getBoolean("offline", false)) { if (isNetworkUnAvailable()) { // loading offline mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); Snackbar.make(mWebView, R.string.toast_cache, Snackbar.LENGTH_SHORT).show(); } } else { Snackbar.make(mWebView, R.string.toast_noInternet, Snackbar.LENGTH_SHORT).show(); } } mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(final String url, String userAgent, final String contentDisposition, final String mimetype, long contentLength) { registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); Snackbar snackbar = Snackbar .make(mWebView, getString(R.string.toast_download_1) + " " + filename, Snackbar.LENGTH_LONG) .setAction(getString(R.string.toast_yes), new View.OnClickListener() { @Override public void onClick(View view) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.toast_download) + " " + filename, Snackbar.LENGTH_SHORT).show(); } }); snackbar.show(); } }); helper_browser.toolbar(Browser_right.this, Browser_left.class, mWebView, toolbar); helper_editText.editText_EditorAction(editText, Browser_right.this, mWebView, urlBar); helper_editText.editText_FocusChange(editText, Browser_right.this); helper_main.grantPermissionsStorage(Browser_right.this); onNewIntent(getIntent()); }
From source file:mgks.os.webview.MainActivity.java
@SuppressLint({ "SetJavaScriptEnabled", "WrongViewCast" }) @Override/*from w w w. ja v a2s. c o m*/ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.w("READ_PERM = ", Manifest.permission.READ_EXTERNAL_STORAGE); Log.w("WRITE_PERM = ", Manifest.permission.WRITE_EXTERNAL_STORAGE); //Prevent the app from being started again when it is still alive in the background if (!isTaskRoot()) { finish(); return; } setContentView(R.layout.activity_main); asw_view = findViewById(R.id.msw_view); final SwipeRefreshLayout pullfresh = findViewById(R.id.pullfresh); if (ASWP_PULLFRESH) { pullfresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { pull_fresh(); pullfresh.setRefreshing(false); } }); asw_view.getViewTreeObserver() .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { if (asw_view.getScrollY() == 0) { pullfresh.setEnabled(true); } else { pullfresh.setEnabled(false); } } }); } else { pullfresh.setRefreshing(false); pullfresh.setEnabled(false); } if (ASWP_PBAR) { asw_progress = findViewById(R.id.msw_progress); } else { findViewById(R.id.msw_progress).setVisibility(View.GONE); } asw_loading_text = findViewById(R.id.msw_loading_text); Handler handler = new Handler(); //Launching app rating request if (ASWP_RATINGS) { handler.postDelayed(new Runnable() { public void run() { get_rating(); } }, 1000 * 60); //running request after few moments } //Getting basic device information get_info(); //Getting GPS location of device if given permission if (ASWP_LOCATION && !check_permission(1)) { ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, loc_perm); } get_location(); //Webview settings; defaults are customized for best performance WebSettings webSettings = asw_view.getSettings(); if (!ASWP_OFFLINE) { webSettings.setJavaScriptEnabled(ASWP_JSCRIPT); } webSettings.setSaveFormData(ASWP_SFORM); webSettings.setSupportZoom(ASWP_ZOOM); webSettings.setGeolocationEnabled(ASWP_LOCATION); webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); webSettings.setAllowUniversalAccessFromFileURLs(true); webSettings.setUseWideViewPort(true); webSettings.setDomStorageEnabled(true); asw_view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return true; } }); asw_view.setHapticFeedbackEnabled(false); asw_view.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) { if (!check_permission(2)) { ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, file_perm); } else { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setMimeType(mimeType); String cookies = CookieManager.getInstance().getCookie(url); request.addRequestHeader("cookie", cookies); request.addRequestHeader("User-Agent", userAgent); request.setDescription(getString(R.string.dl_downloading)); request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType)); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); assert dm != null; dm.enqueue(request); Toast.makeText(getApplicationContext(), getString(R.string.dl_downloading2), Toast.LENGTH_LONG) .show(); } } }); if (Build.VERSION.SDK_INT >= 21) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); asw_view.setLayerType(View.LAYER_TYPE_HARDWARE, null); webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } else if (Build.VERSION.SDK_INT >= 19) { asw_view.setLayerType(View.LAYER_TYPE_HARDWARE, null); } asw_view.setVerticalScrollBarEnabled(false); asw_view.setWebViewClient(new Callback()); //Rendering the default URL aswm_view(ASWV_URL, false); asw_view.setWebChromeClient(new WebChromeClient() { //Handling input[type="file"] requests for android API 16+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { if (ASWP_FUPLOAD) { asw_file_message = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType(ASWV_F_TYPE); if (ASWP_MULFILE) { i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); } startActivityForResult(Intent.createChooser(i, getString(R.string.fl_chooser)), asw_file_req); } } //Handling input[type="file"] requests for android API 21+ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (check_permission(2) && check_permission(3)) { if (ASWP_FUPLOAD) { if (asw_file_path != null) { asw_file_path.onReceiveValue(null); } asw_file_path = filePathCallback; Intent takePictureIntent = null; if (ASWP_CAMUPLOAD) { takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) { File photoFile = null; try { photoFile = create_image(); takePictureIntent.putExtra("PhotoPath", asw_cam_message); } catch (IOException ex) { Log.e(TAG, "Image file creation failed", ex); } if (photoFile != null) { asw_cam_message = "file:" + photoFile.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); } else { takePictureIntent = null; } } } Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); if (!ASWP_ONLYCAM) { contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType(ASWV_F_TYPE); if (ASWP_MULFILE) { contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); } } 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, getString(R.string.fl_chooser)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooserIntent, asw_file_req); } return true; } else { get_file(); return false; } } //Getting webview rendering progress @Override public void onProgressChanged(WebView view, int p) { if (ASWP_PBAR) { asw_progress.setProgress(p); if (p == 100) { asw_progress.setProgress(0); } } } // overload the geoLocations permissions prompt to always allow instantly as app permission was granted previously public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { if (Build.VERSION.SDK_INT < 23 || check_permission(1)) { // location permissions were granted previously so auto-approve callback.invoke(origin, true, false); } else { // location permissions not granted so request them ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, loc_perm); } } }); if (getIntent().getData() != null) { String path = getIntent().getDataString(); /* If you want to check or use specific directories or schemes or hosts Uri data = getIntent().getData(); String scheme = data.getScheme(); String host = data.getHost(); List<String> pr = data.getPathSegments(); String param1 = pr.get(0); */ aswm_view(path, false); } }
From source file:de.baumann.browser.Browser_left.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor(ContextCompat.getColor(Browser_left.this, R.color.colorPrimaryDark)); WebView.enableSlowWholeDocumentDraw(); setContentView(R.layout.activity_browser); helper_main.checkPin(Browser_left.this); helper_main.onStart(Browser_left.this); PreferenceManager.setDefaultValues(this, R.xml.user_settings, false); PreferenceManager.setDefaultValues(this, R.xml.user_settings_search, false); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); sharedPref.edit().putBoolean("isOpened", true).apply(); boolean show = sharedPref.getBoolean("introShowDo_notShow", true); if (show) {//from ww w. j a va 2 s. c o m helper_main.switchToActivity(Browser_left.this, Activity_intro.class, "", false); } sharedPref.edit() .putString("openURL", sharedPref.getString("startURL", "https://github.com/scoute-dich/browser/")) .apply(); sharedPref.edit().putInt("keyboard", 0).apply(); sharedPref.getInt("keyboard", 0); if (sharedPref.getString("saved_key_ok", "no").equals("no")) { char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!$%&/()=?;:_-.,+#*<>" .toCharArray(); StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 25; i++) { char c = chars[random.nextInt(chars.length)]; sb.append(c); } sharedPref.edit().putString("saved_key", sb.toString()).apply(); sharedPref.edit().putString("saved_key_ok", "yes").apply(); } Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); actionBar = getSupportActionBar(); customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); progressBar = (ProgressBar) findViewById(R.id.progressBar); editText = (EditText) findViewById(R.id.editText); editText.setVisibility(View.GONE); editText.setHint(R.string.app_search_hint); editText.clearFocus(); urlBar = (TextView) findViewById(R.id.urlBar); imageButton = (ImageButton) findViewById(R.id.imageButton); imageButton.setVisibility(View.INVISIBLE); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mWebView.scrollTo(0, 0); imageButton.setVisibility(View.INVISIBLE); if (!actionBar.isShowing()) { urlBar.setText(mWebView.getTitle()); actionBar.show(); } helper_browser.setNavArrows(mWebView, imageButton_left, imageButton_right); } }); SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe); assert swipeView != null; swipeView.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mWebView.reload(); } }); mWebView = (ObservableWebView) findViewById(R.id.webView); if (sharedPref.getString("fullscreen", "2").equals("2") || sharedPref.getString("fullscreen", "2").equals("3")) { mWebView.setScrollViewCallbacks(this); } mWebChromeClient = new myWebChromeClient(); mWebView.setWebChromeClient(mWebChromeClient); imageButton_left = (ImageButton) findViewById(R.id.imageButton_left); imageButton_right = (ImageButton) findViewById(R.id.imageButton_right); if (sharedPref.getBoolean("arrow", false)) { imageButton_left.setVisibility(View.VISIBLE); imageButton_right.setVisibility(View.VISIBLE); } else { imageButton_left.setVisibility(View.INVISIBLE); imageButton_right.setVisibility(View.INVISIBLE); } helper_webView.webView_Settings(Browser_left.this, mWebView); helper_webView.webView_WebViewClient(Browser_left.this, swipeView, mWebView, urlBar); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); if (isNetworkUnAvailable()) { if (sharedPref.getBoolean("offline", false)) { if (isNetworkUnAvailable()) { // loading offline mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); Snackbar.make(mWebView, R.string.toast_cache, Snackbar.LENGTH_SHORT).show(); } } else { Snackbar.make(mWebView, R.string.toast_noInternet, Snackbar.LENGTH_SHORT).show(); } } mWebView.setDownloadListener(new DownloadListener() { public void onDownloadStart(final String url, String userAgent, final String contentDisposition, final String mimetype, long contentLength) { registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); final String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); Snackbar snackbar = Snackbar .make(mWebView, getString(R.string.toast_download_1) + " " + filename, Snackbar.LENGTH_LONG) .setAction(getString(R.string.toast_yes), new View.OnClickListener() { @Override public void onClick(View view) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.toast_download) + " " + filename, Snackbar.LENGTH_SHORT).show(); } }); snackbar.show(); } }); helper_browser.toolbar(Browser_left.this, Browser_right.class, mWebView, toolbar); helper_editText.editText_EditorAction(editText, Browser_left.this, mWebView, urlBar); helper_editText.editText_FocusChange(editText, Browser_left.this); helper_main.grantPermissionsStorage(Browser_left.this); onNewIntent(getIntent()); }