Newer
Older
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.briarproject.android.blogs;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import org.briarproject.R;
import org.briarproject.android.ActivityComponent;
import org.briarproject.android.BriarActivity;
import org.briarproject.api.db.DbException;
import org.briarproject.api.feed.FeedManager;
import org.briarproject.api.lifecycle.IoExecutor;
import org.briarproject.api.sync.GroupId;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static java.util.logging.Level.WARNING;
public class RssFeedImportActivity extends BriarActivity {
private static final Logger LOG =
Logger.getLogger(RssFeedImportActivity.class.getName());
private EditText urlInput;
private Button importButton;
private ProgressBar progressBar;
@Inject
@IoExecutor
protected Executor ioExecutor;
// Fields that are accessed from background threads must be volatile
private volatile GroupId groupId = null;
@Inject
@SuppressWarnings("WeakerAccess")
volatile FeedManager feedManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// GroupId from Intent
Intent i = getIntent();
byte[] b = i.getByteArrayExtra(GROUP_ID);
if (b == null) throw new IllegalStateException("No Group in intent.");
groupId = new GroupId(b);
setContentView(R.layout.activity_rss_feed_import);
urlInput = (EditText) findViewById(R.id.urlInput);
urlInput.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable s) {
enableOrDisableImportButton();
}
});
importButton = (Button) findViewById(R.id.importButton);
importButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publish();
}
});
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void injectActivity(ActivityComponent component) {
component.inject(this);
}
private void enableOrDisableImportButton() {
String url = urlInput.getText().toString();
if (url.startsWith("http://") || url.startsWith("https://"))
importButton.setEnabled(true);
else
importButton.setEnabled(false);
}
private void publish() {
// hide import button, show progress bar
importButton.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
importFeed(urlInput.getText().toString());
}
private void importFeed(final String url) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
try {
feedManager.addFeed(url, groupId);
feedImported();
} catch (DbException | IOException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
importFailed();
}
}
});
}
private void feedImported() {
runOnUiThread(new Runnable() {
@Override
public void run() {
supportFinishAfterTransition();
}
});
}
private void importFailed() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// hide progress bar, show publish button
progressBar.setVisibility(GONE);
importButton.setVisibility(VISIBLE);
// show error dialog
AlertDialog.Builder builder =
new AlertDialog.Builder(RssFeedImportActivity.this,
R.style.BriarDialogTheme);
builder.setMessage(R.string.blogs_rss_feeds_import_error);
builder.setNegativeButton(R.string.cancel_button, null);
builder.setPositiveButton(R.string.try_again_button,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
publish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}