Skip to content

Commit

Permalink
Implement sharing of logs using Hastebin
Browse files Browse the repository at this point in the history
  • Loading branch information
PixelyIon committed Aug 29, 2019
1 parent da55a1d commit 62cb561
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "lightswitch.emu"
minSdkVersion 21
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand Down
52 changes: 49 additions & 3 deletions app/src/main/java/emu/lightswitch/LogActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package emu.lightswitch;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.FileObserver;
Expand All @@ -14,8 +15,13 @@
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import org.json.JSONObject;

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;

Expand All @@ -37,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
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));
log_list.setAdapter(adapter);
log_file = new File(getApplicationInfo().dataDir + "/log.bin");
log_file = new File(getApplicationInfo().dataDir + "/lightswitch.log");
try {
InputStream inputStream = new FileInputStream(log_file);
reader = new BufferedReader(new InputStreamReader(inputStream));
Expand Down Expand Up @@ -110,15 +116,55 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_clear:
try {
FileWriter file = new FileWriter(log_file, false);
file.close();
FileWriter fileWriter = new FileWriter(log_file, false);
fileWriter.close();
} catch (IOException e) {
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.cleared), Toast.LENGTH_LONG).show();
finish();
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:
return super.onOptionsItemSelected(item);
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/emu/lightswitch/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (adapter.getItemViewType(position) == ContentType.Item) {
String path = ((GameItem) parent.getItemAtPosition(position)).getPath();
notifyUser(getString(R.string.launching) + " " + path);
loadFile(path, getApplicationInfo().dataDir + "/shared_prefs/" + getApplicationInfo().packageName + "_preferences.xml", getApplicationInfo().dataDir + "/log.bin");
GameItem item = ((GameItem) parent.getItemAtPosition(position));
notifyUser(getString(R.string.launching) + " " + item.getTitle());
loadFile(item.getPath(), getApplicationInfo().dataDir + "/shared_prefs/" + getApplicationInfo().packageName + "_preferences.xml", getApplicationInfo().dataDir + "/lightswitch.log");
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_share.xml
Original file line number Diff line number Diff line change
@@ -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>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/toolbar_log.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
android:title="@string/search"
app:showAsAction="ifRoom|withText"
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
android:id="@+id/action_clear"
android:icon="@drawable/ic_clear"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
<string name="localization_language">Language</string>
<!-- Toolbar Logger -->
<string name="clear">Clear</string>
<string name="share">Share</string>
<!-- Logger -->
<string name="file_missing">The log file was not found</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>
</resources>

0 comments on commit 62cb561

Please sign in to comment.