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

384 lines
18 KiB
Java
Raw Normal View History

2020-06-25 16:57:13 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* 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>. */
2020-06-25 16:57:13 +02:00
2020-09-03 18:56:48 +02:00
import android.annotation.SuppressLint;
2020-09-17 19:01:31 +02:00
import android.app.Activity;
2020-06-27 12:23:03 +02:00
import android.content.Context;
2020-06-28 12:14:14 +02:00
import android.content.Intent;
2020-06-27 12:23:03 +02:00
import android.content.SharedPreferences;
2020-07-21 18:49:13 +02:00
import android.database.sqlite.SQLiteDatabase;
2020-09-18 14:37:34 +02:00
import android.os.Build;
2020-06-25 16:57:13 +02:00
import android.os.Bundle;
2020-06-27 12:23:03 +02:00
import android.view.Menu;
import android.view.MenuItem;
2020-09-13 19:20:04 +02:00
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
2020-06-25 16:57:13 +02:00
2020-06-27 12:23:03 +02:00
import androidx.appcompat.app.AlertDialog;
2020-06-25 16:57:13 +02:00
import androidx.appcompat.app.AppCompatActivity;
2020-06-28 12:14:14 +02:00
import androidx.appcompat.widget.SearchView;
2020-06-25 16:57:13 +02:00
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavInflater;
2020-06-25 16:57:13 +02:00
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
2020-06-25 16:57:13 +02:00
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
2020-06-27 11:21:25 +02:00
import com.google.android.material.bottomnavigation.BottomNavigationView;
2020-06-27 12:23:03 +02:00
import org.jetbrains.annotations.NotNull;
2020-10-03 11:44:00 +02:00
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
2020-09-27 16:33:43 +02:00
import app.fedilab.fedilabtube.client.entities.Error;
2020-09-26 16:46:51 +02:00
import app.fedilab.fedilabtube.client.entities.OauthParams;
2020-09-03 19:08:53 +02:00
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
2020-10-01 17:42:03 +02:00
import app.fedilab.fedilabtube.client.entities.Token;
import app.fedilab.fedilabtube.client.entities.UserMe;
2020-09-26 16:46:51 +02:00
import app.fedilab.fedilabtube.client.entities.WellKnownNodeinfo;
2020-06-27 12:23:03 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-09-18 14:37:34 +02:00
import app.fedilab.fedilabtube.services.RetrieveInfoService;
2020-07-21 18:49:13 +02:00
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
2020-09-13 19:20:04 +02:00
import es.dmoral.toasty.Toasty;
2020-06-27 12:23:03 +02:00
import static app.fedilab.fedilabtube.helper.Helper.academies;
2020-06-27 11:21:25 +02:00
2020-06-25 16:57:13 +02:00
public class MainActivity extends AppCompatActivity {
2020-09-08 10:11:11 +02:00
public static PeertubeInformation peertubeInformation;
2020-09-17 19:01:31 +02:00
public static int PICK_INSTANCE = 5641;
2020-06-25 16:57:13 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2020-09-18 14:37:34 +02:00
2020-06-25 16:57:13 +02:00
BottomNavigationView navView = findViewById(R.id.nav_view);
if (Helper.isLoggedIn(MainActivity.this)) {
navView.inflateMenu(R.menu.bottom_nav_menu_connected);
2020-09-29 17:42:15 +02:00
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
2020-10-01 17:42:03 +02:00
String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
2020-09-29 17:42:15 +02:00
String instance = Helper.getLiveInstance(MainActivity.this);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-10-01 17:42:03 +02:00
Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr);
2020-09-29 17:42:15 +02:00
if (account != null) {
OauthParams oauthParams = new OauthParams();
oauthParams.setGrant_type("refresh_token");
oauthParams.setClient_id(account.getClient_id());
oauthParams.setClient_secret(account.getClient_secret());
oauthParams.setRefresh_token(account.getRefresh_token());
oauthParams.setAccess_token(account.getToken());
new Thread(() -> {
try {
2020-10-01 17:42:03 +02:00
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
2020-10-02 07:03:36 +02:00
if( token == null) {
2020-10-02 19:29:00 +02:00
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, account));
2020-10-01 17:42:03 +02:00
return;
}
UserMe userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
2020-10-02 07:03:36 +02:00
if( userMe != null && userMe.getAccount() != null) {
2020-10-02 19:29:00 +02:00
new AccountDAO(MainActivity.this, db).updateAccount(userMe.getAccount());
2020-10-01 17:42:03 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
2020-10-03 11:44:00 +02:00
//Sync languages from server
List<String> videoLanguageServer = userMe.getVideoLanguages();
Set<String> videoLanguageServerSet = new TreeSet<>(videoLanguageServer);
videoLanguageServerSet.addAll(videoLanguageServer);
Set<String> videoLanguageLocal = sharedpreferences.getStringSet(getString(R.string.set_video_language_choice), null);
if( videoLanguageServerSet.size() > 0 && videoLanguageLocal != null) {
videoLanguageServer.addAll(videoLanguageLocal);
}
editor.putStringSet(getString(R.string.set_video_language_choice), videoLanguageServerSet);
2020-10-01 17:42:03 +02:00
editor.apply();
}
2020-09-29 17:42:15 +02:00
} catch (Error error) {
error.printStackTrace();
}
}).start();
}
} else {
navView.inflateMenu(R.menu.bottom_nav_menu);
}
2020-06-25 16:57:13 +02:00
// 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(
2020-10-03 11:24:26 +02:00
R.id.navigation_discover, R.id.navigation_subscription, R.id.navigation_trending, R.id.navigation_local, 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();
}
2020-09-29 17:42:15 +02:00
2020-09-18 14:37:34 +02:00
startInForeground();
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);
}
2020-06-25 16:57:13 +02:00
}
2020-09-18 14:37:34 +02:00
private void startInForeground() {
Intent notificationIntent = new Intent(this, RetrieveInfoService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(notificationIntent);
} else {
startService(notificationIntent);
}
}
2020-06-27 12:23:03 +02:00
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
2020-06-30 12:00:55 +02:00
2020-06-28 19:11:39 +02:00
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
2020-06-28 12:14:14 +02:00
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();
2020-07-24 08:53:16 +02:00
String search = query.trim();
b.putString("search", search);
2020-06-28 12:14:14 +02:00
intent.putExtras(b);
startActivity(intent);
2020-06-28 19:11:39 +02:00
if (!searchView.isIconified()) {
2020-06-28 12:14:14 +02:00
searchView.setIconified(true);
}
myActionMenuItem.collapseActionView();
return false;
}
2020-06-28 19:11:39 +02:00
2020-06-28 12:14:14 +02:00
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
2020-06-30 17:33:23 +02:00
MenuItem instanceItem = menu.findItem(R.id.action_change_instance);
2020-06-30 12:00:55 +02:00
MenuItem uploadItem = menu.findItem(R.id.action_upload);
2020-06-30 13:33:43 +02:00
MenuItem myVideosItem = menu.findItem(R.id.action_myvideos);
2020-06-30 17:26:20 +02:00
MenuItem playslistItem = menu.findItem(R.id.action_playlist);
2020-09-03 19:08:53 +02:00
MenuItem historyItem = menu.findItem(R.id.action_history);
2020-10-03 11:24:26 +02:00
MenuItem mostLikedItem = menu.findItem(R.id.action_most_liked);
2020-09-19 15:06:24 +02:00
MenuItem settingsItem = menu.findItem(R.id.action_settings);
2020-06-30 13:33:43 +02:00
if (Helper.isLoggedIn(MainActivity.this)) {
2020-06-30 17:33:23 +02:00
instanceItem.setVisible(false);
2020-06-30 12:00:55 +02:00
uploadItem.setVisible(true);
2020-06-30 13:33:43 +02:00
myVideosItem.setVisible(true);
2020-06-30 17:26:20 +02:00
playslistItem.setVisible(true);
2020-09-03 19:08:53 +02:00
historyItem.setVisible(true);
2020-09-19 15:06:24 +02:00
settingsItem.setVisible(false);
2020-10-03 11:24:26 +02:00
mostLikedItem.setVisible(true);
2020-06-30 13:33:43 +02:00
} else {
2020-06-30 17:33:23 +02:00
instanceItem.setVisible(true);
2020-06-30 12:00:55 +02:00
uploadItem.setVisible(false);
2020-06-30 13:33:43 +02:00
myVideosItem.setVisible(false);
2020-06-30 17:26:20 +02:00
playslistItem.setVisible(false);
2020-09-03 19:08:53 +02:00
historyItem.setVisible(false);
2020-09-19 15:06:24 +02:00
settingsItem.setVisible(true);
2020-10-03 11:24:26 +02:00
mostLikedItem.setVisible(false);
2020-06-30 12:00:55 +02:00
}
2020-06-27 12:23:03 +02:00
return true;
}
2020-09-27 16:33:43 +02:00
2020-06-27 12:23:03 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_instance) {
2020-09-13 19:20:04 +02:00
if (BuildConfig.full_instances) {
showRadioButtonDialogFullInstances();
} else {
showRadioButtonDialog();
}
2020-06-27 12:23:03 +02:00
return true;
2020-09-19 15:06:24 +02:00
} else if (item.getItemId() == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_account) {
2020-09-03 19:08:53 +02:00
Intent intent;
if (Helper.isLoggedIn(MainActivity.this)) {
intent = new Intent(MainActivity.this, AccountActivity.class);
} else {
intent = new Intent(MainActivity.this, LoginActivity.class);
}
2020-06-28 19:11:39 +02:00
startActivity(intent);
return true;
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_upload) {
2020-06-30 12:00:55 +02:00
Intent intent = new Intent(MainActivity.this, PeertubeUploadActivity.class);
startActivity(intent);
return true;
2020-06-30 13:33:43 +02:00
} else if (item.getItemId() == R.id.action_myvideos) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
2020-07-03 17:35:28 +02:00
Bundle bundle = new Bundle();
2020-09-26 10:22:11 +02:00
bundle.putSerializable("type", TimelineVM.TimelineType.MY_VIDEOS);
2020-07-03 17:35:28 +02:00
intent.putExtras(bundle);
2020-06-30 13:33:43 +02:00
startActivity(intent);
return true;
2020-09-03 19:08:53 +02:00
} else if (item.getItemId() == R.id.action_history) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
Bundle bundle = new Bundle();
2020-09-26 10:22:11 +02:00
bundle.putSerializable("type", TimelineVM.TimelineType.HISTORY);
2020-09-03 19:08:53 +02:00
intent.putExtras(bundle);
startActivity(intent);
2020-10-03 11:24:26 +02:00
return true;
} else if (item.getItemId() == R.id.action_most_liked) {
Intent intent = new Intent(MainActivity.this, MyVideosActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("type", TimelineVM.TimelineType.MOST_LIKED);
intent.putExtras(bundle);
startActivity(intent);
2020-09-03 19:08:53 +02:00
return true;
2020-07-09 17:57:01 +02:00
} else if (item.getItemId() == R.id.action_playlist) {
2020-06-30 17:26:20 +02:00
Intent intent = new Intent(MainActivity.this, AllPlaylistsActivity.class);
startActivity(intent);
return true;
2020-09-16 14:07:44 +02:00
} else if (item.getItemId() == R.id.action_about) {
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
2020-06-30 12:00:55 +02:00
}
2020-06-27 12:23:03 +02:00
return super.onOptionsItemSelected(item);
}
2020-06-28 19:11:39 +02:00
@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();
}
}
}
2020-06-27 12:23:03 +02:00
2020-09-03 18:56:48 +02:00
@SuppressLint("ApplySharedPref")
2020-06-27 12:23:03 +02:00
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);
2020-07-03 17:04:04 +02:00
String acad = Helper.getLiveInstance(MainActivity.this);
2020-06-27 12:23:03 +02:00
int i = 0;
2020-06-27 19:08:52 +02:00
for (String item : academies) {
2020-09-12 18:26:34 +02:00
if (Helper.getPeertubeUrl(item).compareTo(acad) == 0) {
2020-06-27 12:23:03 +02:00
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();
});
2020-09-12 18:10:52 +02:00
alt_bld.setPositiveButton(R.string.close, (dialog, id) -> dialog.dismiss());
2020-06-27 12:23:03 +02:00
AlertDialog alert = alt_bld.create();
alert.show();
}
2020-09-13 19:20:04 +02:00
@SuppressLint("ApplySharedPref")
private void showRadioButtonDialogFullInstances() {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setTitle(R.string.instance_choice);
String instance = Helper.getLiveInstance(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alt_bld.setView(input);
input.setText(instance);
alt_bld.setPositiveButton(R.string.validate,
(dialog, which) -> new Thread(() -> {
try {
String newInstance = input.getText().toString().trim();
2020-09-29 17:42:15 +02:00
WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(MainActivity.this, newInstance, null).getNodeInfo();
2020-09-26 16:46:51 +02:00
if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) {
2020-09-13 19:20:04 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, newInstance);
editor.commit();
runOnUiThread(() -> {
dialog.dismiss();
recreate();
});
} else {
runOnUiThread(() -> Toasty.error(MainActivity.this, getString(R.string.not_valide_instance), Toast.LENGTH_LONG).show());
}
} catch (Exception e) {
e.printStackTrace();
}
}).start());
alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
2020-09-16 17:27:07 +02:00
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
Intent intent = new Intent(MainActivity.this, InstancePickerActivity.class);
2020-09-17 19:01:31 +02:00
startActivityForResult(intent, PICK_INSTANCE);
2020-09-16 17:27:07 +02:00
});
2020-09-13 19:20:04 +02:00
AlertDialog alert = alt_bld.create();
alert.show();
}
2020-09-17 19:01:31 +02:00
@SuppressLint("ApplySharedPref")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_INSTANCE && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, String.valueOf(data.getData()));
editor.commit();
recreate();
}
}
}
2020-06-25 16:57:13 +02:00
}