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

493 lines
23 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-10-13 14:30:08 +02:00
import android.os.Handler;
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-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-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-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-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-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-10-08 16:21:14 +02:00
final FragmentManager fm = getSupportFragmentManager();
Fragment active;
private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment;
private DisplayOverviewFragment overviewFragment;
2020-10-09 10:19:46 +02:00
2020-10-14 10:31:02 +02:00
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
2020-10-08 16:21:14 +02:00
= item -> {
DisplayVideosFragment displayVideosFragment = null;
2020-10-14 10:31:02 +02:00
int itemId = item.getItemId();
if (itemId == R.id.navigation_subscription) {
displayVideosFragment = subscriptionFragment;
setTitle(R.string.subscriptions);
} else if (itemId == R.id.navigation_trending) {
setTitle(R.string.title_trending);
displayVideosFragment = trendingFragment;
} else if (itemId == R.id.navigation_most_liked) {
setTitle(R.string.title_most_liked);
displayVideosFragment = mostLikedFragment;
} else if (itemId == R.id.navigation_recently_added) {
setTitle(R.string.title_recently_added);
displayVideosFragment = recentFragment;
} else if (itemId == R.id.navigation_local) {
setTitle(R.string.title_local);
displayVideosFragment = locaFragment;
} else if (itemId == R.id.navigation_discover) {
setTitle(R.string.title_discover);
fm.beginTransaction().hide(active).show(overviewFragment).commit();
active = overviewFragment;
return true;
2020-10-08 16:21:14 +02:00
}
if (displayVideosFragment != null) {
fm.beginTransaction().hide(active).show(displayVideosFragment).commit();
active = displayVideosFragment;
return true;
} else {
return false;
}
};
2020-09-17 19:01:31 +02:00
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-10-03 16:22:19 +02:00
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
2020-09-18 14:37:34 +02: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-10-09 19:14:20 +02:00
Fragment fragment = getSupportFragmentManager().findFragmentByTag("5");
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("4");
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("3");
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("2");
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("1");
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
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-10-09 10:19:46 +02:00
if( active == null) {
active = overviewFragment;
}
2020-10-08 16:21:14 +02:00
fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit();
if (!Helper.isLoggedIn(MainActivity.this)) {
fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commit();
2020-10-08 17:16:42 +02:00
fm.beginTransaction().add(R.id.nav_host_fragment, mostLikedFragment, "3").hide(mostLikedFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "2").hide(trendingFragment).commit();
2020-10-08 16:21:14 +02:00
fm.beginTransaction().add(R.id.nav_host_fragment, overviewFragment, "1").commit();
}
2020-10-08 18:30:47 +02:00
toolbar.setOnClickListener(v->{
if(active instanceof DisplayVideosFragment) {
((DisplayVideosFragment) active).scrollToTop();
}else if(active instanceof DisplayOverviewFragment) {
((DisplayOverviewFragment) active).scrollToTop();
}
});
2020-10-08 16:21:14 +02:00
2020-10-08 18:35:35 +02:00
setTitle(R.string.title_discover);
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-03 18:37:34 +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;
}
2020-10-13 14:30:08 +02:00
2020-10-08 16:21:14 +02:00
runOnUiThread(() -> {
//To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed.
2020-10-13 14:30:08 +02:00
new Handler().post(() -> {
fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "3").hide(trendingFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, subscriptionFragment, "2").hide(subscriptionFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, overviewFragment, "1").commitAllowingStateLoss();
});
2020-10-08 16:21:14 +02:00
});
2020-10-01 17:42:03 +02:00
UserMe userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
2020-10-03 18:37:34 +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();
2020-10-11 17:21:45 +02:00
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();
2020-10-03 11:44:00 +02:00
}
2020-10-01 17:42:03 +02:00
}
2020-09-29 17:42:15 +02:00
} catch (Error error) {
error.printStackTrace();
}
}).start();
}
} else {
navView.inflateMenu(R.menu.bottom_nav_menu);
}
2020-09-18 14:37:34 +02:00
startInForeground();
2020-06-25 16:57:13 +02:00
2020-10-10 15:33:25 +02:00
if(BuildConfig.google_restriction && BuildConfig.full_instances) {
RateThisApp.onCreate(this);
RateThisApp.showRateDialogIfNeeded(this);
}
2020-10-08 16:21:14 +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-10-09 10:19:46 +02:00
MenuItem sepiaSearchItem = menu.findItem(R.id.action_sepia_search);
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-10-09 10:19:46 +02:00
if( !BuildConfig.full_instances) {
sepiaSearchItem.setVisible(false);
}
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) {
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-06-30 17:26:20 +02:00
Intent intent = new Intent(MainActivity.this, AllPlaylistsActivity.class);
startActivity(intent);
return true;
2020-10-09 10:19:46 +02:00
}else if(item.getItemId() == R.id.action_sepia_search) {
Intent intent = new Intent(MainActivity.this, SepiaSearchActivity.class);
startActivity(intent);
return true;
}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-06-30 12:00:55 +02:00
}
2020-06-27 12:23:03 +02:00
return super.onOptionsItemSelected(item);
}
2020-10-10 10:28:45 +02:00
public void setActive(DisplayVideosFragment displayVideosFragment){
this.active = displayVideosFragment;
}
public void setSubscriptionFragment(DisplayVideosFragment displayVideosFragment) {
this.subscriptionFragment = displayVideosFragment;
}
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
}