NeuroAgent

How to Create an Android TV Wrapper App for Hayer.tv with Ad Blocking

Complete guide to creating an Android TV wrapper app for Hayer.tv with ad blocking, remote control support, and session disconnection problem solutions.

How can I create an Android TV app wrapper for a third-party website (Hayer.tv) with ad blocking and enhanced functionality?

I’m trying to create an Android TV app that will work as a wrapper for the Hayer.tv website - an Armenian online cinema. The problem is that when viewing through a browser on Smart TV, the site automatically breaks the session after about 40 minutes.

I need to create an app that:

  1. Automatically blocks all advertisements
  2. Ensures proper remote control functionality
  3. Ensures stable video player operation

I’ve tried implementing this through WebView in Android Studio as recommended by AI, but I haven’t been able to achieve stable operation. Could you advise on how to properly create such an app and solve the mentioned problems?

NeuroAgent

Creating an Android TV Wrapper App for Hayer.tv with Ad Blocking

Creating an Android TV wrapper app for Hayer.tv with ad blocking requires a comprehensive approach that includes proper WebView configuration, integration of ad blocking systems, and optimization for remote control management. The main issue of session disconnection after 40 minutes is resolved by creating a full-fledged Android TV app with persistent session support and complete remote control event handling.

Table of Contents

Main Components of the App

To create a full-featured wrapper app, you’ll need several key components:

  1. Android TV Activity - the main activity configured to work with the remote control
  2. WebView component - for displaying Hayer.tv web content
  3. JavaScript Interface - for interaction between Android and web content
  4. Ad Blocker - ad blocking system
  5. Custom WebView Client - for handling navigation and loading

Your app should support the Android TV Remote Control API and properly handle focus events. According to research, special attention must be paid to focus handling for interface elements in Android TV, as standard WebView doesn’t always work correctly with remote controls.

Ad Blocking

Several approaches are available for effective ad blocking in a wrapper app:

AdGuard for Android TV

AdGuard for Android TV is one of the most effective solutions for blocking ads on televisions. It blocks ads, protects privacy, and encrypts TV traffic. AdGuard can be integrated into your app at the system level.

DNS Filtering

Use DNS filtering at the app level:

java
// Example of DNS filtering setup
private void setupDNSFiltering() {
    PrivateDnsHelper.enablePrivateDns(context, "dns.adguard.com");
}

WebView Ad Blocking

To block ads directly in WebView, you can use:

  • Blocking through JavaScript injection
  • Using system ad-blockers as WebView providers
  • Configuring WebView to use Chromium with ad-blocking support

As noted in Reddit discussions, you can use the Bromite browser as an external browser for WebView, although this requires root access.

WebView Setup for Android TV

Proper WebView configuration is critical for Android TV:

java
public class MainActivity extends Activity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webview);
        setupWebView();
    }

    private void setupWebView() {
        // Enable JavaScript
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setMediaPlaybackRequiresUserGesture(false);

        // Optimization for Android TV
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setDisplayZoomControls(false);

        // Settings for remote control
        webView.requestFocus();
        webView.setFocusableInTouchMode(true);
        
        // Set WebViewClient
        webView.setWebViewClient(new CustomWebViewClient());
        
        // Set WebChromeClient for media handling
        webView.setWebChromeClient(new CustomWebChromeClient());
    }
}

It’s important to note that according to patent CN103905863A, for Android TV you need to implement:

  1. An HTML page with custom controls
  2. WebView receiving keys from the remote
  3. Key mapping
  4. Calling corresponding JavaScript functions

Remote Control Management

To properly work with the remote control, you need to implement special focus handling:

Remote Control Event Handling

java
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_UP:
        case KeyEvent.KEYCODE_DPAD_DOWN:
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_CENTER:
            // Handle navigation
            handleNavigation(keyCode);
            return true;
        default:
            return super.onKeyDown(keyCode, event);
    }
}

private void handleNavigation(int keyCode) {
    // Send command to WebView through JavaScript interface
    String command = "handleNavigation('" + keyCode + "')";
    webView.evaluateJavascript(command, null);
}

JavaScript Interface for Navigation

Create a JavaScript interface for two-way communication:

