Open
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,5 +97,6 @@

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

<service android:name=".service.CheckCaptchaService" />
</application>
</manifest>
Original file line numberDiff line numberDiff 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>
Original file line numberDiff line numberDiff 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
Original file line numberDiff line numberDiff 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
Original file line numberDiff line numberDiff 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 DownExpand 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
Original file line numberDiff line numberDiff 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));
}
}