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 42b0d98d..1565b37d 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 @@ -48,7 +48,6 @@ import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; -import android.support.v7.app.NotificationCompat; import android.support.v7.widget.ActionMenuView; import android.support.v7.widget.Toolbar; import android.text.Html; @@ -85,6 +84,7 @@ import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener; import com.github.dfa.diaspora_android.ui.ContextMenuWebView; import com.github.dfa.diaspora_android.ui.CustomWebViewClient; import com.github.dfa.diaspora_android.util.Helpers; +import com.github.dfa.diaspora_android.util.WebHelper; import org.json.JSONException; @@ -218,7 +218,7 @@ public class MainActivity extends AppCompatActivity .setAction(android.R.string.yes, new View.OnClickListener() { @Override public void onClick(View view) { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/notifications"); } else { Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); @@ -238,7 +238,7 @@ public class MainActivity extends AppCompatActivity String url = "https://" + podDomain; if (savedInstanceState == null) { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadData("", "text/html", null); webView.loadUrl(url); } else { @@ -296,7 +296,7 @@ public class MainActivity extends AppCompatActivity swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.reload(); } else { snackbarNoInternet.show(); @@ -314,12 +314,12 @@ public class MainActivity extends AppCompatActivity progressBar.setProgress(progress); if (progress > 0 && progress <= 60) { - Helpers.getUserProfile(wv); - Helpers.optimizeMobileSiteLayout(wv); + WebHelper.getUserProfile(wv); + WebHelper.optimizeMobileSiteLayout(wv); } if (progress > 60) { - Helpers.optimizeMobileSiteLayout(wv); + WebHelper.optimizeMobileSiteLayout(wv); } progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE); @@ -618,7 +618,7 @@ public class MainActivity extends AppCompatActivity public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_notifications: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/notifications"); return true; } else { @@ -628,7 +628,7 @@ public class MainActivity extends AppCompatActivity } case R.id.action_conversations: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/conversations"); return true; } else { @@ -649,7 +649,7 @@ public class MainActivity extends AppCompatActivity } case R.id.action_compose: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/status_messages/new"); } else { snackbarNoInternet.show(); @@ -685,7 +685,7 @@ public class MainActivity extends AppCompatActivity } case R.id.action_search: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); @@ -844,12 +844,13 @@ public class MainActivity extends AppCompatActivity void handleSendText(Intent intent) { webView.loadUrl("https://"+podDomain+"/status_messages/new"); - String content = intent.getStringExtra(Intent.EXTRA_TEXT); + String content = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_TEXT)); if(appSettings.isAppendSharedViaApp()) { - //TODO: Make \n work - content = content + " \n" +getString(R.string.shared_by_diaspora_android); + // = \n + content = content + "\n\n" + getString(R.string.shared_by_diaspora_android); } - final String sharedText = content; + + final String sharedText = WebHelper.escapeHtmlText(content); if (sharedText != null) { webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { @@ -876,20 +877,24 @@ public class MainActivity extends AppCompatActivity */ void handleSendSubject(Intent intent) { webView.loadUrl("https://"+podDomain+"/status_messages/new"); - String content = intent.getStringExtra(Intent.EXTRA_TEXT); - final String sharedSubject = intent.getStringExtra(Intent.EXTRA_SUBJECT); + String content = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_TEXT)); + String subject = WebHelper.replaceUrlWithMarkdown(intent.getStringExtra(Intent.EXTRA_SUBJECT)); + if (appSettings.isAppendSharedViaApp()) { - //TODO: Make \n work - content = content + " \n" + getString(R.string.shared_by_diaspora_android); + // = \n + content = content + "\n\n" + getString(R.string.shared_by_diaspora_android); } - final String sharedText = content; - if (sharedSubject != null) { + + final String sharedSubject = WebHelper.escapeHtmlText(subject); + final String sharedContent = WebHelper.escapeHtmlText(content); + + if (subject != null) { webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('textarea')[0].style.height='110px'; " + - "document.getElementsByTagName('textarea')[0].innerHTML = '**" + sharedSubject + "** " + sharedText + "'; " + + "document.getElementsByTagName('textarea')[0].innerHTML = '**" + sharedSubject + "** " + sharedContent + "'; " + " if(document.getElementById(\"main_nav\")) {" + " document.getElementById(\"main_nav\").parentNode.removeChild(" + " document.getElementById(\"main_nav\"));" + @@ -952,7 +957,7 @@ public class MainActivity extends AppCompatActivity // Handle navigation view item clicks here. switch (item.getItemId()) { case R.id.nav_stream: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/stream"); } else { snackbarNoInternet.show(); @@ -961,7 +966,7 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_profile: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/people/" + appSettings.getProfileId()); } else { snackbarNoInternet.show(); @@ -970,9 +975,9 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_followed_tags: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { // webView.loadUrl("https://" + podDomain + "/followed_tags"); - Helpers.showFollowedTagsList(webView, app); + WebHelper.showFollowedTagsList(webView, app); setTitle(R.string.nav_followed_tags); } else { snackbarNoInternet.show(); @@ -981,9 +986,9 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_aspects: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { // webView.loadUrl("https://" + podDomain + "/aspects"); - Helpers.showAspectList(webView, app); + WebHelper.showAspectList(webView, app); setTitle(R.string.aspects); } else { snackbarNoInternet.show(); @@ -992,7 +997,7 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_activities: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/activity"); } else { snackbarNoInternet.show(); @@ -1001,7 +1006,7 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_liked: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/liked"); } else { snackbarNoInternet.show(); @@ -1010,7 +1015,7 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_commented: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/commented"); } else { snackbarNoInternet.show(); @@ -1019,7 +1024,7 @@ public class MainActivity extends AppCompatActivity break; case R.id.nav_mentions: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/mentions"); } else { snackbarNoInternet.show(); @@ -1028,7 +1033,7 @@ public class MainActivity extends AppCompatActivity } case R.id.nav_public: { - if (Helpers.isOnline(MainActivity.this)) { + if (WebHelper.isOnline(MainActivity.this)) { webView.loadUrl("https://" + podDomain + "/public"); } else { snackbarNoInternet.show(); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionActivity.java index b661df12..9dea2697 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionActivity.java @@ -46,6 +46,7 @@ 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; +import com.github.dfa.diaspora_android.util.WebHelper; import java.util.ArrayList; @@ -80,7 +81,7 @@ public class PodSelectionActivity extends AppCompatActivity { setListedPods(app.getSettings().getPreviousPodlist()); LocalBroadcastManager.getInstance(this).registerReceiver(podListReceiver, new IntentFilter(GetPodsService.MESSAGE_PODS_RECEIVED)); - if (!Helpers.isOnline(PodSelectionActivity.this)) { + if (!WebHelper.isOnline(PodSelectionActivity.this)) { Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } @@ -164,7 +165,7 @@ public class PodSelectionActivity extends AppCompatActivity { Linkify.addLinks(dialogMessage, Linkify.ALL); // Check if online - if (!Helpers.isOnline(PodSelectionActivity.this)) { + if (!WebHelper.isOnline(PodSelectionActivity.this)) { Snackbar.make(listPods, R.string.no_internet, Snackbar.LENGTH_LONG).show(); return; } @@ -232,7 +233,7 @@ public class PodSelectionActivity extends AppCompatActivity { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_reload: { - if (Helpers.isOnline(PodSelectionActivity.this)) { + if (WebHelper.isOnline(PodSelectionActivity.this)) { Intent i = new Intent(PodSelectionActivity.this, GetPodsService.class); startService(i); return true; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/SplashActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/SplashActivity.java index 250bbbc3..99535239 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/SplashActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/SplashActivity.java @@ -28,6 +28,7 @@ import android.widget.ImageView; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.util.Helpers; +import com.github.dfa.diaspora_android.util.WebHelper; import butterknife.BindView; import butterknife.ButterKnife; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java index ecb584fc..b7cb6f5c 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/Helpers.java @@ -16,33 +16,15 @@ If not, see . */ - package com.github.dfa.diaspora_android.util; import android.app.Activity; -import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.support.v4.content.ContextCompat; -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.PodAspect; -import com.github.dfa.diaspora_android.data.PodUserProfile; - -import java.util.Locale; public class Helpers { - public static boolean isOnline(Context context) { - ConnectivityManager cnm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo ni = cnm.getActiveNetworkInfo(); - return ni != null && ni.isConnectedOrConnecting(); - } - public static void animateToActivity(Activity from, Class to, boolean finishFromActivity) { Intent intent = new Intent(from, to); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); @@ -52,87 +34,4 @@ public class Helpers { from.finish(); } } - - public static void optimizeMobileSiteLayout(final WebView wv) { - wv.loadUrl("javascript: ( function() {" + - " if (document.documentElement == null || document.documentElement.style == null) { return; }" + - " document.documentElement.style.paddingBottom = '260px';" + - " document.getElementById('main').style.paddingTop = '5px';" + - " if(document.getElementById('main_nav')) {" + - " document.getElementById('main_nav').parentNode.removeChild(" + - " document.getElementById('main_nav'));" + - " } else if (document.getElementById('main-nav')) {" + - " document.getElementById('main-nav').parentNode.removeChild(" + - " document.getElementById('main-nav'));" + - " }" + - "})();"); - } - - public static void getUserProfile(final WebView wv) { - // aspects":[{"id":124934,"name":"Friends","selected":true},{"id":124937,"name":"Liked me","selected":false},{"id":124938,"name":"Follow","selected":false},{"id":128327,"name":"Nur ich","selected":false}] - wv.loadUrl("javascript: ( function() {" + - " if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined') {" + - " var followed_tags = document.getElementById(\"followed_tags\");" + - " if(followed_tags != null) {" + - " try {" + - " var links = followed_tags.nextElementSibling.children[0].children;" + - " var tags = [];" + - " for(var i = 0; i < links.length - 1; i++) {" + // the last element is "Manage followed tags" link - " tags.push(links[i].innerText.substring(1));" + - " }" + - " gon.user[\"android_app.followed_tags\"] = tags;" + - " } catch(e) {}" + - " }" + - " var userProfile = JSON.stringify(gon.user);" + - " AndroidBridge.setUserProfile(userProfile.toString());" + - " } " + - "})();"); - } - - public static void showAspectList(final WebView wv, final App app) { - wv.stopLoading(); - PodUserProfile profile = app.getPodUserProfile(); - StringBuilder sb = new StringBuilder(); - - sb.append(""); - - // Content - for (PodAspect aspect : profile.getAspects()) { - sb.append("»  "); - sb.append(aspect.toHtmlLink(app)); - sb.append("
"); - } - - // End - sb.append(""); - wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null); - } - - public static void showFollowedTagsList(final WebView wv, final App app) { - wv.stopLoading(); - PodUserProfile profile = app.getPodUserProfile(); - StringBuilder sb = new StringBuilder(); - - sb.append(""); - - // Content - AppSettings appSettings = app.getSettings(); - sb.append("»  "); - sb.append(String.format(Locale.getDefault(), - "%s", - appSettings.getPodDomain(), 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)); - sb.append("
"); - } - - // End - sb.append(""); - wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null); - } - } 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 new file mode 100644 index 00000000..ae0abd9c --- /dev/null +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/WebHelper.java @@ -0,0 +1,148 @@ +/* + This file is part of the Diaspora for Android. + + Diaspora for Android is free software: you can redistribute it and/or modify + 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. + + Diaspora for Android is distributed in the hope that it will be useful, + 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 + along with the Diaspora for Android. + + If not, see . + */ + +package com.github.dfa.diaspora_android.util; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.support.v4.content.ContextCompat; +import android.text.Html; +import android.webkit.URLUtil; +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.PodAspect; +import com.github.dfa.diaspora_android.data.PodUserProfile; + +import java.net.URL; +import java.util.Locale; + +/** + * Created by Gregor Santner on 07.08.16. + * https://gsantner.github.io + */ +public class WebHelper { + + public static boolean isOnline(Context context) { + ConnectivityManager cnm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo ni = cnm.getActiveNetworkInfo(); + return ni != null && ni.isConnectedOrConnecting(); + } + + public static String replaceUrlWithMarkdown(String url){ + if( url != null && URLUtil.isHttpUrl(url) || URLUtil.isHttpsUrl(url)){ + return "<" + url + ">"; + } + return url; + } + + public static String escapeHtmlText(String text){ + text = Html.escapeHtml(text);; + text = text.replace("\n", " "); + return text; + } + + public static void optimizeMobileSiteLayout(final WebView wv) { + wv.loadUrl("javascript: ( function() {" + + " if (document.documentElement == null || document.documentElement.style == null) { return; }" + + " document.documentElement.style.paddingBottom = '260px';" + + " document.getElementById('main').style.paddingTop = '5px';" + + " if(document.getElementById('main_nav')) {" + + " document.getElementById('main_nav').parentNode.removeChild(" + + " document.getElementById('main_nav'));" + + " } else if (document.getElementById('main-nav')) {" + + " document.getElementById('main-nav').parentNode.removeChild(" + + " document.getElementById('main-nav'));" + + " }" + + "})();"); + } + + public static void getUserProfile(final WebView wv) { + // aspects":[{"id":124934,"name":"Friends","selected":true},{"id":124937,"name":"Liked me","selected":false},{"id":124938,"name":"Follow","selected":false},{"id":128327,"name":"Nur ich","selected":false}] + wv.loadUrl("javascript: ( function() {" + + " if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined') {" + + " var followed_tags = document.getElementById(\"followed_tags\");" + + " if(followed_tags != null) {" + + " try {" + + " var links = followed_tags.nextElementSibling.children[0].children;" + + " var tags = [];" + + " for(var i = 0; i < links.length - 1; i++) {" + // the last element is "Manage followed tags" link + " tags.push(links[i].innerText.substring(1));" + + " }" + + " gon.user[\"android_app.followed_tags\"] = tags;" + + " } catch(e) {}" + + " }" + + " var userProfile = JSON.stringify(gon.user);" + + " AndroidBridge.setUserProfile(userProfile.toString());" + + " } " + + "})();"); + } + + public static void showAspectList(final WebView wv, final App app) { + wv.stopLoading(); + PodUserProfile profile = app.getPodUserProfile(); + StringBuilder sb = new StringBuilder(); + + sb.append(""); + + // Content + for (PodAspect aspect : profile.getAspects()) { + sb.append("»  "); + sb.append(aspect.toHtmlLink(app)); + sb.append("
"); + } + + // End + sb.append(""); + wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null); + } + + public static void showFollowedTagsList(final WebView wv, final App app) { + wv.stopLoading(); + PodUserProfile profile = app.getPodUserProfile(); + StringBuilder sb = new StringBuilder(); + + sb.append(""); + + // Content + AppSettings appSettings = app.getSettings(); + sb.append("»  "); + sb.append(String.format(Locale.getDefault(), + "%s", + appSettings.getPodDomain(), 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)); + sb.append("
"); + } + + // End + sb.append(""); + wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "UTF-16", null); + } + +}