java
public class WebAppInterface {
    Context context;

    WebAppInterface(Context c) {
        context = c;
    }

    @JavascriptInterface
    public void navigate(String direction) {
        // Handle navigation in Android
        runOnUiThread(() -> {
            switch (direction) {
                case "up":
                    // Up navigation logic
                    break;
                case "down":
                    // Down navigation logic
                    break;
            }
        });
    }
}

// Set up the interface
webView.addJavascriptInterface(new WebAppInterface(this), "AndroidTV");

Video Player Optimization

For stable operation of the Hayer.tv video player, you need:

Media Playback Settings

java
webSettings.setMediaPlaybackRequiresUserGesture(false);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);

// For HTML5 video
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);

Fullscreen Mode Handling

java
private class CustomWebChromeClient extends WebChromeClient {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        // Handle fullscreen mode
        setContentView(view);
        customViewCallback = callback;
    }

    @Override
    public void onHideCustomView() {
        // Return to normal mode
        if (customViewCallback != null) {
            customViewCallback.onCustomViewHidden();
            customViewCallback = null;
        }
        setContentView(webView);
    }
}

Solving Session Disconnection Issues

The session disconnection problem after 40 minutes is solved through several approaches:

Session Storage

java
// Save cookies and session
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie("https://hayer.tv", "session_id=" + sessionId);

// Automatic session refresh
private void refreshSession() {
    new Handler().postDelayed(() -> {
        // Send request to maintain session
        webView.evaluateJavascript("keepSessionAlive()", null);
    }, 30 * 60 * 1000); // Every 30 minutes
}

Error Handling

java
private class CustomWebViewClient extends WebViewClient {
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        // Handle errors and session restoration
        handleSessionError();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        // Check and restore session
        checkAndRestoreSession();
    }
}

Testing and Debugging

For testing Android TV apps, use:

Android TV Emulator

xml
<!-- AndroidManifest.xml -->
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.software.leanback" android:required="true" />

WebView Debugging

java
// Enable WebView debugging
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    WebView.setWebContentsDebuggingEnabled(true);
}

// Error logging
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        Log.e("WebViewError", "Error loading: " + request.getUrl());
    }
});

Alternative Solutions

If WebView doesn’t provide the necessary stability, consider alternative approaches:

Using Ready-Made Browsers

As mentioned in Reddit discussions, you can use existing browsers with ad-blocking support:

  • BrowseHere - browser with good ad blocking
  • TV Bro - optimized for Android TV
  • SponsorBlock - automatic skipping of sponsors and segments

React Native Solutions

For more complex interfaces, you can use React Native with special libraries for Android TV, as described in the article from Norigin Media.


Sources

  1. AdGuard for Android TV: The only adblock for TV
  2. Tutorial: Touch enable web apps for Fire TV
  3. How to make Webview for Android TV Using Remote Control Instead Mouse?
  4. Any Web Browsers w/ Good Ad-Blocking for Android 10 TV’s?
  5. Webview with ad blocking
  6. Ad blocker for Android TV
  7. Focus Navigation in AndroidTV with React Native
  8. Android TV WebView: Building a Custom Web Browser for TV
  9. CN103905863A - Webview-adopting remote controller control method based on Android television
  10. Block Ads on Any Smart TV for Free

Conclusion

Creating an Android TV wrapper app for Hayer.tv requires a comprehensive approach to solving several key issues:

  1. Ad Blocking - Use a combination of DNS filtering and JavaScript injection for effective ad blocking without performance degradation.

  2. Remote Control Management - Implement special focus and navigation handling through a JavaScript interface to ensure proper remote control functionality.

  3. Stable Operation - Solve the session disconnection issue through automatic session refresh and error handling.

  4. Player Optimization - Configure WebView for proper video playback in fullscreen mode with complete media event handling.

It’s recommended to start with a basic WebView implementation with proper Android TV configuration, then gradually add ad blocking functionality and optimizations. Test your app on an actual Android TV device, as the emulator may not fully reflect real remote control behavior and performance.

For more complex cases, consider alternative solutions such as using ready-made browsers with ad-blocking support or transitioning to hybrid frameworks like React Native with special libraries for Android TV.