Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captcha Notification Service #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@

<activity android:name=".RemoteSettings" android:label="@string/remotesettings_activity"/>

<service android:name=".service.CheckCaptchaService" />
</application>
</manifest>
5 changes: 5 additions & 0 deletions res/menu/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
android:icon="@drawable/ic_menu_restart"
android:title="@string/restart_failed" />

<item
android:id="@+id/ic_menu_check_captcha"
android:title="@string/check_captcha"
android:showAsAction="never"
android:checkable="true" />
</menu>
1 change: 1 addition & 0 deletions res/values-de/strings.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<string name="connect_error">Konnte keine Verbindung zu pyLoad herstellen. Bitte überprüfen sie, ob pyLoad läuft und ihre Servereinstellungen korrekt sind.</string>
<string name="language">Sprache</string>
<string name="language_desc">Sprache der App. Ein Neustart ist notwendig, damit die Änderungen in Kraft treten.</string>
<string name="check_captcha">Prüfe auf Captchas im Hintergrund</string>
<string-array name="destination_list">
<item>Warteschlange</item>
<item>Linksammler</item>
Expand Down
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<string name="connect_error">Could not connect to pyLoad. Check that pyLoad is running and server settings are
correct.
</string>
<string name="check_captcha">Check for Captchas in Background</string>
<string name="language">Language</string>
<string name="language_desc">Language of the app. Needs a restart before changes take effect.</string>
<string-array name="languages">
Expand Down
3 changes: 3 additions & 0 deletions src/org/pyload/android/client/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions src/org/pyload/android/client/pyLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -312,4 +360,5 @@ public void run() {
public MenuItem getRefreshItem() {
return refreshItem;
}

}
4 changes: 4 additions & 0 deletions src/org/pyload/android/client/pyLoadApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"};
Expand Down
92 changes: 92 additions & 0 deletions src/org/pyload/android/client/service/CheckCaptchaService.java
Original file line number Diff line number Diff line change
@@ -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<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@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));
}
}