public static void manageAds(Activity activity) { final PackageManager pm = activity.getPackageManager(); // get a list of installed apps. List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); boolean yboTvProFound = false; for (ApplicationInfo info : packages) { if (PACKAGE_PRO.equals(info.packageName)) { yboTvProFound = true; break; } } // Look up the AdView as a resource and load a request. AdView adView = (AdView) activity.findViewById(R.id.adView); if (yboTvProFound) { View layout = activity.findViewById(R.id.ad_container); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) layout.getLayoutParams(); layoutParams.bottomMargin = 0; layout.setLayoutParams(layoutParams); adView.setVisibility(View.GONE); } else { adView.loadAd(new AdRequest.Builder().build()); } }
// ADS private void startAds() { AdView adView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().addTestDevice("FA62693DA83DCD07CE56E8226B7FAF61").build(); adView.loadAd(adRequest); adView.setVisibility(View.VISIBLE); }
/** Called when an ad failed to load. */ @Override public void onAdFailedToLoad(int error) { Log.d(TAG, "onBannerFailedToLoad"); Log.d(TAG, "Hiding banner with id " + id); AdView view = AdMobExtension.getBannerViewForUnitId(id); view.setVisibility(View.INVISIBLE); AdMobExtension.getLayout().removeView(view); AdMobExtension.getLayout().bringToFront(); AdMobExtension.callHaxe("onBannerFailedToLoad", new Object[] {id}); }
private void initAdView() { mAdView = (AdView) findViewById(R.id.adViewSetWallpaper); mAdView.setVisibility(View.INVISIBLE); AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build(); // Start loading the ad in sthe background. mAdView.loadAd(adRequest); mAdView.setAdListener( new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); mAdView.setVisibility(View.VISIBLE); } }); }
private void renderView() { // reference final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview); final AdView adView = (AdView) mRootView.findViewById(R.id.fragment_main_adview); // webview settings webView.getSettings().setBuiltInZoomControls(false); webView.getSettings().setSupportZoom(true); webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); webView.getSettings().setJavaScriptEnabled(true); webView.setBackgroundColor(getResources().getColor(R.color.global_bg_front)); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // fixes scrollbar on Froyo webView.setWebChromeClient( new WebChromeClient() { public void openFileChooser(ValueCallback<Uri> filePathCallback) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult( Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } public void openFileChooser(ValueCallback filePathCallback, String acceptType) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult( Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } public void openFileChooser( ValueCallback<Uri> filePathCallback, String acceptType, String capture) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult( Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } @Override public boolean onShowFileChooser( WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { mFilePathCallback5 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult( Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); return true; } }); webView.setWebViewClient( new WebViewClient() { private boolean mSuccess = true; @Override public void onPageFinished(final WebView view, final String url) { runTaskCallback( new Runnable() { public void run() { if (getActivity() != null && mSuccess) { showContent( 500); // hide progress bar with delay to show webview content smoothly showActionBarProgress(false); } } }); } @Override public void onReceivedError( final WebView view, final int errorCode, final String description, final String failingUrl) { runTaskCallback( new Runnable() { public void run() { if (getActivity() != null) { mSuccess = false; showEmpty(); showActionBarProgress(false); } } }); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (DownloadUtility.isDownloadableFile(url)) { Toast.makeText(getActivity(), R.string.fragment_main_downloading, Toast.LENGTH_LONG) .show(); DownloadUtility.downloadFile(getActivity(), url, DownloadUtility.getFileName(url)); return true; } else if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) { // determine for opening the link externally or internally boolean external = isLinkExternal(url); boolean internal = isLinkInternal(url); if (!external && !internal) { external = WebViewAppConfig.OPEN_LINKS_IN_EXTERNAL_BROWSER; } // open the link if (external) { startWebActivity(url); return true; } else { showActionBarProgress(true); return false; } } else if (url != null && url.startsWith("mailto:")) { MailTo mailTo = MailTo.parse(url); startEmailActivity(mailTo.getTo(), mailTo.getSubject(), mailTo.getBody()); return true; } else if (url != null && url.startsWith("tel:")) { startCallActivity(url); return true; } else if (url != null && url.startsWith("sms:")) { startSmsActivity(url); return true; } else if (url != null && url.startsWith("geo:")) { startMapSearchActivity(url); return true; } else { return false; } } }); webView.setOnKeyListener( new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { WebView webView = (WebView) v; switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (webView.canGoBack()) { webView.goBack(); return true; } break; } } return false; } }); webView.requestFocus( View .FOCUS_DOWN); // http://android24hours.blogspot.cz/2011/12/android-soft-keyboard-not-showing-on.html webView.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); // admob if (WebViewAppConfig.ADMOB && NetworkManager.isOnline(getActivity())) { AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(getString(R.string.admob_test_device_id)) .build(); adView.loadAd(adRequest); adView.setVisibility(View.VISIBLE); } else { adView.setVisibility(View.GONE); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* Views */ toolbar = (Toolbar) findViewById(R.id.toolbar); list = (ListView) findViewById(R.id.listView); webView = (WebView) findViewById(R.id.webView); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); tabs = (TabLayout) findViewById(R.id.tabs); /* Tabs */ // Tabs are invisible hideTabs(); // Listener to receive tab changes tabs.setOnTabSelectedListener( new TabLayout.OnTabSelectedListener() { // User selected new tab @Override public void onTabSelected(TabLayout.Tab tab) { // Find page for (Pages p : Pages.values()) { if (p.title.equals(actual.title)) { // Load URL in webview if (actual.url != null) actual.actualURL = p.url.split("\\|\\|")[tab.getPosition()]; if (actual.localPage != null) actual.actualAlternativeURL = actual.localPage.split("\\|\\|")[tab.getPosition()]; } } if (actual.url != null) { webView.loadUrl(actual.actualURL); } else { if (actual.localPage != null) webView.loadUrl("file:///android_asset/" + actual.actualAlternativeURL); } } // We don't need this methods @Override public void onTabUnselected(TabLayout.Tab tab) {} @Override public void onTabReselected(TabLayout.Tab tab) {} }); /* Drawer items */ // Create list of all items for the drawer final ArrayList<Pages> pages = new ArrayList<Pages>(); for (Pages p : Pages.values()) { if (p.menu == false) pages.add(p); } // Set actual page to the first item if (actual == null) { actual = pages.get(0); } /* White design */ if (actual.useWhiteFont) { toolbar.setTitleTextColor(0xFFFFFFFF); tabs.setTabTextColors(0xFFFFFFFF, 0xFFFFFFFF); } /* Toolbar */ toolbar.setTitle(actual.title); setSupportActionBar(toolbar); /* Admob */ AdView mAdView = (AdView) findViewById(R.id.adView); // Hide AdView when not needed if (!actual.useAds) { mAdView.setVisibility(View.INVISIBLE); } else { // Init AdView when needed AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } /* Drawer */ ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.setDrawerListener(mDrawerToggle); drawerLayout.setDrawerShadow(R.drawable.shadow, Gravity.LEFT); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); mDrawerToggle.syncState(); /* Pages to drawer */ // Add pages to the drawer ListAdapter adapter = new ListAdapter(this, pages); list.setAdapter(adapter); adapter.notifyDataSetChanged(); /* Webview */ webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient( new WebViewClient() { // Handle URL loading // Handle Intents (e.g. mailto:) @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if ((url.contains("http:") || url.contains("https:")) && actual.homeDomains.contains(URI.create(url).getHost())) { view.loadUrl(url); return false; } else { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); PackageManager manager = getPackageManager(); List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0); if (infos.size() > 0) { startActivity(intent); } return true; } } // Needed for correct back-button functionality @Override public void onPageFinished(WebView view, String url) { webView.clearHistory(); super.onPageFinished(view, url); } // Handle loading errors @Override public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { if (actual.actualAlternativeURL != null) webView.loadUrl("file:///android_asset/" + actual.actualAlternativeURL); else view.loadData(actual.error, "text/html", actual.localEncoding); } }); /* Site listener */ // Change page via drawer list.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { changePage(pages.get(position)); } }); /* Update */ // Load actual (first) page changePage(actual); }
private void stopAds() { AdView adView = (AdView) findViewById(R.id.adView); adView.destroy(); adView.setVisibility(View.INVISIBLE); }