Skip to content

Commit 0f1b922

Browse files
authored
Merge pull request #2 from mightytry/1-Auto-Updater
Added updater to app
2 parents 0da2365 + 554057d commit 0f1b922

18 files changed

Lines changed: 647 additions & 286 deletions

File tree

.idea/misc.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "com.example.picturetopc"
1010
minSdk 29
1111
targetSdk 32
12-
versionCode 1
13-
versionName "1.0"
12+
versionCode 2
13+
versionName "0.8.0"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}
@@ -32,7 +32,11 @@ android {
3232
}
3333

3434
dependencies {
35-
35+
implementation 'androidx.navigation:navigation-fragment:2.3.5'
36+
implementation 'androidx.navigation:navigation-ui:2.3.5'
37+
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
38+
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
39+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
3640
def camerax_version = "1.1.0"
3741
implementation "androidx.camera:camera-core:${camerax_version}"
3842
implementation "androidx.camera:camera-camera2:${camerax_version}"
@@ -50,6 +54,9 @@ dependencies {
5054
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
5155
implementation 'androidx.gridlayout:gridlayout:1.0.0'
5256

57+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
58+
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
59+
5360
testImplementation 'junit:junit:4.13.2'
5461
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5562
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

app/release/output-metadata.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.example.picturetopc">
55

6-
76
<uses-permission android:name="android.permission.CAMERA" />
87
<uses-permission android:name="android.permission.INTERNET" />
98
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
109
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
10+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
1111

1212
<uses-feature
1313
android:name="android.hardware.camera.any"
@@ -31,6 +31,7 @@
3131
android:exported="true">
3232
<intent-filter>
3333
<action android:name="android.intent.action.MAIN" />
34+
3435
<category android:name="android.intent.category.LAUNCHER" />
3536
</intent-filter>
3637
</activity>
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
package com.example.picturetopc;
2+
3+
import android.graphics.Bitmap;
4+
import android.widget.Button;
5+
import android.widget.EditText;
6+
import android.widget.ProgressBar;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.OutputStream;
12+
import java.net.DatagramPacket;
13+
import java.net.InetAddress;
14+
import java.net.MulticastSocket;
15+
import java.net.ServerSocket;
16+
import java.net.Socket;
17+
import java.net.SocketException;
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
24+
class ConnHanlder extends Thread {
25+
MulticastSocket socket;
26+
27+
InetAddress ip;
28+
int port;
29+
30+
EditText code;
31+
32+
33+
public ConnHanlder(String _ip, int _port, EditText _code) throws IOException {
34+
socket = new MulticastSocket();
35+
ip = InetAddress.getByName(_ip);
36+
socket.joinGroup(ip);
37+
socket.setTimeToLive(20);
38+
39+
40+
port = _port;
41+
42+
code = _code;
43+
}
44+
45+
public void run() {
46+
socket.connect(ip, port);
47+
48+
while (true) {
49+
try {
50+
sleep(1000);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
55+
String msg = null;
56+
57+
58+
msg = "{\"code\": \"" + code.getText().toString() + "\", \"ip\": \"" + "0" + "\", \"port\":" + 42069 + "}";
59+
60+
61+
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, this.ip, this.port);
62+
63+
64+
try {
65+
socket.send(msgPacket);
66+
} catch (IOException e) {
67+
run();
68+
e.printStackTrace();
69+
}
70+
}
71+
}
72+
}
73+
74+
class ConnListener extends Thread
75+
{
76+
ServerSocket socket;
77+
ProgressBar progressBar;
78+
List<Connection> connections;
79+
80+
Button button;
81+
82+
public ConnListener(ProgressBar _progressBar, Button _button) throws IOException{
83+
socket = new ServerSocket(42069);
84+
progressBar = _progressBar;
85+
86+
connections = new ArrayList<>();
87+
88+
button = _button;
89+
}
90+
91+
public void run(){
92+
while (socket != null){
93+
try {
94+
connections.add(new Connection(socket.accept(), this));
95+
button.setText("Connected: " + connections.size());
96+
} catch (IOException e) {
97+
run();
98+
return;
99+
}
100+
}
101+
}
102+
103+
public void Disconnect(Connection connection) {
104+
connections.remove(connection);
105+
button.setText("Connected: " + connections.size());
106+
}
107+
108+
public void SendAll(Bitmap pic) {
109+
for (Connection conn:
110+
connections) {
111+
conn.Send(pic);
112+
}
113+
}
114+
115+
public void DisconnectAll() {
116+
for (Connection conn:
117+
connections) {
118+
conn.Disconnect();
119+
}
120+
try {
121+
socket.close();
122+
} catch (Exception e) {
123+
e.printStackTrace();
124+
}
125+
socket = null;
126+
}
127+
}
128+
129+
class ConnSender extends Thread {
130+
Connection Listener;
131+
132+
List<byte[]> Sendable;
133+
134+
public ConnSender(Connection listener){
135+
Sendable = new LinkedList<>();
136+
Listener = listener;
137+
}
138+
139+
public void run() {
140+
while (!Listener.Stop){
141+
SendAll();
142+
}
143+
}
144+
145+
146+
private void SendAll() {
147+
if (Sendable.isEmpty()) {
148+
try {
149+
sleep(100);
150+
} catch (InterruptedException e) {
151+
e.printStackTrace();
152+
}
153+
return;
154+
}
155+
156+
int size = 1024;
157+
byte[] send = Sendable.remove(0);
158+
byte[] sdata = new byte[size];
159+
160+
Listener.ConnListener.progressBar.setMax(send.length);
161+
try {
162+
for (int i = 0; i < send.length; i += size) {
163+
Listener.ConnListener.progressBar.setProgress(i);
164+
byte[] data = Arrays.copyOfRange(send, i, i+size);
165+
Listener.Output.write(data);
166+
}
167+
} catch (IOException e) {
168+
Listener.Disconnect();
169+
return;
170+
}
171+
}
172+
public void KeepAlive(){
173+
final byte[] biteses = "-1".getBytes(StandardCharsets.UTF_8);
174+
Sendable.add(biteses);
175+
}
176+
177+
public void Send(Bitmap bmp){
178+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
179+
180+
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
181+
byte[] b = baos.toByteArray();
182+
183+
Sendable.add((String.valueOf(b.length)).getBytes(StandardCharsets.UTF_8));
184+
Sendable.add(b);
185+
186+
}
187+
}
188+
189+
190+
class ConnTimeout extends Thread {
191+
ConnSender Sender;
192+
193+
public ConnTimeout(ConnSender sender){
194+
Sender = sender;
195+
}
196+
197+
public void run() {
198+
while (!Sender.Listener.Stop){
199+
SendAll();
200+
try {
201+
sleep(1000);
202+
} catch (InterruptedException e) {
203+
e.printStackTrace();
204+
}
205+
}
206+
}
207+
208+
209+
private void SendAll() {
210+
Sender.KeepAlive();
211+
}
212+
}
213+
214+
class Connection {
215+
java.net.Socket Socket;
216+
InputStream Input;
217+
OutputStream Output;
218+
219+
//ConnReader Reader;
220+
ConnSender Sender;
221+
222+
ConnTimeout Timeout;
223+
224+
225+
226+
ConnListener ConnListener;
227+
boolean Stop;
228+
229+
public Connection(Socket socket, ConnListener connListener) throws SocketException {
230+
Socket = socket;
231+
socket.setSoTimeout(1*1000);
232+
ConnListener = connListener;
233+
234+
Stop = false;
235+
236+
try {
237+
Input = Socket.getInputStream();
238+
Output = Socket.getOutputStream();
239+
240+
} catch (IOException e) {
241+
return;
242+
}
243+
244+
//Reader = new ConnReader(this);
245+
Sender = new ConnSender(this);
246+
247+
Timeout = new ConnTimeout(Sender);
248+
249+
//Reader.start();
250+
Sender.start();
251+
252+
Timeout.start();
253+
}
254+
255+
public void Send(Bitmap bmp){
256+
Sender.Send(bmp);
257+
}
258+
259+
public void Disconnect() {
260+
try {
261+
Socket.close();}
262+
catch (IOException e)
263+
{}
264+
Stop = true;
265+
266+
//Sender.join();
267+
//Reader.join();
268+
269+
ConnListener.Disconnect(this);
270+
}
271+
}

0 commit comments

Comments
 (0)