PodSelectionFragment

This commit is contained in:
Gregor Santner 2016-10-06 05:26:54 +02:00
parent 50207181f9
commit 654a00d647
7 changed files with 411 additions and 88 deletions

View File

@ -416,6 +416,23 @@ public class DiasporaPodList implements Iterable<DiasporaPodList.DiasporaPod>, S
return json;
}
/**
* Set default values for https
*/
public void setHttpsDefaults(){
setProtocol("https");
setPort(443);
}
/**
* Set default values for http
*/
public void setHttpDefaults(){
setProtocol("http");
setPort(80);
}
/**
* Tells if the ports needs to shown
*/

View File

@ -1,9 +1,7 @@
package com.github.dfa.diaspora_android.fragment;
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;
@ -12,8 +10,6 @@ import android.support.design.widget.Snackbar;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -32,10 +28,10 @@ import com.github.dfa.diaspora_android.data.AppSettings;
import com.github.dfa.diaspora_android.data.DiasporaPodList;
import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod;
import com.github.dfa.diaspora_android.task.GetPodsService;
import com.github.dfa.diaspora_android.ui.PodSelectionDialog;
import com.github.dfa.diaspora_android.util.AppLog;
import com.github.dfa.diaspora_android.util.DiasporaUrlHelper;
import com.github.dfa.diaspora_android.util.Helpers;
import com.github.dfa.diaspora_android.util.WebHelper;
import org.json.JSONException;
import org.json.JSONObject;
@ -44,16 +40,17 @@ import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* Fragment that lets the user choose a Pod
* Created by vanitas on 01.10.16.
*/
public class PodSelectionFragment extends CustomFragment implements SearchView.OnQueryTextListener {
public class PodSelectionFragment extends CustomFragment implements SearchView.OnQueryTextListener, PodSelectionDialog.PodSelectionDialogResultListener {
public static final String TAG = "com.github.dfa.diaspora_android.PodSelectionFragment";
@BindView(R.id.podselection__listpods)
@BindView(R.id.podselection__fragment__listpods)
protected ListView listViewPod;
protected App app;
@ -85,9 +82,8 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
listViewPod.setTextFilterEnabled(true);
listViewPod.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
showPodConfirmationDialog((String) listViewPod.getAdapter().getItem(i));
showPodSelectionDialog(podList.getPodAt(i));
}
});
LocalBroadcastManager.getInstance(getContext()).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED));
@ -104,13 +100,9 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
}
}
// Called when a pod was clicked (or custom)
@OnClick(R.id.podselection__fragment__button_use_custom_pod)
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();
//}
showPodSelectionDialog(new DiasporaPod());
}
@Override
@ -118,16 +110,6 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
return TAG;
}
@Override
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
/* Nothing to do */
}
@Override
public boolean onBackPressed() {
return false;
}
private final BroadcastReceiver podListReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -175,49 +157,9 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
listViewPodAdapter.getFilter().filter(filterString);
}
private void showPodConfirmationDialog(final String selectedPod) {
// Make a clickable link
final SpannableString dialogMessage = new SpannableString(getString(R.string.confirm_pod, selectedPod));
Linkify.addLinks(dialogMessage, Linkify.ALL);
// Check if online
if (!WebHelper.isOnline(getContext())) {
Snackbar.make(listViewPod, R.string.no_internet, Snackbar.LENGTH_LONG).show();
return;
}
// Show dialog
new AlertDialog.Builder(getContext())
.setTitle(getString(R.string.confirmation))
.setMessage(dialogMessage)
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onPodSelectionConfirmed(selectedPod);
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
private void onPodSelectionConfirmed(String selectedPod) {
app.getSettings().setPodDomain(selectedPod);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().removeSessionCookies(null);
} else {
//noinspection deprecation
CookieManager.getInstance().removeAllCookie();
//noinspection deprecation
CookieManager.getInstance().removeSessionCookie();
}
} catch (Exception e) {
e.printStackTrace();
}
((MainActivity) getActivity()).openDiasporaUrl(new DiasporaUrlHelper(appSettings).getPodUrl());
private void showPodSelectionDialog(final DiasporaPod selectedPod) {
PodSelectionDialog dialog = PodSelectionDialog.newInstance(selectedPod, this);
dialog.show(getFragmentManager(), PodSelectionDialog.TAG);
}
@Override
@ -253,11 +195,6 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (listViewPodAdapter != null) {
@ -265,4 +202,48 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O
}
return true;
}
@Override
public void onPodSelectionDialogResult(DiasporaPod pod, boolean accepted) {
System.out.println(accepted + ": " + pod.toString());
if (accepted) {
//TODO: Rework for new pod url system ;)
app.getSettings().setPodDomain(pod.getPodUrls().get(0).getHost());
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().removeSessionCookies(null);
} else {
//noinspection deprecation
CookieManager.getInstance().removeAllCookie();
//noinspection deprecation
CookieManager.getInstance().removeSessionCookie();
}
} catch (Exception e) {
e.printStackTrace();
}
((MainActivity) getActivity()).openDiasporaUrl(new DiasporaUrlHelper(appSettings).getPodUrl());
}
}
/*
* Dummy implementations
*/
@Override
public void onCreateBottomOptionsMenu(Menu menu, MenuInflater inflater) {
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onBackPressed() {
return false;
}
}

