#1546 Handle custom server urls on login

This commit is contained in:
sictiru 2021-09-14 21:10:54 -07:00
parent c2e84c5cd9
commit bec491e02a
7 changed files with 179 additions and 167 deletions

View file

@ -31,10 +31,10 @@
<FrameLayout
android:id="@+id/login_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingRight="30dp" />
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</ScrollView>

View file

@ -4,12 +4,13 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out" >
android:outAnimation="@android:anim/fade_out">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp">
android:animateLayoutChanges="true"
android:paddingBottom="20dp">
<LinearLayout
android:id="@+id/login_fields_container"
@ -17,7 +18,7 @@
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:orientation="vertical" >
android:orientation="vertical">
<EditText
android:id="@+id/login_username"
@ -46,11 +47,11 @@
android:layout_below="@id/login_fields_container"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:contentDescription="@string/description_login_button"
android:paddingLeft="16dp"
android:paddingTop="2dp"
android:paddingRight="16dp"
android:paddingBottom="2dp"
android:text="@string/login_button_login" />
<TextView
@ -83,30 +84,52 @@
android:text="@string/login_custom_server"
android:textSize="16sp" />
<TextView
android:id="@+id/text_custom_server"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login_custom_server"
android:layout_alignParentLeft="true"
android:layout_marginTop="60dp"
android:text="@string/login_registration_custom_server"
android:textSize="20sp"
android:visibility="invisible" />
<EditText
android:id="@+id/login_custom_server_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_custom_server"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_below="@id/text_custom_server"
android:hint="@string/login_custom_server_hint"
android:textSize="20sp"
android:inputType="textNoSuggestions|textMultiLine"
android:textSize="17sp"
android:visibility="invisible" />
<TextView
android:id="@+id/button_reset_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login_custom_server"
android:layout_alignBaseline="@+id/text_custom_server"
android:layout_alignParentEnd="true"
android:layout_gravity="end|center_vertical"
android:text="@string/login_registration_reset_url"
android:textSize="16sp"
android:visibility="invisible" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp">
android:paddingBottom="20dp">
<LinearLayout
android:id="@+id/registration_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
android:orientation="horizontal">
<TextView
android:id="@+id/login_change_to_login"
@ -129,14 +152,14 @@
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/registration_button_container" >
android:layout_above="@id/registration_button_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical" >
android:orientation="vertical">
<EditText
android:id="@+id/registration_username"
@ -144,7 +167,7 @@
android:layout_height="wrap_content"
android:hint="@string/login_username_hint"
android:inputType="textEmailAddress"
android:textSize="22sp" >
android:textSize="22sp">
</EditText>
@ -171,5 +194,5 @@
</LinearLayout>
</ScrollView>
</RelativeLayout>
</ViewSwitcher>

View file

@ -238,6 +238,8 @@
<string name="empty_list_view_no_focus_stories">You have no unread stories in Focus mode.\n\nSwitch to All or Unread.</string>
<string name="empty_list_view_no_saved_stories">You have no saved stories.\n\nSwitch to All or Unread.</string>
<string name="login_registration_custom_server">Custom server</string>
<string name="login_registration_reset_url"><u>Reset</u></string>
<string name="login_registration_register">Register</string>
<string name="register_message_error">There was problem registering for NewsBlur.</string>
<string name="get_started">Let\'s get started</string>

View file

@ -1,142 +0,0 @@
package com.newsblur.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import com.newsblur.R;
import com.newsblur.activity.LoginProgress;
import com.newsblur.activity.RegisterProgress;
import com.newsblur.databinding.FragmentLoginregisterBinding;
import com.newsblur.network.APIConstants;
import com.newsblur.util.AppConstants;
import com.newsblur.util.PrefsUtils;
public class LoginRegisterFragment extends Fragment {
private FragmentLoginregisterBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_loginregister, container, false);
binding = FragmentLoginregisterBinding.bind(v);
binding.loginPassword.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ) {
logIn();
}
return false;
}
});
binding.registrationEmail.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ) {
signUp();
}
return false;
}
});
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
binding.loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logIn();
}
});
binding.registrationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signUp();
}
});
binding.loginChangeToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLogin();
}
});
binding.loginChangeToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegister();
}
});
binding.loginForgotPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchForgotPasswordPage();
}
});
binding.loginCustomServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomServer();
}
});
}
private void logIn() {
if (!TextUtils.isEmpty(binding.loginUsername.getText().toString())) {
// set the custom server endpoint before any API access, even the cookie fetch.
APIConstants.setCustomServer(binding.loginCustomServerValue.getText().toString());
PrefsUtils.saveCustomServer(getActivity(), binding.loginCustomServerValue.getText().toString());
Intent i = new Intent(getActivity(), LoginProgress.class);
i.putExtra("username", binding.loginUsername.getText().toString());
i.putExtra("password", binding.loginPassword.getText().toString());
startActivity(i);
}
}
private void signUp() {
Intent i = new Intent(getActivity(), RegisterProgress.class);
i.putExtra("username", binding.registrationUsername.getText().toString());
i.putExtra("password", binding.registrationPassword.getText().toString());
i.putExtra("email", binding.registrationEmail.getText().toString());
startActivity(i);
}
private void showLogin() {
binding.loginViewswitcher.showPrevious();
}
private void showRegister() {
binding.loginViewswitcher.showNext();
}
private void launchForgotPasswordPage() {
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(AppConstants.FORGOT_PASWORD_URL));
startActivity(i);
} catch (Exception e) {
android.util.Log.wtf(this.getClass().getName(), "device cannot even open URLs to report feedback");
}
}
private void showCustomServer() {
binding.loginCustomServer.setVisibility(View.GONE);
binding.loginCustomServerValue.setVisibility(View.VISIBLE);
}
}

