TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java

248 lines
11 KiB
Java

package app.fedilab.fedilabtube;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavInflater;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedHashMap;
import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import static app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask.peertubeInformation;
import static app.fedilab.fedilabtube.helper.Helper.academies;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
if (Helper.isLoggedIn(MainActivity.this)) {
navView.inflateMenu(R.menu.bottom_nav_menu_connected);
} else {
navView.inflateMenu(R.menu.bottom_nav_menu);
}
try {
peertubeInformation = new PeertubeInformation();
peertubeInformation.setCategories(new LinkedHashMap<>());
peertubeInformation.setLanguages(new LinkedHashMap<>());
peertubeInformation.setLicences(new LinkedHashMap<>());
peertubeInformation.setPrivacies(new LinkedHashMap<>());
peertubeInformation.setPlaylistPrivacies(new LinkedHashMap<>());
peertubeInformation.setTranslations(new LinkedHashMap<>());
new RetrievePeertubeInformationAsyncTask(MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (Exception ignored) {
}
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration;
//Bottom menu won't be the same if the user is authenticated
//When the user is authenticated, the subscription entry will be added and the local one removed.
if (Helper.isLoggedIn(MainActivity.this)) {
appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_discover, R.id.navigation_subscription, R.id.navigation_trending, R.id.navigation_most_liked, R.id.navigation_recently_added)
.build();
} else {
appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_discover, R.id.navigation_trending, R.id.navigation_most_liked, R.id.navigation_recently_added, R.id.navigation_home)
.build();
}
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
if (navHostFragment != null) {
NavInflater inflater = navHostFragment.getNavController().getNavInflater();
NavGraph graph;
//the menu is inflated for authenticated or not authenticated account
if (Helper.isLoggedIn(MainActivity.this)) {
graph = inflater.inflate(R.navigation.mobile_navigation_connected);
} else {
graph = inflater.inflate(R.navigation.mobile_navigation);
}
navHostFragment.getNavController().setGraph(graph);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(MainActivity.this, SearchActivity.class);
Bundle b = new Bundle();
String search = query.trim();
b.putString("search", search);
intent.putExtras(b);
startActivity(intent);
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
MenuItem instanceItem = menu.findItem(R.id.action_change_instance);
MenuItem uploadItem = menu.findItem(R.id.action_upload);
MenuItem myVideosItem = menu.findItem(R.id.action_myvideos);
MenuItem playslistItem = menu.findItem(R.id.action_playlist);
MenuItem historyItem = menu.findItem(R.id.action_history);
if (Helper.isLoggedIn(MainActivity.this)) {
instanceItem.setVisible(false);
uploadItem.setVisible(true);
myVideosItem.setVisible(true);
playslistItem.setVisible(true);
historyItem.setVisible(true);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(MainActivity.this);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(MainActivity.this, db).getUniqAccount(userId, instance);
if (account != null) {
new Thread(() -> new PeertubeAPI(MainActivity.this).refreshToken(account.getToken(), account.getInstance())).start();
}
} else {
instanceItem.setVisible(true);
uploadItem.setVisible(false);
myVideosItem.setVisible(false);
playslistItem.setVisible(false);
historyItem.setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_instance) {
showRadioButtonDialog();
return true;
} else if (item.getItemId() == R.id.action_account) {
Intent intent;
if (Helper.isLoggedIn(MainActivity.this)) {
intent = new Intent(MainActivity.this, AccountActivity.class);
} else {
intent = new Intent(MainActivity.this, LoginActivity.class);
}
startActivity(intent);
return true;
} else if (item.getItemId() == R.id.action_upload) {
Intent intent = new Intent(MainActivity.this, PeertubeUploadActivity.class);
startActivity(intent);
return true;
} else if (item.getItemId() == R.id.action_myvideos) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.MYVIDEOS);
intent.putExtras(bundle);
startActivity(intent);
return true;
} else if (item.getItemId() == R.id.action_history) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.PEERTUBE_HISTORY);
intent.putExtras(bundle);
startActivity(intent);
return true;
} else if (item.getItemId() == R.id.action_playlist) {
Intent intent = new Intent(MainActivity.this, AllPlaylistsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent == null)
return;
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(Helper.INTENT_ACTION)) {
if (extras.getInt(Helper.INTENT_ACTION) == Helper.ADD_USER_INTENT) {
recreate();
}
}
}
@SuppressLint("ApplySharedPref")
private void showRadioButtonDialog() {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setTitle(R.string.instance_choice);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String acad = Helper.getLiveInstance(MainActivity.this);
int i = 0;
for (String item : academies) {
if (item.compareTo(acad) == 0) {
break;
}
i++;
}
alt_bld.setSingleChoiceItems(academies, i, (dialog, item) -> {
String newInstance = academies[item];
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, newInstance);
editor.commit();
dialog.dismiss();
recreate();
});
AlertDialog alert = alt_bld.create();
alert.show();
}
}