summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/fragments/MotdFragment.java
blob: 16834ab5e0de3421e35212d603cf0259a6e80646 (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
package se.leap.bitmaskclient.base.fragments;

import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_VPN_FRAGMENT;
import static se.leap.bitmaskclient.base.models.Constants.EXTRA_MOTD_MSG;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;

import java.util.Locale;

import de.blinkt.openvpn.core.VpnStatus;
import motd.IMessage;
import motd.Motd;
import se.leap.bitmaskclient.base.MainActivity;
import se.leap.bitmaskclient.databinding.FMotdBinding;


public class MotdFragment extends Fragment {

    private static final String TAG = MotdFragment.class.getSimpleName();
    private IMessage message;
    FMotdBinding binding;
    AppCompatTextView messageView;
    AppCompatImageButton nextButton;

    public MotdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            String messageString = getArguments().getString(EXTRA_MOTD_MSG);
            if (messageString != null) {
                Log.d(TAG, "MotdFragment received: " + messageString);
                message = Motd.newMessage(messageString);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FMotdBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        messageView = binding.motdContent;
        nextButton = binding.nextBtn;
        String currentLang = Locale.getDefault().getLanguage();
        String text = message.getLocalizedText(currentLang);
        if (TextUtils.isEmpty(text)) {
            text = message.getLocalizedText("en");
        }

        if (TextUtils.isEmpty(text)) {
            String error = "Message of the day cannot be shown. Unsupported app language and unknown default langauge.";
            Log.e(TAG, error);
            VpnStatus.logError(error);
            showVpnFragment(view.getContext());
            return;
        }

        Log.d(TAG, "set motd text: " + text);
        messageView.setText(Html.fromHtml(text));
        messageView.setMovementMethod(LinkMovementMethod.getInstance());
        nextButton.setOnClickListener(v -> {
                showVpnFragment(v.getContext());
        });

    }

    private void showVpnFragment(Context context) {
        try {
            Intent intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setAction(ACTION_SHOW_VPN_FRAGMENT);
            context.startActivity(intent);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }

    }
}