|
18 | 18 |
|
19 | 19 | package com.vrem.wifianalyzer.about; |
20 | 20 |
|
| 21 | +import android.app.Activity; |
21 | 22 | import android.app.AlertDialog; |
22 | 23 | import android.content.ActivityNotFoundException; |
23 | 24 | import android.content.DialogInterface; |
|
26 | 27 | import android.os.Build; |
27 | 28 | import android.os.Bundle; |
28 | 29 | import android.support.annotation.NonNull; |
29 | | -import android.support.annotation.RawRes; |
30 | | -import android.support.annotation.StringRes; |
31 | 30 | import android.support.v4.app.Fragment; |
32 | 31 | import android.view.LayoutInflater; |
33 | 32 | import android.view.View; |
| 33 | +import android.view.View.OnClickListener; |
34 | 34 | import android.view.ViewGroup; |
35 | 35 | import android.widget.TextView; |
36 | 36 | import android.widget.Toast; |
|
46 | 46 |
|
47 | 47 | public class AboutFragment extends Fragment { |
48 | 48 | private static final String YEAR_FORMAT = "yyyy"; |
49 | | - private AlertDialog alertDialog; |
50 | 49 |
|
51 | 50 | @Override |
52 | 51 | public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
53 | 52 | View view = inflater.inflate(R.layout.about_content, container, false); |
54 | 53 | setCopyright(view); |
55 | 54 | setExtraInformation(view); |
| 55 | + setOnClicks(view); |
56 | 56 | return view; |
57 | 57 | } |
58 | 58 |
|
| 59 | + private void setOnClicks(View view) { |
| 60 | + AlertDialogClickListener gpl = new AlertDialogClickListener(getActivity(), R.string.gpl, R.raw.gpl); |
| 61 | + view.findViewById(R.id.license).setOnClickListener(gpl); |
| 62 | + |
| 63 | + AlertDialogClickListener contributors = new AlertDialogClickListener(getActivity(), R.string.about_contributor_title, R.raw.contributors, false); |
| 64 | + view.findViewById(R.id.contributors).setOnClickListener(contributors); |
| 65 | + |
| 66 | + AlertDialogClickListener al = new AlertDialogClickListener(getActivity(), R.string.al, R.raw.al); |
| 67 | + view.findViewById(R.id.apacheCommonsLicense).setOnClickListener(al); |
| 68 | + view.findViewById(R.id.graphViewLicense).setOnClickListener(al); |
| 69 | + view.findViewById(R.id.materialDesignIconsLicense).setOnClickListener(al); |
| 70 | + |
| 71 | + view.findViewById(R.id.writeReview).setOnClickListener(new WriteReviewClickListener(getActivity())); |
| 72 | + } |
| 73 | + |
59 | 74 | private void setCopyright(View view) { |
60 | 75 | String year = new SimpleDateFormat(YEAR_FORMAT).format(new Date()); |
61 | 76 | String message = getResources().getString(R.string.app_copyright); |
@@ -85,60 +100,65 @@ private void setText(View view, int id, String text) { |
85 | 100 | } |
86 | 101 | } |
87 | 102 |
|
88 | | - public void writeReview(@NonNull View view) { |
89 | | - String url = "market://details?id=" + BuildConfig.APPLICATION_ID; |
90 | | - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
91 | | - try { |
92 | | - startActivity(intent); |
93 | | - } catch (ActivityNotFoundException e) { |
94 | | - Toast.makeText(view.getContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); |
95 | | - } |
96 | | - } |
| 103 | + static class WriteReviewClickListener implements OnClickListener { |
| 104 | + private final Activity activity; |
97 | 105 |
|
98 | | - public void showALv2(@NonNull View view) { |
99 | | - alertDialog = show(view, R.string.al, R.raw.al); |
100 | | - changeFont(alertDialog); |
101 | | - } |
| 106 | + private WriteReviewClickListener(@NonNull Activity activity) { |
| 107 | + this.activity = activity; |
| 108 | + } |
102 | 109 |
|
103 | | - public void showGPLv3(@NonNull View view) { |
104 | | - alertDialog = show(view, R.string.gpl, R.raw.gpl); |
105 | | - changeFont(alertDialog); |
| 110 | + @Override |
| 111 | + public void onClick(View view) { |
| 112 | + String url = "market://details?id=" + BuildConfig.APPLICATION_ID; |
| 113 | + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 114 | + try { |
| 115 | + activity.startActivity(intent); |
| 116 | + } catch (ActivityNotFoundException e) { |
| 117 | + Toast.makeText(view.getContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); |
| 118 | + } |
| 119 | + } |
106 | 120 | } |
107 | 121 |
|
108 | | - public void showContributors(@NonNull View view) { |
109 | | - alertDialog = show(view, R.string.about_contributor_title, R.raw.contributors); |
110 | | - } |
| 122 | + static class AlertDialogClickListener implements OnClickListener { |
| 123 | + private final Activity activity; |
| 124 | + private final int titleId; |
| 125 | + private final int resourceId; |
| 126 | + private final boolean isSmallFont; |
111 | 127 |
|
112 | | - private AlertDialog show(@NonNull View view, @StringRes int titleId, @RawRes int id) { |
113 | | - if (getActivity().isFinishing()) { |
114 | | - return null; |
| 128 | + AlertDialogClickListener(@NonNull Activity activity, int titleId, int resourceId) { |
| 129 | + this(activity, titleId, resourceId, true); |
115 | 130 | } |
116 | | - String text = FileUtils.readFile(getResources(), id); |
117 | | - AlertDialog dialog = new AlertDialog |
118 | | - .Builder(view.getContext()) |
119 | | - .setTitle(titleId) |
120 | | - .setMessage(text) |
121 | | - .setNeutralButton(android.R.string.ok, new Close()) |
122 | | - .create(); |
123 | | - dialog.show(); |
124 | | - return dialog; |
125 | | - } |
126 | 131 |
|
127 | | - private void changeFont(AlertDialog alertDialog) { |
128 | | - if (alertDialog != null) { |
129 | | - TextView textView = alertDialog.findViewById(android.R.id.message); |
130 | | - textView.setTextSize(8); |
| 132 | + AlertDialogClickListener(@NonNull Activity activity, int titleId, int resourceId, boolean isSmallFont) { |
| 133 | + this.activity = activity; |
| 134 | + this.titleId = titleId; |
| 135 | + this.resourceId = resourceId; |
| 136 | + this.isSmallFont = isSmallFont; |
131 | 137 | } |
132 | | - } |
133 | | - |
134 | | - AlertDialog getAlertDialog() { |
135 | | - return alertDialog; |
136 | | - } |
137 | 138 |
|
138 | | - private static class Close implements DialogInterface.OnClickListener { |
139 | 139 | @Override |
140 | | - public void onClick(DialogInterface dialog, int which) { |
141 | | - dialog.dismiss(); |
| 140 | + public void onClick(View view) { |
| 141 | + if (!activity.isFinishing()) { |
| 142 | + String text = FileUtils.readFile(activity.getResources(), resourceId); |
| 143 | + AlertDialog alertDialog = new AlertDialog |
| 144 | + .Builder(view.getContext()) |
| 145 | + .setTitle(titleId) |
| 146 | + .setMessage(text) |
| 147 | + .setNeutralButton(android.R.string.ok, new Close()) |
| 148 | + .create(); |
| 149 | + alertDialog.show(); |
| 150 | + if (isSmallFont) { |
| 151 | + TextView textView = alertDialog.findViewById(android.R.id.message); |
| 152 | + textView.setTextSize(8); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static class Close implements DialogInterface.OnClickListener { |
| 158 | + @Override |
| 159 | + public void onClick(DialogInterface dialog, int which) { |
| 160 | + dialog.dismiss(); |
| 161 | + } |
142 | 162 | } |
143 | 163 | } |
144 | 164 |
|
|
0 commit comments