2016-10-01 01:18:42 +02:00
|
|
|
package com.github.dfa.diaspora_android.fragment;
|
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-10-05 02:30:10 +02:00
|
|
|
import android.support.v4.view.MenuItemCompat;
|
|
|
|
import android.support.v7.widget.SearchView;
|
2016-06-02 13:58:09 +02:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.util.Linkify;
|
2016-10-01 01:18:42 +02:00
|
|
|
import android.view.LayoutInflater;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.view.Menu;
|
2016-10-01 01:18:42 +02:00
|
|
|
import android.view.MenuInflater;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
2016-10-01 01:18:42 +02:00
|
|
|
import android.view.ViewGroup;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.webkit.CookieManager;
|
2016-10-01 01:18:42 +02:00
|
|
|
import android.widget.AdapterView;
|
2016-03-03 17:46:31 +01:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
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;
|
2016-10-01 01:18:42 +02:00
|
|
|
import com.github.dfa.diaspora_android.activity.MainActivity;
|
|
|
|
import com.github.dfa.diaspora_android.data.AppSettings;
|
2016-10-05 02:30:10 +02:00
|
|
|
import com.github.dfa.diaspora_android.data.DiasporaPodList;
|
|
|
|
import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod;
|
2016-03-29 19:38:50 +02:00
|
|
|
import com.github.dfa.diaspora_android.task.GetPodsService;
|
2016-10-01 01:18:42 +02:00
|
|
|
import com.github.dfa.diaspora_android.util.AppLog;
|
|
|
|
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
|
2016-10-05 02:30:10 +02:00
|
|
|
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-10-05 02:30:10 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2016-10-01 01:18:42 +02:00
|
|
|
/**
|
|
|
|
* Fragment that lets the user choose a Pod
|
|
|
|
* Created by vanitas on 01.10.16.
|
|
|
|
*/
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
public class PodSelectionFragment extends CustomFragment implements SearchView.OnQueryTextListener {
|
2016-10-01 01:18:42 +02:00
|
|
|
public static final String TAG = "com.github.dfa.diaspora_android.PodSelectionFragment";
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
@BindView(R.id.podselection__listpods)
|
|
|
|
protected ListView listViewPod;
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-10-01 01:18:42 +02:00
|
|
|
protected App app;
|
|
|
|
protected AppSettings appSettings;
|
2016-10-05 02:30:10 +02:00
|
|
|
private DiasporaPodList podList;
|
|
|
|
private ArrayAdapter<String> listViewPodAdapter;
|
|
|
|
private String filterString = "";
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-10-01 01:18:42 +02:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
AppLog.d(this, "onCreateView()");
|
2016-10-05 02:30:10 +02:00
|
|
|
View view = inflater.inflate(R.layout.podselection__fragment, container, false);
|
|
|
|
ButterKnife.bind(this, view);
|
|
|
|
return view;
|
2016-10-01 01:18:42 +02:00
|
|
|
}
|
2016-06-02 13:58:09 +02:00
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
@Override
|
2016-10-01 01:18:42 +02:00
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
2016-10-05 02:30:10 +02:00
|
|
|
app = (App) getActivity().getApplication();
|
|
|
|
appSettings = app.getSettings();
|
|
|
|
|
|
|
|
// Load local podlist
|
|
|
|
podList = new DiasporaPodList();
|
|
|
|
mergePodlistWithRessources(podList);
|
|
|
|
podList.setTrackMergeChanges(true);
|
|
|
|
updateListedPods();
|
2016-03-03 17:46:31 +01:00
|
|
|
|
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
listViewPod.setTextFilterEnabled(true);
|
|
|
|
listViewPod.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
2016-10-01 01:18:42 +02:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
2016-10-05 02:30:10 +02:00
|
|
|
showPodConfirmationDialog((String) listViewPod.getAdapter().getItem(i));
|
2016-10-01 01:18:42 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
LocalBroadcastManager.getInstance(getContext()).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED));
|
2016-10-05 02:30:10 +02:00
|
|
|
Helpers.showInfoIfUserNotConnectedToInternet(getContext(), listViewPod);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void mergePodlistWithRessources(DiasporaPodList podlist) {
|
|
|
|
String sPodlist = Helpers.readTextfileFromRawRessource(getContext(), R.raw.podlist, "", "");
|
|
|
|
try {
|
|
|
|
JSONObject jPodlist = new JSONObject(sPodlist);
|
|
|
|
podlist.mergeWithNewerEntries(new DiasporaPodList().fromJson(jPodlist));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
2016-10-05 02:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when a pod was clicked (or custom)
|
|
|
|
public void onPodButtonClicked(View v) {
|
|
|
|
//if (editFilter.getText().length() > 4 && editFilter.getText().toString().contains("")) {
|
|
|
|
showPodConfirmationDialog(filterString);
|
|
|
|
//} else {
|
|
|
|
// Snackbar.make(listViewPod, R.string.valid_pod, Snackbar.LENGTH_LONG).show();
|
|
|
|
//}
|
2016-06-02 13:58:09 +02:00
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
|
2016-10-01 01:18:42 +02:00
|
|
|
@Override
|
|
|
|
public String getFragmentTag() {
|
|
|
|
return TAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onBackPressed() {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-04 03:48:57 +02:00
|
|
|
|
|
|
|
private final BroadcastReceiver podListReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2016-10-05 02:30:10 +02:00
|
|
|
if (intent.hasExtra(GetPodsService.EXTRA_PODLIST)) {
|
2016-06-04 03:48:57 +02:00
|
|
|
Bundle extras = intent.getExtras();
|
2016-10-05 02:30:10 +02:00
|
|
|
DiasporaPodList newPods = (DiasporaPodList) extras.get(GetPodsService.EXTRA_PODLIST);
|
|
|
|
if (newPods != null && newPods.getPods().size() > 0) {
|
|
|
|
try {
|
|
|
|
podList.mergeWithNewerEntries(newPods);
|
|
|
|
updateListedPods();
|
|
|
|
} catch (JSONException ignored) {
|
|
|
|
}
|
2016-06-05 13:57:34 +02:00
|
|
|
} else {
|
2016-10-05 02:30:10 +02:00
|
|
|
Snackbar.make(listViewPod, R.string.podlist_error, Snackbar.LENGTH_SHORT).show();
|
2016-06-04 03:48:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-03 17:46:31 +01:00
|
|
|
@Override
|
2016-10-01 01:18:42 +02:00
|
|
|
public void onResume() {
|
2016-03-03 17:46:31 +01:00
|
|
|
super.onResume();
|
2016-10-01 01:18:42 +02:00
|
|
|
Intent i = new Intent(getContext(), GetPodsService.class);
|
|
|
|
getContext().startService(i);
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
private void updateListedPods() {
|
2016-06-04 03:48:57 +02:00
|
|
|
final ArrayList<String> listedPodsList = new ArrayList<>();
|
2016-10-05 02:30:10 +02:00
|
|
|
for (DiasporaPod pod : this.podList) {
|
|
|
|
listedPodsList.add(pod.getPodUrls().get(0).getHost());
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
listViewPodAdapter = new ArrayAdapter<>(
|
2016-10-01 01:18:42 +02:00
|
|
|
getContext(),
|
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
|
2016-10-05 02:30:10 +02:00
|
|
|
int index = listViewPod.getFirstVisiblePosition();
|
|
|
|
View v = listViewPod.getChildAt(0);
|
|
|
|
int top = (v == null) ? 0 : (v.getTop() - listViewPod.getPaddingTop());
|
|
|
|
listViewPod.setAdapter(listViewPodAdapter);
|
|
|
|
listViewPod.setSelectionFromTop(index, top);
|
2016-06-04 03:48:57 +02:00
|
|
|
|
2016-10-05 02:30:10 +02:00
|
|
|
listViewPodAdapter.getFilter().filter(filterString);
|
2016-03-03 17:46:31 +01: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-10-01 01:18:42 +02:00
|
|
|
if (!WebHelper.isOnline(getContext())) {
|
2016-10-05 02:30:10 +02:00
|
|
|
Snackbar.make(listViewPod, R.string.no_internet, Snackbar.LENGTH_LONG).show();
|
2016-06-04 03:48:57 +02:00
|
|
|
return;
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
2016-06-04 03:48:57 +02:00
|
|
|
|
|
|
|
// Show dialog
|
2016-10-01 01:18:42 +02:00
|
|
|
new AlertDialog.Builder(getContext())
|
2016-06-04 03:48:57 +02:00
|
|
|
.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-10-05 02:30:10 +02:00
|
|
|
((MainActivity) getActivity()).openDiasporaUrl(new DiasporaUrlHelper(appSettings).getPodUrl());
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-10-01 01:18:42 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(podListReceiver);
|
2016-03-03 17:46:31 +01:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-10-01 01:18:42 +02:00
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.podselection__menu, menu);
|
2016-10-05 02:30:10 +02:00
|
|
|
|
|
|
|
MenuItem searchItem = menu.findItem(R.id.podselection__action_search);
|
|
|
|
if (searchItem != null) {
|
|
|
|
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
|
|
|
|
searchView.setOnQueryTextListener(this);
|
|
|
|
}
|
|
|
|
|
2016-10-01 01:18:42 +02:00
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2016-06-02 13:58:09 +02:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_reload: {
|
2016-10-05 02:30:10 +02:00
|
|
|
if (!Helpers.showInfoIfUserNotConnectedToInternet(getContext(), listViewPod)) {
|
2016-10-01 01:18:42 +02:00
|
|
|
Intent i = new Intent(getContext(), GetPodsService.class);
|
|
|
|
getContext().startService(i);
|
2016-06-02 13:58:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-03 17:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2016-10-05 02:30:10 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
|
|
|
if (listViewPodAdapter != null) {
|
|
|
|
(listViewPodAdapter).getFilter().filter(newText);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|