summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/tasker/TaskerReceiver.java
blob: b1200b38a8657ac5294cf776024da9d73bd86e77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.fox.ttrss.tasker;

import org.fox.ttrss.CommonActivity;
import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.offline.OfflineDownloadService;
import org.fox.ttrss.util.SimpleLoginManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class TaskerReceiver extends BroadcastReceiver {
	private final String TAG = this.getClass().getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d(TAG, "Got action: " + intent.getAction());
		
		final Context fContext = context;
		
		if (com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING.equals(intent.getAction())) {
			Log.d(TAG, "about to download stuff!");
			
			SimpleLoginManager loginMgr = new SimpleLoginManager() {
				
				@Override
				protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {
					Log.d(TAG, "Got SID=" + sessionId);
					
					Intent intent = new Intent(
							fContext,
							OfflineDownloadService.class);
					intent.putExtra("sessionId", sessionId);
					intent.putExtra("batchMode", true);

					fContext.startService(intent);
				}
				
				@Override
				protected void onLoginFailed(int requestId) {
					Toast toast = Toast.makeText(fContext, "Could not download articles: login failed", Toast.LENGTH_SHORT);
					toast.show();
				}
				
				@Override
				protected void onLoggingIn(int requestId) {
					//
				}
			};
			
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			
			String login = prefs.getString("login", "").trim();
			String password = prefs.getString("password", "").trim();
			
			if (login.equals("") || password.equals("")) {
				Toast toast = Toast.makeText(fContext, "Could not download articles: not configured?", Toast.LENGTH_SHORT);
				toast.show();
			} else {				
				loginMgr.logIn(context, 1, login, password);
			}			
		}
	}

}