Search in sources :

Example 1 with CustomSwipeRefresh

use of com.shapps.mintubeapp.CustomViews.CustomSwipeRefresh in project mintube by imshyam.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    mainAct = this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewStub = (ViewStub) findViewById(R.id.view_stub);
    if (isInternetAvailable(mainAct)) {
        viewStub.setLayoutResource(R.layout.content_main);
        viewStub.inflate();
        exit = false;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Swipe Refresh WebView
        swipeRefreshLayout = (CustomSwipeRefresh) findViewById(R.id.swipe_refresh);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        youtubeView.loadUrl(youtubeView.getUrl());
                    }
                });
            }
        });
        // after initialization
        swipeRefreshLayout.setCanChildScrollUpCallback(new CustomSwipeRefresh.CanChildScrollUpCallback() {

            @Override
            public boolean canSwipeRefreshChildScrollUp() {
                return youtubeView.getScrollY() > 0;
            }
        });
        youtubeView = (WebView) findViewById(R.id.youtube_view);
        youtubeView.getSettings().setJavaScriptEnabled(true);
        youtubeView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String str, Bitmap bitmap) {
                super.onPageStarted(view, str, bitmap);
                Log.d("Main Page Loading ", str);
                swipeRefreshLayout.setRefreshing(true);
                currUrl = str;
            }

            @Override
            public void onPageFinished(WebView view, String str) {
                super.onPageFinished(view, str);
                swipeRefreshLayout.setRefreshing(false);
                Log.d("Main Page Finished", str);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("?app=desktop") && !url.contains("signin?app=desktop")) {
                    Log.d("Url stopped to load : ", url);
                    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
                    final Snackbar snackbar = Snackbar.make(coordinatorLayout, "Desktop View Unavailable", Snackbar.LENGTH_LONG);
                    // Changing Text Color
                    View snkBar = snackbar.getView();
                    TextView tv = (TextView) snkBar.findViewById(android.support.design.R.id.snackbar_text);
                    tv.setTextColor(Color.parseColor("#e52d27"));
                    snackbar.show();
                    return true;
                }
                return false;
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (String.valueOf(request.getUrl()).contains("http://m.youtube.com/watch?") || String.valueOf(request.getUrl()).contains("https://m.youtube.com/watch?")) {
                        String url = String.valueOf(request.getUrl());
                        Log.d("Yay Catches!!!! ", url);
                        // Video Id
                        VID = url.substring(url.indexOf("&v=") + 3, url.length());
                        Log.d("VID ", VID);
                        // Playlist Id
                        final String listID = url.substring(url.indexOf("&list=") + 6, url.length());
                        Pattern pattern = Pattern.compile("([A-Za-z0-9_-]+)&[\\w]+=.*", Pattern.CASE_INSENSITIVE);
                        Matcher matcher = pattern.matcher(listID.toString());
                        Log.d("ListID", listID);
                        PID = "";
                        if (matcher.matches()) {
                            PID = matcher.group(1);
                        }
                        if (listID.contains("m.youtube.com")) {
                            Log.d("Not a ", "Playlist.");
                            PID = null;
                        } else {
                            Constants.linkType = 1;
                            Log.d("PlaylistID ", PID);
                        }
                        Handler handler = new Handler(getMainLooper());
                        final String finalPID = PID;
                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                youtubeView.stopLoading();
                                youtubeView.goBack();
                                if (isServiceRunning(PlayerService.class)) {
                                    Log.d("Service : ", "Already Running!");
                                    PlayerService.startVid(VID, finalPID);
                                } else {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(MainActivity.this)) {
                                        Intent i = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                                        startActivityForResult(i, OVERLAY_PERMISSION_REQ);
                                    } else {
                                        Intent i = new Intent(MainActivity.this, PlayerService.class);
                                        i.putExtra("VID_ID", VID);
                                        i.putExtra("PLAYLIST_ID", finalPID);
                                        i.setAction(Constants.ACTION.STARTFOREGROUND_WEB_ACTION);
                                        startService(i);
                                    }
                                // Intent i = new Intent(MainActivity.this, PlayerService.class);
                                // i.putExtra("VID_ID", VID);
                                // i.putExtra("PLAYLIST_ID", finalPID);
                                // i.setAction(Constants.ACTION.STARTFOREGROUND_WEB_ACTION);
                                // startService(i);
                                }
                            }
                        });
                    }
                }
                return super.shouldInterceptRequest(view, request);
            }
        });
        youtubeView.canGoBack();
        currUrl = "https://m.youtube.com/";
        youtubeView.loadUrl(currUrl);
    } else {
        viewStub.setLayoutResource(R.layout.content_main_no_internet);
        viewStub.inflate();
        exit = true;
        retry = (Button) findViewById(R.id.retry_internet);
        changeSettings = (Button) findViewById(R.id.change_settings);
        exitApp = (Button) findViewById(R.id.exit_app);
        retry.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mainAct.recreate();
            }
        });
        changeSettings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(Settings.ACTION_SETTINGS), 0);
            }
        });
        exitApp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
Also used : Pattern(java.util.regex.Pattern) CustomSwipeRefresh(com.shapps.mintubeapp.CustomViews.CustomSwipeRefresh) WebResourceRequest(android.webkit.WebResourceRequest) Matcher(java.util.regex.Matcher) Handler(android.os.Handler) Intent(android.content.Intent) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) CoordinatorLayout(android.support.design.widget.CoordinatorLayout) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) TextView(android.widget.TextView) WebView(android.webkit.WebView) Toolbar(android.support.v7.widget.Toolbar) WebViewClient(android.webkit.WebViewClient) Snackbar(android.support.design.widget.Snackbar)

Aggregations

Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 Handler (android.os.Handler)1 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)1 Snackbar (android.support.design.widget.Snackbar)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 SearchView (android.support.v7.widget.SearchView)1 Toolbar (android.support.v7.widget.Toolbar)1 View (android.view.View)1 WebResourceRequest (android.webkit.WebResourceRequest)1 WebResourceResponse (android.webkit.WebResourceResponse)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 CustomSwipeRefresh (com.shapps.mintubeapp.CustomViews.CustomSwipeRefresh)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1