11package com .d4rk .androidtutorials .java .utils ;
22
3+ import android .app .Activity ;
34import android .content .Context ;
4- import android .os .Handler ;
5- import android .os .Looper ;
6- import android .util .Log ;
5+ import android .content .Intent ;
76
8- import com .d4rk .androidtutorials .java .R ;
9-
10- import org .commonmark .node .Node ;
11- import org .commonmark .parser .Parser ;
12- import org .commonmark .renderer .html .HtmlRenderer ;
13-
14- import java .io .BufferedReader ;
15- import java .io .InputStreamReader ;
16- import java .net .HttpURLConnection ;
17- import java .net .URL ;
18- import java .util .Objects ;
19- import java .util .concurrent .ExecutorService ;
20- import java .util .concurrent .Executors ;
21- import java .util .regex .Matcher ;
22- import java .util .regex .Pattern ;
23-
24- public class OpenSourceLicensesUtils {
25- private static final String TAG = "OpenSourceLicensesUtils" ;
26- private static final ExecutorService executor = Executors .newSingleThreadExecutor ();
27- private static final Handler mainHandler = new Handler (Looper .getMainLooper ());
28-
29- public static void loadHtmlData (final Context context , final HtmlDataCallback callback ) {
30- executor .execute (() -> {
31- String packageName = context .getPackageName ();
32- String currentVersion = getAppVersion (context );
33- String changelogUrl = "https://raw.githubusercontent.com/MihaiCristianCondrea/" + packageName + "/refs/heads/main/CHANGELOG.md" ;
34- String eulaUrl = "https://raw.githubusercontent.com/MihaiCristianCondrea/" + packageName + "/refs/heads/main/EULA.md" ;
35-
36- String changelogMarkdown = loadMarkdown (context , changelogUrl , R .string .error_loading_changelog );
37- String extractedChangelog = extractLatestVersionChangelog (changelogMarkdown , currentVersion );
38- String changelogHtml = markdownToHtml (extractedChangelog );
39-
40- String eulaMarkdown = loadMarkdown (context , eulaUrl , R .string .error_loading_eula );
41- String eulaHtml = markdownToHtml (eulaMarkdown );
7+ import androidx .annotation .Nullable ;
428
43- mainHandler .post (() -> callback .onHtmlDataLoaded (changelogHtml , eulaHtml ));
44- });
45- }
9+ import com .d4rk .androidtutorials .java .R ;
10+ import com .google .android .gms .oss .licenses .OssLicensesMenuActivity ;
4611
47- private static String loadMarkdown (Context context , String urlString , int errorStringId ) {
48- HttpURLConnection connection = null ;
49- BufferedReader reader = null ;
50- try {
51- URL url = new URL (urlString );
52- connection = (HttpURLConnection ) url .openConnection ();
53- connection .setRequestMethod ("GET" );
54- connection .setConnectTimeout (10000 );
55- connection .setReadTimeout (10000 );
12+ public final class OpenSourceLicensesUtils {
5613
57- int responseCode = connection .getResponseCode ();
58- if (responseCode == HttpURLConnection .HTTP_OK ) {
59- reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
60- StringBuilder content = new StringBuilder ();
61- String line ;
62- while ((line = reader .readLine ()) != null ) {
63- content .append (line ).append ("\n " );
64- }
65- return content .toString ();
66- } else {
67- Log .e (TAG , "Failed to load URL: " + urlString + " with response code: " + responseCode );
68- return context .getString (errorStringId );
69- }
70- } catch (Exception e ) {
71- Log .e (TAG , "Error loading markdown from URL: " + urlString , e );
72- return context .getString (errorStringId );
73- } finally {
74- if (reader != null ) {
75- try {
76- reader .close ();
77- } catch (Exception e ) {
78- Log .e (TAG , "Error closing reader" , e );
79- }
80- }
81- if (connection != null ) {
82- connection .disconnect ();
83- }
84- }
14+ private OpenSourceLicensesUtils () {
15+ // Utility class
8516 }
8617
87- private static String extractLatestVersionChangelog (String markdown , String currentVersion ) {
88- // Define the regex pattern to match the latest version section
89- String regexPattern = "(?m)^#\\ s+Version\\ s+" + Pattern .quote (currentVersion ) + ":\\ s*(.*?)^(#\\ s+Version\\ s+|$)" ;
90- Pattern pattern = Pattern .compile (regexPattern , Pattern .DOTALL | Pattern .MULTILINE );
91- Matcher matcher = pattern .matcher (markdown );
92-
93- if (matcher .find ()) {
94- // Group 1 contains the changelog for the current version
95- return Objects .requireNonNull (matcher .group (1 )).trim ();
96- } else {
97- Log .e (TAG , "No changelog available for version " + currentVersion );
98- return "No changelog available for version " + currentVersion ;
18+ public static void openLicensesScreen (@ Nullable Context context ) {
19+ if (context == null ) {
20+ return ;
9921 }
100- }
10122
102- private static String markdownToHtml (String markdown ) {
103- Parser parser = Parser .builder ().build ();
104- HtmlRenderer renderer = HtmlRenderer .builder ().build ();
105- Node document = parser .parse (markdown );
106- return renderer .render (document );
107- }
23+ OssLicensesMenuActivity .setActivityTitle (context .getString (R .string .open_source_licenses ));
10824
109- private static String getAppVersion (Context context ) {
110- try {
111- return context .getPackageManager ()
112- .getPackageInfo (context .getPackageName (), 0 )
113- .versionName ;
114- } catch (Exception e ) {
115- Log .e (TAG , "Error getting app version" , e );
116- return "1.0.0" ; // Fallback version
25+ Intent intent = new Intent (context , OssLicensesMenuActivity .class );
26+ if (!(context instanceof Activity )) {
27+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
11728 }
118- }
11929
120- public interface HtmlDataCallback {
121- void onHtmlDataLoaded (String changelogHtml , String eulaHtml );
30+ context .startActivity (intent );
12231 }
12332}
0 commit comments