View file

@ -0,0 +1,115 @@
package com.newsblur.fragment
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import com.newsblur.R
import com.newsblur.activity.LoginProgress
import com.newsblur.activity.RegisterProgress
import com.newsblur.databinding.FragmentLoginregisterBinding
import com.newsblur.network.APIConstants
import com.newsblur.util.AppConstants
import com.newsblur.util.PrefsUtils
class LoginRegisterFragment : Fragment() {
private lateinit var binding: FragmentLoginregisterBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_loginregister, container, false)
binding = FragmentLoginregisterBinding.bind(v)
binding.loginPassword.setOnEditorActionListener { _, actionId: Int, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
logIn()
}
false
}
binding.registrationEmail.setOnEditorActionListener { _, actionId: Int, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
signUp()
}
false
}
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.loginButton.setOnClickListener { logIn() }
binding.registrationButton.setOnClickListener { signUp() }
binding.loginChangeToLogin.setOnClickListener { showLogin() }
binding.loginChangeToRegister.setOnClickListener { showRegister() }
binding.loginForgotPassword.setOnClickListener { launchForgotPasswordPage() }
binding.loginCustomServer.setOnClickListener { showCustomServer(true) }
binding.buttonResetUrl.setOnClickListener { showCustomServer(false) }
val customServerUrl = PrefsUtils.getCustomSever(requireContext())
if (!TextUtils.isEmpty(customServerUrl)) {
binding.loginCustomServerValue.setText(customServerUrl)
showCustomServer(true)
}
}
private fun logIn() {
if (!TextUtils.isEmpty(binding.loginUsername.text.toString())) {
// set the custom server endpoint before any API access, even the cookie fetch.
val customServerValue = binding.loginCustomServerValue.text.toString()
if (!TextUtils.isEmpty(customServerValue)) {
APIConstants.setCustomServer(customServerValue)
PrefsUtils.saveCustomServer(activity, customServerValue)
}
val loginIntent = Intent(activity, LoginProgress::class.java).apply {
putExtra("username", binding.loginUsername.text.toString())
putExtra("password", binding.loginPassword.text.toString())
}
startActivity(loginIntent)
}
}
private fun signUp() {
val registerIntent = Intent(activity, RegisterProgress::class.java).apply {
putExtra("username", binding.registrationUsername.text.toString())
putExtra("password", binding.registrationPassword.text.toString())
putExtra("email", binding.registrationEmail.text.toString())
}
startActivity(registerIntent)
}
private fun showLogin() {
binding.loginViewswitcher.showPrevious()
}
private fun showRegister() {
binding.loginViewswitcher.showNext()
}
private fun launchForgotPasswordPage() {
try {
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(AppConstants.FORGOT_PASWORD_URL)
startActivity(i)
} catch (e: Exception) {
Log.wtf(this.javaClass.name, "device cannot even open URLs to report feedback")
}
}
private fun showCustomServer(isVisible: Boolean) {
binding.loginCustomServer.visibility = if (isVisible) View.GONE else View.VISIBLE
binding.loginCustomServerValue.visibility = if (isVisible) View.VISIBLE else View.GONE
binding.buttonResetUrl.visibility = if (isVisible) View.VISIBLE else View.GONE
binding.textCustomServer.visibility = if (isVisible) View.VISIBLE else View.GONE
if (!isVisible) {
binding.loginCustomServerValue.setText("")
APIConstants.unsetCustomServer()
PrefsUtils.clearCustomServer(context)
}
}
}

View file

@ -1,5 +1,7 @@
package com.newsblur.network;
import android.text.TextUtils;
public class APIConstants {
private APIConstants() {} // util class - no instances
@ -9,10 +11,9 @@ public class APIConstants {
private static String CurrentUrlBase = DEFAULT_NEWSBLUR_URL_BASE;
public static void setCustomServer(String newUrlBase) {
if (newUrlBase == null) return;
if (newUrlBase.length() <= 0) return;
android.util.Log.i(APIConstants.class.getName(), "setting custom server: " + newUrlBase);
if (TextUtils.isEmpty(newUrlBase)) return;
CurrentUrlBase = newUrlBase;
android.util.Log.i(APIConstants.class.getName(), "setting custom server: " + newUrlBase);
}
public static void unsetCustomServer() {

View file

@ -47,6 +47,19 @@ public class PrefsUtils {
edit.commit();
}
@Nullable
public static String getCustomSever(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
return preferences.getString(PrefConstants.PREF_CUSTOM_SERVER, null);
}
public static void clearCustomServer(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
Editor edit = preferences.edit();
edit.remove(PrefConstants.PREF_CUSTOM_SERVER);
edit.commit();
}
public static void saveLogin(Context context, String userName, String cookie) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
Editor edit = preferences.edit();