Newer
Older
package org.briarproject.android.blogs;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import org.briarproject.R;
import org.briarproject.android.ActivityComponent;
import org.briarproject.android.BriarActivity;
import org.briarproject.api.blogs.Blog;
import org.briarproject.api.blogs.BlogManager;
import org.briarproject.api.db.DbException;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.util.StringUtils;
import java.util.Collection;
import java.util.logging.Logger;
import javax.inject.Inject;
import static android.support.v4.app.ActivityOptionsCompat.makeCustomAnimation;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static android.widget.Toast.LENGTH_LONG;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.android.blogs.BlogActivity.BLOG_NAME;
import static org.briarproject.android.blogs.BlogActivity.IS_MY_BLOG;
import static org.briarproject.android.blogs.BlogActivity.IS_NEW_BLOG;
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
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_DESC_LENGTH;
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_TITLE_LENGTH;
public class CreateBlogActivity extends BriarActivity
implements OnEditorActionListener, OnClickListener {
private static final Logger LOG =
Logger.getLogger(CreateBlogActivity.class.getName());
private TextInputEditText titleInput, descInput;
private Button button;
private ProgressBar progress;
// Fields that are accessed from background threads must be volatile
@Inject
protected volatile IdentityManager identityManager;
@Inject
volatile BlogManager blogManager;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_create_blog);
TextInputLayout titleLayout =
(TextInputLayout) findViewById(R.id.titleLayout);
if (titleLayout != null) {
titleLayout.setCounterMaxLength(MAX_BLOG_TITLE_LENGTH);
}
titleInput = (TextInputEditText) findViewById(R.id.titleInput);
TextWatcher nameEntryWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence text, int start,
int lengthBefore, int lengthAfter) {
enableOrDisableCreateButton();
}
};
titleInput.setOnEditorActionListener(this);
titleInput.addTextChangedListener(nameEntryWatcher);
TextInputLayout descLayout =
(TextInputLayout) findViewById(R.id.descLayout);
if (descLayout != null) {
descLayout.setCounterMaxLength(MAX_BLOG_DESC_LENGTH);
}
descInput = (TextInputEditText) findViewById(R.id.descInput);
if (descInput != null) {
descInput.addTextChangedListener(nameEntryWatcher);
}
button = (Button) findViewById(R.id.createBlogButton);
if (button != null) {
button.setOnClickListener(this);
}
progress = (ProgressBar) findViewById(R.id.createBlogProgressBar);
}
@Override
public void injectActivity(ActivityComponent component) {
component.inject(this);
}
private void enableOrDisableCreateButton() {
if (progress == null) return; // Not created yet
button.setEnabled(validateTitle() && validateDescription());
}
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent e) {
descInput.requestFocus();
return true;
}
private boolean validateTitle() {
String name = titleInput.getText().toString();
int length = StringUtils.toUtf8(name).length;
return length <= MAX_BLOG_TITLE_LENGTH && length > 0;
}
private boolean validateDescription() {
String name = descInput.getText().toString();
int length = StringUtils.toUtf8(name).length;
return length <= MAX_BLOG_DESC_LENGTH && length > 0;
}
@Override
public void onClick(View view) {
if (view == button) {
hideSoftKeyboard(view);
if (!validateTitle()) return;
button.setVisibility(GONE);
progress.setVisibility(VISIBLE);
addBlog(titleInput.getText().toString(),
descInput.getText().toString());
}
}
private void addBlog(final String title, final String description) {
runOnDbThread(new Runnable() {
@Override
public void run() {
try {
long now = System.currentTimeMillis();
Collection<LocalAuthor> authors =
identityManager.getLocalAuthors();
// take first identity, don't support more for now
LocalAuthor author = authors.iterator().next();
Blog f = blogManager.addBlog(author, title, description);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Storing blog took " + duration + " ms");
displayBlog(f);
} catch (DbException e) {
// TODO show error, e.g. blog with same title exists
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
finishOnUiThread();
}
}
});
}
private void displayBlog(final Blog b) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i =
new Intent(CreateBlogActivity.this, BlogActivity.class);
i.putExtra(GROUP_ID, b.getId().getBytes());
i.putExtra(BLOG_NAME, b.getName());
i.putExtra(IS_MY_BLOG, true);
i.putExtra(IS_NEW_BLOG, true);
ActivityOptionsCompat options =
makeCustomAnimation(CreateBlogActivity.this,
android.R.anim.fade_in,
android.R.anim.fade_out);
ActivityCompat.startActivity(CreateBlogActivity.this, i,
options.toBundle());
supportFinishAfterTransition();
}
});
}
}