Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PushNotificationActivity.java « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cdc20ed50febfa63d4968aebb8973632be175e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package it.niedermann.nextcloud.deck.ui;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import it.niedermann.android.util.ColorUtil;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.api.IResponseCallback;
import it.niedermann.nextcloud.deck.databinding.ActivityPushNotificationBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.ui.card.EditActivity;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.util.ProjectUtil;

import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;

public class PushNotificationActivity extends AppCompatActivity {

    private ActivityPushNotificationBinding binding;
    private PushNotificationViewModel viewModel;

    // Provided by Files app NotificationJob
    private static final String KEY_SUBJECT = "subject";
    private static final String KEY_MESSAGE = "message";
    private static final String KEY_LINK = "link";
    private static final String KEY_CARD_REMOTE_ID = "objectId";
    private static final String KEY_ACCOUNT = "account";

    @Override
    protected void onResume() {
        super.onResume();

        Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler(this));

        if (getIntent() == null) {
            throw new IllegalArgumentException("Could not retrieve intent");
        }

        binding = ActivityPushNotificationBinding.inflate(getLayoutInflater());
        viewModel = new ViewModelProvider(this).get(PushNotificationViewModel.class);

        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);

        binding.subject.setText(getIntent().getStringExtra(KEY_SUBJECT));

        final String message = getIntent().getStringExtra(KEY_MESSAGE);
        if (!TextUtils.isEmpty(message)) {
            binding.message.setText(message);
            binding.message.setVisibility(View.VISIBLE);
        }

        final String link = getIntent().getStringExtra(KEY_LINK);
        long[] ids = ProjectUtil.extractBoardIdAndCardIdFromUrl(link);

        binding.cancel.setOnClickListener((v) -> finish());

        final String cardRemoteIdString = getIntent().getStringExtra(KEY_CARD_REMOTE_ID);
        final String accountString = getIntent().getStringExtra(KEY_ACCOUNT);

        DeckLog.verbose("cardRemoteIdString = " + cardRemoteIdString);
        if (ids.length == 2) {
            if (cardRemoteIdString != null) {
                try {
                    final int cardRemoteId = Integer.parseInt(cardRemoteIdString);
                    observeOnce(viewModel.readAccount(accountString), this, (account -> {
                        if (account != null) {
                            viewModel.setAccount(account.getName());
                            DeckLog.verbose("account: " + account);
                            observeOnce(viewModel.getBoardByRemoteId(account.getId(), ids[0]), PushNotificationActivity.this, (board -> {
                                DeckLog.verbose("BoardLocalId " + board);
                                if (board != null) {
                                    observeOnce(viewModel.getCardByRemoteID(account.getId(), cardRemoteId), PushNotificationActivity.this, (card -> {
                                        DeckLog.verbose("Card: " + card);
                                        if (card != null) {
                                            viewModel.synchronizeCard(new IResponseCallback<Boolean>(account) {
                                                @Override
                                                public void onResponse(Boolean response) {
                                                    openCardOnSubmit(account, board.getLocalId(), card.getLocalId());
                                                }

                                                @Override
                                                public void onError(Throwable throwable) {
                                                    super.onError(throwable);
                                                    openCardOnSubmit(account, board.getLocalId(), card.getLocalId());
                                                }
                                            }, card);
                                        } else {
                                            DeckLog.info("Card is not yet available locally. Synchronize board with localId " + board);

                                            viewModel.synchronizeBoard(new IResponseCallback<Boolean>(account) {
                                                @Override
                                                public void onResponse(Boolean response) {
                                                    runOnUiThread(() -> {
                                                        observeOnce(viewModel.getCardByRemoteID(account.getId(), cardRemoteId), PushNotificationActivity.this, (card -> {
                                                            DeckLog.verbose("Card: " + card);
                                                            if (card != null) {
                                                                openCardOnSubmit(account, board.getLocalId(), card.getLocalId());
                                                            } else {
                                                                DeckLog.warn("Something went wrong while synchronizing the card " + cardRemoteId + " (cardRemoteId). Given fullCard is null.");
                                                                applyBrandToSubmitButton(account);
                                                                fallbackToBrowser(link);
                                                            }
                                                        }));
                                                    });
                                                }

                                                @Override
                                                public void onError(Throwable throwable) {
                                                    super.onError(throwable);
                                                    DeckLog.warn("Something went wrong while synchronizing the board with localId " + board + ".");
                                                    applyBrandToSubmitButton(account);
                                                    fallbackToBrowser(link);
                                                }
                                            }, board.getLocalId());
                                        }
                                    }));
                                } else {
                                    DeckLog.warn("Given localBoardId for cardRemoteId " + cardRemoteId + " is null.");
                                    applyBrandToSubmitButton(account);
                                    fallbackToBrowser(link);
                                }
                            }));
                        } else {
                            DeckLog.warn("Given account for " + accountString + " is null.");
                            fallbackToBrowser(link);
                        }
                    }));
                } catch (NumberFormatException e) {
                    DeckLog.logError(e);
                    fallbackToBrowser(link);
                }
            } else {
                DeckLog.warn(KEY_CARD_REMOTE_ID + " is null.");
                fallbackToBrowser(link);
            }
        } else {
            DeckLog.warn("Link does not contain two IDs (expected one board id and one card id): " + link);
            fallbackToBrowser(link);
        }
    }

    private void openCardOnSubmit(@NonNull Account account, long boardLocalId, long cardlocalId) {
        runOnUiThread(() -> {
            binding.submit.setOnClickListener((v) -> launchEditActivity(account, boardLocalId, cardlocalId));
            binding.submit.setText(R.string.simple_open);
            applyBrandToSubmitButton(account);
            binding.submit.setEnabled(true);
            binding.progress.setVisibility(View.INVISIBLE);
        });
    }

    /**
     * If anything goes wrong and we cannot open the card directly, we fall back to open the given link in the webbrowser
     */
    private void fallbackToBrowser(String link) {
        DeckLog.warn("Falling back to browser as notification handler.");
        runOnUiThread(() -> {
            try {
                final Uri uri = Uri.parse(link);
                binding.submit.setOnClickListener((v) -> {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(browserIntent);
                });
                binding.submit.setText(R.string.open_in_browser);
                binding.submit.setEnabled(true);
                binding.progress.setVisibility(View.INVISIBLE);
            } catch (Throwable t) {
                DeckLog.logError(t);
            }
        });
    }

    @UiThread
    private void launchEditActivity(@NonNull Account account, Long boardId, Long cardId) {
        DeckLog.info("starting " + EditActivity.class.getSimpleName() + " with [" + account + ", " + boardId + ", " + cardId + "]");
        startActivity(EditActivity.createEditCardIntent(this, account, boardId, cardId));
        finish();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish(); // close this activity as oppose to navigating up
        return true;
    }

    // TODO implement Branded interface
    // TODO apply branding based on board color
    public void applyBrandToSubmitButton(@NonNull Account account) {
        @ColorInt final int mainColor = account.getColor();
        try {
            binding.submit.setBackgroundColor(mainColor);
            binding.submit.setTextColor(ColorUtil.INSTANCE.getForegroundColorForBackgroundColor(mainColor));
        } catch (Throwable t) {
            DeckLog.logError(t);
        }
    }
}