Implement sharing of logs using Hastebin

This commit is contained in:
◱ PixelyIon 2019-08-29 18:10:39 +05:30
parent da55a1d9ba
commit 62cb561888
7 changed files with 66 additions and 7 deletions

View File

@ -5,7 +5,7 @@ android {
buildToolsVersion "29.0.0" buildToolsVersion "29.0.0"
defaultConfig { defaultConfig {
applicationId "lightswitch.emu" applicationId "lightswitch.emu"
minSdkVersion 21 minSdkVersion 24
targetSdkVersion 29 targetSdkVersion 29
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"

View File

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -1,5 +1,6 @@
package emu.lightswitch; package emu.lightswitch;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.FileObserver; import android.os.FileObserver;
@ -14,8 +15,13 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.json.JSONObject;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
import static java.lang.Thread.interrupted; import static java.lang.Thread.interrupted;
@ -37,7 +43,7 @@ public class LogActivity extends AppCompatActivity {
final ListView log_list = this.findViewById(R.id.log_list); final ListView log_list = this.findViewById(R.id.log_list);
adapter = new LogAdapter(this, Integer.parseInt(prefs.getString("log_level", "3")), getResources().getStringArray(R.array.log_level)); adapter = new LogAdapter(this, Integer.parseInt(prefs.getString("log_level", "3")), getResources().getStringArray(R.array.log_level));
log_list.setAdapter(adapter); log_list.setAdapter(adapter);
log_file = new File(getApplicationInfo().dataDir + "/log.bin"); log_file = new File(getApplicationInfo().dataDir + "/lightswitch.log");
try { try {
InputStream inputStream = new FileInputStream(log_file); InputStream inputStream = new FileInputStream(log_file);
reader = new BufferedReader(new InputStreamReader(inputStream)); reader = new BufferedReader(new InputStreamReader(inputStream));
@ -110,8 +116,8 @@ public class LogActivity extends AppCompatActivity {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_clear: case R.id.action_clear:
try { try {
FileWriter file = new FileWriter(log_file, false); FileWriter fileWriter = new FileWriter(log_file, false);
file.close(); fileWriter.close();
} catch (IOException e) { } catch (IOException e) {
Log.w("Logger", "IO Error while clearing the log file: " + e.getMessage()); Log.w("Logger", "IO Error while clearing the log file: " + e.getMessage());
Toast.makeText(getApplicationContext(), getString(R.string.io_error) + ": " + e.getMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), getString(R.string.io_error) + ": " + e.getMessage(), Toast.LENGTH_LONG).show();
@ -119,6 +125,46 @@ public class LogActivity extends AppCompatActivity {
Toast.makeText(getApplicationContext(), getString(R.string.cleared), Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), getString(R.string.cleared), Toast.LENGTH_LONG).show();
finish(); finish();
return true; return true;
case R.id.action_share_log:
Thread share_thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://hastebin.com/documents");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
FileReader fileReader = new FileReader(log_file);
int chr;
while ((chr = fileReader.read()) != -1) {
bufferedWriter.write(chr);
}
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//urlConnection.connect();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String key = new JSONObject(bufferedReader.lines().collect(Collectors.joining())).getString("key");
bufferedReader.close();
inputStream.close();
urlConnection.disconnect();
String result = "https://hastebin.com/" + key;
Intent sharingIntent = new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, result);
startActivity(Intent.createChooser(sharingIntent, "Share log url with:"));
} catch (Exception e) {
runOnUiThread(new Runnable() {@Override public void run() {Toast.makeText(getApplicationContext(), getString(R.string.share_error), Toast.LENGTH_LONG).show();}});
e.printStackTrace();
}
}});
share_thread.start();
try {
share_thread.join();
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), getString(R.string.share_error), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }

View File

@ -111,9 +111,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (adapter.getItemViewType(position) == ContentType.Item) { if (adapter.getItemViewType(position) == ContentType.Item) {
String path = ((GameItem) parent.getItemAtPosition(position)).getPath(); GameItem item = ((GameItem) parent.getItemAtPosition(position));
notifyUser(getString(R.string.launching) + " " + path); notifyUser(getString(R.string.launching) + " " + item.getTitle());
loadFile(path, getApplicationInfo().dataDir + "/shared_prefs/" + getApplicationInfo().packageName + "_preferences.xml", getApplicationInfo().dataDir + "/log.bin"); loadFile(item.getPath(), getApplicationInfo().dataDir + "/shared_prefs/" + getApplicationInfo().packageName + "_preferences.xml", getApplicationInfo().dataDir + "/lightswitch.log");
} }
} }
}); });

View File

@ -0,0 +1,5 @@
<vector android:alpha="0.85" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
</vector>

View File

@ -7,6 +7,11 @@
android:title="@string/search" android:title="@string/search"
app:showAsAction="ifRoom|withText" app:showAsAction="ifRoom|withText"
app:actionViewClass="androidx.appcompat.widget.SearchView"/> app:actionViewClass="androidx.appcompat.widget.SearchView"/>
<item
android:id="@+id/action_share_log"
android:icon="@drawable/ic_share"
android:title="@string/share"
app:showAsAction="ifRoom"/>
<item <item
android:id="@+id/action_clear" android:id="@+id/action_clear"
android:icon="@drawable/ic_clear" android:icon="@drawable/ic_clear"

View File

@ -22,8 +22,10 @@
<string name="localization_language">Language</string> <string name="localization_language">Language</string>
<!-- Toolbar Logger --> <!-- Toolbar Logger -->
<string name="clear">Clear</string> <string name="clear">Clear</string>
<string name="share">Share</string>
<!-- Logger --> <!-- Logger -->
<string name="file_missing">The log file was not found</string> <string name="file_missing">The log file was not found</string>
<string name="io_error">An I/O error has occurred</string> <string name="io_error">An I/O error has occurred</string>
<string name="share_error">An error has occurred while sharing</string>
<string name="cleared">The logs have been cleared</string> <string name="cleared">The logs have been cleared</string>
</resources> </resources>