Skip to content

Commit

Permalink
Added PCSettings UI, and made receiving json packet
Browse files Browse the repository at this point in the history
  • Loading branch information
km2442 committed Jun 24, 2019
1 parent 9a09c3a commit e12dea9
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 60 deletions.
Binary file modified Android/.idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified Android/.idea/caches/gradle_models.ser
Binary file not shown.
3 changes: 2 additions & 1 deletion Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".AddCustomTask"></activity>
<activity android:name=".PCSettings"></activity>
<activity android:name=".AddCustomTask" />
<activity android:name=".CommunicatorUI" />
<activity android:name=".ConnectWait" />
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ private void TurnMonitorOff() {
}

private void OpenSettings() {
Packet p = new Packet();
p.Exec = "GetSettings";
sendMsg(p);
Intent i = new Intent(CommunicatorUI.this, PCSettings.class);
startActivity(i);
}

public void sendMsg(Packet p) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package dev.kamilklecha.autoswitchmobile;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PCSettings extends AppCompatActivity {

ProgressBar loadingBar;
ConstraintLayout settingsLayout;

Spinner toggleLanguage;
Spinner toggleTheme;
ToggleButton toggleTray;
ToggleButton toggleStatistics;
ToggleButton toggleMultiInstance;
ToggleButton toggleLogging;
ToggleButton toggleWarningHide;
ToggleButton toggleTestMode;

Button btnSave;
Button btnCancel;

public void setJson(String json) {
this.json = json;
}

String json = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pcsettings);

loadingBar = (ProgressBar) findViewById(R.id.PCSettings_LoadingBar);
settingsLayout = (ConstraintLayout) findViewById(R.id.PCSettings_SettingsLayout);

toggleLanguage = (Spinner) findViewById(R.id.PCSettings_LanguageSpinner);
toggleTheme = (Spinner) findViewById(R.id.PCSettings_ThemeSpinner);
toggleTray = (ToggleButton) findViewById(R.id.PCSettings_TrayToggle);
toggleStatistics = (ToggleButton) findViewById(R.id.PCSettings_MultiInstanceToggle);
toggleMultiInstance = (ToggleButton) findViewById(R.id.PCSettings_LoggingToggle);
toggleLogging = (ToggleButton) findViewById(R.id.PCSettings_HideWarningToggle);
toggleWarningHide = (ToggleButton) findViewById(R.id.PCSettings_HideWarningToggle);
toggleTestMode = (ToggleButton) findViewById(R.id.PCSettings_TestModeToggle);

btnSave = (Button) findViewById(R.id.PCSettings_SaveSettings);
btnCancel = (Button) findViewById(R.id.PCSettings_Cancel);

try {
getSettings();
} catch (InterruptedException e) {
e.printStackTrace();
}
loadingBar.setVisibility(View.INVISIBLE);
settingsLayout.setVisibility(View.VISIBLE);
}

private boolean getSettings() throws InterruptedException {
Packet p = new Packet();
p.Exec = "GetSettings";
Hub.cui.sendMsg(p);
final Handler handler = new Handler(Looper.getMainLooper());

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(Hub.sc.socket.getInputStream()));
final String response = in.readLine();

handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(PCSettings.this, response, Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
// Thread thread = new Thread(new Runnable() {
// @Override
// public void run() {
// Socket s = Hub.sc.socket;
// BufferedReader input = null;
// try {
// input = new BufferedReader(new InputStreamReader(s.getInputStream()));
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// while(json.length() == 0) {
// final String st = input.readLine();
// if(st.trim().length() != 0) {
// setJson(st.toString());
// }
// }
// } catch (SocketTimeoutException e) {
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// });
// thread.start();
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package dev.kamilklecha.autoswitchmobile;

import android.os.Handler;
import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class SocketCommunicator {

private static final String TAG = "SocketCommunicator";

public Socket getSocket() {
return socket;
}

public Socket socket;

private OutputStream out;
Expand Down Expand Up @@ -74,51 +71,52 @@ public void run() {
}

public void startReceiver() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
boolean exception = false;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (!exception) {
if(socket.isClosed()) break;

try {
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final String st = input.readLine();

handler.post(new Runnable() {
@Override
public void run() {
if (st.trim().length() != 0) {
Gson gson = new Gson();
JsonElement jsonTree = gson.toJsonTree(gson.toJson(st));
JsonObject jsonObject = jsonTree.getAsJsonObject();
if(jsonObject.getAsString("Type") == "Settings")
Log.d(TAG, "run: Działa xD");
}
}
});
} catch (SocketException e) {
//TODO info
cw.finish();
} catch (IOException e) {
e.printStackTrace();
exception = true;
} catch (NetworkOnMainThreadException e) {
Log.e(TAG, "startReceiver: NetworkOnMainThreadException");
e.printStackTrace();
exception = true;
} catch (Exception e) {
e.printStackTrace();
exception = true;
}
}
}
});
thread.start();
// Thread thread = new Thread(new Runnable() {
// @Override
// public void run() {
// boolean exception = false;
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// while (!exception) {
// if(socket.isClosed()) {
// Log.e(TAG, "Socket Closedd");
// break;
// }
//
// try {
// BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// final String st = input.readLine();
//
// handler.post(new Runnable() {
// @Override
// public void run() {
// if (st.trim().length() != 0) {
// JsonObject json = (JsonObject) new JsonParser().parse(st);
// Set<String> keys = json.keySet();
// Log.e(TAG, keys.toString());
// }
// }
// });
// } catch (SocketException e) {
// //TODO info
// cw.finish();
// } catch (IOException e) {
// e.printStackTrace();
// exception = true;
// } catch (NetworkOnMainThreadException e) {
// Log.e(TAG, "startReceiver: NetworkOnMainThreadException");
// e.printStackTrace();
// exception = true;
// } catch (Exception e) {
// e.printStackTrace();
// exception = true;
// }
// }
// }
// });
// thread.start();
}
}
Loading

0 comments on commit e12dea9

Please sign in to comment.