Fix login issue with internet access. Better solution to check internet connection
with broadcast receiver. Prioritize token login.
This commit is contained in:
parent
6842ba1410
commit
d52ff1557f
@ -23,6 +23,7 @@ import androidx.appcompat.app.AlertDialog;
|
||||
import com.tooltip.Tooltip;
|
||||
import org.mian.gitnex.R;
|
||||
import org.mian.gitnex.clients.RetrofitClient;
|
||||
import org.mian.gitnex.helpers.NetworkObserver;
|
||||
import org.mian.gitnex.helpers.Toasty;
|
||||
import org.mian.gitnex.helpers.VersionCheck;
|
||||
import org.mian.gitnex.models.GiteaVersion;
|
||||
@ -65,7 +66,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
TinyDB tinyDb = new TinyDB(getApplicationContext());
|
||||
boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext());
|
||||
NetworkObserver networkMonitor = new NetworkObserver(this);
|
||||
|
||||
loginButton = findViewById(R.id.login_button);
|
||||
instanceUrlET = findViewById(R.id.instance_url);
|
||||
@ -105,30 +106,39 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
|
||||
|
||||
info_button.setOnClickListener(infoListener);
|
||||
|
||||
if(!connToInternet) {
|
||||
loginMethod.setOnCheckedChangeListener((group, checkedId) -> {
|
||||
|
||||
Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection));
|
||||
return;
|
||||
if(checkedId == R.id.loginToken) {
|
||||
|
||||
}
|
||||
|
||||
loginMethod.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
||||
if(checkedId == R.id.loginUsernamePassword){
|
||||
loginUidET.setVisibility(View.VISIBLE);
|
||||
loginPassword.setVisibility(View.VISIBLE);
|
||||
otpCode.setVisibility(View.VISIBLE);
|
||||
otpInfo.setVisibility(View.VISIBLE);
|
||||
loginTokenCode.setVisibility(View.GONE);
|
||||
} else {
|
||||
loginUidET.setVisibility(View.GONE);
|
||||
loginPassword.setVisibility(View.GONE);
|
||||
otpCode.setVisibility(View.GONE);
|
||||
otpInfo.setVisibility(View.GONE);
|
||||
loginTokenCode.setVisibility(View.VISIBLE);
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
loginUidET.setVisibility(View.VISIBLE);
|
||||
loginPassword.setVisibility(View.VISIBLE);
|
||||
otpCode.setVisibility(View.VISIBLE);
|
||||
otpInfo.setVisibility(View.VISIBLE);
|
||||
loginTokenCode.setVisibility(View.GONE);
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
networkMonitor.onInternetStateListener(isAvailable -> {
|
||||
|
||||
if(isAvailable) {
|
||||
enableProcessButton();
|
||||
}
|
||||
else {
|
||||
disableProcessButton();
|
||||
Toasty.info(getApplicationContext(), getResources().getString(R.string.checkNetConnection));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
//login_button.setOnClickListener(this);
|
||||
|
@ -0,0 +1,82 @@
|
||||
package org.mian.gitnex.helpers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
|
||||
/**
|
||||
* Author M M Arif
|
||||
*/
|
||||
|
||||
public class NetworkObserver implements LifecycleObserver {
|
||||
|
||||
private ConnectivityManager mConnectivityMgr;
|
||||
private Context mContext;
|
||||
private NetworkStateReceiver mNetworkStateReceiver;
|
||||
|
||||
public interface ConnectionStateListener {
|
||||
void onAvailable(boolean isAvailable);
|
||||
}
|
||||
|
||||
public NetworkObserver(Context context) {
|
||||
mContext = context;
|
||||
mConnectivityMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
((AppCompatActivity) mContext).getLifecycle().addObserver(this);
|
||||
}
|
||||
|
||||
|
||||
public void onInternetStateListener(ConnectionStateListener listener) {
|
||||
|
||||
mNetworkStateReceiver = new NetworkStateReceiver(listener);
|
||||
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
mContext.registerReceiver(mNetworkStateReceiver, intentFilter);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
public void onDestroy() {
|
||||
|
||||
((AppCompatActivity) mContext).getLifecycle().removeObserver(this);
|
||||
|
||||
if (mNetworkStateReceiver != null) {
|
||||
mContext.unregisterReceiver(mNetworkStateReceiver);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class NetworkStateReceiver extends BroadcastReceiver {
|
||||
|
||||
ConnectionStateListener mListener;
|
||||
|
||||
public NetworkStateReceiver(ConnectionStateListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getExtras() != null) {
|
||||
NetworkInfo activeNetworkInfo = mConnectivityMgr.getActiveNetworkInfo();
|
||||
|
||||
if (activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
|
||||
|
||||
mListener.onAvailable(true); // connected
|
||||
|
||||
} else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
|
||||
|
||||
mListener.onAvailable(false); // disconnected
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -45,20 +45,21 @@
|
||||
android:background="@drawable/shape_inputs"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/loginUsernamePassword"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/loginViaPassword"
|
||||
android:checked="true"
|
||||
android:layout_marginEnd="30dp"
|
||||
android:theme="@style/radioButtonsInDarkTheme"
|
||||
android:textColor="?attr/primaryTextColor"/>
|
||||
<RadioButton
|
||||
android:id="@+id/loginToken"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/copyToken"
|
||||
android:checked="true"
|
||||
android:theme="@style/radioButtonsInDarkTheme"
|
||||
android:textColor="?attr/primaryTextColor"/>
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/loginUsernamePassword"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/loginViaPassword"
|
||||
android:layout_marginEnd="30dp"
|
||||
android:theme="@style/radioButtonsInDarkTheme"
|
||||
android:textColor="?attr/primaryTextColor"/>
|
||||
|
||||
@ -129,7 +130,8 @@
|
||||
android:textColorHint="?attr/hintColor"
|
||||
android:textColorHighlight="?attr/hintColor"
|
||||
android:hint="@string/userName"
|
||||
android:inputType="text" />
|
||||
android:inputType="text"
|
||||
android:visibility="gone" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/login_passwd"
|
||||
@ -146,7 +148,8 @@
|
||||
android:textColorHint="?attr/hintColor"
|
||||
android:textColorHighlight="?attr/hintColor"
|
||||
android:hint="@string/passWord"
|
||||
android:inputType="textPassword" />
|
||||
android:inputType="textPassword"
|
||||
android:visibility="gone" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/otpCode"
|
||||
@ -163,7 +166,8 @@
|
||||
android:textColorHint="?attr/hintColor"
|
||||
android:textColorHighlight="?attr/hintColor"
|
||||
android:hint="@string/loginOTP"
|
||||
android:inputType="number" />
|
||||
android:inputType="number"
|
||||
android:visibility="gone" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/loginTokenCode"
|
||||
@ -181,7 +185,7 @@
|
||||
android:textColorHighlight="?attr/hintColor"
|
||||
android:hint="@string/copyToken"
|
||||
android:inputType="text"
|
||||
android:visibility="gone" />
|
||||
android:visibility="visible" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/otpInfo"
|
||||
@ -192,7 +196,8 @@
|
||||
android:textSize="12sp"
|
||||
android:paddingStart="10dp"
|
||||
android:paddingEnd="10dp"
|
||||
android:gravity="end" />
|
||||
android:gravity="end"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:layout_width="match_parent"
|
||||
|
Loading…
x
Reference in New Issue
Block a user