2018-03-03 01:10:13 +01:00
|
|
|
package net.schueller.peertube.activity;
|
|
|
|
|
2018-03-11 02:24:06 +01:00
|
|
|
import android.Manifest;
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.app.Activity;
|
2018-03-04 01:06:32 +01:00
|
|
|
import android.content.Intent;
|
2018-03-04 01:44:34 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-03-11 02:24:06 +01:00
|
|
|
import android.content.pm.PackageManager;
|
2018-03-11 14:51:39 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2018-03-11 02:24:06 +01:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2018-11-10 22:40:27 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2018-03-03 23:08:18 +01:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.util.Log;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.view.Menu;
|
2018-03-04 01:06:32 +01:00
|
|
|
import android.view.MenuInflater;
|
2018-03-03 02:46:01 +01:00
|
|
|
import android.view.MenuItem;
|
2018-03-03 01:10:13 +01:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
|
|
|
|
import com.google.android.gms.common.GooglePlayServicesRepairableException;
|
|
|
|
import com.google.android.gms.common.GooglePlayServicesUtil;
|
|
|
|
import com.google.android.gms.security.ProviderInstaller;
|
2018-11-10 22:40:27 +01:00
|
|
|
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
|
2018-03-04 01:06:32 +01:00
|
|
|
import com.joanzapata.iconify.IconDrawable;
|
|
|
|
import com.joanzapata.iconify.Iconify;
|
|
|
|
import com.joanzapata.iconify.fonts.FontAwesomeIcons;
|
|
|
|
import com.joanzapata.iconify.fonts.FontAwesomeModule;
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
import net.schueller.peertube.R;
|
|
|
|
import net.schueller.peertube.adapter.VideoAdapter;
|
2018-03-11 14:51:39 +01:00
|
|
|
import net.schueller.peertube.helper.APIUrlHelper;
|
2018-03-03 01:10:13 +01:00
|
|
|
import net.schueller.peertube.model.VideoList;
|
|
|
|
import net.schueller.peertube.network.GetVideoDataService;
|
|
|
|
import net.schueller.peertube.network.RetrofitInstance;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
public class VideoListActivity extends AppCompatActivity {
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
private String TAG = "VideoListActivity";
|
|
|
|
|
2018-03-04 01:06:32 +01:00
|
|
|
public static final String EXTRA_VIDEOID = "VIDEOID ";
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
private VideoAdapter videoAdapter;
|
2018-03-03 23:08:18 +01:00
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
2018-03-03 01:10:13 +01:00
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
private int currentStart = 0;
|
|
|
|
private int count = 12;
|
|
|
|
private String sort = "-createdAt";
|
2018-07-01 15:11:26 +02:00
|
|
|
private String filter = "";
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
private boolean isLoading = false;
|
|
|
|
|
2018-11-10 22:40:27 +01:00
|
|
|
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
|
2018-03-03 23:08:18 +01:00
|
|
|
= item -> {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.navigation_home:
|
2018-03-04 01:06:32 +01:00
|
|
|
//Log.v(TAG, "navigation_home");
|
|
|
|
|
|
|
|
if (!isLoading) {
|
|
|
|
sort = "-createdAt";
|
|
|
|
currentStart = 0;
|
2018-07-01 15:11:26 +02:00
|
|
|
loadVideos(currentStart, count, sort, filter);
|
2018-03-04 01:06:32 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
return true;
|
|
|
|
case R.id.navigation_trending:
|
2018-03-04 01:06:32 +01:00
|
|
|
//Log.v(TAG, "navigation_trending");
|
|
|
|
|
|
|
|
if (!isLoading) {
|
2018-11-10 22:40:27 +01:00
|
|
|
sort = "-trending";
|
2018-03-04 01:06:32 +01:00
|
|
|
currentStart = 0;
|
2018-07-01 15:11:26 +02:00
|
|
|
loadVideos(currentStart, count, sort, filter);
|
2018-03-04 01:06:32 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
return true;
|
|
|
|
case R.id.navigation_subscriptions:
|
2018-11-10 22:40:27 +01:00
|
|
|
//Log.v(TAG, "navigation_subscriptions");
|
2018-03-11 14:51:39 +01:00
|
|
|
Toast.makeText(VideoListActivity.this, "Subscriptions Not Implemented", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case R.id.navigation_account:
|
2018-11-10 22:40:27 +01:00
|
|
|
//Log.v(TAG, "navigation_account");
|
2018-03-11 14:51:39 +01:00
|
|
|
Toast.makeText(VideoListActivity.this, "Account Not Implemented", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
return false;
|
2018-03-03 23:08:18 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_video_list);
|
|
|
|
|
2018-11-07 23:11:19 +01:00
|
|
|
filter = "";
|
2018-07-01 15:11:26 +02:00
|
|
|
|
2018-03-04 01:06:32 +01:00
|
|
|
// Init icons
|
|
|
|
Iconify.with(new FontAwesomeModule());
|
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
// Attaching the layout to the toolbar object
|
2018-11-10 22:40:27 +01:00
|
|
|
Toolbar toolbar = findViewById(R.id.tool_bar);
|
2018-03-03 02:46:01 +01:00
|
|
|
// Setting toolbar as the ActionBar with setSupportActionBar() call
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2018-03-03 01:10:13 +01:00
|
|
|
// fix android trying to use SSLv3 for handshake
|
|
|
|
updateAndroidSecurityProvider(this);
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
// Bottom Navigation
|
2018-11-10 22:40:27 +01:00
|
|
|
BottomNavigationViewEx navigation = findViewById(R.id.navigation);
|
|
|
|
|
|
|
|
navigation.enableAnimation(false);
|
|
|
|
navigation.enableShiftingMode(false);
|
|
|
|
navigation.enableItemShiftingMode(false);
|
|
|
|
|
2018-03-04 01:06:32 +01:00
|
|
|
Menu navMenu = navigation.getMenu();
|
|
|
|
navMenu.findItem(R.id.navigation_home).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_home));
|
|
|
|
navMenu.findItem(R.id.navigation_trending).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_fire));
|
|
|
|
navMenu.findItem(R.id.navigation_subscriptions).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_folder));
|
2018-03-11 14:51:39 +01:00
|
|
|
navMenu.findItem(R.id.navigation_account).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_user_circle));
|
|
|
|
|
2018-03-03 23:08:18 +01:00
|
|
|
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
|
|
|
|
|
2018-03-11 14:51:39 +01:00
|
|
|
|
|
|
|
// load Video List
|
2018-03-03 02:02:00 +01:00
|
|
|
createList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2018-03-04 01:06:32 +01:00
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.menu_main, menu);
|
|
|
|
|
|
|
|
// Set an icon in the ActionBar
|
2018-03-14 14:33:17 +01:00
|
|
|
menu.findItem(R.id.action_search).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_search)
|
|
|
|
.colorRes(R.color.cardview_light_background)
|
|
|
|
.actionBarSize());
|
|
|
|
|
2018-03-11 14:51:39 +01:00
|
|
|
menu.findItem(R.id.action_settings).setIcon(
|
|
|
|
new IconDrawable(this, FontAwesomeIcons.fa_cog)
|
2018-03-04 01:06:32 +01:00
|
|
|
.colorRes(R.color.cardview_light_background)
|
|
|
|
.actionBarSize());
|
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-04 01:06:32 +01:00
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
// action with ID action_refresh was selected
|
2018-03-14 14:33:17 +01:00
|
|
|
case R.id.action_search:
|
2018-11-10 22:40:27 +01:00
|
|
|
//Toast.makeText(this, "Search Selected", Toast.LENGTH_SHORT).show();
|
|
|
|
onSearchRequested();
|
2018-03-14 14:33:17 +01:00
|
|
|
|
|
|
|
return false;
|
2018-03-11 14:51:39 +01:00
|
|
|
case R.id.action_settings:
|
2018-03-04 01:06:32 +01:00
|
|
|
// Toast.makeText(this, "Login Selected", Toast.LENGTH_SHORT).show();
|
2018-03-11 14:51:39 +01:00
|
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
2018-03-04 01:06:32 +01:00
|
|
|
this.startActivity(intent);
|
|
|
|
|
2018-03-03 02:46:01 +01:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
private void createList() {
|
2018-11-10 22:40:27 +01:00
|
|
|
RecyclerView recyclerView = findViewById(R.id.recyclerView);
|
2018-03-03 23:08:18 +01:00
|
|
|
swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(VideoListActivity.this);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
|
|
|
|
videoAdapter = new VideoAdapter(new ArrayList<>(), VideoListActivity.this);
|
|
|
|
recyclerView.setAdapter(videoAdapter);
|
|
|
|
|
2018-07-01 15:11:26 +02:00
|
|
|
loadVideos(currentStart, count, sort, filter);
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
@Override
|
2018-11-10 22:40:27 +01:00
|
|
|
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
|
2018-03-03 02:02:00 +01:00
|
|
|
super.onScrollStateChanged(recyclerView, newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-10 22:40:27 +01:00
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
if (dy > 0) {
|
|
|
|
// is at end of list?
|
|
|
|
if(!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN)){
|
|
|
|
if (!isLoading) {
|
|
|
|
currentStart = currentStart + count;
|
2018-07-01 15:11:26 +02:00
|
|
|
loadVideos(currentStart, count, sort, filter);
|
2018-03-03 02:02:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2018-03-03 23:08:18 +01:00
|
|
|
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
// Refresh items
|
|
|
|
if (!isLoading) {
|
|
|
|
currentStart = 0;
|
2018-07-01 15:11:26 +02:00
|
|
|
loadVideos(currentStart, count, sort, filter);
|
2018-03-03 23:08:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-03 02:02:00 +01:00
|
|
|
}
|
|
|
|
|
2018-07-01 15:11:26 +02:00
|
|
|
private void loadVideos(int start, int count, String sort, String filter) {
|
2018-03-03 02:02:00 +01:00
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
|
2018-11-07 23:11:19 +01:00
|
|
|
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
|
2018-11-10 22:40:27 +01:00
|
|
|
String nsfw = sharedPref.getBoolean("pref_show_nsfw", true) ? "both" : "false";
|
2018-11-07 23:11:19 +01:00
|
|
|
|
2018-03-11 14:51:39 +01:00
|
|
|
String apiBaseURL = APIUrlHelper.getUrl(this);
|
2018-03-04 01:44:34 +01:00
|
|
|
|
2018-03-04 20:24:32 +01:00
|
|
|
GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL + "/api/v1/").create(GetVideoDataService.class);
|
2018-03-03 01:10:13 +01:00
|
|
|
|
2018-11-07 23:11:19 +01:00
|
|
|
Call<VideoList> call = service.getVideosData(start, count, sort, nsfw);
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
/*Log the URL called*/
|
2018-03-03 23:08:18 +01:00
|
|
|
Log.d("URL Called", call.request().url() + "");
|
2018-11-10 17:48:09 +01:00
|
|
|
// Toast.makeText(VideoListActivity.this, "URL Called: " + call.request().url(), Toast.LENGTH_SHORT).show();
|
2018-03-03 01:10:13 +01:00
|
|
|
|
|
|
|
call.enqueue(new Callback<VideoList>() {
|
|
|
|
@Override
|
2018-03-03 02:46:01 +01:00
|
|
|
public void onResponse(@NonNull Call<VideoList> call, @NonNull Response<VideoList> response) {
|
2018-03-03 23:08:18 +01:00
|
|
|
|
|
|
|
if (currentStart == 0) {
|
|
|
|
videoAdapter.clearData();
|
|
|
|
}
|
|
|
|
|
2018-11-07 23:11:19 +01:00
|
|
|
if (response.body() != null) {
|
|
|
|
videoAdapter.setData(response.body().getVideoArrayList());
|
|
|
|
}
|
2018-03-03 02:02:00 +01:00
|
|
|
isLoading = false;
|
2018-03-03 23:08:18 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-03 02:46:01 +01:00
|
|
|
public void onFailure(@NonNull Call<VideoList> call, @NonNull Throwable t) {
|
2018-03-03 01:10:13 +01:00
|
|
|
Log.wtf("err", t.fillInStackTrace());
|
|
|
|
Toast.makeText(VideoListActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
|
2018-03-03 02:02:00 +01:00
|
|
|
isLoading = false;
|
2018-03-03 23:08:18 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force android to not use SSLv3
|
|
|
|
*
|
|
|
|
* @param callingActivity Activity
|
|
|
|
*/
|
|
|
|
private void updateAndroidSecurityProvider(Activity callingActivity) {
|
|
|
|
try {
|
|
|
|
ProviderInstaller.installIfNeeded(this);
|
|
|
|
} catch (GooglePlayServicesRepairableException e) {
|
|
|
|
// Thrown when Google Play Services is not installed, up-to-date, or enabled
|
|
|
|
// Show dialog to allow users to install, update, or otherwise enable Google Play services.
|
|
|
|
GooglePlayServicesUtil.getErrorDialog(e.getConnectionStatusCode(), callingActivity, 0);
|
|
|
|
} catch (GooglePlayServicesNotAvailableException e) {
|
|
|
|
Log.e("SecurityException", "Google Play Services not available.");
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 02:24:06 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 01:10:13 +01:00
|
|
|
}
|