View File

@ -0,0 +1,195 @@
package com.github.dfa.diaspora_android.ui;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod;
import com.github.dfa.diaspora_android.data.DiasporaPodList.DiasporaPod.DiasporaPodUrl;
import org.json.JSONException;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;
/**
* Created by gsantner (https://gsantner.github.io) on 06.10.16.
*/
public class PodSelectionDialog extends AppCompatDialogFragment {
public static final String TAG = "com.github.dfa.diaspora_android.PodSelectionDialog";
public static interface PodSelectionDialogResultListener {
void onPodSelectionDialogResult(DiasporaPod pod, boolean accepted);
}
public static PodSelectionDialog newInstance(PodSelectionDialogResultListener resultListener) {
return newInstance(new DiasporaPod(), resultListener);
}
public static PodSelectionDialog newInstance(DiasporaPod pod, PodSelectionDialogResultListener resultListener) {
PodSelectionDialog dialog = new PodSelectionDialog();
dialog.setPod(pod);
dialog.setResultListener(resultListener);
return dialog;
}
/*
//
//
//
//
//
//
*/
@BindView(R.id.podselection__dialog__edit_podaddress)
EditText editPodAddress;
@BindView(R.id.podselection__dialog__edit_podname)
EditText editPodName;
@BindView(R.id.podselection__dialog__radiogroup_protocol)
RadioGroup radiogrpProtocol;
@BindView(R.id.podselection__dialog__text_profile)
TextView textProfile;
@BindView(R.id.podselection__dialog__spinner_profile)
Spinner spinnerProfile;
private PodSelectionDialogResultListener resultListener;
private View root;
private DiasporaPod pod = new DiasporaPod();
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Bind UI
root = inflater.inflate(R.layout.podselection__dialog, null);
ButterKnife.bind(this, root);
editPodName.setText(pod.getName());
List<DiasporaPodUrl> podUrls = pod.getPodUrls();
if (podUrls.size() > 0) {
uiLoadDiasporaUrl(0);
}
if (podUrls.size() > 1) {
textProfile.setVisibility(View.VISIBLE);
spinnerProfile.setVisibility(View.VISIBLE);
String[] podUrlss = new String[podUrls.size()];
for (int i = 0; i < podUrls.size(); podUrlss[i] = podUrls.get(i++).getBaseUrl()) ;
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, podUrlss);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProfile.setAdapter(spinnerAdapter);
}
builder.setView(root);
return builder.create();
}
@OnItemSelected(R.id.podselection__dialog__spinner_profile)
public void spinnerItemSelected(Spinner spinner, int position) {
uiLoadDiasporaUrl(position);
}
public void uiLoadDiasporaUrl(int wantedPodUrlPos) {
List<DiasporaPodUrl> podUrls = pod.getPodUrls();
if (podUrls.size() == 0) {
return;
}
wantedPodUrlPos = wantedPodUrlPos < podUrls.size() ? wantedPodUrlPos : 0;
DiasporaPodUrl url1 = podUrls.get(wantedPodUrlPos);
editPodAddress.setText(url1.getHost());
radiogrpProtocol.check(url1.getProtocol().equals("https")
? R.id.podselection__dialog__radio_https : R.id.podselection__dialog__radio_http);
}
@OnClick({R.id.podselection__dialog__btn_ok, R.id.podselection__dialog__btn_cancel})
public void onResultButtonClicked(View view) {
boolean POSITIVE_PRESSED = view.getId() == R.id.podselection__dialog__btn_ok;
if (POSITIVE_PRESSED) {
if (!checkInputs()) {
return;
}
DiasporaPodUrl podUrl = new DiasporaPodUrl();
if (radiogrpProtocol.getCheckedRadioButtonId() == R.id.podselection__dialog__radio_https) {
podUrl.setHttpsDefaults();
} else {
podUrl.setHttpDefaults();
}
podUrl.setHost(editPodAddress.getText().toString());
pod.setName(editPodName.getText().toString());
pod.getPodUrls().clear();
pod.getPodUrls().add(podUrl);
getDialog().dismiss();
publishResult(POSITIVE_PRESSED);
} else {
getDialog().cancel();
publishResult(POSITIVE_PRESSED);
}
}
public boolean checkInputs() {
boolean ok = true;
String s = editPodAddress.getText().toString();
if(TextUtils.isEmpty(s) || s.length() < 3){
editPodAddress.setError(getString(R.string.missing_value));
ok = false;
}
s = editPodName.getText().toString();
if(TextUtils.isEmpty(s) || s.length() < 3){
editPodName.setError(getString(R.string.missing_value));
ok = false;
}
return ok;
}
public void publishResult(boolean accepted) {
if (resultListener != null) {
resultListener.onPodSelectionDialogResult(pod, accepted);
}
}
/*
* GETTER & SETTER
*/
public PodSelectionDialogResultListener getResultListener() {
return resultListener;
}
public void setResultListener(PodSelectionDialogResultListener resultListener) {
this.resultListener = resultListener;
}
public DiasporaPod getPod() {
return pod;
}
public void setPod(DiasporaPod pod) {
try {
this.pod = new DiasporaPod().fromJson(pod.toJson());
} catch (JSONException ignored) {
}
}
}

