Change instances

This commit is contained in:
Thomas 2023-03-15 17:33:06 +01:00
parent 462a9a1d44
commit d95e48ef86
10 changed files with 164 additions and 112 deletions

View File

@ -197,6 +197,8 @@ public class Helper {
public static final String RECEIVE_ERROR_MESSAGE = "RECEIVE_ERROR_MESSAGE";
public static final String RECEIVE_RECREATE_ACTIVITY = "RECEIVE_RECREATE_ACTIVITY";
public static final String RECEIVE_RECREATE_PEERTUBE_ACTIVITY = "RECEIVE_RECREATE_PEERTUBE_ACTIVITY";
public static final String RECEIVE_NEW_MESSAGE = "RECEIVE_NEW_MESSAGE";
public static final String RECEIVE_COMPOSE_ERROR_MESSAGE = "RECEIVE_COMPOSE_ERROR_MESSAGE";
public static final String RECEIVE_MASTODON_LIST = "RECEIVE_MASTODON_LIST";

View File

@ -16,15 +16,11 @@ package app.fedilab.android.peertube.activities;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE_PEERTUBE_BROWSING;
import static app.fedilab.android.mastodon.helper.Helper.addFragment;
import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS;
import static app.fedilab.android.peertube.activities.PeertubeMainActivity.PICK_INSTANCE;
import static app.fedilab.android.peertube.helper.Helper.recreatePeertubeActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
@ -32,8 +28,6 @@ import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
@ -52,7 +46,6 @@ import app.fedilab.android.peertube.client.data.InstanceData;
import app.fedilab.android.peertube.client.entities.WellKnownNodeinfo;
import app.fedilab.android.peertube.drawer.AboutInstanceAdapter;
import app.fedilab.android.peertube.fragment.FragmentLoginPickInstancePeertube;
import app.fedilab.android.peertube.helper.Helper;
import app.fedilab.android.peertube.helper.HelperInstance;
import app.fedilab.android.peertube.sqlite.StoredInstanceDAO;
import app.fedilab.android.peertube.viewmodel.InfoInstanceVM;
@ -67,69 +60,6 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns
private AboutInstanceAdapter aboutInstanceAdapter;
@SuppressLint("ApplySharedPref")
public static void showRadioButtonDialogFullInstances(Activity activity, FragmentManager fragmentManager) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);
AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(activity);
alt_bld.setTitle(R.string.instance_choice);
String instance = HelperInstance.getLiveInstance(activity);
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();
if (!newInstance.startsWith("http")) {
newInstance = "http://" + newInstance;
}
URL url = new URL(newInstance);
newInstance = url.getHost();
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(PREF_USER_INSTANCE_PEERTUBE_BROWSING, newInstance);
editor.commit();
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(() -> 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) -> {
fragmentManager.setFragmentResultListener(PICK_INSTANCE, (LifecycleOwner) activity, (requestKey, result) -> {
new Thread(() -> {
String newInstance = result.getString(INSTANCE_ADDRESS);
InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(activity, newInstance, null).getAboutInstance();
SQLiteDatabase db = Sqlite.getInstance(activity, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StoredInstanceDAO(activity, db).insertInstance(aboutInstance, newInstance);
activity.runOnUiThread(() -> new Handler().post(() -> Helper.logoutNoRemoval(activity)));
}).start();
fragmentManager.clearFragmentResultListener(PICK_INSTANCE);
});
addFragment(
fragmentManager, android.R.id.content, new FragmentLoginPickInstancePeertube(),
null, null, FragmentLoginPickInstancePeertube.class.getName());
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -144,7 +74,58 @@ public class ManageInstancesActivity extends BaseBarActivity implements AboutIns
binding.loader.setVisibility(View.VISIBLE);
binding.noAction.setVisibility(View.GONE);
binding.lvInstances.setVisibility(View.GONE);
binding.actionButton.setOnClickListener(v -> showRadioButtonDialogFullInstances(ManageInstancesActivity.this, getSupportFragmentManager()));
binding.actionButton.setOnClickListener(v -> {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(this);
alt_bld.setTitle(R.string.instance_choice);
String instance = HelperInstance.getLiveInstance(this);
final EditText input = new EditText(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();
if (!newInstance.startsWith("http")) {
newInstance = "http://" + newInstance;
}
URL url = new URL(newInstance);
newInstance = url.getHost();
WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitPeertubeAPI(this, newInstance, null).getNodeInfo();
if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("peertube") == 0) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_INSTANCE_PEERTUBE_BROWSING, newInstance);
editor.commit();
newInstance = newInstance.trim().toLowerCase();
InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(this, newInstance, null).getAboutInstance();
SQLiteDatabase db = Sqlite.getInstance(this.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StoredInstanceDAO(this, db).insertInstance(aboutInstance, newInstance);
this.runOnUiThread(() -> {
dialog.dismiss();
recreatePeertubeActivity(this);
});
} else {
runOnUiThread(() -> Toasty.error(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());
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
addFragment(
getSupportFragmentManager(), android.R.id.content, new FragmentLoginPickInstancePeertube(),
null, null, FragmentLoginPickInstancePeertube.class.getName());
});
AlertDialog alert = alt_bld.create();
alert.show();
});
aboutInstances = new ArrayList<>();
aboutInstanceAdapter = new AboutInstanceAdapter(this.aboutInstances);
aboutInstanceAdapter.allInstancesRemoved = this;

View File

@ -33,7 +33,10 @@ import static app.fedilab.android.peertube.helper.Helper.peertubeInformation;
import static app.fedilab.android.peertube.helper.SwitchAccountHelper.switchDialog;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
@ -57,6 +60,7 @@ import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
@ -118,6 +122,17 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
private ActivityMainPeertubeBinding binding;
private final BroadcastReceiver broadcast_data = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
if (b != null) {
if (b.getBoolean(app.fedilab.android.mastodon.helper.Helper.RECEIVE_RECREATE_PEERTUBE_ACTIVITY, false)) {
recreate();
}
}
}
};
private void setTitleCustom(int titleRId) {
Toolbar toolbar = findViewById(R.id.toolbar);
@ -132,6 +147,7 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
public void onDestroy() {
super.onDestroy();
binding = null;
LocalBroadcastManager.getInstance(PeertubeMainActivity.this).unregisterReceiver(broadcast_data);
}
@SuppressLint("ApplySharedPref")
@ -141,7 +157,9 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
super.onCreate(savedInstanceState);
binding = super.binding;
LocalBroadcastManager.getInstance(PeertubeMainActivity.this).registerReceiver(
broadcast_data, new IntentFilter(app.fedilab.android.mastodon.helper.Helper.BROADCAST_DATA)
);
Intent intentActvity = getIntent();
if (intentActvity != null) {
Bundle extras = intentActvity.getExtras();
@ -389,8 +407,23 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
headerMainBinding.headerOptionInfo.setOnClickListener(v -> headerOptionInfoClick(PeertubeMainActivity.this, headerMainBinding, getSupportFragmentManager()));
fetchRecentAccounts(PeertubeMainActivity.this, headerMainBinding);
} else {
binding.navView.inflateMenu(R.menu.bottom_nav_menu_peertube);
binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
new Thread(() -> {
if (currentToken == null || currentToken.trim().isEmpty()) {
currentToken = sharedpreferences.getString(app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN, null);
}
try {
currentAccount = new Account(PeertubeMainActivity.this).getConnectedAccount();
} catch (DBException e) {
e.printStackTrace();
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
binding.navView.inflateMenu(R.menu.bottom_nav_menu_peertube);
binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
app.fedilab.android.mastodon.helper.Helper.loadPP(this, binding.profilePicture, currentAccount);
};
mainHandler.post(myRunnable);
}).start();
}
overviewFragment = new DisplayOverviewFragment();
if (!Helper.isLoggedIn()) {
@ -624,20 +657,25 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
MenuItem incognitoItem = menu.findItem(R.id.action_incognito);
MenuItem changeInstanceItem = menu.findItem(R.id.action_change_instance);
MenuItem exitItem = menu.findItem(R.id.action_exit);
MenuItem sepiaSearchItem = menu.findItem(R.id.action_sepia_search);
switch (typeOfConnection) {
case NORMAL:
if (Helper.isLoggedIn()) {
incognitoItem.setVisible(true);
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this);
boolean checked = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true);
incognitoItem.setChecked(checked);
} else {
incognitoItem.setVisible(false);
}
incognitoItem.setVisible(true);
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(PeertubeMainActivity.this);
boolean checked = sharedpreferences.getBoolean(getString(R.string.set_store_in_history), true);
incognitoItem.setChecked(checked);
changeInstanceItem.setVisible(false);
exitItem.setVisible(false);
sepiaSearchItem.setVisible(false);
break;
case REMOTE_ACCOUNT:
case SURFING:
incognitoItem.setVisible(false);
changeInstanceItem.setVisible(true);
exitItem.setVisible(true);
sepiaSearchItem.setVisible(true);
break;
}
@ -709,6 +747,13 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity {
}
}).start();
return false;
} else if (item.getItemId() == R.id.action_exit) {
Intent intent = new Intent(PeertubeMainActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else if (item.getItemId() == R.id.action_sepia_search) {
Intent intent = new Intent(PeertubeMainActivity.this, SepiaSearchActivity.class);
startActivity(intent);
}
return true;
}

View File

@ -19,7 +19,6 @@ import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
@ -46,7 +45,6 @@ import java.util.List;
import app.fedilab.android.R;
import app.fedilab.android.databinding.DrawerAboutInstancePeertubeBinding;
import app.fedilab.android.peertube.client.data.InstanceData;
import app.fedilab.android.peertube.helper.Helper;
import app.fedilab.android.peertube.sqlite.StoredInstanceDAO;
import app.fedilab.android.sqlite.Sqlite;
@ -133,7 +131,7 @@ public class AboutInstanceAdapter extends RecyclerView.Adapter<RecyclerView.View
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_INSTANCE, aboutInstance.getHost());
editor.commit();
Helper.logoutNoRemoval((Activity) context);
//Helper.logoutNoRemoval((Activity) context);
});
holder.binding.instanceMore.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(context, holder.binding.instanceMore);

