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();
}
});
}
}
Aggregations