2016-03-03 17:46:31 +01:00
|
|
|
/*
|
2016-06-04 03:48:57 +02:00
|
|
|
This file is part of the Diaspora for Android.
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
Diaspora for Android is free software: you can redistribute it and/or modify
|
2016-03-03 17:46:31 +01:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
Diaspora for Android is distributed in the hope that it will be useful,
|
2016-03-03 17:46:31 +01:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2016-06-04 03:48:57 +02:00
|
|
|
along with the Diaspora for Android.
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 19:38:50 +02:00
|
|
|
package com.github.dfa.diaspora_android.activity;
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.design.widget.Snackbar;
|
2016-06-05 13:57:34 +02:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-03-23 22:26:23 +01:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2016-06-04 03:48:57 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.text.Editable;
|
2016-06-02 13:58:09 +02:00
|
|
|
import android.text.SpannableString;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.text.TextWatcher;
|
2016-06-02 13:58:09 +02:00
|
|
|
import android.text.util.Linkify;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.webkit.CookieManager;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ListView;
|
|
|
|
|
2016-03-29 19:38:50 +02:00
|
|
|
import com.github.dfa.diaspora_android.App;
|
|
|
|
import com.github.dfa.diaspora_android.R;
|
|
|
|
import com.github.dfa.diaspora_android.task.GetPodsService;
|
|
|
|
import com.github.dfa.diaspora_android.util.Helpers;
|
2016-08-07 14:29:52 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.WebHelper;
|
2016-03-29 19:38:50 +02:00
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2016-06-02 13:58:09 +02:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import butterknife.OnClick;
|
2016-06-04 03:48:57 +02:00
|
|
|
import butterknife.OnItemClick;
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
|
2016-06-02 13:58:09 +02:00
|
|
|
public class PodSelectionActivity extends AppCompatActivity {
|
2016-03-25 00:12:00 +01:00
|
|
|
private App app;
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-06-02 13:58:09 +02:00
|
|
|
@BindView(R.id.podselection__edit_filter)
|
2016-08-23 21:23:36 +02:00
|
|
|
EditText editFilter;
|
2016-06-02 13:58:09 +02:00
|
|
|
|
|
|
|
@BindView(R.id.podselection__listpods)
|
2016-08-23 21:23:36 +02:00
|
|
|
ListView listPods;
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
@BindView(R.id.toolbar)
|
2016-08-23 21:23:36 +02:00
|
|
|
Toolbar toolbar;
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2016-06-04 11:11:51 +02:00
|
|
|
setContentView(R.layout.podselection__activity);
|
2016-06-02 13:58:09 +02:00
|
|
|
ButterKnife.bind(this);
|
2016-03-25 00:12:00 +01:00
|
|
|
app = (App) getApplication();
|
2016-06-04 03:48:57 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
listPods.setTextFilterEnabled(true);
|
2016-06-05 13:57:34 +02:00
|
|
|
setListedPods(app.getSettings().getPreviousPodlist());
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED));
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-08-07 14:29:52 +02:00
|
|
|
if (!WebHelper.isOnline(PodSelectionActivity.this)) {
|
2016-06-04 03:48:57 +02:00
|
|
|
Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
2016-06-02 13:58:09 +02:00
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
|
|
|
|
private final BroadcastReceiver podListReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (intent.hasExtra("pods")) {
|
|
|
|
Bundle extras = intent.getExtras();
|
|
|
|
String[] pods = extras.getStringArray("pods");
|
2016-06-05 13:57:34 +02:00
|
|
|
if (pods != null && pods.length > 0) {
|
|
|
|
app.getSettings().setPreviousPodlist(pods);
|
2016-06-04 03:48:57 +02:00
|
|
|
setListedPods(pods);
|
2016-06-05 13:57:34 +02:00
|
|
|
} else {
|
|
|
|
setListedPods(app.getSettings().getPreviousPodlist());
|
|
|
|
Snackbar.make(listPods, R.string.podlist_error, Snackbar.LENGTH_SHORT).show();
|
2016-06-04 03:48:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-02 13:58:09 +02:00
|
|
|
@OnClick(R.id.podselection__button_select_pod)
|
|
|
|
public void onButtonSelectPodClicked(View view) {
|
2016-06-04 03:48:57 +02:00
|
|
|
if (editFilter.getText().length() > 4 && editFilter.getText().toString().contains("")) {
|
|
|
|
showPodConfirmationDialog(editFilter.getText().toString());
|
2016-06-02 13:58:09 +02:00
|
|
|
} else {
|
2016-06-04 03:48:57 +02:00
|
|
|
Snackbar.make(listPods, R.string.valid_pod, Snackbar.LENGTH_LONG).show();
|
2016-06-02 13:58:09 +02:00
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2016-06-02 13:58:09 +02:00
|
|
|
Intent i = new Intent(PodSelectionActivity.this, GetPodsService.class);
|
2016-03-03 17:46:31 +01:00
|
|
|
startService(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
private void setListedPods(String[] listedPodsArr) {
|
|
|
|
final ArrayList<String> listedPodsList = new ArrayList<>();
|
|
|
|
for (String pod : listedPodsArr) {
|
|
|
|
listedPodsList.add(pod.toLowerCase());
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:26:23 +01:00
|
|
|
final ArrayAdapter<String> adapter = new ArrayAdapter<>(
|
2016-06-02 13:58:09 +02:00
|
|
|
PodSelectionActivity.this,
|
2016-03-03 17:46:31 +01:00
|
|
|
android.R.layout.simple_list_item_1,
|
2016-06-04 03:48:57 +02:00
|
|
|
listedPodsList);
|
2016-06-05 13:57:34 +02:00
|
|
|
|
|
|
|
// save index and top position
|
|
|
|
int index = listPods.getFirstVisiblePosition();
|
|
|
|
View v = listPods.getChildAt(0);
|
|
|
|
int top = (v == null) ? 0 : (v.getTop() - listPods.getPaddingTop());
|
2016-06-04 03:48:57 +02:00
|
|
|
listPods.setAdapter(adapter);
|
2016-06-05 13:57:34 +02:00
|
|
|
listPods.setSelectionFromTop(index, top);
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
adapter.getFilter().filter(editFilter.getText());
|
|
|
|
editFilter.addTextChangedListener(new TextWatcher() {
|
2016-03-03 17:46:31 +01:00
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
(adapter).getFilter().filter(s.toString());
|
|
|
|
}
|
2016-06-04 03:48:57 +02:00
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
2016-06-04 03:48:57 +02:00
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
@OnItemClick(R.id.podselection__listpods)
|
|
|
|
public void onListPodsItemClicked(int position) {
|
|
|
|
showPodConfirmationDialog((String) listPods.getAdapter().getItem(position));
|
|
|
|
}
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
private void showPodConfirmationDialog(final String selectedPod) {
|
|
|
|
// Make a clickable link
|
|
|
|
final SpannableString dialogMessage = new SpannableString(getString(R.string.confirm_pod, selectedPod));
|
2016-06-02 13:58:09 +02:00
|
|
|
Linkify.addLinks(dialogMessage, Linkify.ALL);
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
// Check if online
|
2016-08-07 14:29:52 +02:00
|
|
|
if (!WebHelper.isOnline(PodSelectionActivity.this)) {
|
2016-06-04 03:48:57 +02:00
|
|
|
Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
|
|
|
return;
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
2016-06-04 03:48:57 +02:00
|
|
|
|
|
|
|
// Show dialog
|
|
|
|
new AlertDialog.Builder(PodSelectionActivity.this)
|
|
|
|
.setTitle(getString(R.string.confirmation))
|
|
|
|
.setMessage(dialogMessage)
|
2016-06-04 11:11:51 +02:00
|
|
|
.setPositiveButton(android.R.string.yes,
|
2016-06-04 03:48:57 +02:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
|
|
onPodSelectionConfirmed(selectedPod);
|
|
|
|
}
|
|
|
|
})
|
2016-06-04 11:11:51 +02:00
|
|
|
.setNegativeButton(android.R.string.no, null)
|
2016-06-04 03:48:57 +02:00
|
|
|
.show();
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
2016-08-23 21:23:36 +02:00
|
|
|
private void onPodSelectionConfirmed(String selectedPod) {
|
2016-06-04 03:48:57 +02:00
|
|
|
app.getSettings().setPodDomain(selectedPod);
|
2016-08-23 21:23:36 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2016-06-02 13:58:09 +02:00
|
|
|
CookieManager.getInstance().removeAllCookies(null);
|
|
|
|
CookieManager.getInstance().removeSessionCookies(null);
|
2016-08-23 21:23:36 +02:00
|
|
|
} else {
|
|
|
|
//noinspection deprecation
|
2016-06-02 13:58:09 +02:00
|
|
|
CookieManager.getInstance().removeAllCookie();
|
2016-08-23 21:23:36 +02:00
|
|
|
//noinspection deprecation
|
2016-06-02 13:58:09 +02:00
|
|
|
CookieManager.getInstance().removeSessionCookie();
|
|
|
|
}
|
2016-08-23 21:23:36 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2016-06-02 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 03:48:57 +02:00
|
|
|
Helpers.animateToActivity(this, MainActivity.class, true);
|
2016-06-02 13:58:09 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
2016-06-04 03:48:57 +02:00
|
|
|
Snackbar.make(listPods, R.string.confirm_exit, Snackbar.LENGTH_LONG)
|
|
|
|
.setAction(android.R.string.yes, new View.OnClickListener() {
|
2016-03-03 17:46:31 +01:00
|
|
|
public void onClick(View view) {
|
2016-06-04 03:48:57 +02:00
|
|
|
finish();
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
2016-06-02 13:58:09 +02:00
|
|
|
})
|
|
|
|
.show();
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2016-06-05 13:57:34 +02:00
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(podListReceiver);
|
2016-03-03 17:46:31 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2016-06-04 11:11:51 +02:00
|
|
|
getMenuInflater().inflate(R.menu.pods__menu, menu);
|
2016-03-03 17:46:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2016-06-02 13:58:09 +02:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_reload: {
|
2016-08-07 14:29:52 +02:00
|
|
|
if (WebHelper.isOnline(PodSelectionActivity.this)) {
|
2016-06-02 13:58:09 +02:00
|
|
|
Intent i = new Intent(PodSelectionActivity.this, GetPodsService.class);
|
|
|
|
startService(i);
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-06-04 03:48:57 +02:00
|
|
|
Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
2016-06-02 13:58:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|