View File

@ -19,8 +19,10 @@ import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE_PEER
import static app.fedilab.android.peertube.activities.PeertubeMainActivity.INSTANCE_ADDRESS;
import static app.fedilab.android.peertube.activities.PeertubeMainActivity.typeOfConnection;
import static app.fedilab.android.peertube.helper.Helper.peertubeInformation;
import static app.fedilab.android.peertube.helper.Helper.recreatePeertubeActivity;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@ -61,7 +63,9 @@ import app.fedilab.android.peertube.client.entities.InstanceParams;
import app.fedilab.android.peertube.client.entities.PeertubeInformation;
import app.fedilab.android.peertube.drawer.InstanceAdapter;
import app.fedilab.android.peertube.helper.RoundedBackgroundSpan;
import app.fedilab.android.peertube.sqlite.StoredInstanceDAO;
import app.fedilab.android.peertube.viewmodel.InstancesVM;
import app.fedilab.android.sqlite.Sqlite;
import es.dmoral.toasty.Toasty;
@ -300,14 +304,21 @@ public class FragmentLoginPickInstancePeertube extends Fragment implements Insta
}
@Override
public void instance(String instance) {
public void instance(final String instance) {
if (typeOfConnection == PeertubeMainActivity.TypeOfConnection.REMOTE_ACCOUNT) {
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_INSTANCE_PEERTUBE_BROWSING, instance);
editor.commit();
requireActivity().recreate();
new Thread(() -> {
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_INSTANCE_PEERTUBE_BROWSING, instance);
editor.commit();
InstanceData.AboutInstance aboutInstance = new RetrofitPeertubeAPI(requireActivity(), instance, null).getAboutInstance();
SQLiteDatabase db = Sqlite.getInstance(requireActivity(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new StoredInstanceDAO(requireActivity(), db).insertInstance(aboutInstance, instance);
requireActivity().runOnUiThread(() -> {
recreatePeertubeActivity(requireActivity());
requireActivity().finish();
});
}).start();
} else {
Bundle bundle = new Bundle();
bundle.putString(INSTANCE_ADDRESS, instance);

View File

@ -17,8 +17,6 @@ package app.fedilab.android.peertube.helper;
import static android.content.Context.DOWNLOAD_SERVICE;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_SOFTWARE;
import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN;
import static app.fedilab.android.peertube.activities.PeertubeMainActivity.typeOfConnection;
import android.Manifest;
@ -39,6 +37,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.util.DisplayMetrics;
@ -58,6 +57,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import com.avatarfirst.avatargenlib.AvatarGenerator;
@ -613,26 +613,18 @@ public class Helper {
/**
* Log out without removing user in db
* Send broadcast to recreate Mainactivity
*
* @param activity Activity
* @param activity - Activity
*/
public static void logoutNoRemoval(Activity activity) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(PREF_USER_TOKEN, null);
editor.putString(CLIENT_ID, null);
editor.putString(CLIENT_SECRET, null);
editor.putString(PREF_USER_ID, null);
editor.putString(PREF_USER_INSTANCE, null);
editor.putString(PREF_USER_SOFTWARE, null);
editor.apply();
Intent loginActivity = new Intent(activity, PeertubeMainActivity.class);
activity.startActivity(loginActivity);
activity.finish();
public static void recreatePeertubeActivity(Activity activity) {
Bundle b = new Bundle();
b.putBoolean(app.fedilab.android.mastodon.helper.Helper.RECEIVE_RECREATE_PEERTUBE_ACTIVITY, true);
Intent intentBD = new Intent(app.fedilab.android.mastodon.helper.Helper.BROADCAST_DATA);
intentBD.putExtras(b);
LocalBroadcastManager.getInstance(activity).sendBroadcast(intentBD);
}
public static int getAttColor(Context context, int attColor) {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attColor, typedValue, true);

View File

@ -1,7 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path

View File

@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="#FFFFFF"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M10.09,15.59L11.5,17l5,-5 -5,-5 -1.41,1.41L12.67,11H3v2h9.67l-2.58,2.59zM19,3H5c-1.11,0 -2,0.9 -2,2v4h2V5h14v14H5v-4H3v4c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>

View File

@ -2,4 +2,5 @@
<resources>
<string name="exit">Exit</string>
</resources>

View File

@ -20,9 +20,20 @@
android:title="@string/enable_history"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="never" />
<item
android:id="@+id/action_sepia_search"
android:icon="@drawable/ic_baseline_search_24"
android:title="@string/sepia_search"
android:visible="false"
app:showAsAction="never" />
<item
android:id="@+id/action_about"
android:icon="@drawable/ic_baseline_info_24"
android:title="@string/about_the_app"
app:showAsAction="never" />
<item
android:id="@+id/action_exit"
android:icon="@drawable/baseline_exit_to_app_24"
android:title="@string/exit"
app:showAsAction="never" />
</menu>