Added logout system. Fixed bug when logging in (when network error). Code cleanup.

This commit is contained in:
ChronosX88 2019-05-23 15:23:03 +04:00
parent bc5583104f
commit 398adfeece
19 changed files with 123 additions and 52 deletions

View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="org.greenrobot.eventbus.Subscribe" />
</list>
</component>
<component name="NullableNotNullManager"> <component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" /> <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />

View File

@ -92,6 +92,7 @@ public class XMPPConnection implements ConnectionListener {
.setKeystoreType(null) .setKeystoreType(null)
.setSecurityMode(ConnectionConfiguration.SecurityMode.required) .setSecurityMode(ConnectionConfiguration.SecurityMode.required)
.setCompressionEnabled(true) .setCompressionEnabled(true)
.setConnectTimeout(7000)
.build(); .build();
connection = new XMPPTCPConnection(conf); connection = new XMPPTCPConnection(conf);
@ -104,6 +105,8 @@ public class XMPPConnection implements ConnectionListener {
connection.login(credentials.username, credentials.password); connection.login(credentials.username, credentials.password);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} catch (NullPointerException e) {
throw new IOException();
} }
ChatManager.getInstanceFor(connection).addIncomingListener(networkHandler); ChatManager.getInstanceFor(connection).addIncomingListener(networkHandler);

View File

@ -72,10 +72,10 @@ public class XMPPConnectionService extends Service {
isThreadAlive = false; isThreadAlive = false;
threadHandler.post(() -> { threadHandler.post(() -> {
if(connection != null) { if(connection != null) {
connection.disconnect();
connection = null;
thread.interrupt(); thread.interrupt();
thread = null; thread = null;
connection.disconnect();
connection = null;
} }
}); });
} }

View File

