diff --git a/app/src/main/java/com/github/dfa/diaspora_android/App.java b/app/src/main/java/com/github/dfa/diaspora_android/App.java index 0453e4fe..4c0bee57 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/App.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/App.java @@ -28,6 +28,8 @@ import android.webkit.CookieSyncManager; import android.webkit.WebView; 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.data.PodUserProfile; import com.github.dfa.diaspora_android.util.AppLog; import com.github.dfa.diaspora_android.util.AvatarImageLoader; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java index cc9cf36d..4eeca889 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/AboutActivity.java @@ -236,7 +236,9 @@ public class AboutActivity extends AppCompatActivity { osVersion.setText(getString(R.string.fragment_debug__android_version, Build.VERSION.RELEASE)); deviceName.setText(getString(R.string.fragment_debug__device_name, Build.MANUFACTURER + " " + Build.MODEL)); - podDomain.setText(getString(R.string.fragment_debug__pod_domain, settings.getPodDomain())); + if (settings.getPod() != null) { + podDomain.setText(getString(R.string.fragment_debug__pod_domain, settings.getPod().getName())); + } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 80ae786f..07febd63 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -60,6 +60,7 @@ import android.widget.Toast; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.data.AppSettings; +import com.github.dfa.diaspora_android.data.DiasporaPodList; import com.github.dfa.diaspora_android.data.PodUserProfile; import com.github.dfa.diaspora_android.fragment.BrowserFragment; import com.github.dfa.diaspora_android.fragment.CustomFragment; @@ -178,7 +179,7 @@ public class MainActivity extends AppCompatActivity } }); - if(!appSettings.hasPodDomain()) { + if(!appSettings.hasPod()) { AppLog.d(this, "We have no pod. Show PodSelectionFragment"); showFragment(getFragment(PodSelectionFragment.TAG)); } else { @@ -324,8 +325,8 @@ public class MainActivity extends AppCompatActivity if (!appSettings.getName().equals("")) { navheaderTitle.setText(appSettings.getName()); } - if (!appSettings.getPodDomain().equals("")) { - navheaderDescription.setText(appSettings.getPodDomain()); + if (appSettings.getPod() != null) { + navheaderDescription.setText(appSettings.getPod().getName()); } String avatarUrl = appSettings.getAvatarUrl(); if (!avatarUrl.equals("")) { @@ -356,6 +357,8 @@ public class MainActivity extends AppCompatActivity navMenu.findItem(R.id.nav_mentions).setVisible(appSettings.isVisibleInNavMentions()); navMenu.findItem(R.id.nav_profile).setVisible(appSettings.isVisibleInNavProfile()); navMenu.findItem(R.id.nav_public).setVisible(appSettings.isVisibleInNavPublic_activities()); + // Hide all pod related options if no pod is selected + navMenu.setGroupVisible(navMenu.findItem(R.id.nav_exit).getGroupId(), appSettings.getPod() != null); } @OnClick(R.id.main__topbar) @@ -397,6 +400,7 @@ public class MainActivity extends AppCompatActivity } } else if (ACTION_CHANGE_ACCOUNT.equals(action)) { AppLog.v(this, "Reset pod data and show PodSelectionFragment"); + appSettings.setPod(null); app.resetPodData(((DiasporaStreamFragment) getFragment(DiasporaStreamFragment.TAG)).getWebView()); showFragment(getFragment(PodSelectionFragment.TAG)); } else if (ACTION_CLEAR_CACHE.equals(action)) { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java index 88f13171..322f5f06 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/SettingsActivity.java @@ -35,7 +35,10 @@ import android.view.MenuItem; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; 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.util.AppLog; +import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; /** * @author vanitas @@ -124,21 +127,22 @@ public class SettingsActivity extends AppCompatActivity { @Override public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { Intent intent = new Intent(getActivity(), MainActivity.class); - String podDomain = ((App) getActivity().getApplication()).getSettings().getPodDomain(); + App app = ((App) getActivity().getApplication()); + DiasporaUrlHelper diasporaUrlHelper = new DiasporaUrlHelper(app.getSettings()); switch (preference.getTitleRes()) { case R.string.pref_title__personal_settings: { intent.setAction(MainActivity.ACTION_OPEN_URL); - intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/user/edit"); + intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getPersonalSettingsUrl()); break; } case R.string.pref_title__manage_tags: { intent.setAction(MainActivity.ACTION_OPEN_URL); - intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/tag_followings/manage"); + intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getManageTagsUrl()); break; } case R.string.pref_title__manage_contacts: { intent.setAction(MainActivity.ACTION_OPEN_URL); - intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/contacts"); + intent.putExtra(MainActivity.URL_MESSAGE, diasporaUrlHelper.getManageContactsUrl()); break; } case R.string.pref_title__change_account: { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java index da5839c9..8bd184d3 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java @@ -23,6 +23,11 @@ import android.content.Context; import android.content.SharedPreferences; 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 org.json.JSONObject; /** * Settings @@ -32,6 +37,7 @@ public class AppSettings { private final SharedPreferences prefApp; private final SharedPreferences prefPod; private final Context context; + private DiasporaPod currentPod0Cached; public AppSettings(Context context) { this.context = context.getApplicationContext(); @@ -138,24 +144,44 @@ public class AppSettings { setString(prefPod, R.string.pref_key__podprofile_name, name); } - public String getPodDomain() { - return getString(prefPod, R.string.pref_key__poddomain, ""); + + // TODO: Remove legacy at some time ;) + public void upgradeLegacyPoddomain() { + String legacy = getString(prefPod, R.string.pref_key__poddomain_legacy, ""); + if (!legacy.equals("")) { + DiasporaPod pod = new DiasporaPod(); + pod.setName(legacy); + pod.getPodUrls().add(new DiasporaPodUrl().setHost(legacy)); + setPod(pod); + } } - public void setPodDomain(String podDomain) { - setString(prefPod, R.string.pref_key__poddomain, podDomain); + public DiasporaPod getPod() { + upgradeLegacyPoddomain(); + if (currentPod0Cached == null) { + String pref = getString(prefPod, R.string.pref_key__current_pod_0, ""); + + try { + currentPod0Cached = new DiasporaPod().fromJson(new JSONObject(pref)); + } catch (JSONException e) { + currentPod0Cached = null; + } + } + return currentPod0Cached; } - public boolean hasPodDomain() { - return !getString(prefPod, R.string.pref_key__poddomain, "").equals(""); + public void setPod(DiasporaPod pod) { + try { + setString(prefPod, R.string.pref_key__current_pod_0, + pod == null ? null : pod.toJson().toString()); + currentPod0Cached = pod; + } catch (JSONException ignored) { + } } - public String[] getPreviousPodlist() { - return getStringArray(prefApp, R.string.pref_key__previous_podlist); - } - - public void setPreviousPodlist(String[] pods) { - setStringArray(prefApp, R.string.pref_key__previous_podlist, pods); + public boolean hasPod() { + upgradeLegacyPoddomain(); + return !getString(prefPod, R.string.pref_key__current_pod_0, "").equals(""); } public void setPodAspects(PodAspect[] aspects) { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java index 36216206..5373ca09 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/DiasporaPodList.java @@ -1,5 +1,7 @@ package com.github.dfa.diaspora_android.data; +import android.support.annotation.NonNull; + import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; @@ -287,6 +289,7 @@ public class DiasporaPodList implements Iterable, S /* * Getter & Setter */ + @NonNull public List getPodUrls() { return podUrls; } @@ -296,6 +299,7 @@ public class DiasporaPodList implements Iterable, S return this; } + @NonNull public List getMainLangs() { return mainLangs; } @@ -312,6 +316,16 @@ public class DiasporaPodList implements Iterable, S return this; } + /** + * Returns the first DiasporaPodUrl in the list + */ + public DiasporaPodUrl getPodUrl(){ + if(podUrls.size() > 0){ + return podUrls.get(0); + } + return null; + } + public DiasporaPod appendPodUrls(DiasporaPodUrl... values) { for (DiasporaPodUrl value : values) { this.podUrls.add(value); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/PodAspect.java b/app/src/main/java/com/github/dfa/diaspora_android/data/PodAspect.java index b878f679..7c9c2fab 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/PodAspect.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/PodAspect.java @@ -70,8 +70,8 @@ public class PodAspect { public String toHtmlLink(final App app) { final AppSettings appSettings = app.getSettings(); return String.format(Locale.getDefault(), - "%s", - appSettings.getPodDomain(), id, name); + "%s", + appSettings.getPod().getPodUrl().getBaseUrl(), id, name); } @Override diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java index 132b8cb8..43f6e92e 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/PodSelectionFragment.java @@ -208,7 +208,7 @@ public class PodSelectionFragment extends CustomFragment implements SearchView.O System.out.println(accepted + ": " + pod.toString()); if (accepted) { //TODO: Rework for new pod url system ;) - app.getSettings().setPodDomain(pod.getPodUrls().get(0).getHost()); + app.getSettings().setPod(pod); try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java b/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java index b3c99805..56f4c638 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java @@ -21,6 +21,8 @@ package com.github.dfa.diaspora_android.util; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; 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.data.PodAspect; /** @@ -31,7 +33,6 @@ import com.github.dfa.diaspora_android.data.PodAspect; public class DiasporaUrlHelper { private final AppSettings settings; - public static final String HTTPS = "https://"; public static final String SUBURL_NOTIFICATIONS = "/notifications"; public static final String SUBURL_POSTS = "/posts/"; public static final String SUBURL_STREAM = "/stream"; @@ -49,6 +50,9 @@ public class DiasporaUrlHelper { public static final String SUBURL_FOLOWED_TAGS = "/followed_tags"; public static final String SUBURL_ASPECTS = "/aspects"; public static final String SUBURL_STATISTICS = "/statistics"; + public static final String SUBURL_PERSONAL_SETTINGS = "/user/edit"; + public static final String SUBURL_MANAGE_TAGS = "/tag_followings/manage"; + public static final String SUBURL_MANAGE_CONTACTS = "/contacts"; public static final String URL_BLANK = "about:blank"; public DiasporaUrlHelper(AppSettings settings) { @@ -56,17 +60,22 @@ public class DiasporaUrlHelper { } /** - * Return a https url of the pod set in AppSettings. + * Return a url of the pod set in AppSettings. * Eg. https://pod.geraspora.de * * @return https://(pod-domain.tld) */ public String getPodUrl() { - return HTTPS + settings.getPodDomain(); + DiasporaPod pod = settings.getPod(); + if (pod != null){ + return settings.getPod().getPodUrl().getBaseUrl(); + } + return "http://127.0.0.1"; + } /** - * Return a https url that points to the stream of the configured diaspora account + * Return a url that points to the stream of the configured diaspora account * * @return https://(pod-domain.tld)/stream */ @@ -75,7 +84,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the notifications feed of the configured diaspora account + * Return a url that points to the notifications feed of the configured diaspora account * * @return https://(pod-domain.tld)/notifications */ @@ -84,7 +93,7 @@ public class DiasporaUrlHelper { } /** - * Returns a https url that points to the post with the id postId + * Returns a url that points to the post with the id postId * * @return https://(pod-domain.tld)/posts/(postId) */ @@ -93,7 +102,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the conversations overview of the registered diaspora account + * Return a url that points to the conversations overview of the registered diaspora account * * @return https://(pod-domain.tld)/conversations */ @@ -102,7 +111,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the new-post form that lets the user create a new post + * Return a url that points to the new-post form that lets the user create a new post * * @return https://(pod-domain.tld)/status_messages/new */ @@ -111,7 +120,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that shows the profile of the currently registered diaspora account + * Return a url that shows the profile of the currently registered diaspora account * * @return https://(pod-domain.tld)/people/(profileId) */ @@ -120,7 +129,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that shows the profile of the user with user id profileId + * Return a url that shows the profile of the user with user id profileId * * @param profileId Id of the profile to be shown * @return https://(pod-domain.tld)/people/(profileId) @@ -130,7 +139,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the activities feed of the currently registered diaspora account + * Return a url that points to the activities feed of the currently registered diaspora account * * @return https://(pod-domain.tld)/activity */ @@ -139,7 +148,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the feed of posts that were liked by the currently registered diaspora account + * Return a url that points to the feed of posts that were liked by the currently registered diaspora account * * @return https://(pod-domain.tld)/liked */ @@ -148,7 +157,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the stream of posts that were commented by the currently registered diaspora account + * Return a url that points to the stream of posts that were commented by the currently registered diaspora account * * @return https://(pod-domain.tld)/commented */ @@ -157,7 +166,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the stream of posts in which the currently registered diaspora account has been mentioned in + * Return a url that points to the stream of posts in which the currently registered diaspora account has been mentioned in * * @return https://(pod-domain.tld)/mentions */ @@ -166,7 +175,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the stream of public posts + * Return a url that points to the stream of public posts * * @return https://(pod-domain.tld)/public */ @@ -175,7 +184,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that toggles between mobile and desktop view when opened + * Return a url that toggles between mobile and desktop view when opened * * @return https://(pod-domain.tld)/mobile/toggle */ @@ -184,7 +193,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that queries posts for the given hashtag query + * Return a url that queries posts for the given hashtag query * * @param query hashtag to be searched * @return https://(pod-domain.tld)/tags/query @@ -194,7 +203,7 @@ public class DiasporaUrlHelper { } /** - * Return a https url that queries user accounts for query + * Return a url that queries user accounts for query * * @param query search term * @return https://(pod-domain.tld)/people.mobile?q=(query) @@ -204,13 +213,37 @@ public class DiasporaUrlHelper { } /** - * Return a https url that points to the statistics page of the pod. + * Return a url that points to the statistics page of the pod. * @return https://(pod-domain.tld)/statistics */ public String getStatisticsUrl() { return getPodUrl() + SUBURL_STATISTICS; } + /** + * Return a url that points to the personal settings page of the pod. + * @return https://(pod-domain.tld)/user/edit + */ + public String getPersonalSettingsUrl() { + return getPodUrl() + SUBURL_PERSONAL_SETTINGS; + } + + /** + * Return a url that points to the manage tags page of the pod. + * @return https://(pod-domain.tld)/tag_followings/manage + */ + public String getManageTagsUrl() { + return getPodUrl() + SUBURL_MANAGE_TAGS; + } + + /** + * Return a url that points to the manage tags page of the pod. + * @return https://(pod-domain.tld)/contacts + */ + public String getManageContactsUrl() { + return getPodUrl() + SUBURL_MANAGE_CONTACTS; + } + /** * Returns the url of the blank WebView * diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java b/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java index ba760d7b..7b1ea57b 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java @@ -29,6 +29,7 @@ import android.webkit.WebView; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.data.AppSettings; +import com.github.dfa.diaspora_android.data.DiasporaPodList; import com.github.dfa.diaspora_android.data.PodAspect; import com.github.dfa.diaspora_android.data.PodUserProfile; @@ -139,16 +140,17 @@ public class WebHelper { // Content AppSettings appSettings = app.getSettings(); + String pod0BaseUrl = appSettings.getPod().getPodUrl().getBaseUrl(); sb.append("»  "); sb.append(String.format(Locale.getDefault(), - "%s", - appSettings.getPodDomain(), app.getString(R.string.all_tags))); + "%s", + pod0BaseUrl, app.getString(R.string.all_tags))); sb.append("
"); for (String tag: profile.getFollowedTags()) { sb.append("»  "); sb.append(String.format(Locale.getDefault(), - "#%s", - appSettings.getPodDomain(), tag, tag)); + "#%s", + pod0BaseUrl, tag, tag)); sb.append("
"); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java b/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java index 914abcbb..beb85d19 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java @@ -19,7 +19,6 @@ package com.github.dfa.diaspora_android.webview; import android.content.Intent; -import android.net.Uri; import android.support.v4.content.LocalBroadcastManager; import android.webkit.CookieManager; import android.webkit.WebView; @@ -39,7 +38,7 @@ public class CustomWebViewClient extends WebViewClient { //Open non-diaspora links in customtab/external browser public boolean shouldOverrideUrlLoading(WebView view, String url) { - if (!url.contains(app.getSettings().getPodDomain())) { + if (!url.contains(app.getSettings().getPod().getPodUrl().getHost())) { Intent i = new Intent(MainActivity.ACTION_OPEN_EXTERNAL_URL); i.putExtra(MainActivity.EXTRA_URL, url); LocalBroadcastManager.getInstance(app.getApplicationContext()).sendBroadcast(i); @@ -57,7 +56,7 @@ public class CustomWebViewClient extends WebViewClient { if (cookies != null) { cookieManager.setCookie(url, cookies); - cookieManager.setCookie("https://" + app.getSettings().getPodDomain(), cookies); + cookieManager.setCookie(app.getSettings().getPod().getPodUrl().getBaseUrl(), cookies); //for (String c : cookies.split(";")) { //AppLog.d(this, "Cookie: " + c.split("=")[0] + " Value:" + c.split("=")[1]); //} diff --git a/app/src/main/res/layout/podselection__dialog.xml b/app/src/main/res/layout/podselection__dialog.xml index c2f6a736..8f6bf3e0 100644 --- a/app/src/main/res/layout/podselection__dialog.xml +++ b/app/src/main/res/layout/podselection__dialog.xml @@ -48,6 +48,24 @@ android:textAppearance="@style/TextAppearance.AppCompat.Medium" tools:text="Geraspora"/> + + + + - - - - - + - podDomain - previousPodlist + podDomain + pref_key__current_pod_0 pref_key_font_size pref_key_intellihide_toolbars pref_key_category_pod_settings