summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/AboutDialogFragment.java
blob: 0d9fc45856efdde5692793d1c680278fcecf44d1 (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
/*
 *
 *  * This file is part of the LibreOffice project.
 *  * This Source Code Form is subject to the terms of the Mozilla Public
 *  * License, v. 2.0. If a copy of the MPL was not distributed with this
 *  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

package org.libreoffice;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

public class AboutDialogFragment extends DialogFragment {

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        @SuppressLint("InflateParams") //suppressed because the view will be placed in a dialog
        View messageView = getActivity().getLayoutInflater().inflate(R.layout.about, null, false);

        // When linking text, force to always use default color. This works
        // around a pressed color state bug.
        TextView textView = messageView.findViewById(R.id.about_credits);
        int defaultColor = textView.getTextColors().getDefaultColor();
        textView.setTextColor(defaultColor);

        // Take care of placeholders and set text in version and vendor text views.
        try
        {
            String versionName = getActivity().getPackageManager()
                    .getPackageInfo(getActivity().getPackageName(), 0).versionName;
            String version = String.format(getString(R.string.app_version), versionName, BuildConfig.BUILD_ID_SHORT);
            @SuppressWarnings("deprecation") // since 24 with additional option parameter
            Spanned versionString = Html.fromHtml(version);
            TextView versionView = messageView.findViewById(R.id.about_version);
            versionView.setText(versionString);
            versionView.setMovementMethod(LinkMovementMethod.getInstance());
            TextView vendorView = messageView.findViewById(R.id.about_vendor);
            String vendor = getString(R.string.app_vendor).replace("$VENDOR", BuildConfig.VENDOR);
            vendorView.setText(vendor);
        }
        catch (PackageManager.NameNotFoundException e)
        {
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder .setIcon(R.mipmap.ic_launcher)
                .setTitle(R.string.app_name)
                .setView(messageView)
                .setNegativeButton(R.string.about_license, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        loadFromAbout(R.raw.license);
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.about_notice, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        loadFromAbout(R.raw.notice);
                        dialog.dismiss();
                    }
                });

        // when privacy policy URL is set (via '--with-privacy-policy-url=<url>' autogen option),
        // add button to open that URL
        final String privacyUrl = BuildConfig.PRIVACY_POLICY_URL;
        if (!privacyUrl.isEmpty() && privacyUrl != "undefined") {
            builder.setNeutralButton(R.string.about_privacy_policy, (DialogInterface dialog, int id) -> {
                Intent openPrivacyUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(privacyUrl));
                startActivity(openPrivacyUrlIntent);
                dialog.dismiss();
            });
        }

        return builder.create();
    }

    private void loadFromAbout(int resourceId) {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/" + resourceId));
        String packageName = getActivity().getApplicationContext().getPackageName();
        ComponentName componentName = new ComponentName(packageName, LibreOfficeMainActivity.class.getName());
        i.setComponent(componentName);
        getActivity().startActivity(i);
    }
}