@ -41,11 +41,15 @@ interface CoreContracts {
interface IMainLogicContract { interface IMainLogicContract {
fun startService() fun startService()
fun logout()
} }
interface IMainPresenterContract { interface IMainPresenterContract {
fun initConnection() fun initConnection()
fun startChatWithPeer(username: String) fun startChatWithPeer(username: String)
fun logoutFromAccount()
fun onStart()
fun onStop()
} }
interface IMainViewContract { interface IMainViewContract {

View File

@ -82,6 +82,12 @@ public class AppHelper extends MultiDexApplication {
AppHelper.setJid(currentLoginCredentials.username + "@" + currentLoginCredentials.jabberHost); AppHelper.setJid(currentLoginCredentials.username + "@" + currentLoginCredentials.jabberHost);
} }
public static void resetLoginCredentials() {
currentLoginCredentials = new LoginCredentials();
preferences.edit().remove("jid").apply();
preferences.edit().remove("pass").apply();
}
private static void initTrueTime() { private static void initTrueTime() {
new Thread(() -> { new Thread(() -> {
boolean isTrueTimeIsOn = false; boolean isTrueTimeIsOn = false;
@ -106,11 +112,11 @@ public class AppHelper extends MultiDexApplication {
return mainUIThreadHandler; return mainUIThreadHandler;
} }
public static ServiceConnection getServiceConnection() {
return serviceConnection;
}
public static void setServiceConnection(ServiceConnection serviceConnection) { public static void setServiceConnection(ServiceConnection serviceConnection) {
AppHelper.serviceConnection = serviceConnection; AppHelper.serviceConnection = serviceConnection;
} }
public static ServiceConnection getServiceConnection() {
return serviceConnection;
}
} }

View File

@ -58,4 +58,9 @@ public class LocalDBWrapper {
public static void updateMessage(MessageEntity messageEntity) { public static void updateMessage(MessageEntity messageEntity) {
dbInstance.messageDao().updateMessage(messageEntity); dbInstance.messageDao().updateMessage(messageEntity);
} }
public static void clearDatabase() {
dbInstance.messageDao().clearMessages();
dbInstance.chatDao().clearChats();
}
} }

View File

@ -1,22 +0,0 @@
/*
* Copyright (C) 2019 ChronosX88
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.chronosx88.influence.helpers;
public class ObservableActions {
public static final int NEW_CHAT_CREATED = 0x0;
}

View File

@ -9,6 +9,7 @@ import android.os.IBinder;
import io.github.chronosx88.influence.XMPPConnectionService; import io.github.chronosx88.influence.XMPPConnectionService;
import io.github.chronosx88.influence.contracts.CoreContracts; import io.github.chronosx88.influence.contracts.CoreContracts;
import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.AppHelper;
import io.github.chronosx88.influence.helpers.LocalDBWrapper;
public class MainLogic implements CoreContracts.IMainLogicContract { public class MainLogic implements CoreContracts.IMainLogicContract {
private static final String LOG_TAG = MainLogic.class.getName(); private static final String LOG_TAG = MainLogic.class.getName();
@ -34,6 +35,15 @@ public class MainLogic implements CoreContracts.IMainLogicContract {
AppHelper.setXmppConnection(null); AppHelper.setXmppConnection(null);
} }
}; };
AppHelper.setServiceConnection(connection);
context.bindService(new Intent(context, XMPPConnectionService.class), connection,Context.BIND_AUTO_CREATE); context.bindService(new Intent(context, XMPPConnectionService.class), connection,Context.BIND_AUTO_CREATE);
} }
@Override
public void logout() {
LocalDBWrapper.clearDatabase();
AppHelper.resetLoginCredentials();
context.unbindService(AppHelper.getServiceConnection());
context.stopService(new Intent(context, XMPPConnectionService.class));
}
} }

View File

@ -25,4 +25,7 @@ public interface ChatDao {
@Update @Update
void updateChat(ChatEntity chat); void updateChat(ChatEntity chat);
@Query("DELETE FROM chats")
void clearChats();
} }

View File

@ -28,4 +28,7 @@ public interface MessageDao {
@Update @Update
void updateMessage(MessageEntity message); void updateMessage(MessageEntity message);
@Query("DELETE FROM messages")
void clearMessages();
} }

View File

@ -1,3 +1,20 @@
/*
* Copyright (C) 2019 ChronosX88
*
* 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.chronosx88.influence.presenters; package io.github.chronosx88.influence.presenters;
import android.content.Intent; import android.content.Intent;

View File

@ -15,10 +15,6 @@ import org.greenrobot.eventbus.ThreadMode
class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract { class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreContracts.IMainPresenterContract {
private val logic: CoreContracts.IMainLogicContract = MainLogic() private val logic: CoreContracts.IMainLogicContract = MainLogic()
init {
EventBus.getDefault().register(this)
}
override fun initConnection() { override fun initConnection() {
logic.startService() logic.startService()
} }
@ -38,4 +34,16 @@ class MainPresenter(private val view: CoreContracts.IMainViewContract) : CoreCon
} }
} }
} }
override fun logoutFromAccount() {
logic.logout()
}
override fun onStart() {
EventBus.getDefault().register(this)
}
override fun onStop() {
EventBus.getDefault().unregister(this)
}
} }

View File

@ -166,11 +166,13 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL
case AuthenticationStatusEvent.INCORRECT_LOGIN_OR_PASSWORD: { case AuthenticationStatusEvent.INCORRECT_LOGIN_OR_PASSWORD: {
loadingScreen(false); loadingScreen(false);
passwordInputLayout.setError("Invalid JID/Password"); passwordInputLayout.setError("Invalid JID/Password");
AppHelper.resetLoginCredentials();
break; break;
} }
case AuthenticationStatusEvent.NETWORK_ERROR: { case AuthenticationStatusEvent.NETWORK_ERROR: {
loadingScreen(false); loadingScreen(false);
jidInputLayout.setError("Network error"); jidInputLayout.setError("Network error");
AppHelper.resetLoginCredentials();
break; break;
} }
} }

View File

@ -1,6 +1,7 @@
package io.github.chronosx88.influence.views; package io.github.chronosx88.influence.views;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -87,6 +88,21 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa
return true; return true;
} }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_logout_item: {
presenter.logoutFromAccount();
startActivity(new Intent(this, LoginActivity.class));
finish();
break;
}
}
return true;
}
@Override @Override
public void showSnackbar(@NotNull String message) { public void showSnackbar(@NotNull String message) {
runOnUiThread(() -> Snackbar.make(getRootView(), message, Snackbar.LENGTH_LONG).show()); runOnUiThread(() -> Snackbar.make(getRootView(), message, Snackbar.LENGTH_LONG).show());
@ -115,4 +131,16 @@ public class MainActivity extends AppCompatActivity implements CoreContracts.IMa
return rootView; return rootView;
} }
@Override
protected void onStart() {
super.onStart();
presenter.onStart();
}
@Override
protected void onStop() {
super.onStop();
presenter.onStop();
}
} }

View File

@ -26,7 +26,8 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/jid_input_layout" android:id="@+id/jid_input_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:paddingTop="5dp">
<EditText <EditText
android:id="@+id/login_jid" android:id="@+id/login_jid"
@ -42,7 +43,8 @@
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_input_layout" android:id="@+id/password_input_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:paddingTop="10dp">
<EditText <EditText
android:id="@+id/login_password" android:id="@+id/login_password"
@ -63,7 +65,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="SIGN IN" android:text="@string/sign_in_button"/>
android:textStyle="bold" />
</LinearLayout> </LinearLayout>

View File

@ -16,5 +16,11 @@
~ along with this program. If not, see <https://www.gnu.org/licenses/>. ~ along with this program. If not, see <https://www.gnu.org/licenses/>.
--> -->
<menu> <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_logout_item"
app:showAsAction="never"
android:title="@string/logout">
</item>
</menu> </menu>

View File

@ -1,14 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="app_name">Influence</string> <string name="app_name">Influence</string>
<string name="username_saved">Ваше имя пользователя успешно сохранено!</string>
<string name="username_isnt_saved">К сожалению, это имя пользователя занято.</string>
<string name="ok">OK</string> <string name="ok">OK</string>
<string name="cancel">Отмена</string> <string name="cancel">Отмена</string>
<string name="username_settings">Ваше имя пользователя</string> <string name="input_companion_username">Введите JID собеседника</string>
<string name="username_hint">Имя пользователя</string>
<string name="reconnect_network">Переподключиться к сети</string>
<string name="node_already_running">Узел уже запущен</string>
<string name="input_companion_username">Введите имя пользователя собеседника</string>
<string name="hint_enter_a_message">Введите сообщение...</string> <string name="hint_enter_a_message">Введите сообщение...</string>
<string name="logout">Выйти из аккаунта</string>
<string name="sign_in_button">Войти</string>
</resources> </resources>

View File

@ -1,13 +1,9 @@
<resources> <resources>
<string name="app_name">Influence</string> <string name="app_name">Influence</string>
<string name="username_saved">Your username saved successfully!</string>
<string name="username_isnt_saved">Sorry, this username is busy.</string>
<string name="ok">OK</string> <string name="ok">OK</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string name="username_settings">Your username</string> <string name="input_companion_username">Input interlocutor\'s JID</string>
<string name="username_hint">Username</string>
<string name="reconnect_network">Reconnect to the network</string>
<string name="node_already_running">Node already running</string>
<string name="input_companion_username">Input interlocutor\'s username</string>
<string name="hint_enter_a_message">Enter message...</string> <string name="hint_enter_a_message">Enter message...</string>
<string name="logout">Log out from account</string>
<string name="sign_in_button">Sign In</string>
</resources> </resources>

View File

@ -12,7 +12,7 @@
<item name="alertDialogStyle">@style/CustomAlertStyle</item> <item name="alertDialogStyle">@style/CustomAlertStyle</item>
<item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item>
<item name="android:textColorPrimary">#FFFFFF</item> <item name="android:textColorPrimary">#FFFFFF</item>
<item name="colorAccent">@color/colorPrimary</item> <item name="colorAccent">@color/colorAccent</item>
<item name="android:backgroundDimEnabled">true</item> <item name="android:backgroundDimEnabled">true</item>
<item name="android:textColor">#FFFFFF</item> <item name="android:textColor">#FFFFFF</item>
<item name="android:textStyle">normal</item> <item name="android:textStyle">normal</item>