diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index bdff19d..2d44416 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -97,5 +97,6 @@
+
diff --git a/res/menu/menu.xml b/res/menu/menu.xml
index d24935a..0ae8607 100644
--- a/res/menu/menu.xml
+++ b/res/menu/menu.xml
@@ -32,4 +32,9 @@
android:icon="@drawable/ic_menu_restart"
android:title="@string/restart_failed" />
+
diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml
old mode 100755
new mode 100644
index a05245b..e9326c4
--- a/res/values-de/strings.xml
+++ b/res/values-de/strings.xml
@@ -91,6 +91,7 @@
Konnte keine Verbindung zu pyLoad herstellen. Bitte überprüfen sie, ob pyLoad läuft und ihre Servereinstellungen korrekt sind.
Sprache
Sprache der App. Ein Neustart ist notwendig, damit die Änderungen in Kraft treten.
+ Prüfe auf Captchas im Hintergrund
- Warteschlange
- Linksammler
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 91ec5d2..3d3f62a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -100,6 +100,7 @@
Could not connect to pyLoad. Check that pyLoad is running and server settings are
correct.
+ Check for Captchas in Background
Language
Language of the app. Needs a restart before changes take effect.
diff --git a/src/org/pyload/android/client/Preferences.java b/src/org/pyload/android/client/Preferences.java
index 70f1d5a..7ec467a 100644
--- a/src/org/pyload/android/client/Preferences.java
+++ b/src/org/pyload/android/client/Preferences.java
@@ -5,6 +5,9 @@
import android.view.MenuItem;
public class Preferences extends PreferenceActivity {
+
+ public static final String CHECK_CAPTCHA_SERVICE_ENABLE = "check_captcha_bg";
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
diff --git a/src/org/pyload/android/client/pyLoad.java b/src/org/pyload/android/client/pyLoad.java
index 443d72d..9bc7740 100644
--- a/src/org/pyload/android/client/pyLoad.java
+++ b/src/org/pyload/android/client/pyLoad.java
@@ -7,7 +7,12 @@
import java.util.HashMap;
import java.util.Locale;
+import android.app.AlarmManager;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.SharedPreferences;
import android.content.res.Configuration;
+import android.os.Build;
import android.view.*;
import org.pyload.android.client.components.FragmentTabsPager;
import org.pyload.android.client.dialogs.AccountDialog;
@@ -16,6 +21,7 @@
import org.pyload.android.client.fragments.QueueFragment;
import org.pyload.android.client.module.Eula;
import org.pyload.android.client.module.GuiTask;
+import org.pyload.android.client.service.CheckCaptchaService;
import org.pyload.thrift.Destination;
import org.pyload.thrift.PackageDoesNotExists;
import org.pyload.thrift.Pyload.Client;
@@ -30,6 +36,7 @@
import android.util.Log;
import android.widget.TabHost;
import android.support.v4.view.MenuItemCompat;
+import android.widget.Toast;
public class pyLoad extends FragmentTabsPager {
@@ -38,6 +45,10 @@ public class pyLoad extends FragmentTabsPager {
// keep reference to set indeterminateProgress
private MenuItem refreshItem;
+ // AlarmManager and PendingIntent for CheckCaptchaService
+ private AlarmManager alarmManager;
+ private PendingIntent checkCaptchaIntent;
+
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
@@ -113,18 +124,41 @@ protected void onStart() {
}
intent.setData(null);
}
+
+ // get AlarmManager and create PendingIntent for CheckCaptchaService
+ alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
+ checkCaptchaIntent = PendingIntent.getService(this.getApplication(), 0,
+ new Intent(this.getApplication(),
+ CheckCaptchaService.class), 0);
+ app.notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
app.refreshTab();
+ alarmManager.cancel(checkCaptchaIntent);
+ app.notificationManager.cancel(CheckCaptchaService.NOTIFICATION_ID);
}
@Override
protected void onPause() {
super.onPause();
app.clearTasks();
+ // create background service to check for captchas
+ if(app.prefs.getBoolean(Preferences.CHECK_CAPTCHA_SERVICE_ENABLE, true)) {
+ int interval = 5;
+ try {
+ interval = Integer.parseInt(app.prefs
+ .getString("refresh_rate", "5"));
+ } catch (NumberFormatException e) {
+ interval = 5;
+ }
+ Toast.makeText(this, "Checking for Captcha every " + interval + "s", Toast.LENGTH_SHORT).show();
+ interval *= 1000;
+ alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval,
+ interval, checkCaptchaIntent);
+ }
}
@Override
@@ -137,6 +171,9 @@ public boolean onCreateOptionsMenu(Menu menu) {
MenuItemCompat.setShowAsAction(menu.findItem(R.id.add_links),
MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
+ menu.findItem(R.id.ic_menu_check_captcha).setChecked(
+ app.prefs.getBoolean(Preferences.CHECK_CAPTCHA_SERVICE_ENABLE, true));
+
return true;
}
@@ -183,6 +220,17 @@ public void run() {
return true;
+ case R.id.ic_menu_check_captcha:
+ SharedPreferences.Editor prefEdit = app.prefs.edit();
+ Log.d("pyLoad", "CheckCaptcha: " + item.isChecked());
+ prefEdit.putBoolean(Preferences.CHECK_CAPTCHA_SERVICE_ENABLE, !item.isChecked());
+ if(Build.VERSION.SDK_INT >= 7)
+ prefEdit.apply();
+ else
+ prefEdit.commit();
+ item.setChecked(!item.isChecked());
+ return true;
+
default:
return super.onOptionsItemSelected(item);
}
@@ -312,4 +360,5 @@ public void run() {
public MenuItem getRefreshItem() {
return refreshItem;
}
+
}
diff --git a/src/org/pyload/android/client/pyLoadApp.java b/src/org/pyload/android/client/pyLoadApp.java
index aca8ec9..1a2378e 100644
--- a/src/org/pyload/android/client/pyLoadApp.java
+++ b/src/org/pyload/android/client/pyLoadApp.java
@@ -13,6 +13,7 @@
import javax.net.ssl.*;
import android.annotation.TargetApi;
+import android.app.NotificationManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuItem;
@@ -53,6 +54,9 @@ public class pyLoadApp extends Application {
public SharedPreferences prefs;
public ConnectivityManager cm;
+ /** NotificationManager used by CheckCaptchaService */
+ public NotificationManager notificationManager;
+
private pyLoad main;
private static final String[] clientVersion = {"0.4.8", "0.4.9"};
diff --git a/src/org/pyload/android/client/service/CheckCaptchaService.java b/src/org/pyload/android/client/service/CheckCaptchaService.java
new file mode 100644
index 0000000..a284735
--- /dev/null
+++ b/src/org/pyload/android/client/service/CheckCaptchaService.java
@@ -0,0 +1,92 @@
+package org.pyload.android.client.service;
+
+import android.app.AlarmManager;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.app.Service;
+import android.content.Intent;
+import android.media.RingtoneManager;
+import android.os.AsyncTask;
+import android.os.Binder;
+import android.os.IBinder;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+import org.pyload.android.client.R;
+import org.pyload.android.client.pyLoad;
+import org.pyload.android.client.pyLoadApp;
+import org.pyload.thrift.Pyload;
+
+/** Background Service checking for new captchas */
+public class CheckCaptchaService extends Service {
+
+ public static final int NOTIFICATION_ID = 1000;
+
+ private pyLoadApp app;
+ private Pyload.Client client;
+ private final IBinder mBinder = new CheckCaptchaBinder();
+
+ @Override
+ public void onCreate() {
+ Log.d("pyLoad", "create CaptchaService");
+ app = new pyLoadApp();
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ Log.d("pyLoad", "Captcha check service");
+ app.prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ app.notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
+ AsyncTask asyncTask = new AsyncTask() {
+ @Override
+ protected Void doInBackground(Void... params) {
+ Log.d("pyLoad", "getting client in background");
+ client = app.getClient();
+ if(client.isCaptchaWaiting()) {
+ showNotification();
+ }
+ return null;
+ }
+ };
+ asyncTask.execute();
+ return START_NOT_STICKY;
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ Log.d("pyLoad", "CaptchaBinder");
+ return mBinder;
+ }
+
+ public class CheckCaptchaBinder extends Binder {
+ CheckCaptchaService getService() {
+ return CheckCaptchaService.this;
+ }
+ }
+
+ private void showNotification() {
+ // Build notification
+ Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.retrieve_captcha),
+ System.currentTimeMillis());
+ notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+ notification.defaults |= Notification.DEFAULT_VIBRATE;
+
+ // The PendingIntent to launch our activity if the user selects this notification
+ PendingIntent contentIntent;
+ contentIntent = PendingIntent.getActivity(this, 0,
+ new Intent(this, pyLoad.class), 0);
+
+ // Set the info for the views that show in the notification panel.
+ notification.setLatestEventInfo(this, getText(R.string.app_name),
+ getText(R.string.retrieve_captcha), contentIntent);
+
+ // Send the notification.
+ app.notificationManager.notify(NOTIFICATION_ID, notification);
+
+ // disable repeating background service, as we notify now
+ ((AlarmManager) getSystemService(ALARM_SERVICE)).cancel(PendingIntent.getService(
+ this.getApplication(), 0, new Intent(this.getApplication(),
+ CheckCaptchaService.class), 0));
+ }
+}