Skip to content

Commit fb38ac0

Browse files
committed
feat: add mclo.gs share option to share log dialog
1 parent a74efc0 commit fb38ac0

4 files changed

Lines changed: 152 additions & 2 deletions

File tree

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.util.DisplayMetrics;
4040
import android.util.Log;
4141
import android.view.InputDevice;
42+
import android.view.LayoutInflater;
4243
import android.view.View;
4344
import android.view.WindowManager;
4445
import android.widget.EditText;
@@ -73,6 +74,7 @@
7374
import net.kdt.pojavlaunch.utils.JREUtils;
7475
import net.kdt.pojavlaunch.utils.JSONUtils;
7576
import net.kdt.pojavlaunch.utils.MCOptionUtils;
77+
import net.kdt.pojavlaunch.utils.McLogsApi;
7678
import net.kdt.pojavlaunch.utils.OldVersionsUtils;
7779
import net.kdt.pojavlaunch.value.DependentLibrary;
7880
import net.kdt.pojavlaunch.value.MinecraftAccount;
@@ -1578,11 +1580,66 @@ public static void runOnUiThread(Runnable runnable) {
15781580
return runtime;
15791581
}
15801582

1581-
/** Triggers the share intent chooser, with the latestlog file attached to it */
1583+
/** Shows a dialog letting the user pick between uploading the log to mclo.gs or sharing
1584+
* the raw log file directly, like before this dialog existed. */
15821585
public static void shareLog(Context context){
1586+
View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_share_log, null);
1587+
1588+
AlertDialog dialog = new AlertDialog.Builder(context)
1589+
.setTitle(R.string.share_log_dialog_title)
1590+
.setView(dialogView)
1591+
.setNegativeButton(android.R.string.cancel, null)
1592+
.create();
1593+
1594+
dialogView.findViewById(R.id.share_log_mclogs_button).setOnClickListener(v -> {
1595+
dialog.dismiss();
1596+
shareLogToMcLogs(context);
1597+
});
1598+
dialogView.findViewById(R.id.share_log_file_button).setOnClickListener(v -> {
1599+
dialog.dismiss();
1600+
shareLogFile(context);
1601+
});
1602+
1603+
dialog.show();
1604+
}
1605+
1606+
/** Triggers the share intent chooser, with the latestlog file attached to it */
1607+
private static void shareLogFile(Context context){
15831608
openPath(context, new File(Tools.DIR_GAME_HOME, "latestlog.txt"), true);
15841609
}
15851610

