1 /*
2  *
3  *  * This file is part of the LibreOffice project.
4  *  * This Source Code Form is subject to the terms of the Mozilla Public
5  *  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  *  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  */
9 
10 package org.libreoffice;
11 
12 import android.annotation.SuppressLint;
13 import android.app.AlertDialog;
14 import android.app.Dialog;
15 import android.content.ComponentName;
16 import android.content.DialogInterface;
17 import android.content.Intent;
18 import android.content.pm.PackageManager;
19 import android.net.Uri;
20 import android.os.Bundle;
21 import android.support.annotation.NonNull;
22 import android.support.v4.app.DialogFragment;
23 import android.text.Html;
24 import android.text.Spanned;
25 import android.text.method.LinkMovementMethod;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 public class AboutDialogFragment extends DialogFragment {
30 
31     @NonNull @Override
onCreateDialog(Bundle savedInstanceState)32     public Dialog onCreateDialog(Bundle savedInstanceState) {
33 
34         @SuppressLint("InflateParams") //suppressed because the view will be placed in a dialog
35         View messageView = getActivity().getLayoutInflater().inflate(R.layout.about, null, false);
36 
37         // When linking text, force to always use default color. This works
38         // around a pressed color state bug.
39         TextView textView = messageView.findViewById(R.id.about_credits);
40         int defaultColor = textView.getTextColors().getDefaultColor();
41         textView.setTextColor(defaultColor);
42 
43         // Take care of placeholders in the version and vendor text views.
44         TextView versionView = messageView.findViewById(R.id.about_version);
45         TextView vendorView = messageView.findViewById(R.id.about_vendor);
46         try
47         {
48             String versionName = getActivity().getPackageManager()
49                     .getPackageInfo(getActivity().getPackageName(), 0).versionName;
50             String[] tokens = versionName.split("/");
51             if (tokens.length == 3)
52             {
53                 String version = String.format(versionView.getText().toString().replace("\n", "<br/>"),
54                         tokens[0], "<a href=\"https://hub.libreoffice.org/git-core/" + tokens[1] + "\">" + tokens[1] + "</a>");
55                 @SuppressWarnings("deprecation") // since 24 with additional option parameter
56                 Spanned versionString = Html.fromHtml(version);
57                 versionView.setText(versionString);
58                 versionView.setMovementMethod(LinkMovementMethod.getInstance());
59                 String vendor = vendorView.getText().toString();
60                 vendor = vendor.replace("$VENDOR", tokens[2]);
61                 vendorView.setText(vendor);
62             }
63             else
64                 throw new PackageManager.NameNotFoundException();
65         }
66         catch (PackageManager.NameNotFoundException e)
67         {
68             versionView.setText("");
69             vendorView.setText("");
70         }
71 
72         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
73         builder .setIcon(R.drawable.lo_icon)
74                 .setTitle(R.string.app_name)
75                 .setView(messageView)
76                 .setNegativeButton(R.string.about_license, new DialogInterface.OnClickListener() {
77                     @Override
78                     public void onClick(DialogInterface dialog, int id) {
79                         loadFromAbout(R.raw.license);
80                         dialog.dismiss();
81                     }
82                 })
83                 .setPositiveButton(R.string.about_notice, new DialogInterface.OnClickListener() {
84                     @Override
85                     public void onClick(DialogInterface dialog, int id) {
86                         loadFromAbout(R.raw.notice);
87                         dialog.dismiss();
88                     }
89                 })
90                 .setNeutralButton(R.string.about_moreinfo, new DialogInterface.OnClickListener() {
91                     @Override
92                     public void onClick(DialogInterface dialog, int id) {
93                         loadFromAbout(R.raw.example);
94                         dialog.dismiss();
95                     }
96                 });
97 
98         return builder.create();
99     }
100 
loadFromAbout(int resourceId)101     private void loadFromAbout(int resourceId) {
102         Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/" + resourceId));
103         String packageName = getActivity().getApplicationContext().getPackageName();
104         ComponentName componentName = new ComponentName(packageName, LibreOfficeMainActivity.class.getName());
105         i.setComponent(componentName);
106         getActivity().startActivity(i);
107     }
108 }
109