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-12-05 17:28:25 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
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-12-05 17:28:25 +01:00
|
|
|
import android.content.IntentFilter;
|
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-11-07 10:38:30 +01:00
|
|
|
import android.net.Uri;
|
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-10-13 14:30:08 +02:00
|
|
|
import android.os.Handler;
|
2020-12-02 18:05:35 +01:00
|
|
|
import android.os.Looper;
|
2020-06-27 12:23:03 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2020-11-09 18:48:10 +01:00
|
|
|
import android.view.View;
|
2020-09-13 19:20:04 +02:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.LinearLayout;
|
2020-11-09 18:48:10 +01:00
|
|
|
import android.widget.TextView;
|
2020-09-13 19:20:04 +02:00
|
|
|
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-10-03 16:22:19 +02:00
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2020-10-08 16:21:14 +02:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.fragment.app.FragmentManager;
|
2020-11-27 18:36:31 +01:00
|
|
|
import androidx.fragment.app.FragmentStatePagerAdapter;
|
2020-12-05 17:28:25 +01:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2020-11-27 18:36:31 +01:00
|
|
|
import androidx.viewpager.widget.PagerAdapter;
|
|
|
|
import androidx.viewpager.widget.ViewPager;
|
2020-06-25 16:57:13 +02:00
|
|
|
|
2020-06-27 11:21:25 +02:00
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
2020-10-10 15:33:25 +02:00
|
|
|
import com.kobakei.ratethisapp.RateThisApp;
|
2020-06-27 11:21:25 +02:00
|
|
|
|
2020-06-27 12:23:03 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2020-12-02 18:05:35 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.NetworkInterface;
|
2020-12-05 11:27:27 +01:00
|
|
|
import java.util.ArrayList;
|
2020-12-02 18:05:35 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Enumeration;
|
2020-10-24 16:36:49 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2020-10-03 11:44:00 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.TreeSet;
|
2020-11-07 10:38:30 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2020-10-03 11:44:00 +02:00
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
|
|
|
|
import app.fedilab.fedilabtube.client.data.AccountData.Account;
|
2020-11-10 18:44:48 +01:00
|
|
|
import app.fedilab.fedilabtube.client.data.InstanceData;
|
2020-12-09 15:45:54 +01:00
|
|
|
import app.fedilab.fedilabtube.client.data.VideoData;
|
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-10-30 14:45:37 +01:00
|
|
|
import app.fedilab.fedilabtube.client.entities.UserSettings;
|
2020-09-26 16:46:51 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.WellKnownNodeinfo;
|
2020-11-27 18:36:31 +01:00
|
|
|
import app.fedilab.fedilabtube.databinding.ActivityMainBinding;
|
2020-10-08 16:21:14 +02:00
|
|
|
import app.fedilab.fedilabtube.fragment.DisplayOverviewFragment;
|
|
|
|
import app.fedilab.fedilabtube.fragment.DisplayVideosFragment;
|
2020-06-27 12:23:03 +02:00
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
2020-12-01 18:35:54 +01:00
|
|
|
import app.fedilab.fedilabtube.helper.HelperInstance;
|
2020-11-13 18:45:55 +01:00
|
|
|
import app.fedilab.fedilabtube.helper.PlaylistExportHelper;
|
2020-11-10 18:44:48 +01:00
|
|
|
import app.fedilab.fedilabtube.helper.SwitchAccountHelper;
|
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-11-10 18:44:48 +01:00
|
|
|
import app.fedilab.fedilabtube.sqlite.StoredInstanceDAO;
|
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-12-05 11:27:27 +01:00
|
|
|
import su.litvak.chromecast.api.v2.ChromeCast;
|
2020-12-02 18:05:35 +01:00
|
|
|
import su.litvak.chromecast.api.v2.ChromeCasts;
|
2020-12-05 11:27:27 +01:00
|
|
|
import su.litvak.chromecast.api.v2.ChromeCastsListener;
|
2020-12-09 15:45:54 +01:00
|
|
|
import su.litvak.chromecast.api.v2.MediaStatus;
|
2020-06-27 12:23:03 +02:00
|
|
|
|
2020-11-10 18:44:48 +01:00
|
|
|
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.NORMAL;
|
|
|
|
import static app.fedilab.fedilabtube.MainActivity.TypeOfConnection.SURFING;
|
2020-11-20 08:58:28 +01:00
|
|
|
import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
|
2020-12-01 18:35:54 +01:00
|
|
|
import static app.fedilab.fedilabtube.helper.HelperAcadInstance.academies;
|
2020-06-27 11:21:25 +02:00
|
|
|
|
2020-12-05 11:27:27 +01:00
|
|
|
public class MainActivity extends AppCompatActivity implements ChromeCastsListener {
|
2020-06-25 16:57:13 +02:00
|
|
|
|
2020-09-08 10:11:11 +02:00
|
|
|
|
2020-09-17 19:01:31 +02:00
|
|
|
public static int PICK_INSTANCE = 5641;
|
2020-11-10 18:44:48 +01:00
|
|
|
public static int PICK_INSTANCE_SURF = 5642;
|
2020-11-11 10:34:28 +01:00
|
|
|
public static UserMe userMe;
|
2020-11-24 17:32:18 +01:00
|
|
|
public static InstanceData.InstanceConfig instanceConfig;
|
2020-11-13 18:45:55 +01:00
|
|
|
public static TypeOfConnection typeOfConnection;
|
2020-10-08 16:21:14 +02:00
|
|
|
private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment;
|
|
|
|
private DisplayOverviewFragment overviewFragment;
|
2020-11-27 18:36:31 +01:00
|
|
|
private ActivityMainBinding binding;
|
2020-12-05 17:28:25 +01:00
|
|
|
private BroadcastReceiver manage_chromecast;
|
2020-12-09 15:45:54 +01:00
|
|
|
public static List<ChromeCast> chromeCasts;
|
|
|
|
public static ChromeCast chromeCast;
|
|
|
|
private VideoData.Video castedTube;
|
2020-09-17 19:01:31 +02:00
|
|
|
|
2020-12-09 18:26:11 +01:00
|
|
|
public static boolean chromecastActivated = false;
|
|
|
|
|
2020-11-11 10:34:28 +01:00
|
|
|
@SuppressLint("ApplySharedPref")
|
|
|
|
public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) {
|
|
|
|
final SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
AlertDialog.Builder alt_bld = new AlertDialog.Builder(activity);
|
|
|
|
alt_bld.setTitle(R.string.instance_choice);
|
2020-12-01 18:35:54 +01:00
|
|
|
String instance = HelperInstance.getLiveInstance(activity);
|
2020-11-11 10:34:28 +01:00
|
|
|
final EditText input = new EditText(activity);
|
|
|
|
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();
|
|
|
|
WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(activity, newInstance, null).getNodeInfo();
|
|
|
|
if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) {
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_INSTANCE, newInstance);
|
|
|
|
editor.commit();
|
|
|
|
if (storeInDb) {
|
|
|
|
newInstance = newInstance.trim().toLowerCase();
|
|
|
|
InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance();
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance);
|
|
|
|
activity.runOnUiThread(() -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
Helper.logoutNoRemoval(activity);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
activity.runOnUiThread(() -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
Intent intent = new Intent(activity, MainActivity.class);
|
|
|
|
activity.startActivity(intent);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
activity.runOnUiThread(() -> Toasty.error(activity, activity.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());
|
|
|
|
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
|
|
|
|
Intent intent = new Intent(activity, InstancePickerActivity.class);
|
|
|
|
if (storeInDb) {
|
|
|
|
activity.startActivityForResult(intent, PICK_INSTANCE_SURF);
|
|
|
|
} else {
|
|
|
|
activity.startActivityForResult(intent, PICK_INSTANCE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
AlertDialog alert = alt_bld.create();
|
|
|
|
alert.show();
|
|
|
|
}
|
2020-11-09 18:48:10 +01:00
|
|
|
|
|
|
|
private void setTitleCustom(int titleRId) {
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
TextView mTitle = toolbar.findViewById(R.id.toolbar_title);
|
2020-11-27 14:06:42 +01:00
|
|
|
if (mTitle != null) {
|
|
|
|
mTitle.setText(getString(titleRId));
|
|
|
|
}
|
2020-11-09 18:48:10 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 18:36:31 +01:00
|
|
|
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
|
|
|
|
= item -> {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
if (itemId == R.id.navigation_discover) {
|
|
|
|
setTitleCustom(R.string.title_discover);
|
|
|
|
binding.viewpager.setCurrentItem(0);
|
|
|
|
} else if (itemId == R.id.navigation_subscription) {
|
|
|
|
binding.viewpager.setCurrentItem(1);
|
|
|
|
setTitleCustom(R.string.subscriptions);
|
|
|
|
} else if (itemId == R.id.navigation_trending) {
|
|
|
|
setTitleCustom(R.string.title_trending);
|
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
binding.viewpager.setCurrentItem(2);
|
|
|
|
} else {
|
|
|
|
binding.viewpager.setCurrentItem(1);
|
|
|
|
}
|
|
|
|
} else if (itemId == R.id.navigation_most_liked) {
|
|
|
|
setTitleCustom(R.string.title_most_liked);
|
|
|
|
binding.viewpager.setCurrentItem(2);
|
|
|
|
} else if (itemId == R.id.navigation_recently_added) {
|
|
|
|
setTitleCustom(R.string.title_recently_added);
|
|
|
|
binding.viewpager.setCurrentItem(3);
|
|
|
|
} else if (itemId == R.id.navigation_local) {
|
|
|
|
setTitleCustom(R.string.title_local);
|
|
|
|
binding.viewpager.setCurrentItem(4);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-06-25 16:57:13 +02:00
|
|
|
@Override
|
2020-12-05 11:27:27 +01:00
|
|
|
public void newChromeCastDiscovered(ChromeCast chromeCast) {
|
|
|
|
if (chromeCasts == null) {
|
|
|
|
chromeCasts = new ArrayList<>();
|
|
|
|
chromeCasts.add(chromeCast);
|
|
|
|
} else {
|
|
|
|
boolean canBeAdded = true;
|
|
|
|
for (ChromeCast cast : chromeCasts) {
|
|
|
|
if (cast.getName().compareTo(chromeCast.getName()) == 0) {
|
|
|
|
canBeAdded = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canBeAdded) {
|
|
|
|
chromeCasts.add(chromeCast);
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 18:26:11 +01:00
|
|
|
try {
|
|
|
|
if (chromeCast.isAppRunning(Helper.CAST_ID) && chromeCast.getMediaStatus() != null && chromeCast.getMediaStatus().playerState != null) {
|
|
|
|
if (binding.castInfo.getVisibility() == View.GONE) {
|
|
|
|
binding.castInfo.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-12-09 15:45:54 +01:00
|
|
|
|
2020-12-05 11:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void chromeCastRemoved(ChromeCast chromeCast) {
|
|
|
|
|
|
|
|
}
|
2020-09-18 14:37:34 +02:00
|
|
|
|
2020-12-05 11:27:27 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
binding = null;
|
|
|
|
ChromeCasts.unregisterListener(this);
|
2020-12-05 17:28:25 +01:00
|
|
|
if (manage_chromecast != null) {
|
|
|
|
LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(manage_chromecast);
|
2020-12-09 15:45:54 +01:00
|
|
|
|
|
|
|
new Thread(() -> {
|
|
|
|
if (chromeCasts != null && chromeCasts.size() > 0) {
|
|
|
|
for (ChromeCast cast : chromeCasts) {
|
|
|
|
try {
|
2020-12-09 18:26:11 +01:00
|
|
|
cast.stopApp();
|
2020-12-09 15:45:54 +01:00
|
|
|
cast.disconnect();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-12-05 17:28:25 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-09 15:45:54 +01:00
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
if (chromeCasts != null) {
|
2020-12-05 17:28:25 +01:00
|
|
|
chromeCasts = null;
|
|
|
|
}
|
2020-12-09 15:45:54 +01:00
|
|
|
if (chromeCast != null) {
|
|
|
|
chromeCast = null;
|
|
|
|
}
|
2020-12-05 11:27:27 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 17:28:25 +01:00
|
|
|
//Method for discovering cast devices
|
2020-12-05 11:27:27 +01:00
|
|
|
public void discoverCast() {
|
2020-12-02 18:05:35 +01:00
|
|
|
new Thread(() -> {
|
2020-12-05 11:27:27 +01:00
|
|
|
if (chromeCasts != null) {
|
|
|
|
for (ChromeCast cast : chromeCasts) {
|
|
|
|
try {
|
|
|
|
cast.disconnect();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chromeCasts = null;
|
|
|
|
}
|
|
|
|
chromeCasts = new ArrayList<>();
|
2020-12-02 18:05:35 +01:00
|
|
|
try {
|
|
|
|
List<NetworkInterface> interfaces;
|
|
|
|
interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
|
|
|
for (NetworkInterface ni : interfaces) {
|
|
|
|
if ((!ni.isLoopback()) && ni.isUp() && (ni.getName().equals("wlan0"))) {
|
|
|
|
Enumeration<InetAddress> inetAddressEnumeration = ni.getInetAddresses();
|
|
|
|
while (inetAddressEnumeration.hasMoreElements()) {
|
|
|
|
InetAddress inetAddress = inetAddressEnumeration.nextElement();
|
|
|
|
ChromeCasts.restartDiscovery(inetAddress);
|
|
|
|
int tryFind = 0;
|
2020-12-05 11:27:27 +01:00
|
|
|
while (ChromeCasts.get().isEmpty() && tryFind < 5) {
|
2020-12-02 18:05:35 +01:00
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
tryFind++;
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 17:28:25 +01:00
|
|
|
ChromeCasts.stopDiscovery();
|
2020-12-02 18:05:35 +01:00
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = this::invalidateOptionsMenu;
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2020-12-05 11:27:27 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 17:28:25 +01:00
|
|
|
|
2020-12-05 11:27:27 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
binding = ActivityMainBinding.inflate(getLayoutInflater());
|
|
|
|
View view = binding.getRoot();
|
|
|
|
setContentView(view);
|
|
|
|
ChromeCastsListener chromeCastsListener = this;
|
|
|
|
ChromeCasts.registerListener(chromeCastsListener);
|
2020-12-05 17:28:25 +01:00
|
|
|
|
2020-10-03 16:22:19 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
2020-09-18 14:37:34 +02:00
|
|
|
|
2020-11-10 18:44:48 +01:00
|
|
|
typeOfConnection = TypeOfConnection.UNKNOWN;
|
2020-11-09 18:48:10 +01:00
|
|
|
|
2020-06-25 16:57:13 +02:00
|
|
|
BottomNavigationView navView = findViewById(R.id.nav_view);
|
2020-10-08 16:21:14 +02:00
|
|
|
|
|
|
|
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
|
|
|
|
|
2020-11-11 10:34:28 +01:00
|
|
|
if (getSupportActionBar() != null) {
|
2020-11-09 18:48:10 +01:00
|
|
|
getSupportActionBar().setDisplayShowTitleEnabled(false);
|
|
|
|
}
|
2020-11-10 18:44:48 +01:00
|
|
|
checkIfConnectedUsers();
|
2020-11-09 18:48:10 +01:00
|
|
|
|
2020-10-08 16:21:14 +02:00
|
|
|
recentFragment = new DisplayVideosFragment();
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.RECENT);
|
|
|
|
recentFragment.setArguments(bundle);
|
|
|
|
|
|
|
|
locaFragment = new DisplayVideosFragment();
|
|
|
|
bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.LOCAL);
|
|
|
|
locaFragment.setArguments(bundle);
|
|
|
|
|
|
|
|
trendingFragment = new DisplayVideosFragment();
|
|
|
|
bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.TRENDING);
|
|
|
|
trendingFragment.setArguments(bundle);
|
|
|
|
|
|
|
|
subscriptionFragment = new DisplayVideosFragment();
|
|
|
|
bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.SUBSCRIBTIONS);
|
|
|
|
subscriptionFragment.setArguments(bundle);
|
|
|
|
|
|
|
|
mostLikedFragment = new DisplayVideosFragment();
|
|
|
|
bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.MOST_LIKED);
|
|
|
|
mostLikedFragment.setArguments(bundle);
|
|
|
|
|
|
|
|
overviewFragment = new DisplayOverviewFragment();
|
2020-11-27 18:36:31 +01:00
|
|
|
if (!Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
|
|
|
|
binding.viewpager.setAdapter(mPagerAdapter);
|
2020-10-09 10:19:46 +02:00
|
|
|
}
|
2020-11-27 18:36:31 +01:00
|
|
|
binding.viewpager.setOffscreenPageLimit(5);
|
|
|
|
|
|
|
|
|
|
|
|
binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
|
|
|
MenuItem item = binding.navView.getMenu().getItem(position);
|
|
|
|
binding.navView.setSelectedItemId(item.getItemId());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int state) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
2020-11-05 17:38:57 +01:00
|
|
|
|
2020-10-08 16:21:14 +02:00
|
|
|
|
2020-10-17 18:50:20 +02:00
|
|
|
toolbar.setOnClickListener(v -> {
|
2020-12-01 16:09:14 +01:00
|
|
|
if (binding.viewpager.getAdapter() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-27 18:36:31 +01:00
|
|
|
if (binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem()) instanceof DisplayVideosFragment) {
|
|
|
|
((DisplayVideosFragment) binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem())).scrollToTop();
|
|
|
|
} else if (binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem()) instanceof DisplayOverviewFragment) {
|
|
|
|
((DisplayOverviewFragment) binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem())).scrollToTop();
|
2020-10-08 18:30:47 +02:00
|
|
|
}
|
|
|
|
});
|
2020-10-08 16:21:14 +02:00
|
|
|
|
2020-11-09 18:48:10 +01:00
|
|
|
setTitleCustom(R.string.title_discover);
|
2020-10-08 18:35:35 +02:00
|
|
|
|
2020-09-03 18:57:26 +02:00
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
2020-11-09 18:48:10 +01:00
|
|
|
|
2020-09-03 18:57:26 +02:00
|
|
|
navView.inflateMenu(R.menu.bottom_nav_menu_connected);
|
2020-11-28 10:33:13 +01:00
|
|
|
refreshToken();
|
2020-09-29 17:42:15 +02:00
|
|
|
|
2020-09-03 18:57:26 +02:00
|
|
|
} else {
|
|
|
|
navView.inflateMenu(R.menu.bottom_nav_menu);
|
|
|
|
}
|
2020-10-24 16:36:49 +02:00
|
|
|
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<>());
|
2020-09-18 14:37:34 +02:00
|
|
|
startInForeground();
|
2020-06-25 16:57:13 +02:00
|
|
|
|
2020-10-17 18:50:20 +02:00
|
|
|
if (BuildConfig.google_restriction && BuildConfig.full_instances) {
|
2020-10-10 15:33:25 +02:00
|
|
|
RateThisApp.onCreate(this);
|
|
|
|
RateThisApp.showRateDialogIfNeeded(this);
|
|
|
|
}
|
2020-11-13 18:45:55 +01:00
|
|
|
if (!BuildConfig.full_instances) {
|
|
|
|
PlaylistExportHelper.manageIntentUrl(MainActivity.this, getIntent());
|
|
|
|
}
|
2020-12-05 17:28:25 +01:00
|
|
|
|
2020-12-09 18:26:11 +01:00
|
|
|
binding.castClose.setOnClickListener(v -> {
|
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_CAST_SETTINGS);
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putInt("displayed", 0);
|
|
|
|
intentBC.putExtras(b);
|
|
|
|
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intentBC);
|
|
|
|
});
|
2020-12-09 15:45:54 +01:00
|
|
|
|
|
|
|
binding.castTogglePlay.setOnClickListener(v -> {
|
|
|
|
if (chromeCast != null) {
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = () -> binding.castTogglePlay.setVisibility(View.GONE);
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
int icon = -1;
|
|
|
|
if (chromeCast.getMediaStatus().playerState == MediaStatus.PlayerState.PLAYING) {
|
|
|
|
chromeCast.pause();
|
|
|
|
icon = R.drawable.ic_baseline_play_arrow_32;
|
|
|
|
} else if (chromeCast.getMediaStatus().playerState == MediaStatus.PlayerState.PAUSED) {
|
|
|
|
chromeCast.play();
|
|
|
|
icon = R.drawable.ic_baseline_pause_32;
|
|
|
|
}
|
|
|
|
if (icon != -1) {
|
|
|
|
int finalIcon = icon;
|
|
|
|
myRunnable = () -> binding.castTogglePlay.setImageResource(finalIcon);
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
}
|
|
|
|
myRunnable = () -> binding.castTogglePlay.setVisibility(View.VISIBLE);
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
});
|
2020-12-05 17:28:25 +01:00
|
|
|
manage_chromecast = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
Bundle b = intent.getExtras();
|
|
|
|
assert b != null;
|
2020-12-09 15:45:54 +01:00
|
|
|
int state = b.getInt("state_asked", -1);
|
|
|
|
int displayed = b.getInt("displayed", -1);
|
|
|
|
castedTube = b.getParcelable("castedTube");
|
|
|
|
|
|
|
|
if (state == 1) {
|
2020-12-05 17:28:25 +01:00
|
|
|
discoverCast();
|
2020-12-09 15:45:54 +01:00
|
|
|
} else if (state == 0) {
|
2020-12-05 17:28:25 +01:00
|
|
|
new Thread(() -> {
|
2020-12-09 15:45:54 +01:00
|
|
|
try {
|
|
|
|
if (chromeCast != null) {
|
2020-12-09 18:26:11 +01:00
|
|
|
chromeCast.stopApp();
|
2020-12-09 15:45:54 +01:00
|
|
|
chromeCast.disconnect();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
if (displayed == 1) {
|
2020-12-09 18:26:11 +01:00
|
|
|
chromecastActivated = true;
|
2020-12-09 15:45:54 +01:00
|
|
|
if (castedTube != null) {
|
|
|
|
binding.castInfo.setVisibility(View.VISIBLE);
|
|
|
|
Helper.loadGiF(MainActivity.this, castedTube.getThumbnailPath(), binding.castView);
|
|
|
|
binding.castTitle.setText(castedTube.getTitle());
|
|
|
|
binding.castDescription.setText(castedTube.getDescription());
|
|
|
|
}
|
|
|
|
} else if (displayed == 0) {
|
2020-12-09 18:26:11 +01:00
|
|
|
chromecastActivated = false;
|
|
|
|
binding.castInfo.setVisibility(View.GONE);
|
2020-12-09 15:45:54 +01:00
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
if (chromeCast != null) {
|
2020-12-09 18:26:11 +01:00
|
|
|
chromeCast.stopApp();
|
2020-12-05 17:28:25 +01:00
|
|
|
}
|
2020-12-09 15:45:54 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2020-12-05 17:28:25 +01:00
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(manage_chromecast, new IntentFilter(Helper.RECEIVE_CAST_SETTINGS));
|
2020-12-09 15:45:54 +01:00
|
|
|
int search_cast = sharedpreferences.getInt(getString(R.string.set_cast_choice), 0);
|
|
|
|
if (search_cast == 1) {
|
2020-12-05 17:28:25 +01:00
|
|
|
discoverCast();
|
|
|
|
}
|
2020-10-08 16:21:14 +02:00
|
|
|
}
|
2020-09-18 14:37:34 +02:00
|
|
|
|
2020-11-27 18:36:31 +01:00
|
|
|
public DisplayVideosFragment getSubscriptionFragment() {
|
|
|
|
return subscriptionFragment;
|
|
|
|
}
|
|
|
|
|
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-11-28 10:33:13 +01:00
|
|
|
private void refreshToken() {
|
|
|
|
new Thread(() -> {
|
|
|
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
2020-12-01 18:35:54 +01:00
|
|
|
String instance = HelperInstance.getLiveInstance(MainActivity.this);
|
2020-11-28 10:33:13 +01:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
String instanceShar = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
|
|
|
|
String userIdShar = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr);
|
|
|
|
if (account == null) {
|
|
|
|
account = new AccountDAO(MainActivity.this, db).getAccountByIdInstance(userIdShar, instanceShar);
|
|
|
|
}
|
|
|
|
if (account != null) {
|
|
|
|
Account finalAccount = account;
|
|
|
|
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());
|
|
|
|
try {
|
|
|
|
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
|
|
|
|
if (token == null && Helper.instanceOnline(instance)) {
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
|
|
|
|
alt_bld.setTitle(R.string.refresh_token_failed);
|
|
|
|
alt_bld.setMessage(R.string.refresh_token_failed_message);
|
|
|
|
alt_bld.setNegativeButton(R.string.action_logout, (dialog, id) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
Helper.logoutCurrentUser(MainActivity.this, finalAccount);
|
|
|
|
});
|
|
|
|
alt_bld.setPositiveButton(R.string._retry, (dialog, id) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
refreshToken();
|
|
|
|
});
|
|
|
|
AlertDialog alert = alt_bld.create();
|
|
|
|
alert.show();
|
|
|
|
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
} else if (token == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
//To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed.
|
|
|
|
new Handler().post(() -> {
|
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
|
|
|
|
binding.viewpager.setAdapter(mPagerAdapter);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
|
|
|
|
if (userMe != null && userMe.getAccount() != null) {
|
|
|
|
new AccountDAO(MainActivity.this, db).updateAccount(userMe.getAccount());
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_ID, account.getId());
|
|
|
|
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
|
|
|
|
editor.putBoolean(getString(R.string.set_autoplay_choice), userMe.isAutoPlayVideo());
|
|
|
|
editor.putBoolean(getString(R.string.set_store_in_history), userMe.isVideosHistoryEnabled());
|
|
|
|
editor.putBoolean(getString(R.string.set_autoplay_next_video_choice), userMe.isAutoPlayNextVideo());
|
|
|
|
editor.putString(getString(R.string.set_video_sensitive_choice), userMe.getNsfwPolicy());
|
|
|
|
//Sync languages from server
|
|
|
|
List<String> videoLanguageServer = userMe.getVideoLanguages();
|
|
|
|
if (videoLanguageServer != null) {
|
|
|
|
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);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
instanceConfig = new RetrofitPeertubeAPI(MainActivity.this).getConfigInstance();
|
|
|
|
} catch (Error error) {
|
|
|
|
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount));
|
|
|
|
error.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-06 10:46:18 +01:00
|
|
|
Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/watch/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})(\\?start=(\\d+[hH])?(\\d+[mM])?(\\d+[sS])?)?$");
|
2020-11-07 10:38:30 +01:00
|
|
|
Matcher matcherLink = link.matcher(query.trim());
|
|
|
|
if (matcherLink.find()) {
|
2020-11-11 10:34:28 +01:00
|
|
|
Intent intent = new Intent(MainActivity.this, PeertubeActivity.class);
|
|
|
|
intent.setData(Uri.parse(query.trim()));
|
|
|
|
startActivity(intent);
|
|
|
|
myActionMenuItem.collapseActionView();
|
|
|
|
return false;
|
2020-11-07 10:38:30 +01:00
|
|
|
}
|
2020-06-28 12:14:14 +02:00
|
|
|
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-11-10 18:44:48 +01:00
|
|
|
|
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-10-09 10:19:46 +02:00
|
|
|
MenuItem sepiaSearchItem = menu.findItem(R.id.action_sepia_search);
|
2020-10-30 14:45:37 +01:00
|
|
|
MenuItem incognitoItem = menu.findItem(R.id.action_incognito);
|
2020-11-10 18:44:48 +01:00
|
|
|
MenuItem instanceItem = menu.findItem(R.id.action_change_instance);
|
|
|
|
MenuItem accountItem = menu.findItem(R.id.action_account);
|
|
|
|
|
2020-12-01 16:09:14 +01:00
|
|
|
|
|
|
|
if (BuildConfig.surfing_mode && ((Helper.isLoggedIn(MainActivity.this) && typeOfConnection == NORMAL) || typeOfConnection == SURFING)) {
|
|
|
|
binding.instances.setVisibility(View.VISIBLE);
|
|
|
|
binding.instances.setOnClickListener(null);
|
|
|
|
binding.instances.setOnClickListener(v -> {
|
2020-11-10 18:44:48 +01:00
|
|
|
Intent intent = new Intent(MainActivity.this, ManageInstancesActivity.class);
|
|
|
|
startActivity(intent);
|
2020-11-11 10:34:28 +01:00
|
|
|
overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
|
2020-11-10 18:44:48 +01:00
|
|
|
});
|
2020-11-11 10:34:28 +01:00
|
|
|
} else {
|
2020-12-01 16:09:14 +01:00
|
|
|
binding.instances.setVisibility(View.GONE);
|
2020-11-10 18:44:48 +01:00
|
|
|
}
|
2020-11-11 10:34:28 +01:00
|
|
|
switch (typeOfConnection) {
|
2020-11-10 18:44:48 +01:00
|
|
|
case UNKNOWN:
|
|
|
|
instanceItem.setVisible(false);
|
|
|
|
accountItem.setVisible(false);
|
|
|
|
uploadItem.setVisible(false);
|
|
|
|
myVideosItem.setVisible(false);
|
|
|
|
playslistItem.setVisible(false);
|
|
|
|
historyItem.setVisible(false);
|
|
|
|
settingsItem.setVisible(false);
|
|
|
|
mostLikedItem.setVisible(false);
|
|
|
|
incognitoItem.setVisible(false);
|
|
|
|
break;
|
|
|
|
case NORMAL:
|
|
|
|
accountItem.setVisible(true);
|
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
instanceItem.setVisible(false);
|
|
|
|
uploadItem.setVisible(true);
|
|
|
|
myVideosItem.setVisible(true);
|
|
|
|
playslistItem.setVisible(true);
|
|
|
|
historyItem.setVisible(true);
|
|
|
|
settingsItem.setVisible(false);
|
|
|
|
mostLikedItem.setVisible(true);
|
|
|
|
incognitoItem.setVisible(true);
|
|
|
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean checked = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true);
|
|
|
|
incognitoItem.setChecked(checked);
|
|
|
|
} else {
|
|
|
|
instanceItem.setVisible(true);
|
|
|
|
uploadItem.setVisible(false);
|
|
|
|
myVideosItem.setVisible(false);
|
2020-11-14 12:19:37 +01:00
|
|
|
playslistItem.setVisible(!BuildConfig.full_instances);
|
2020-11-10 18:44:48 +01:00
|
|
|
historyItem.setVisible(false);
|
|
|
|
settingsItem.setVisible(true);
|
|
|
|
mostLikedItem.setVisible(false);
|
|
|
|
incognitoItem.setVisible(false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SURFING:
|
|
|
|
instanceItem.setVisible(false);
|
|
|
|
accountItem.setVisible(true);
|
|
|
|
uploadItem.setVisible(false);
|
|
|
|
myVideosItem.setVisible(false);
|
|
|
|
playslistItem.setVisible(false);
|
|
|
|
historyItem.setVisible(false);
|
|
|
|
settingsItem.setVisible(false);
|
|
|
|
mostLikedItem.setVisible(false);
|
|
|
|
incognitoItem.setVisible(false);
|
|
|
|
break;
|
2020-06-30 12:00:55 +02:00
|
|
|
}
|
2020-11-10 18:44:48 +01:00
|
|
|
|
|
|
|
|
2020-12-01 16:09:14 +01:00
|
|
|
if (!BuildConfig.sepia_search) {
|
2020-10-09 10:19:46 +02:00
|
|
|
sepiaSearchItem.setVisible(false);
|
|
|
|
}
|
2020-06-27 12:23:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:45:54 +01:00
|
|
|
|
2020-11-11 10:34:28 +01:00
|
|
|
private void checkIfConnectedUsers() {
|
2020-11-10 18:44:48 +01:00
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
typeOfConnection = NORMAL;
|
2020-11-11 10:34:28 +01:00
|
|
|
if (!Helper.isLoggedIn(MainActivity.this)) {
|
2020-11-10 18:44:48 +01:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
List<Account> accounts = new AccountDAO(MainActivity.this, db).getAllAccount();
|
2020-11-11 10:34:28 +01:00
|
|
|
if (accounts != null && accounts.size() > 0) {
|
2020-11-10 18:44:48 +01:00
|
|
|
//The user is not authenticated and there accounts in db. That means the user is surfing some other instances
|
|
|
|
typeOfConnection = TypeOfConnection.SURFING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runOnUiThread(this::invalidateOptionsMenu);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
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) {
|
2020-11-10 18:44:48 +01:00
|
|
|
showRadioButtonDialogFullInstances(MainActivity.this, false);
|
2020-09-13 19:20:04 +02:00
|
|
|
} 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;
|
2020-11-11 10:34:28 +01:00
|
|
|
if (typeOfConnection == SURFING) {
|
2020-11-10 18:44:48 +01:00
|
|
|
SwitchAccountHelper.switchDialog(MainActivity.this, false);
|
2020-09-03 19:08:53 +02:00
|
|
|
} else {
|
2020-11-10 18:44:48 +01:00
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
intent = new Intent(MainActivity.this, AccountActivity.class);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(MainActivity.this, LoginActivity.class);
|
|
|
|
}
|
|
|
|
startActivity(intent);
|
2020-09-03 19:08:53 +02:00
|
|
|
}
|
2020-06-28 19:11:39 +02:00
|
|
|
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) {
|
2020-10-08 17:16:42 +02:00
|
|
|
Intent intent = new Intent(MainActivity.this, VideosTimelineActivity.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) {
|
2020-10-08 17:16:42 +02:00
|
|
|
Intent intent = new Intent(MainActivity.this, VideosTimelineActivity.class);
|
2020-09-03 19:08:53 +02:00
|
|
|
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) {
|
2020-10-08 17:16:42 +02:00
|
|
|
Intent intent = new Intent(MainActivity.this, VideosTimelineActivity.class);
|
2020-10-03 11:24:26 +02:00
|
|
|
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-11-14 12:19:37 +01:00
|
|
|
Intent intent;
|
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
intent = new Intent(MainActivity.this, AllPlaylistsActivity.class);
|
|
|
|
} else {
|
|
|
|
intent = new Intent(MainActivity.this, AllLocalPlaylistsActivity.class);
|
|
|
|
}
|
2020-06-30 17:26:20 +02:00
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
2020-10-17 18:50:20 +02:00
|
|
|
} else if (item.getItemId() == R.id.action_sepia_search) {
|
2020-10-09 10:19:46 +02:00
|
|
|
Intent intent = new Intent(MainActivity.this, SepiaSearchActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
2020-10-17 18:50:20 +02:00
|
|
|
} else if (item.getItemId() == R.id.action_about) {
|
2020-09-16 14:07:44 +02:00
|
|
|
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
2020-10-30 14:45:37 +01:00
|
|
|
} else if (item.getItemId() == R.id.action_incognito) {
|
|
|
|
item.setChecked(!item.isChecked());
|
|
|
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putBoolean(getString(R.string.set_store_in_history), item.isChecked());
|
|
|
|
editor.apply();
|
|
|
|
new Thread(() -> {
|
|
|
|
UserSettings userSettings = new UserSettings();
|
|
|
|
userSettings.setVideosHistoryEnabled(item.isChecked());
|
|
|
|
try {
|
|
|
|
RetrofitPeertubeAPI api = new RetrofitPeertubeAPI(MainActivity.this);
|
|
|
|
api.updateUser(userSettings);
|
2020-10-31 18:24:52 +01:00
|
|
|
} catch (Exception | Error e) {
|
2020-10-30 14:45:37 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
return false;
|
2020-06-30 12:00:55 +02:00
|
|
|
}
|
2020-10-30 14:45:37 +01:00
|
|
|
|
2020-06-27 12:23:03 +02:00
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2020-10-10 10:28:45 +02:00
|
|
|
|
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-11-13 18:45:55 +01:00
|
|
|
} else if (!BuildConfig.full_instances) {
|
|
|
|
PlaylistExportHelper.manageIntentUrl(MainActivity.this, intent);
|
2020-06-28 19:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 12:23:03 +02:00
|
|
|
|
2020-11-13 18:45:55 +01: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-12-01 18:35:54 +01:00
|
|
|
String acad = HelperInstance.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-12-01 18:35:54 +01:00
|
|
|
if (HelperInstance.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
|
|
|
|
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();
|
2020-11-10 18:44:48 +01:00
|
|
|
finish();
|
|
|
|
}
|
2020-09-17 19:01:31 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 10:34:28 +01:00
|
|
|
|
2020-11-27 18:36:31 +01:00
|
|
|
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
|
|
|
|
|
|
|
|
ScreenSlidePagerAdapter(FragmentManager fm) {
|
|
|
|
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(final int position) {
|
|
|
|
if (Helper.isLoggedIn(MainActivity.this)) {
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
|
|
|
return overviewFragment;
|
|
|
|
case 1:
|
|
|
|
return subscriptionFragment;
|
|
|
|
case 2:
|
|
|
|
return trendingFragment;
|
|
|
|
case 3:
|
|
|
|
return recentFragment;
|
|
|
|
case 4:
|
|
|
|
return locaFragment;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
|
|
|
return overviewFragment;
|
|
|
|
case 1:
|
|
|
|
return trendingFragment;
|
|
|
|
case 2:
|
|
|
|
return mostLikedFragment;
|
|
|
|
case 3:
|
|
|
|
return recentFragment;
|
|
|
|
case 4:
|
|
|
|
return locaFragment;
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 18:05:35 +01:00
|
|
|
return overviewFragment;
|
2020-11-27 18:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 10:34:28 +01:00
|
|
|
|
|
|
|
public enum TypeOfConnection {
|
|
|
|
UNKNOWN,
|
|
|
|
NORMAL,
|
|
|
|
SURFING
|
|
|
|
}
|
2020-06-25 16:57:13 +02:00
|
|
|
}
|