forked from pyload/pyload-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a Service to listen to a configurable port and send the receive…
…d link list to the server See issue pyload#37
- Loading branch information
Daniel Prange
committed
Apr 30, 2021
1 parent
520035a
commit b8088d5
Showing
8 changed files
with
255 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/src/main/java/org/pyload/android/client/services/clicknload/ClickNLoadService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.pyload.android.client.services.clicknload; | ||
|
||
import android.app.Service; | ||
import android.content.ComponentName; | ||
import android.content.Intent; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
import org.pyload.android.client.pyLoadApp; | ||
|
||
import java.util.concurrent.Executors; | ||
|
||
public class ClickNLoadService extends Service { | ||
|
||
private final static String LOGTAG = "ClickNLoad Service"; | ||
|
||
private ClickNLoadTask clickNLoadTask; | ||
private int port; | ||
|
||
@Override | ||
public int onStartCommand(final Intent intent, int flags, int startId) { | ||
|
||
if (intent != null) { | ||
String action = intent.getAction(); | ||
port = intent.getExtras().getInt("port"); | ||
switch(action) { | ||
case "START": startService(); break; | ||
case "STOP": stopService(); break; | ||
default: Log.d(LOGTAG, "This should never happen. No action in the received intent"); | ||
} | ||
} else { | ||
Log.d(LOGTAG, "with a null intent. It has been probably restarted by the system."); | ||
} | ||
|
||
return Service.START_STICKY; | ||
} | ||
|
||
private void stopService() { | ||
clickNLoadTask.stop(); | ||
} | ||
|
||
public void startService() { | ||
clickNLoadTask = new ClickNLoadTask(port, (pyLoadApp) getApplicationContext()); | ||
Executors.newSingleThreadExecutor().submit(clickNLoadTask); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
app/src/main/java/org/pyload/android/client/services/clicknload/ClickNLoadTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.pyload.android.client.services.clicknload; | ||
|
||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.content.res.Configuration; | ||
import android.content.res.Resources; | ||
import android.provider.Settings; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
import org.pyload.android.client.R; | ||
import org.pyload.android.client.pyLoadApp; | ||
import org.pyload.thrift.Destination; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintStream; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.net.URLDecoder; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class ClickNLoadTask implements Runnable{ | ||
|
||
private final static String LOGTAG= "ClickNLoadTask"; | ||
|
||
private volatile boolean stopped = false; | ||
private int port; | ||
private pyLoadApp app; | ||
|
||
public ClickNLoadTask(int port, pyLoadApp app){ | ||
this.port = port; | ||
this.app = app; | ||
|
||
app.prefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { | ||
@Override | ||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||
//stop if clicknload gets disabled | ||
if(key.equals("check_box_clicknload") && !sharedPreferences.getBoolean(key, true)){ | ||
stop(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Socket clientSocket = null; | ||
BufferedReader in; | ||
PrintStream out; | ||
ServerSocket serverSocket = null; | ||
|
||
while (!stopped) { | ||
|
||
try { | ||
if (serverSocket != null) { | ||
serverSocket.close(); | ||
clientSocket.close(); | ||
} | ||
serverSocket = new ServerSocket(port); | ||
clientSocket = serverSocket.accept(); | ||
|
||
Log.d(LOGTAG, "Receiving ClickNLoad Event"); | ||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
out = new PrintStream(clientSocket.getOutputStream()); | ||
} catch (IOException e) { | ||
Log.e(LOGTAG, "Socket could not be opened", e); | ||
stop(); | ||
break; | ||
} | ||
|
||
try { | ||
String input; | ||
while ((input = in.readLine()) != null) { | ||
if (input.startsWith("source")) { | ||
List<String> urlList = getURLParamsAsMap(input).get("urls"); | ||
app.getClient().addPackage("TestName", urlList, Destination.Collector); | ||
app.showToast(String.format(getLocalizedResources(app).getString(R.string.clicknload_toast_msg), urlList.size()), Toast.LENGTH_LONG); | ||
} | ||
} | ||
out.println("success"); | ||
} catch (Exception e) { | ||
Log.e(LOGTAG, "Data could not be parsed"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
public static Map<String, List<String>> getURLParamsAsMap(String parameters) throws UnsupportedEncodingException { | ||
final Map<String, List<String>> parameterMap = new LinkedHashMap<>(); | ||
final String[] pairs = parameters.split("&"); | ||
for (String pair : pairs) { | ||
final int idx = pair.indexOf("="); | ||
final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair; | ||
if (!parameterMap.containsKey(key)) { | ||
parameterMap.put(key, new LinkedList<String>()); | ||
} | ||
final String[] values = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8").split("\\r?\\n") : new String[0]; | ||
for (String value : values) { | ||
parameterMap.get(key).add(value); | ||
} | ||
} | ||
return parameterMap; | ||
} | ||
|
||
public void stop() { | ||
stopped = true; | ||
} | ||
|
||
Resources getLocalizedResources(Context context) { | ||
Configuration conf = context.getResources().getConfiguration(); | ||
conf = new Configuration(conf); | ||
conf.setLocale(Locale.getDefault()); | ||
Context localizedContext = context.createConfigurationContext(conf); | ||
return localizedContext.getResources(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters