summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/tasker/TaskerReceiver.java
blob: 52c600bfbd86336eae428258ff3dccaf1b02926b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.fox.ttrss.tasker;

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

import org.fox.ttrss.ApiCommon;
import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.offline.OfflineDownloadService;
import org.fox.ttrss.offline.OfflineUploadService;
import org.fox.ttrss.util.SimpleLoginManager;

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())) {
			
			final Bundle settings = intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
			final int actionId = settings != null ? settings.getInt("actionId", -1) : -1;
			
			Log.d(TAG, "received action id=" + actionId);
			
			SimpleLoginManager loginMgr = new SimpleLoginManager() {
				
				@Override
				protected void onLoginSuccess(int requestId, String sessionId, int apiLevel) {

					switch (actionId) {
					case TaskerSettingsActivity.ACTION_DOWNLOAD:
						if (true) {
							Intent intent = new Intent(fContext,
									OfflineDownloadService.class);
							intent.putExtra("sessionId", sessionId);
							intent.putExtra("batchMode", true);

							if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
								fContext.startForegroundService(intent);
							} else {
								fContext.startService(intent);
							}
						}
						break;
					case TaskerSettingsActivity.ACTION_UPLOAD:
						if (true) {
							Intent intent = new Intent(fContext,
									OfflineUploadService.class);
							intent.putExtra("sessionId", sessionId);
							intent.putExtra("batchMode", true);

							if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
								fContext.startForegroundService(intent);
							} else {
								fContext.startService(intent);
							}

						}						
						break;
					default:
						Log.d(TAG, "unknown action id=" + actionId);
					}					
				}
				
				@Override
				protected void onLoginFailed(int requestId, ApiRequest ar) {
					Toast toast = Toast.makeText(fContext, fContext.getString(ar.getErrorMessage()), 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();
			String ttrssUrl = prefs.getString("ttrss_url", "").trim();
			ApiCommon.trustAllHosts(prefs.getBoolean("ssl_trust_any", false), prefs.getBoolean("ssl_trust_any_host", false));
			
			if (ttrssUrl.equals("")) {
				Toast toast = Toast.makeText(fContext, "Could not download articles: not configured?", Toast.LENGTH_SHORT);
				toast.show();
			} else {				
				loginMgr.logIn(context, 1, login, password);
			}			
		}
	}

}