1611+
/** Uploads the latest log file to mclo.gs and shares the resulting link, copying it to the
1612+
* clipboard as well so it isn't lost if the share sheet gets dismissed. */
1613+
private static void shareLogToMcLogs(Context context){
1614+
final File logFile = new File(Tools.DIR_GAME_HOME, "latestlog.txt");
1615+
final ProgressDialog progressDialog = getWaitingDialog(context, R.string.share_log_mclogs_uploading);
1616+
1617+
sExecutorService.execute(() -> {
1618+
try {
1619+
String url = McLogsApi.upload(logFile);
1620+
Tools.runOnUiThread(() -> {
1621+
progressDialog.dismiss();
1622+
1623+
ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
1624+
if(clipboardManager != null) clipboardManager.setPrimaryClip(ClipData.newPlainText("mclo.gs", url));
1625+
Toast.makeText(context, R.string.share_log_mclogs_copied, Toast.LENGTH_SHORT).show();
1626+
1627+
Intent shareIntent = new Intent(Intent.ACTION_SEND);
1628+
shareIntent.setType("text/plain");
1629+
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
1630+
Intent chooserIntent = Intent.createChooser(shareIntent, url);
1631+
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1632+
context.startActivity(chooserIntent);
1633+
});
1634+
} catch (IOException e) {
1635+
Tools.runOnUiThread(() -> {
1636+
progressDialog.dismiss();
1637+
Tools.showError(context, R.string.share_log_mclogs_error, e);
1638+
});
1639+
}
1640+
});
1641+
}
1642+
15861643
/**
15871644
* Determine the MIME type of a File.
15881645
* @param file The file to determine the type of
@@ -1872,4 +1929,4 @@ static class SDL {
18721929
*/
18731930
public static native void initializeControllerSubsystems();
18741931
}
1875-
}
1932+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.kdt.pojavlaunch.utils;
2+
3+
import com.google.gson.Gson;
4+
5+
import net.kdt.pojavlaunch.Tools;
6+
import net.kdt.pojavlaunch.modloaders.modpacks.api.ApiHandler;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
/**
12+
* Minimal client for the mclo.gs log sharing API.
13+
* See <a href="https://api.mclo.gs/">https://api.mclo.gs/</a> for the API documentation.
14+
*/
15+
public class McLogsApi {
16+
private static final String UPLOAD_URL = "https://api.mclo.gs/1/log";
17+
private static final String SOURCE_NAME = "CopperLauncher";
18+
19+
/** JSON body sent to the mclo.gs upload endpoint. */
20+
private static class UploadRequest {
21+
final String content;
22+
final String source = SOURCE_NAME;
23+
UploadRequest(String content) { this.content = content; }
24+
}
25+
26+
/** JSON response returned by the mclo.gs upload endpoint. */
27+
private static class UploadResponse {
28+
boolean success;
29+
String id;
30+
String url;
31+
String error;
32+
}
33+
34+
/**
35+
* Uploads the given log file to mclo.gs.
36+
* @param logFile the log file to upload
37+
* @return the public mclo.gs URL pointing to the uploaded log
38+
* @throws IOException if the log file is empty/missing, the request fails, or the server
39+
* reports an error
40+
*/
41+
public static String upload(File logFile) throws IOException {
42+
if (logFile == null || !logFile.exists() || logFile.length() == 0) {
43+
throw new IOException("Log file is empty or doesn't exist");
44+
}
45+
46+
String content = Tools.read(logFile);
47+
String requestBody = new Gson().toJson(new UploadRequest(content));
48+
String rawResponse = ApiHandler.postRaw(UPLOAD_URL, requestBody);
49+
50+
if (rawResponse == null) {
51+
throw new IOException("No response from mclo.gs");
52+
}
53+
54+
UploadResponse response = new Gson().fromJson(rawResponse, UploadResponse.class);
55+
if (response == null || !response.success || response.url == null) {
56+
String reason = (response != null && response.error != null) ? response.error : "unknown error";
57+
throw new IOException("mclo.gs upload failed: " + reason);
58+
}
59+
60+
return response.url;
61+
}
62+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="vertical"
6+
android:paddingVertical="@dimen/padding_medium">
7+
8+
<!-- Upload the log to mclo.gs and share the resulting link -->
9+
<com.kdt.mcgui.LauncherMenuButton
10+
android:id="@+id/share_log_mclogs_button"
11+
style="@style/LauncherMenuButton.Universal"
12+
android:layout_width="match_parent"
13+
android:drawableStart="@drawable/ic_mclogs_share"
14+
android:text="@string/share_log_mclogs_option" />
15+
16+
<View style="@style/ThickDivider" />
17+
18+
<!-- Share the raw log file, same behaviour as before this dialog existed -->
19+
<com.kdt.mcgui.LauncherMenuButton
20+
android:id="@+id/share_log_file_button"
21+
style="@style/LauncherMenuButton.Universal"
22+
android:layout_width="match_parent"
23+
android:drawableStart="@android:drawable/ic_menu_share"
24+
android:text="@string/main_share_logs" />
25+
26+
</LinearLayout>

app_pojavlauncher/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@
177177
<string name="customctrl_export">Export controlmap</string>
178178

179179
<string name="main_share_logs">Share log file</string>
180+
<string name="share_log_dialog_title">Share log</string>
181+
<string name="share_log_mclogs_option">Share to mclo.gs</string>
182+
<string name="share_log_mclogs_uploading">Uploading log to mclo.gs…</string>
183+
<string name="share_log_mclogs_copied">mclo.gs link copied to clipboard</string>
184+
<string name="share_log_mclogs_error">Failed to upload log to mclo.gs</string>
180185
<string name="main_install_jar_file">Execute a .jar</string>
181186
<string name="main_play">Play</string>
182187
<string name="autoram_info_msg">Memory set to %d MB</string>

0 commit comments

Comments
 (0)