View File

@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/podselection__dialog__text_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/profile"
android:textAppearance="@style/AppTheme.TextAppearance.Caption"
android:visibility="gone"/>
<Spinner
android:id="@+id/podselection__dialog__spinner_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin_half"
android:visibility="gone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin_half"
android:text="@string/pod_name"
android:textAppearance="@style/AppTheme.TextAppearance.Caption"/>
<EditText
android:id="@+id/podselection__dialog__edit_podname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin_half"
android:ems="10"
android:hint="@string/pod_name"
android:inputType="textPersonName"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
tools:text="Geraspora"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin_half"
android:text="@string/http_protocol"
android:textAppearance="@style/AppTheme.TextAppearance.Caption"/>
<RadioGroup
android:id="@+id/podselection__dialog__radiogroup_protocol"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin_half"
android:checkedButton="@+id/podselection__dialog__radio_https"
android:orientation="horizontal">
<RadioButton
android:id="@+id/podselection__dialog__radio_http"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="http"/>
<RadioButton
android:id="@+id/podselection__dialog__radio_https"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="https"/>
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin_half"
android:text="@string/pod_address"
android:textAppearance="@style/AppTheme.TextAppearance.Caption"/>
<EditText
android:id="@+id/podselection__dialog__edit_podaddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin_half"
android:ems="10"
android:hint="@string/pod_address"
android:inputType="textPersonName"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
tools:text="pod.geraspora.de"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/podselection__dialog__btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel"/>
<Button
android:id="@+id/podselection__dialog__btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -1,32 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<Button
android:id="@+id/podselection__button_use_custom_pod"
android:id="@+id/podselection__fragment__button_use_custom_pod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/podselection__custom_pod"
tools:text="Benutzerdefinierter Pod" />
tools:text="Benutzerdefinierter Pod"/>
<ListView
android:id="@+id/podselection__listpods"
android:id="@+id/podselection__fragment__listpods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/podselection__button_use_custom_pod"
android:choiceMode="singleChoice" />
android:layout_below="@+id/podselection__fragment__button_use_custom_pod"
android:choiceMode="singleChoice"/>
</RelativeLayout>

View File

@ -105,4 +105,8 @@
<string name="permission_denied">Permission denied.</string>
<string name="permission_granted_try_again">Permission granted. Please try again.</string>
<string name="podselection__custom_pod">Custom Pod</string>
<string name="pod_name">Pod name</string>
<string name="http_protocol">Protocol</string>
<string name="pod_address">Pod address</string>
<string name="missing_value">Missing value</string>
</resources>

View File

@ -27,4 +27,8 @@
<item name="android:background">@color/white</item>
</style>
<style name="AppTheme.TextAppearance.Caption" parent="TextAppearance.AppCompat.Caption">
<item name="android:textColor">@color/accent</item>
</style>
</resources>