Java tutorial
/* * ****************************************************************************** * Copyright (c) 2013-2016 Tomas Valenta. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ***************************************************************************** */ package cz.yetanotherview.developerconsole; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.view.View; import android.view.ViewTreeObserver; import android.webkit.CookieManager; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, ViewTreeObserver.OnScrollChangedListener { private static final String DEVELOPER_CONSOLE = "https://play.google.com/apps/publish/"; private WebView browser; private SwipeRefreshLayout swipeRefreshLayout; private boolean doubleBackToExitPressedOnce, error; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.tryAgainButton).setOnClickListener(this); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setColorSchemeResources(R.color.primary, R.color.button); swipeRefreshLayout.setOnRefreshListener(this); browser = (WebView) findViewById(R.id.webView); browser.setWebViewClient(new MyBrowser()); CookieManager.getInstance().setAcceptCookie(true); open(); } @SuppressLint("SetJavaScriptEnabled") private void open() { browser.getSettings().setLoadsImagesAutomatically(true); browser.getSettings().setJavaScriptEnabled(true); browser.getViewTreeObserver().addOnScrollChangedListener(this); browser.loadUrl(DEVELOPER_CONSOLE); } @Override public void onClick(View view) { error = false; browser.loadUrl(DEVELOPER_CONSOLE); new Handler().postDelayed(new Runnable() { @Override public void run() { if (!error) browser.setVisibility(View.VISIBLE); } }, 2000); } @Override public void onRefresh() { browser.reload(); } @Override public void onScrollChanged() { if (browser.getScrollY() == 0) { swipeRefreshLayout.setEnabled(true); } else { swipeRefreshLayout.setEnabled(false); } } private class MyBrowser extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) { if (!swipeRefreshLayout.isRefreshing()) { swipeRefreshLayout.setRefreshing(true); } } @Override public void onPageFinished(WebView webView, String url) { if (swipeRefreshLayout != null) { swipeRefreshLayout.setRefreshing(false); } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { error = true; view.setVisibility(View.GONE); findViewById(R.id.action_error_full).setVisibility(View.VISIBLE); } } @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher).setTitle(R.string.dialog_title) .setMessage(R.string.message) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).setNegativeButton(R.string.no, null).show(); return; } else { if (browser.canGoBack()) { browser.goBack(); } } this.doubleBackToExitPressedOnce = true; if (getApplicationContext() == null) { return; } else { Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show(); } new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); } }