Search in sources :

Example 6 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class WizNavExtrasActivity method onLoadInBackground.

@Override
protected ChatMember onLoadInBackground(Void... arg) {
    if (!NetUtils.isConnected(this)) {
        showNoInternetLayout();
        return null;
    }
    // Get the users lrzId and initialise chat member
    ChatMember currentChatMember = new ChatMember(Utils.getSetting(this, Const.LRZ_ID, ""));
    currentChatMember.setDisplayName(Utils.getSetting(this, Const.CHAT_ROOM_DISPLAY_NAME, ""));
    if (currentChatMember.getLrzId().equals("")) {
        return currentChatMember;
    }
    // Tell the server the new member
    ChatMember member;
    try {
        // After the user has entered their display name, send a request to the server to create the new member
        member = TUMCabeClient.getInstance(this).createMember(currentChatMember);
    } catch (IOException e) {
        Utils.log(e);
        Utils.showToastOnUIThread(this, R.string.error_setup_chat_member);
        return null;
    }
    // Catch a possible error, when we didn't get something returned
    if (member == null || member.getLrzId() == null) {
        Utils.showToastOnUIThread(this, R.string.error_setup_chat_member);
        return null;
    }
    // Generate the private key and upload the public key to the server
    AuthenticationManager am = new AuthenticationManager(this);
    if (!am.generatePrivateKey(member)) {
        // We cannot continue if the upload of the Public Key does not work
        Utils.showToastOnUIThread(this, getString(R.string.failure_uploading_public_key));
        return null;
    }
    // Try to restore already joined chat rooms from server
    try {
        List<ChatRoom> rooms = TUMCabeClient.getInstance(this).getMemberRooms(member.getId(), ChatVerification.Companion.getChatVerification(this, member));
        new ChatRoomController(this).replaceIntoRooms(rooms);
        // Store that this key was activated
        Utils.setSetting(this, Const.PRIVATE_KEY_ACTIVE, true);
        return member;
    } catch (IOException | NoPrivateKey e) {
        Utils.log(e);
    }
    return null;
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) IOException(java.io.IOException) ChatRoomController(de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)

Example 7 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class ChatNotification method prepare.

private void prepare() throws IOException {
    Utils.logv("Received GCM notification: room=" + this.extras.getRoom() + " member=" + this.extras.getMember() + " message=" + this.extras.getMessage());
    // Get the data necessary for the ChatActivity
    ChatMember member = Utils.getSetting(context, Const.CHAT_MEMBER, ChatMember.class);
    chatRoom = TUMCabeClient.getInstance(context).getChatRoom(this.extras.getRoom());
    try {
        this.getNewMessages(chatRoom, member, this.extras.getMessage());
    } catch (NoPrivateKey noPrivateKey) {
        Utils.log(noPrivateKey);
    }
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)

Example 8 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class CardsDetailActivity method onLoadInBackground.

@Override
protected StudyCard onLoadInBackground(Void... arg) {
    if (!card.is_valid()) {
        return null;
    }
    try {
        ChatMember chatMember = Utils.getSetting(this, Const.CHAT_MEMBER, ChatMember.class);
        final ChatVerification v = ChatVerification.Companion.getChatVerification(this.getApplicationContext(), chatMember);
        final Context c = this;
        return TUMCabeClient.getInstance(c).addStudyCard(card, v);
    } catch (IOException e) {
        Utils.log(e);
    } catch (NoPrivateKey e) {
        Utils.log(e);
    }
    return null;
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) Context(android.content.Context) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification) IOException(java.io.IOException)

Example 9 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class SendMessageService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    TcaDb tcaDb = TcaDb.getInstance(this);
    final CompositeDisposable mDisposable = new CompositeDisposable();
    ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
    remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
    ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
    localRepository.setDb(tcaDb);
    ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
    chatMessageViewModel.deleteOldEntries();
    // Get all unsent messages from database
    List<ChatMessage> unsentMsg = chatMessageViewModel.getUnsent();
    if (unsentMsg.isEmpty()) {
        return;
    }
    int numberOfAttempts = 0;
    AuthenticationManager am = new AuthenticationManager(this);
    // Try to send the message 5 times
    while (numberOfAttempts < MAX_SEND_TRIES) {
        try {
            for (ChatMessage message : unsentMsg) {
                // Generate signature and store it in the message
                message.setSignature(am.sign(message.getText()));
                // Send the message to the server
                chatMessageViewModel.sendMessage(message.getRoom(), message, this.getApplicationContext());
                Utils.logv("successfully sent message: " + message.getText());
            }
            // Exit the loop
            return;
        } catch (NoPrivateKey noPrivateKey) {
            // Nothing can be done, just exit
            return;
        } catch (Exception e) {
            Utils.log(e);
            numberOfAttempts++;
        }
        // Sleep for five seconds, maybe the server is currently really busy
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Utils.log(e);
        }
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ChatMessageViewModel(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)

Example 10 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class AuthenticationManager method uploadKey.

/**
 * Try to upload the public key to the server and remember that state.
 *
 * @param publicKey
 */
private void uploadKey(String publicKey, final ChatMember member) {
    // If we already uploaded it we don't need to redo that
    if (Utils.getSettingBool(mContext, Const.PUBLIC_KEY_UPLOADED, false)) {
        this.tryToUploadGcmToken();
        return;
    }
    try {
        DeviceRegister dr = DeviceRegister.Companion.getDeviceRegister(mContext, publicKey, member);
        // Upload public key to the server
        TUMCabeClient.getInstance(mContext).deviceRegister(dr, new Callback<TUMCabeStatus>() {

            @Override
            public void onResponse(Call<TUMCabeStatus> call, Response<TUMCabeStatus> response) {
                // Remember that we are done, only if we have submitted with the member information
                if (response.isSuccessful() && "ok".equals(response.body().getStatus())) {
                    if (member != null) {
                        Utils.setSetting(mContext, Const.PUBLIC_KEY_UPLOADED, true);
                    }
                    AuthenticationManager.this.tryToUploadGcmToken();
                }
            }

            @Override
            public void onFailure(Call<TUMCabeStatus> call, Throwable t) {
                Utils.log(t, "Failure uploading public key");
                Utils.setSetting(mContext, Const.PUBLIC_KEY_UPLOADED, false);
            }
        });
    } catch (NoPrivateKey noPrivateKey) {
        this.clearKeys();
    }
}
Also used : NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) TUMCabeStatus(de.tum.in.tumcampusapp.api.app.model.TUMCabeStatus) DeviceRegister(de.tum.in.tumcampusapp.api.app.model.DeviceRegister)

Aggregations

NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)10 ChatMember (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember)4 ChatRoom (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom)4 ChatVerification (de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)3 IOException (java.io.IOException)3 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)2 TUMCabeStatus (de.tum.in.tumcampusapp.api.app.model.TUMCabeStatus)2 ChatRoomController (de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)2 Context (android.content.Context)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 NoPublicKey (de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)1 DeviceRegister (de.tum.in.tumcampusapp.api.app.model.DeviceRegister)1 DeviceUploadGcmToken (de.tum.in.tumcampusapp.api.app.model.DeviceUploadGcmToken)1 TUMOnlineRequest (de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest)1 LecturesSearchRow (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRow)1 LecturesSearchRowSet (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRowSet)1 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)1 ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)1 ChatRoomAndLastMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage)1